forked from MetaModels/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base.php
516 lines (458 loc) · 15.8 KB
/
Base.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<?php
/**
* The MetaModels extension allows the creation of multiple collections of custom items,
* each with its own unique set of selectable attributes, with attribute extendability.
* The Front-End modules allow you to build powerful listing and filtering of the
* data in each collection.
*
* PHP version 5
*
* @package MetaModels
* @subpackage Core
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @copyright The MetaModels team.
* @license LGPL.
* @filesource
*/
namespace MetaModels\Attribute;
use MetaModels\IMetaModel;
use MetaModels\Render\Setting\ISimple;
use MetaModels\Render\Setting\Simple;
use MetaModels\Render\Template;
/**
* This is the main MetaModels-attribute base class.
* To create a MetaModelAttribute instance, use the {@link MetaModelAttributeFactory}
* This class is the reference implementation for {@link IMetaModelAttribute}.
*
* @package MetaModels
* @subpackage Core
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
*/
abstract class Base implements IAttribute
{
/**
* The MetaModel instance this object belongs to.
*
* @var IMetaModel
*/
private $metaModel;
/**
* The meta information of this attribute.
*
* @var array
*/
protected $arrData = array();
/**
* Instantiate an MetaModel attribute.
*
* Note that you should not use this directly but use the factory classes to instantiate attributes.
*
* @param IMetaModel $objMetaModel The MetaModel instance this attribute belongs to.
*
* @param array $arrData The information array, for attribute information, refer to documentation of
* table tl_metamodel_attribute and documentation of the certain attribute classes
* for information what values are understood.
*/
public function __construct(IMetaModel $objMetaModel, $arrData = array())
{
// Meta information.
foreach ($this->getAttributeSettingNames() as $strSettingName) {
if (isset($arrData[$strSettingName])) {
$this->set($strSettingName, $arrData[$strSettingName]);
}
}
$this->metaModel = $objMetaModel;
}
/**
* Retrieve the human readable name (or title) from the attribute.
*
* If the MetaModel is translated, the currently active language is used,
* with properly falling back to the defined fallback language.
*
* @return string the human readable name
*/
public function getName()
{
if (is_array($this->arrData['name'])) {
return $this->getLangValue($this->get('name'));
}
return $this->arrData['name'];
}
/**
* This extracts the value for the given language from the given language array.
*
* If the language is not contained within the value array, the fallback language from the parenting
* {@link IMetaModel} instance is tried as well.
*
* @param array $arrValues The array holding all language values in the form array('langcode' => $varValue).
*
* @param string $strLangCode The language code of the language to fetch. Optional, if not given,
* $GLOBALS['TL_LANGUAGE'] is used.
*
* @return mixed|null the value for the given language or the fallback language, NULL if neither is present.
*/
protected function getLangValue($arrValues, $strLangCode = null)
{
if (!($this->getMetaModel()->isTranslated() && is_array($arrValues))) {
return $arrValues;
}
if ($strLangCode === null) {
return $this->getLangValue($arrValues, $this->getMetaModel()->getActiveLanguage());
}
if (array_key_exists($strLangCode, $arrValues)) {
return $arrValues[$strLangCode];
}
// Language code not set, use fallback.
return $arrValues[$this->getMetaModel()->getFallbackLanguage()];
}
/**
* Hook additional attribute formatter that want to format the value.
*
* @param array $arrBaseFormatted The current result array. The keys "raw" and "text" are always populated.
*
* @param array $arrRowData The Raw values from the database.
*
* @param string $strOutputFormat The output format to use.
*
* @param ISimple $objSettings The output format settings.
*
* @return mixed
*
* @SuppressWarnings(PHPMD.Superglobals)
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
*/
public function hookAdditionalFormatters($arrBaseFormatted, $arrRowData, $strOutputFormat, $objSettings)
{
$arrResult = $arrBaseFormatted;
if (isset($GLOBALS['METAMODEL_HOOKS']['parseValue']) && is_array($GLOBALS['METAMODEL_HOOKS']['parseValue'])) {
foreach ($GLOBALS['METAMODEL_HOOKS']['parseValue'] as $callback) {
list($strClass, $strMethod) = $callback;
$objCallback = (in_array('getInstance', get_class_methods($strClass)))
? call_user_func(array($strClass, 'getInstance'))
: new $strClass();
$arrResult = $objCallback->$strMethod(
$this,
$arrBaseFormatted,
$arrRowData,
$strOutputFormat,
$objSettings
);
}
}
return $arrResult;
}
/**
* When rendered via a template, this populates the template with values.
*
* @param Template $objTemplate The Template instance to populate.
*
* @param array $arrRowData The row data for the current item.
*
* @param ISimple $objSettings The render settings to use for this attribute.
*
* @return void
*/
protected function prepareTemplate(Template $objTemplate, $arrRowData, $objSettings = null)
{
$objTemplate->attribute = $this;
$objTemplate->settings = $objSettings;
$objTemplate->row = $arrRowData;
$objTemplate->raw = $arrRowData[$this->getColName()];
$objTemplate->additional_class = $objSettings->get('additional_class')
? ' ' . $objSettings->get('additional_class')
: '';
}
/**
* {@inheritdoc}
*/
public function getColName()
{
return $this->arrData['colname'];
}
/**
* {@inheritdoc}
*/
public function getMetaModel()
{
return $this->metaModel;
}
/**
* {@inheritdoc}
*/
public function get($strKey)
{
return isset($this->arrData[$strKey]) ? $this->arrData[$strKey] : null;
}
/**
* {@inheritdoc}
*/
public function set($strKey, $varValue)
{
if (in_array($strKey, $this->getAttributeSettingNames())) {
// @codingStandardsIgnoreStart
$unSerialized = @unserialize($varValue);
// @codingStandardsIgnoreEnd
if (is_array($unSerialized)) {
$this->arrData[$strKey] = $unSerialized;
} else {
$this->arrData[$strKey] = $varValue;
}
}
return $this;
}
/**
* {@inheritdoc}
*/
public function handleMetaChange($strMetaName, $varNewValue)
{
// By default we accept any change of meta information.
$this->set($strMetaName, $varNewValue);
return $this;
}
/**
* {@inheritdoc}
*/
public function destroyAUX()
{
// No-op.
}
/**
* {@inheritdoc}
*/
public function initializeAUX()
{
// No-op.
}
/**
* {@inheritdoc}
*/
public function getAttributeSettingNames()
{
return array(
// Settings originating from tl_metamodel_attribute.
'id', 'pid', 'sorting', 'tstamp', 'name', 'description', 'type', 'colname', 'isvariant',
// Settings originating from tl_metamodel_dcasetting.
'tl_class', 'readonly');
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.Superglobals)
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
*/
public function getFieldDefinition($arrOverrides = array())
{
$strTableName = $this->getMetaModel()->getTableName();
// Only overwrite the language if not already set.
if (empty($GLOBALS['TL_LANG'][$strTableName][$this->getColName()])) {
$GLOBALS['TL_LANG'][$strTableName][$this->getColName()] = array
(
$this->getLangValue($this->get('name')),
$this->getLangValue($this->get('description')),
);
}
$arrFieldDef = array();
if (isset($GLOBALS['TL_DCA'][$strTableName]['fields'][$this->getColName()])) {
$arrFieldDef = $GLOBALS['TL_DCA'][$strTableName]['fields'][$this->getColName()];
}
$arrFieldDef = array_replace_recursive(
array
(
'label' => &$GLOBALS['TL_LANG'][$strTableName][$this->getColName()],
'eval' => array()
),
$arrFieldDef
);
$arrSettingNames = $this->getAttributeSettingNames();
$arrFieldDef['eval']['unique'] = $this->get('isunique') && in_array('isunique', $arrSettingNames);
$arrFieldDef['eval']['mandatory'] = (!empty($arrFieldDef['eval']['unique']))
|| ($this->get('mandatory') && in_array('mandatory', $arrSettingNames));
$arrFieldDef['eval']['alwaysSave'] = (!empty($arrFieldDef['eval']['alwaysSave']))
|| ($this->get('alwaysSave') && in_array('alwaysSave', $arrSettingNames));
foreach (array
(
'tl_class',
'mandatory',
'alwaysSave',
'chosen',
'allowHtml',
'preserveTags',
'decodeEntities',
'rte',
'rows',
'cols',
'spaceToUnderscore',
'includeBlankOption',
'submitOnChange',
'readonly'
) as $strEval) {
if (in_array($strEval, $arrSettingNames) && $arrOverrides[$strEval]) {
$arrFieldDef['eval'][$strEval] = $arrOverrides[$strEval];
}
}
if (in_array('trailingSlash', $arrSettingNames) && ($arrOverrides['trailingSlash'] != 2)) {
$arrFieldDef['eval']['trailingSlash'] = (bool) $arrOverrides['trailingSlash'];
}
// Sorting flag override.
if (in_array('flag', $arrSettingNames) && $arrOverrides['flag']) {
$arrFieldDef['flag'] = $arrOverrides['flag'];
}
// Panel conditions.
if (in_array('filterable', $arrSettingNames) && $arrOverrides['filterable']) {
$arrFieldDef['filter'] = true;
}
if (in_array('searchable', $arrSettingNames) && $arrOverrides['searchable']) {
$arrFieldDef['search'] = true;
}
if (in_array('sortable', $arrSettingNames) && $arrOverrides['sortable']) {
$arrFieldDef['sorting'] = true;
}
return $arrFieldDef;
}
/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.Superglobals)
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
*/
public function getItemDCA($arrOverrides = array())
{
$arrReturn = array
(
'fields' => array_merge(
array($this->getColName() => $this->getFieldDefinition($arrOverrides)),
(array) $GLOBALS['TL_DCA'][$this->getMetaModel()->getTableName()]['fields'][$this->getColName()]
),
);
// Add sorting fields.
if (in_array('sortable', $this->getAttributeSettingNames()) && $arrOverrides['sortable']) {
$arrReturn['list']['sorting']['fields'][] = $this->getColName();
}
return $arrReturn;
}
/**
* {@inheritdoc}
*/
public function valueToWidget($varValue)
{
return $varValue;
}
/**
* {@inheritdoc}
*/
public function widgetToValue($varValue, $intId)
{
return $varValue;
}
/**
* {@inheritdoc}
*/
public function getDefaultRenderSettings()
{
$objSetting = new Simple(
array
(
'template' => 'mm_attr_' . $this->get('type')
)
);
return $objSetting;
}
/**
* {@inheritdoc}
*/
public function parseValue($arrRowData, $strOutputFormat = 'text', $objSettings = null)
{
$arrResult = array(
'raw' => $arrRowData[$this->getColName()],
);
if ($objSettings && $objSettings->get('template')) {
$strTemplate = $objSettings->get('template');
$objTemplate = new Template($strTemplate);
$this->prepareTemplate($objTemplate, $arrRowData, $objSettings);
// Now the desired format.
if ($strValue = $objTemplate->parse($strOutputFormat, false)) {
$arrResult[$strOutputFormat] = $strValue;
}
// Text rendering is mandatory, try with the current setting,
// upon exception, try again with the default settings, as the template name might have changed.
// if this fails again, we are definately out of luck and bail the exception.
try {
$arrResult['text'] = $objTemplate->parse('text', true);
} catch (\Exception $e) {
$objSettingsFallback = $this->getDefaultRenderSettings()->setParent($objSettings->getParent());
$objTemplate = new Template($objSettingsFallback->get('template'));
$this->prepareTemplate($objTemplate, $arrRowData, $objSettingsFallback);
$arrResult['text'] = $objTemplate->parse('text', true);
}
} else {
// Text rendering is mandatory, therefore render using default render settings.
$arrResult = $this->parseValue($arrRowData, 'text', $this->getDefaultRenderSettings());
}
// HOOK: apply additional formatters to attribute.
$arrResult = $this->hookAdditionalFormatters($arrResult, $arrRowData, $strOutputFormat, $objSettings);
return $arrResult;
}
/**
* {@inheritdoc}
*
* This base implementation returns the value itself.
*/
public function getFilterUrlValue($varValue)
{
// We are parsing as text here as this was the way before this method was implemented. See #216.
$arrResult = $this->parseValue(array($this->getColName() => $varValue), 'text');
return urlencode($arrResult['text']);
}
/**
* {@inheritdoc}
*/
public function sortIds($arrIds, $strDirection)
{
// Base implementation, do not perform any sorting.
return $arrIds;
}
/**
* {@inheritdoc}
* Base implementation, do not perform any search;
*/
public function searchFor($strPattern)
{
return array();
}
/**
* {@inheritdoc}
*
* Base implementation, return empty array.
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function filterGreaterThan($varValue, $blnInclusive = false)
{
return array();
}
/**
* {@inheritdoc}
*
* Base implementation, return empty array.
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function filterLessThan($varValue, $blnInclusive = false)
{
return array();
}
/**
* {@inheritdoc}
* Base implementation, merge the result of filterLessThan() and filterGreaterThan().
*/
public function filterNotEqual($varValue)
{
return array_merge($this->filterLessThan($varValue), $this->filterGreaterThan($varValue));
}
/**
* {@inheritdoc}
* Base implementation, do not perform anything.
*/
public function modelSaved($objItem)
{
// No-op.
}
}