-
Notifications
You must be signed in to change notification settings - Fork 1
/
MetaModelsFilterSettingDlhFromTo.php
145 lines (124 loc) · 3.54 KB
/
MetaModelsFilterSettingDlhFromTo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');
/**
* Frontend filter for Contao MetaModels
*
* @copyright 2013 de la Haye Kommunikationsdesign <http://www.delahaye.de>
* @author Christian de la Haye <service@delahaye.de>
* @package metamodels_dlh_frontendfilter
* @license LGPL
* @filesource
*/
/**
* Class MetaModelsFilterSettingDlhFromTo
*
* Filter "value from x to y" for FE-filtering, based on filters by the meta models team.
* @author Christian de la Haye <service@delahaye.de>
*/
class MetaModelsFilterSettingDlhFromTo extends MetaModelFilterSetting
{
/**
* {@inheritdoc}
*/
protected function getParamName()
{
if ($this->get('urlparam'))
{
return $this->get('urlparam');
}
$objAttribute = $this->getMetaModel()->getAttributeById($this->get('attr_id'));
if ($objAttribute)
{
return $objAttribute->getColName();
}
}
/**
* {@inheritdoc}
*/
public function prepareRules(IMetaModelFilter $objFilter, $arrFilterUrl)
{
$objMetaModel = $this->getMetaModel();
$objAttribute = $objMetaModel->getAttributeById($this->get('attr_id'));
$strParamName = $this->getParamName();
$arrParamValue = $arrFilterUrl[$strParamName];
$strColname = $objAttribute->getColName();
if ($objAttribute && $strParamName && $arrParamValue)
{
$strMore = $this->get('moreequal') ? '>=' : '>';
$strLess = $this->get('lessequal') ? '<=' : '<';
if($arrParamValue[0] > 0 && $arrParamValue[1] > 0)
{
// from to
$strWhere = $strColname.' '.$strMore.'? AND '.$strColname.' '.$strLess.'?';
$arrSearch = array($arrParamValue[0], $arrParamValue[1]);
}
elseif($arrParamValue[0] > 0)
{
// from
$strWhere = $strColname.' '.$strMore.'?';
$arrSearch = array($arrParamValue[0]);
}
elseif($arrParamValue[1] > 0)
{
// to
$strWhere = $strColname.' '.$strLess.'?';
$arrSearch = array($arrParamValue[1]);
}
else
{
// nothing
$strWhere = '';
}
if($strWhere)
{
$objQuery = Database::getInstance()->prepare(sprintf(
'SELECT id FROM %s WHERE (%s)',
$this->getMetaModel()->getTableName(),
$strWhere
))
->execute($arrSearch);
$arrIds = $objQuery->fetchEach('id');
$objFilter->addFilterRule(new MetaModelFilterRuleStaticIdList($arrIds));
return;
}
$objFilter->addFilterRule(new MetaModelFilterRuleStaticIdList(NULL));
}
$objFilter->addFilterRule(new MetaModelFilterRuleStaticIdList(NULL));
}
/**
* {@inheritdoc}
*/
public function getParameters()
{
return ($strParamName = $this->getParamName()) ? array($strParamName) : array();
}
/**
* {@inheritdoc}
*/
public function getParameterDCA()
{
$objAttribute = $this->getMetaModel()->getAttributeById($this->get('attr_id'));
$arrOptions = $objAttribute->getFilterOptions();
$strLabel = ($this->get('label') ? $this->get('label') : $objAttribute->getName());
if($this->get('fromfield') && $this->get('tofield'))
{
$strLabel .= ' '.$GLOBALS['TL_LANG']['metamodels_dlh_frontendfilter_filter']['fromto'];
}
elseif($this->get('fromfield') && !$this->get('tofield'))
{
$strLabel .= ' '.$GLOBALS['TL_LANG']['metamodels_dlh_frontendfilter_filter']['from'];
}
else
{
$strLabel .= ' '.$GLOBALS['TL_LANG']['metamodels_dlh_frontendfilter_filter']['to'];
}
return array(
$this->getParamName() => array
(
'label' => $strLabel,
'inputType' => 'doubletext',
'eval' => array('size'=>2,'fromfield'=>($this->get('fromfield')? true:false), 'tofield'=>($this->get('tofield')? true:false))
)
);
}
}
?>