/
SimpleLookup.php
333 lines (295 loc) · 10.1 KB
/
SimpleLookup.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/**
* This file is part of MetaModels/core.
*
* (c) 2012-2015 The MetaModels team.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package MetaModels
* @subpackage Core
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @author David Maack <david.maack@arcor.de>
* @author Stefan Heimes <stefan_heimes@hotmail.com>
* @author Ingolf Steinhardt <info@e-spin.de>
* @copyright 2012-2015 The MetaModels team.
* @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0
* @filesource
*/
namespace MetaModels\Filter\Setting;
use MetaModels\Attribute\IAttribute;
use MetaModels\FrontendIntegration\FrontendFilterOptions;
use MetaModels\IItem;
use MetaModels\Filter\IFilter;
use MetaModels\Filter\Rules\StaticIdList as FilterRuleStaticIdList;
use MetaModels\Filter\Rules\SearchAttribute as FilterRuleSimpleLookup;
use MetaModels\Render\Setting\ICollection as IRenderSettings;
/**
* Filter setting implementation performing a search for a value on a
* configured attribute.
*/
class SimpleLookup extends Simple
{
/**
* Retrieve the filter parameter name to react on.
*
* @return string|null
*/
protected function getParamName()
{
if ($this->get('urlparam')) {
return $this->get('urlparam');
}
$objAttribute = $this->getFilteredAttribute();
if ($objAttribute) {
return $objAttribute->getColName();
}
return null;
}
/**
* Return the label to use.
*
* @return string|null
*/
protected function getLabel()
{
if ($attribute = $this->getFilteredAttribute()) {
// TODO: make this multilingual.
if ($label = $this->get('label')) {
return $label;
}
return $attribute->getName();
}
return null;
}
/**
* Determine if this filter setting shall return all matches if no url param has been specified.
*
* @return bool true if all matches shall be returned, false otherwise.
*/
public function allowEmpty()
{
return (bool) $this->get('allow_empty');
}
/**
* Internal helper function for descendant classes to retrieve the options.
*
* @param IAttribute $objAttribute The attribute to search.
*
* @param string[]|null $arrIds The Id list of items for which to retrieve the options.
*
* @param array $arrCount If non null, the amount of matches will get returned.
*
* @return array
*/
protected function getParameterFilterOptions($objAttribute, $arrIds, &$arrCount = null)
{
$arrOptions = $objAttribute->getFilterOptions(
$this->get('onlypossible') ? $arrIds : null,
(bool) $this->get('onlyused'),
$arrCount
);
// Remove empty values.
foreach ($arrOptions as $mixOptionKey => $mixOptions) {
// Remove html/php tags.
$mixOptions = strip_tags($mixOptions);
$mixOptions = trim($mixOptions);
if ($mixOptions === '' || $mixOptions === null) {
unset($arrOptions[$mixOptionKey]);
}
}
return $arrOptions;
}
/**
* Determine if this filter setting shall be available for frontend filter widget generating.
*
* @return bool true if available, false otherwise.
*/
public function enableFEFilterWidget()
{
// TODO: better use a seperate checkbox or the like? For the moment, this has to be overridden by sub classes.
return (bool) $this->get('predef_param');
}
/**
* {@inheritdoc}
*/
public function prepareRules(IFilter $objFilter, $arrFilterUrl)
{
$objMetaModel = $this->getMetaModel();
$objAttribute = $this->getFilteredAttribute();
$strParam = $this->getParamName();
if ($objAttribute && $strParam) {
$arrFilterValue = isset($arrFilterUrl[$strParam]) ? $arrFilterUrl[$strParam] : null;
if (!$arrFilterValue && $this->get('defaultid')) {
$arrFilterValue = $this->get('defaultid');
}
if ($arrFilterValue) {
if ($objMetaModel->isTranslated() && $this->get('all_langs')) {
$arrLanguages = $objMetaModel->getAvailableLanguages();
} else {
$arrLanguages = array($objMetaModel->getActiveLanguage());
}
$objFilterRule = new FilterRuleSimpleLookup($objAttribute, $arrFilterValue, $arrLanguages);
$objFilter->addFilterRule($objFilterRule);
return;
}
// We found an attribute but no match in URL. So ignore this filter setting if allow_empty is set.
if ($this->allowEmpty()) {
$objFilter->addFilterRule(new FilterRuleStaticIdList(null));
return;
}
}
// Either no attribute found or no match in url, do not return anything.
$objFilter->addFilterRule(new FilterRuleStaticIdList(array()));
}
/**
* {@inheritdoc}
*/
public function generateFilterUrlFrom(IItem $objItem, IRenderSettings $objRenderSetting)
{
if ($objAttribute = $this->getFilteredAttribute()) {
// TODO: shall we omit returning of empty values?
$strResult = $objAttribute->getFilterUrlValue($objItem->get($objAttribute->getColName()));
return array($this->getParamName() => $strResult);
}
return array();
}
/**
* {@inheritdoc}
*/
public function getParameters()
{
return ($strParamName = $this->getParamName()) ? array($strParamName) : array();
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.Superglobals)
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
*/
public function getParameterDCA()
{
// If defined as static, return nothing as not to be manipulated via editors.
if (!$this->get('predef_param')) {
return array();
}
$objAttribute = $this->getFilteredAttribute();
$arrOptions = $objAttribute->getFilterOptions(null, false);
return array(
$this->getParamName() => array
(
'label' => array(
sprintf(
$GLOBALS['TL_LANG']['MSC']['metamodel_filtersettings_parameter']['simplelookup'][0],
$objAttribute->getName()
),
sprintf(
$GLOBALS['TL_LANG']['MSC']['metamodel_filtersettings_parameter']['simplelookup'][1],
$objAttribute->getName()
)
),
'inputType' => 'select',
'options' => $arrOptions,
'eval' => array(
'includeBlankOption' => true,
'style' => 'min-width:450px;width:450px;margin-bottom:16px;margin-right:10px;'
)
)
);
}
/**
* {@inheritdoc}
*/
public function getParameterFilterNames()
{
if (($label = $this->getLabel()) && ($paramName = $this->getParamName())) {
return array(
$paramName => $label
);
}
return array();
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.Superglobals)
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
*/
public function getParameterFilterWidgets(
$arrIds,
$arrFilterUrl,
$arrJumpTo,
FrontendFilterOptions $objFrontendFilterOptions
) {
// If defined as static, return nothing as not to be manipulated via editors.
if (!$this->enableFEFilterWidget()) {
return array();
}
if (!($attribute = $this->getFilteredAttribute())) {
return array();
}
$GLOBALS['MM_FILTER_PARAMS'][] = $this->getParamName();
$arrCount = array();
$arrWidget = array(
'label' => array(
$this->getLabel(),
'GET: ' . $this->getParamName()
),
'inputType' => 'select',
'options' => $this->getParameterFilterOptions($attribute, $arrIds, $arrCount),
'count' => $arrCount,
'showCount' => $objFrontendFilterOptions->isShowCountValues(),
'eval' => array
(
'includeBlankOption' => (
$this->get('blankoption') && !$objFrontendFilterOptions->isHideClearFilter()
? true
: false
),
'blankOptionLabel' => &$GLOBALS['TL_LANG']['metamodels_frontendfilter']['do_not_filter'],
'colname' => $attribute->getColname(),
'urlparam' => $this->getParamName(),
'onlyused' => $this->get('onlyused'),
'onlypossible' => $this->get('onlypossible'),
'template' => $this->get('template'),
)
);
return array
(
$this->getParamName() => $this->prepareFrontendFilterWidget(
$arrWidget,
$arrFilterUrl,
$arrJumpTo,
$objFrontendFilterOptions
)
);
}
/**
* {@inheritdoc}
*/
public function getReferencedAttributes()
{
if ($attribute = $this->getFilteredAttribute()) {
return array($attribute->getColName());
}
return array();
}
/**
* Retrieve the attribute we are filtering on.
*
* @return IAttribute|null
*/
protected function getFilteredAttribute()
{
if (!($attributeId = $this->get('attr_id'))) {
return null;
}
if ($attribute = $this->getMetaModel()->getAttributeById($attributeId)) {
return $attribute;
}
return null;
}
}