|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Vitaly Iegorov <[email protected]> |
|
4
|
|
|
* on 07.08.14 at 17:11 |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace samsoncms\api; |
|
7
|
|
|
|
|
8
|
|
|
use \samsonframework\orm\Condition; |
|
9
|
|
|
use \samsonframework\orm\QueryInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* SamsonCMS Material database record object. |
|
13
|
|
|
* This class extends default ActiveRecord material table record functionality. |
|
14
|
|
|
* @package samson\cms |
|
15
|
|
|
* @author Vitaly Egorov <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class Material extends \samson\activerecord\material |
|
18
|
|
|
{ |
|
19
|
|
|
/** Override table attributes for late static binding */ |
|
20
|
|
|
public static $_attributes = array(); |
|
21
|
|
|
public static $_sql_select = array(); |
|
22
|
|
|
public static $_sql_from = array(); |
|
23
|
|
|
public static $_own_group = array(); |
|
24
|
|
|
public static $_map = array(); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get current entity instances collection by navigation identifier. |
|
28
|
|
|
* |
|
29
|
|
|
* @param QueryInterface $query Database query |
|
30
|
|
|
* @param string $navigationID Navigation identifier |
|
31
|
|
|
* @param self[]|array|null $return Variable where request result would be returned |
|
32
|
|
|
* @return bool|self[] True if material entities has been found and $return is passed |
|
33
|
|
|
* or self[] if only two parameters is passed. |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function byNavigationID(QueryInterface $query, $navigationID, &$return = array()) |
|
36
|
|
|
{ |
|
37
|
|
|
/** @var \samson\activerecord\structurematerial[] $materials Navigation to material relation */ |
|
38
|
|
|
$materials = array(); |
|
39
|
|
|
if ($query->entity('\samson\activerecord\structurematerial') |
|
|
|
|
|
|
40
|
|
|
->cond('StructureID', $navigationID) |
|
41
|
|
|
->fields('MaterialID', $materials) |
|
42
|
|
|
) { |
|
43
|
|
|
$return = $query->entity(get_called_class()) |
|
|
|
|
|
|
44
|
|
|
->cond('MaterialID', $materials) |
|
45
|
|
|
->cond('Active', 1) |
|
46
|
|
|
->cond('Published', 1) |
|
47
|
|
|
->exec(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// If only one argument is passed - return null, otherwise bool |
|
51
|
|
|
return func_num_args() > 2 ? $return == null : $return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get material entities collection by URL(s). |
|
56
|
|
|
* @param QueryInterface $query Object for performing database queries |
|
57
|
|
|
* @param array|string $url Material URL or collection of material URLs |
|
58
|
|
|
* @param self[]|array|null $return Variable where request result would be returned |
|
59
|
|
|
* @return bool|self[] True if material entities has been found |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function byUrl(QueryInterface $query, $url, & $return = array()) |
|
62
|
|
|
{ |
|
63
|
|
|
// Get field record by identifier column |
|
64
|
|
|
$return = static::collectionByColumn($query, 'Url', $url); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
// If only one argument is passed - return null, otherwise bool |
|
67
|
|
|
return func_num_args() > 1 ? $return == null : $return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Set additional material field value by field identifier |
|
72
|
|
|
* @param string $fieldID Field identifier |
|
73
|
|
|
* @param string $value Value to be stored |
|
74
|
|
|
* @param string $locale Locale identifier |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setFieldByID($fieldID, $value, $locale = DEFAULT_LOCALE) |
|
77
|
|
|
{ |
|
78
|
|
|
// TODO: This should be removed |
|
79
|
|
|
/** @var QueryInterface $query This should be removed to use $this->database*/ |
|
80
|
|
|
$query = dbQuery(); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
/** @var Field $fieldRecord Try to find this additional field */ |
|
83
|
|
|
$fieldRecord = null; |
|
84
|
|
|
if (Field::byID($query, $fieldID, $fieldRecord)) { |
|
|
|
|
|
|
85
|
|
|
/** @var MaterialField $materialFieldRecord Try to find additional field value */ |
|
86
|
|
|
$materialFieldRecord = null; |
|
87
|
|
|
if (!MaterialField::byFieldIDAndMaterialID($query, $this->id, $fieldRecord->id, $materialFieldRecord)) { |
|
88
|
|
|
// Create new additional field value record if it does not exists |
|
89
|
|
|
$materialFieldRecord = new MaterialField(); |
|
90
|
|
|
$materialFieldRecord->FieldID = $fieldRecord->id; |
|
91
|
|
|
$materialFieldRecord->MaterialID = $this->id; |
|
92
|
|
|
$materialFieldRecord->Active = 1; |
|
93
|
|
|
$materialFieldRecord->locale = $locale; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Define which field should be filled |
|
97
|
|
|
switch ($fieldRecord->Type) { |
|
98
|
|
|
case 1: |
|
99
|
|
|
$valueFieldName = 'numeric_value'; |
|
100
|
|
|
break; |
|
101
|
|
|
case 2: |
|
102
|
|
|
$valueFieldName = 'key_value'; |
|
103
|
|
|
break; |
|
104
|
|
|
default: |
|
105
|
|
|
$valueFieldName = 'Value'; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// At this point we already have database record instance |
|
109
|
|
|
$fieldRecord->$valueFieldName = $value; |
|
110
|
|
|
$fieldRecord->save(); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get select additional field text value |
|
116
|
|
|
* TODO: Find where do we use it |
|
117
|
|
|
* @return string Select field text |
|
118
|
|
|
*/ |
|
119
|
|
|
public function selectText($fieldID) |
|
120
|
|
|
{ |
|
121
|
|
|
/** @var \samson\activerecord\field $field */ |
|
122
|
|
|
$field = null; |
|
123
|
|
|
if (dbQuery('field')->id($fieldID)->first($field)) { |
|
|
|
|
|
|
124
|
|
|
// If this entity has this field set |
|
125
|
|
|
if (isset($this[$field->Name]{0})) { |
|
126
|
|
|
$types = array(); |
|
127
|
|
|
foreach (explode(',', $field->Value) as $typeValue) { |
|
128
|
|
|
$typeValue = explode(':', $typeValue); |
|
129
|
|
|
$types[$typeValue[0]] = $typeValue[1]; |
|
130
|
|
|
} |
|
131
|
|
|
return $types[$this[$field->Name]]; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
// Value not set |
|
136
|
|
|
return ''; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get collection of images for material by gallery additional field selector. If none is passed |
|
141
|
|
|
* all images from gallery table would be returned for this material entity. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string|null $fieldSelector Additional field selector value |
|
144
|
|
|
* @param string $selector Additional field field name to search for |
|
145
|
|
|
* @return \samson\activerecord\gallery[] Collection of images in this gallery additional field for material |
|
146
|
|
|
*/ |
|
147
|
|
|
public function &gallery($fieldSelector = null, $selector = 'FieldID') |
|
148
|
|
|
{ |
|
149
|
|
|
/** @var \samson\activerecord\gallery[] $images Get material images for this gallery */ |
|
150
|
|
|
$images = array(); |
|
151
|
|
|
|
|
152
|
|
|
/* @var \samson\activerecord\field Get field object if we need to search it by other fields */ |
|
153
|
|
|
$field = null; |
|
|
|
|
|
|
154
|
|
|
if ($selector != 'FieldID') { |
|
155
|
|
|
$field = dbQuery('field')->cond($selector, $fieldSelector)->first(); |
|
|
|
|
|
|
156
|
|
|
$fieldSelector = $field->id; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
// Create query |
|
160
|
|
|
$query = dbQuery('materialfield'); |
|
161
|
|
|
|
|
162
|
|
|
// Add field filter if present |
|
163
|
|
|
if (isset($fieldSelector)) { |
|
164
|
|
|
$query->cond("FieldID", $fieldSelector); |
|
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** @var \samson\activerecord\materialfield $dbMaterialField Find material field gallery record */ |
|
168
|
|
|
$dbMaterialField = null; |
|
169
|
|
|
if ($query->cond('MaterialID', $this->id)->first($dbMaterialField)) { |
|
|
|
|
|
|
170
|
|
|
// Get material images for this materialfield |
|
171
|
|
|
if (dbQuery('gallery')->cond('materialFieldId', $dbMaterialField->id)->exec($images)) { |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $images; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Create copy of current object |
|
181
|
|
|
* @param mixed $clone Material for cloning |
|
182
|
|
|
* @param array $excludedFields excluded from materialfield fields identifiers |
|
183
|
|
|
* @returns void |
|
184
|
|
|
*/ |
|
185
|
|
|
public function ©(& $clone = null, $excludedFields = array()) |
|
186
|
|
|
{ |
|
187
|
|
|
// Create new instance by copying |
|
188
|
|
|
$clone = parent::copy($clone); |
|
189
|
|
|
|
|
190
|
|
|
/** @var \samson\activerecord\structurematerial[] $objects Create structure material relations */ |
|
191
|
|
|
$objects = array(); |
|
192
|
|
View Code Duplication |
if (dbQuery('structurematerial')->cond('MaterialID', $this->MaterialID)->exec($objects)) { |
|
|
|
|
|
|
193
|
|
|
foreach ($objects as $cmsNavigation) { |
|
194
|
|
|
/** @var \samson\activerecord\Record $copy */ |
|
195
|
|
|
$copy = $cmsNavigation->copy(); |
|
196
|
|
|
$copy->MaterialID = $clone->id; |
|
197
|
|
|
$copy->save(); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
/** @var \samson\activerecord\materialfield[] $objects Create material field relations */ |
|
201
|
|
|
$objects = array(); |
|
202
|
|
|
if (dbQuery('materialfield')->cond('MaterialID', $this->MaterialID)->exec($objects)) { |
|
|
|
|
|
|
203
|
|
|
foreach ($objects as $pMaterialField) { |
|
204
|
|
|
// Check if field is NOT excluded from copying |
|
205
|
|
|
if (!in_array($pMaterialField->FieldID, $excludedFields)) { |
|
206
|
|
|
/** @var \samson\activerecord\dbRecord $copy Copy instance */ |
|
207
|
|
|
$copy = $pMaterialField->copy(); |
|
208
|
|
|
$copy->MaterialID = $clone->id; |
|
|
|
|
|
|
209
|
|
|
$copy->save(); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** @var \samson\activerecord\gallery[] $objects Create gallery field relations */ |
|
215
|
|
|
$objects = array(); |
|
216
|
|
View Code Duplication |
if (dbQuery('gallery')->cond('MaterialID', $this->MaterialID)->exec($objects)) { |
|
|
|
|
|
|
217
|
|
|
foreach ($objects as $cmsGallery) { |
|
218
|
|
|
/** @var \samson\activerecord\Record $copy */ |
|
219
|
|
|
$copy = $cmsGallery->copy(); |
|
220
|
|
|
$copy->MaterialID = $clone->id; |
|
221
|
|
|
$copy->save(); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return $clone; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Function to retrieve this material table by specified field |
|
230
|
|
|
* @param string $tableSelector Selector to identify table structure |
|
231
|
|
|
* @param string $selector Database field by which search is performed |
|
232
|
|
|
* @param array $tableColumns Columns names list |
|
233
|
|
|
* @param string $externalHandler External handler to perform some extra code |
|
234
|
|
|
* @param array $params External handler params |
|
235
|
|
|
* @return array Collection of collections of table cells, represented as materialfield objects |
|
236
|
|
|
*/ |
|
237
|
|
|
public function getTable($tableSelector, $selector = 'StructureID', &$tableColumns = null, $externalHandler = null, $params = array()) |
|
238
|
|
|
{ |
|
239
|
|
|
/** @var array $resultTable Collection of collections of field cells */ |
|
240
|
|
|
$resultTable = array(); |
|
241
|
|
|
/** @var array $dbTableFieldsIds Array of table structure column identifiers */ |
|
242
|
|
|
$dbTableFieldsIds = array(); |
|
243
|
|
|
|
|
244
|
|
|
// Get structure object if we need to search it by other fields |
|
245
|
|
|
if ($selector != 'StructureID') { |
|
246
|
|
|
$structure = dbQuery('structure')->cond($selector, $tableSelector)->first(); |
|
|
|
|
|
|
247
|
|
|
$tableSelector = $structure->id; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** If this table has columns */ |
|
251
|
|
|
if (dbQuery('structurefield') |
|
|
|
|
|
|
252
|
|
|
->cond("StructureID", $tableSelector) |
|
253
|
|
|
->fields('FieldID', $dbTableFieldsIds) |
|
|
|
|
|
|
254
|
|
|
) { |
|
255
|
|
|
// Get localized and not localized fields |
|
256
|
|
|
$localizedFields = array(); |
|
257
|
|
|
$unlocalizedFields = array(); |
|
258
|
|
|
/** @var \samson\cms\CMSField $dbTableField Table column */ |
|
259
|
|
|
foreach (dbQuery('field')->order_by('priority')->cond('FieldID', $dbTableFieldsIds)->exec() as $field) { |
|
|
|
|
|
|
260
|
|
|
/** Add table columns names */ |
|
261
|
|
|
$tableColumns[] = $field->Name; |
|
262
|
|
|
if ($field->local == 1) { |
|
263
|
|
|
$localizedFields[] = $field->id; |
|
264
|
|
|
} else { |
|
265
|
|
|
$unlocalizedFields[] = $field->id; |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
// Query to get table rows(table materials) |
|
270
|
|
|
$tableQuery = dbQuery('material') |
|
|
|
|
|
|
271
|
|
|
->cond('parent_id', $this->MaterialID) |
|
272
|
|
|
->cond('Active', '1') |
|
273
|
|
|
->join('structurematerial') |
|
274
|
|
|
->cond('structurematerial_StructureID', $tableSelector) |
|
275
|
|
|
->order_by('priority'); |
|
276
|
|
|
|
|
277
|
|
|
// Call user function if exists |
|
278
|
|
|
if (is_callable($externalHandler)) { |
|
279
|
|
|
// Give it query as parameter |
|
280
|
|
|
call_user_func_array($externalHandler, array_merge(array(&$tableQuery), $params)); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
// Get table row materials |
|
284
|
|
|
$tableMaterialIds = array(); |
|
285
|
|
|
if ($tableQuery->fields('MaterialID', $tableMaterialIds)) { |
|
286
|
|
|
// Create field condition |
|
287
|
|
|
$localizationFieldCond = new Condition('or'); |
|
288
|
|
|
|
|
289
|
|
|
// Create localized condition |
|
290
|
|
|
if (sizeof($localizedFields)) { |
|
291
|
|
|
$localizedFieldCond = new Condition('and'); |
|
292
|
|
|
$localizedFieldCond->add('materialfield_FieldID', $localizedFields) |
|
293
|
|
|
->add('materialfield_locale', locale()); |
|
294
|
|
|
// Add this condition to condition group |
|
295
|
|
|
$localizationFieldCond->add($localizedFieldCond); |
|
|
|
|
|
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
// Create not localized condition |
|
299
|
|
|
if (sizeof($unlocalizedFields)) { |
|
300
|
|
|
$localizationFieldCond->add('materialfield_FieldID', $unlocalizedFields); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
// Create db query |
|
304
|
|
|
$materialFieldQuery = dbQuery('materialfield') |
|
|
|
|
|
|
305
|
|
|
->cond('MaterialID', $tableMaterialIds) |
|
|
|
|
|
|
306
|
|
|
->cond($localizationFieldCond); |
|
|
|
|
|
|
307
|
|
|
|
|
308
|
|
|
// Flip field identifiers as keys |
|
309
|
|
|
$tableColumnIds = array_flip($dbTableFieldsIds); |
|
310
|
|
|
$resultTable = array_flip($tableMaterialIds); |
|
311
|
|
|
|
|
312
|
|
|
/** @var \samson\activerecord\material $dbTableRow Material object (table row) */ |
|
313
|
|
|
foreach ($materialFieldQuery->exec() as $mf) { |
|
314
|
|
|
if (!is_array($resultTable[$mf['MaterialID']])) { |
|
315
|
|
|
$resultTable[$mf['MaterialID']] = array(); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
$resultTable[$mf['MaterialID']][$tableColumnIds[$mf->FieldID]] = |
|
319
|
|
|
!empty($mf->Value) ? $mf->Value : (!empty($mf->numeric_value) ? $mf->numeric_value : $mf->key_value); |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
return array_values($resultTable); |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: