-
Notifications
You must be signed in to change notification settings - Fork 10
/
MetaModelAttributeTags.php
381 lines (348 loc) · 9.73 KB
/
MetaModelAttributeTags.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
<?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 AttributeTags
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @author Christian de la Haye <service@delahaye.de>
* @copyright The MetaModels team.
* @license LGPL.
* @filesource
*/
/**
* This is the MetaModelAttribute class for handling tag attributes.
*
* @package MetaModels
* @subpackage AttributeTags
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @author Christian de la Haye <service@delahaye.de>
*/
class MetaModelAttributeTags extends MetaModelAttributeComplex
{
/**
* when rendered via a template, this returns the values to be stored in the template.
*/
protected function prepareTemplate(MetaModelTemplate $objTemplate, $arrRowData, $objSettings = null)
{
parent::prepareTemplate($objTemplate, $arrRowData, $objSettings);
$objTemplate->alias = $this->get('tag_alias');
$objTemplate->value = $this->get('tag_column');
}
/**
* Determine the column to be used for alias.
* This is either the configured alias column or the id, if
* an alias column is absent.
*
* @return string the name of the column.
*/
public function getAliasCol()
{
$strColNameAlias = $this->get('tag_alias');
if (!$strColNameAlias)
{
$strColNameAlias = $this->get('tag_id');
}
return $strColNameAlias;
}
/////////////////////////////////////////////////////////////////
// interface IMetaModelAttribute
/////////////////////////////////////////////////////////////////
/**
* {@inheritdoc}
*/
public function getAttributeSettingNames()
{
return array_merge(parent::getAttributeSettingNames(), array(
'tag_table',
'tag_column',
'tag_id',
'tag_alias',
'tag_where',
'tag_sorting',
'mandatory',
'filterable',
'searchable',
));
}
/**
* {@inheritdoc}
*/
public function getFieldDefinition($arrOverrides = array())
{
// TODO: add tree support here.
$arrFieldDef=parent::getFieldDefinition($arrOverrides);
$arrFieldDef['inputType'] = 'checkbox';
$arrFieldDef['options'] = $this->getFilterOptions(NULL, false);
$arrFieldDef['eval']['includeBlankOption'] = true;
$arrFieldDef['eval']['multiple'] = true;
return $arrFieldDef;
}
/**
* {@inheritdoc}
*/
public function valueToWidget($varValue)
{
$strColNameAlias = $this->getAliasCol();
$arrResult = array();
if ($varValue)
{
foreach ($varValue as $arrValue)
{
$arrResult[] = $arrValue[$strColNameAlias];
}
}
return $arrResult;
}
/**
* {@inheritdoc}
*/
public function widgetToValue($varValue, $intId)
{
if ((!is_array($varValue)) || empty($varValue))
{
return array();
}
$arrSearch = array();
$arrParams = array();
foreach ($varValue as $strValue)
{
$arrSearch[] = '?';
$arrParams[] = $strValue;
}
$objDB = Database::getInstance();
$objValue = $objDB->prepare(sprintf('SELECT %1$s.* FROM %1$s WHERE %2$s IN (%3$s)',
$this->get('tag_table'),
$this->getAliasCol(),
implode(',', $arrSearch)
))
->execute($arrParams);
$strColNameId = $this->get('tag_id');
$arrResult = array();
while ($objValue->next())
{
$arrResult[$objValue->$strColNameId] = $objValue->row();
}
return $arrResult;
}
/**
* {@inheritdoc}
*
* Fetch filter options from foreign table.
*
*/
public function getFilterOptions($arrIds, $usedOnly)
{
$strTableName = $this->get('tag_table');
$strColNameId = $this->get('tag_id');
$strSortColumn = $this->get('tag_sorting');
$strColNameWhere = ($this->get('tag_where') ? html_entity_decode($this->get('tag_where')) : false);
$arrReturn = array();
if ($strTableName && $strColNameId && $strSortColumn)
{
$strColNameValue = $this->get('tag_column');
$strColNameAlias = $this->getAliasCol();
$objDB = Database::getInstance();
if ($arrIds)
{
if($usedOnly)
{
$strSQL = '
SELECT %1$s.*
FROM %1$s
LEFT JOIN tl_metamodel_tag_relation ON (
(tl_metamodel_tag_relation.att_id=?)
AND (tl_metamodel_tag_relation.value_id=%1$s.%2$s)
)
WHERE (tl_metamodel_tag_relation.item_id IN (%3$s)%5$s)
GROUP BY %1$s.%2$s
ORDER BY %4$s
';
} else {
$strSQL = '
SELECT %1$s.*
FROM %1$s
WHERE %1$s.%2$s IN (%3$s)%5$s
GROUP BY %1$s.%2$s
ORDER BY %4$s';
}
$objValue = $objDB->prepare(sprintf($strSQL, // Source
$strTableName, // 1
$strColNameId, // 2
implode(',', $arrIds), // 3
$strSortColumn, // 4
($strColNameWhere ? ' AND ('.$strColNameWhere.')' : '') //5
))->execute($this->get('id'));
} else {
if($usedOnly)
{
$strSQL = '
SELECT %1$s.*
FROM %1$s
INNER JOIN tl_metamodel_tag_relation as rel
ON (
(rel.att_id="%4$s") AND (rel.value_id=%1$s.%3$s)
)
WHERE rel.att_id=%4$s'
. ($strColNameWhere ? ' AND %5$s' : '') . '
GROUP BY %1$s.%3$s
ORDER BY %2$s';
}
else
{
$strSQL = '
SELECT %1$s.*
FROM %1$s'
. ($strColNameWhere ? ' WHERE %5$s' : '') . '
GROUP BY %1$s.%3$s
ORDER BY %3$s';
}
$objValue = $objDB->prepare(sprintf($strSQL, // Source
$strTableName, // 1
$strSortColumn, // 2
$strColNameId, // 3
$this->get('id'), // 4
$strColNameWhere // 5
))->execute();
}
while ($objValue->next())
{
$arrReturn[$objValue->$strColNameAlias] = $objValue->$strColNameValue;
}
}
return $arrReturn;
}
/**
* {@inheritdoc}
*/
public function searchFor($strPattern)
{
$objFilterRule = new MetaModelFilterRuleTags($this, $strPattern);
return $objFilterRule->getMatchingIds();
}
/////////////////////////////////////////////////////////////////
// interface IMetaModelAttributeComplex
/////////////////////////////////////////////////////////////////
public function getDataFor($arrIds)
{
$strTableName = $this->get('tag_table');
$strColNameId = $this->get('tag_id');
$arrReturn = array();
if ($strTableName && $strColNameId)
{
$objDB = Database::getInstance();
$strMetaModelTableName = $this->getMetaModel()->getTableName();
$strMetaModelTableNameId = $strMetaModelTableName.'_id';
$objValue = $objDB->prepare(sprintf('
SELECT %1$s.*, tl_metamodel_tag_relation.item_id AS %2$s
FROM %1$s
LEFT JOIN tl_metamodel_tag_relation ON (
(tl_metamodel_tag_relation.att_id=?)
AND (tl_metamodel_tag_relation.value_id=%1$s.%3$s)
)
WHERE tl_metamodel_tag_relation.item_id IN (%4$s)',
$strTableName, // 1
$strMetaModelTableNameId, // 2
$strColNameId, // 3
implode(',', $arrIds) // 4
))
->execute($this->get('id'));
while ($objValue->next())
{
if(!$arrReturn[$objValue->$strMetaModelTableNameId])
{
$arrReturn[$objValue->$strMetaModelTableNameId] = array();
}
$arrData = $objValue->row();
unset($arrData[$strMetaModelTableNameId]);
$arrReturn[$objValue->$strMetaModelTableNameId][$objValue->$strColNameId] = $arrData;
}
}
return $arrReturn;
}
public function setDataFor($arrValues)
{
$objDB = Database::getInstance();
$arrItemIds = array_map('intval', array_keys($arrValues));
sort($arrItemIds);
// load all existing tags for all items to be updated, keep the ordering to item Id
// so we can benefit from the batch deletion and insert algorithm.
$objExistingTagIds = $objDB->prepare(sprintf('
SELECT * FROM tl_metamodel_tag_relation
WHERE
att_id=?
AND item_id IN (%1$s)
ORDER BY item_id ASC
', implode(',', $arrItemIds)))
->execute($this->get('id'));
// now loop over all items and update the values for them.
// NOTE: we can not loop over the original array, as the item ids are not neccessarily
// sorted ascending by item id.
$arrSQLInsertValues = array();
foreach ($arrItemIds as $intItemId)
{
$arrTags = $arrValues[$intItemId];
if ($arrTags === NULL)
{
$arrTagIds = array();
} else {
$arrTagIds = array_map('intval', array_keys($arrTags));
}
$arrThisExisting = array();
// determine existing tags for this item.
if (($objExistingTagIds->item_id == $intItemId))
{
$arrThisExisting[] = $objExistingTagIds->value_id;
}
while ($objExistingTagIds->next() && ($objExistingTagIds->item_id == $intItemId))
{
$arrThisExisting[] = $objExistingTagIds->value_id;
}
// first pass, delete all not mentioned anymore.
$arrValuesToRemove = array_diff($arrThisExisting, $arrTagIds);
if ($arrValuesToRemove)
{
$objDB->prepare(sprintf('
DELETE FROM tl_metamodel_tag_relation
WHERE
att_id=?
AND item_id=?
AND value_id IN (%s)
', implode(',', $arrValuesToRemove)))
->execute($this->get('id'), $intItemId);
}
// second pass, add all new values in a row.
$arrValuesToAdd = array_diff($arrTagIds, $arrThisExisting);
if ($arrValuesToAdd)
{
foreach ($arrValuesToAdd as $intValueId)
{
$arrSQLInsertValues[] = sprintf('(%s,%s,%s)', $this->get('id'), $intItemId, $intValueId);
}
}
}
if ($arrSQLInsertValues)
{
$objDB->execute('INSERT INTO tl_metamodel_tag_relation (att_id, item_id, value_id) VALUES ' . implode(',', $arrSQLInsertValues));
}
}
public function unsetDataFor($arrIds)
{
if ($arrIds)
{
if (!is_array($arrIds))
throw new Exception('MetaModelAttributeTags::unsetDataFor() invalid parameter given! Array of ids is needed.', 1);
$objDB = Database::getInstance();
$objDB->prepare(sprintf('
DELETE FROM tl_metamodel_tag_relation
WHERE
att_id=?
AND item_id IN (%s)',
implode(',', $arrIds)))->execute($this->get('id'));
}
}
}