|
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 samson\activerecord\dbQuery; |
|
9
|
|
|
use \samsonframework\orm\Condition; |
|
10
|
|
|
use samsonframework\orm\Query; |
|
11
|
|
|
use \samsonframework\orm\QueryInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* SamsonCMS Material database record object. |
|
15
|
|
|
* This class extends default ActiveRecord material table record functionality. |
|
16
|
|
|
* @package samson\cms |
|
17
|
|
|
* @author Vitaly Egorov <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class Material extends \samson\activerecord\material |
|
20
|
|
|
{ |
|
21
|
|
|
/** Override table attributes for late static binding */ |
|
22
|
|
|
public static $_attributes = array(); |
|
23
|
|
|
public static $_sql_select = array(); |
|
24
|
|
|
public static $_sql_from = array(); |
|
25
|
|
|
public static $_own_group = array(); |
|
26
|
|
|
public static $_map = array(); |
|
27
|
|
|
|
|
28
|
|
|
/** @var integer Primary field */ |
|
29
|
|
|
public $MaterialID; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string Unique identifier */ |
|
32
|
|
|
public $Url; |
|
33
|
|
|
|
|
34
|
|
|
/** @var bool Internal existence flag */ |
|
35
|
|
|
public $Active; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get identifiers collection by field identifier and its value. |
|
39
|
|
|
* Method is optimized for performance. |
|
40
|
|
|
* |
|
41
|
|
|
* @param QueryInterface $query Database query instance |
|
42
|
|
|
* @param string $fieldID Additional field identifier |
|
43
|
|
|
* @param string $fieldValue Additional field value for searching |
|
44
|
|
|
* @param array|null $return Variable where request result would be returned |
|
45
|
|
|
* @param array $materialIDs Collection of material identifiers for filtering query |
|
46
|
|
|
* @return bool|array True if material entities has been found and $return is passed |
|
47
|
|
|
* or identifiers collection if only two parameters is passed. |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function idsByFieldValue( |
|
50
|
|
|
QueryInterface $query, |
|
51
|
|
|
$fieldID, |
|
52
|
|
|
$fieldValue, |
|
53
|
|
|
&$return = array(), |
|
54
|
|
|
$materialIDs = null |
|
55
|
|
|
) { |
|
56
|
|
|
/** @var Field $fieldRecord We need to have field record */ |
|
57
|
|
|
$fieldRecord = null; |
|
58
|
|
|
if (Field::byID($query, $fieldID, $fieldRecord)) { |
|
|
|
|
|
|
59
|
|
|
$materials = array(); |
|
60
|
|
|
|
|
61
|
|
|
// Get material identifiers by field |
|
62
|
|
|
$query->entity(CMS::MATERIAL_FIELD_RELATION_ENTITY) |
|
63
|
|
|
->where('MaterialID', $materials) |
|
|
|
|
|
|
64
|
|
|
->where('Active', 1) |
|
65
|
|
|
->where('FieldID', $fieldID) |
|
66
|
|
|
->where($fieldRecord->valueFieldName(), $fieldValue); |
|
67
|
|
|
|
|
68
|
|
|
// Add material identifier filter if passed |
|
69
|
|
|
if (isset($materialIDs)) { |
|
70
|
|
|
$query->where('MaterialID', $materialIDs); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Perform database query and get only material identifiers collection |
|
74
|
|
|
$return = $query->fields('MaterialID'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// If only one argument is passed - return null, otherwise bool |
|
78
|
|
|
return func_num_args() > 3 ? sizeof($return) : $return; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get current entity identifiers collection by navigation identifier. |
|
83
|
|
|
* |
|
84
|
|
|
* @param QueryInterface $query Database query |
|
85
|
|
|
* @param string $navigationID Navigation identifier |
|
86
|
|
|
* @param array $return Variable where request result would be returned |
|
87
|
|
|
* @param array $materialIDs Collection of material identifiers for filtering query |
|
88
|
|
|
* @return bool|array True if material entities has been found and $return is passed |
|
89
|
|
|
* or collection of identifiers if only two parameters is passed. |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function idsByNavigationID( |
|
92
|
|
|
QueryInterface $query, |
|
93
|
|
|
$navigationID, |
|
94
|
|
|
&$return = array(), |
|
95
|
|
|
$materialIDs = null |
|
96
|
|
|
) { |
|
97
|
|
|
// Prepare query |
|
98
|
|
|
$query->entity(CMS::MATERIAL_NAVIGATION_RELATION_ENTITY) |
|
99
|
|
|
->where('StructureID', $navigationID) |
|
100
|
|
|
->where('Active', 1); |
|
101
|
|
|
|
|
102
|
|
|
// Add material identifier filter if passed |
|
103
|
|
|
if (isset($materialIDs)) { |
|
104
|
|
|
$query->where('MaterialID', $materialIDs); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// Perform database query and get only material identifiers collection |
|
108
|
|
|
$return = $query->fields('MaterialID'); |
|
109
|
|
|
|
|
110
|
|
|
// If only one argument is passed - return null, otherwise bool |
|
111
|
|
|
return func_num_args() > 2 ? sizeof($return) : $return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get current entity instances collection by their identifiers. |
|
116
|
|
|
* Method can accept different query executors. |
|
117
|
|
|
* |
|
118
|
|
|
* @param QueryInterface $query Database query |
|
119
|
|
|
* @param string|array $materialIDs Material identifier or their colleciton |
|
120
|
|
|
* @param self[]|array|null $return Variable where request result would be returned |
|
121
|
|
|
* @param string $executor Method name for query execution |
|
122
|
|
|
* @return bool|self[] True if material entities has been found and $return is passed |
|
123
|
|
|
* or self[] if only two parameters is passed. |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function byIDs(QueryInterface $query, $materialIDs, &$return = array(), $executor = 'exec') |
|
126
|
|
|
{ |
|
127
|
|
|
$return = $query->entity(get_called_class()) |
|
128
|
|
|
->where('MaterialID', $materialIDs) |
|
|
|
|
|
|
129
|
|
|
->where('Active', 1) |
|
130
|
|
|
->where('Published', 1) |
|
131
|
|
|
->$executor(); |
|
132
|
|
|
|
|
133
|
|
|
// If only one argument is passed - return null, otherwise bool |
|
134
|
|
|
return func_num_args() > 2 ? sizeof($return) : $return; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Get self[] by field identifier and its value. |
|
139
|
|
|
* Method is optimized for performance. |
|
140
|
|
|
* |
|
141
|
|
|
* @param QueryInterface $query Database query instance |
|
142
|
|
|
* @param string $fieldID Additional field identifier |
|
143
|
|
|
* @param string $fieldValue Additional field value for searching |
|
144
|
|
|
* @param self[]|array|null $return Variable where request result would be returned |
|
145
|
|
|
* @return bool|self[] True if material entities has been found and $return is passed |
|
146
|
|
|
* or self[] if only two parameters is passed. |
|
147
|
|
|
*/ |
|
148
|
|
View Code Duplication |
public static function byFieldValue(QueryInterface $query, $fieldID, $fieldValue, &$return = array()) |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
|
|
/** @var array $materialIds Collection of entity identifiers filtered by additional field */ |
|
151
|
|
|
$materialIds = null; |
|
152
|
|
|
if (static::idsByFieldValue($query, $fieldID, $fieldValue, $materialIds)) { |
|
153
|
|
|
static::byIDs($query, $materialIds, $return); |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
// If only one argument is passed - return null, otherwise bool |
|
157
|
|
|
return func_num_args() > 3 ? sizeof($return) : $return; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Get current entity instances collection by navigation identifier. |
|
162
|
|
|
* |
|
163
|
|
|
* @param QueryInterface $query Database query |
|
164
|
|
|
* @param string $navigationID Navigation identifier |
|
165
|
|
|
* @param self[]|array|null $return Variable where request result would be returned |
|
166
|
|
|
* @return bool|self[] True if material entities has been found and $return is passed |
|
167
|
|
|
* or self[] if only two parameters is passed. |
|
168
|
|
|
*/ |
|
169
|
|
View Code Duplication |
public static function byNavigationID(QueryInterface $query, $navigationID, &$return = array()) |
|
|
|
|
|
|
170
|
|
|
{ |
|
171
|
|
|
/** @var array $materialIds Collection of entity identifiers filtered by additional field */ |
|
172
|
|
|
$materialIds = null; |
|
173
|
|
|
if (static::idsByNavigationID($query, $navigationID, $materialIds)) { |
|
174
|
|
|
static::byIDs($query, $materialIds, $return); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
// If only one argument is passed - return null, otherwise bool |
|
178
|
|
|
return func_num_args() > 2 ? sizeof($return) : $return; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get current entity instances amount by navigation identifier. |
|
183
|
|
|
* |
|
184
|
|
|
* @param QueryInterface $query Database query |
|
185
|
|
|
* @param string $navigationID Navigation identifier |
|
186
|
|
|
* @param self[]|array|null $return Variable where request result would be returned |
|
187
|
|
|
* @return bool|self[] True if material entities has been found and $return is passed |
|
188
|
|
|
* or self[] if only two parameters is passed. |
|
189
|
|
|
*/ |
|
190
|
|
View Code Duplication |
public static function amountByNavigationID(QueryInterface $query, $navigationID, &$return = array()) |
|
|
|
|
|
|
191
|
|
|
{ |
|
192
|
|
|
/** @var array $materialIds Collection of entity identifiers filtered by additional field */ |
|
193
|
|
|
$materialIds = null; |
|
194
|
|
|
if (static::idsByNavigationID($query, $navigationID, $materialIds)) { |
|
195
|
|
|
static::byIDs($query, $materialIds, $return, 'count'); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
// If only one argument is passed - return null, otherwise bool |
|
199
|
|
|
return func_num_args() > 2 ? sizeof($return) : $return; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Get current entity instances collection by navigation identifier and additional field value. |
|
204
|
|
|
* |
|
205
|
|
|
* @param QueryInterface $query Database query |
|
206
|
|
|
* @param string $navigationID Navigation identifier |
|
207
|
|
|
* @param string $fieldID Additional field identifier |
|
208
|
|
|
* @param string $fieldValue Additional field value for searching |
|
209
|
|
|
* @param self[]|array|null $return Variable where request result would be returned |
|
210
|
|
|
* @return bool|self[] True if material entities has been found and $return is passed |
|
211
|
|
|
* or self[] if only two parameters is passed. |
|
212
|
|
|
*/ |
|
213
|
|
View Code Duplication |
public static function byNavigationIdAndFieldValue( |
|
|
|
|
|
|
214
|
|
|
QueryInterface $query, |
|
215
|
|
|
$navigationID, |
|
216
|
|
|
$fieldID, |
|
217
|
|
|
$fieldValue, |
|
218
|
|
|
&$return = array() |
|
219
|
|
|
) { |
|
220
|
|
|
/** @var array $materialIds Collection of entity identifiers filtered by additional field */ |
|
221
|
|
|
$materialIds = null; |
|
222
|
|
|
if (static::idsByNavigationID($query, $navigationID, $materialIds)) { |
|
223
|
|
|
if (static::idsByFieldValue($query, $fieldID, $fieldValue, $materialIds, $materialIds)) { |
|
224
|
|
|
static::byIDs($query, $materialIds, $return); |
|
|
|
|
|
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
// If only one argument is passed - return null, otherwise bool |
|
229
|
|
|
return func_num_args() > 4 ? sizeof($return) : $return; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Get current entity instances amount by navigation identifier and additional field value. |
|
234
|
|
|
* |
|
235
|
|
|
* @param QueryInterface $query Database query |
|
236
|
|
|
* @param string $navigationID Navigation identifier |
|
237
|
|
|
* @param string $fieldID Additional field identifier |
|
238
|
|
|
* @param string $fieldValue Additional field value for searching |
|
239
|
|
|
* @param self[]|array|null $return Variable where request result would be returned |
|
240
|
|
|
* @return bool|self[] True if material entities has been found and $return is passed |
|
241
|
|
|
* or self[] if only two parameters is passed. |
|
242
|
|
|
*/ |
|
243
|
|
View Code Duplication |
public static function amountByNavigationIdAndFieldValue( |
|
|
|
|
|
|
244
|
|
|
QueryInterface $query, |
|
245
|
|
|
$navigationID, |
|
246
|
|
|
$fieldID, |
|
247
|
|
|
$fieldValue, |
|
248
|
|
|
&$return = array() |
|
249
|
|
|
) { |
|
250
|
|
|
/** @var array $materialIds Collection of entity identifiers filtered by additional field */ |
|
251
|
|
|
$materialIds = null; |
|
252
|
|
|
if (static::idsByNavigationID($query, $navigationID, $materialIds)) { |
|
253
|
|
|
if (static::idsByFieldValue($query, $fieldID, $fieldValue, $materialIds, $materialIds)) { |
|
254
|
|
|
static::byIDs($query, $materialIds, $return, 'count'); |
|
|
|
|
|
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
// If only one argument is passed - return null, otherwise bool |
|
259
|
|
|
return func_num_args() > 4 ? sizeof($return) : $return; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Get material entity by URL(s). |
|
264
|
|
|
* |
|
265
|
|
|
* @param QueryInterface $query Object for performing database queries |
|
266
|
|
|
* @param array|string $url Material URL or collection of material URLs |
|
267
|
|
|
* @param self|array|null $return Variable where request result would be returned |
|
268
|
|
|
* @return bool|self True if material entities has been found |
|
269
|
|
|
*/ |
|
270
|
|
|
public static function byUrl(QueryInterface $query, $url, & $return = array()) |
|
271
|
|
|
{ |
|
272
|
|
|
// Get entities by filtered identifiers |
|
273
|
|
|
$return = $query->entity(get_called_class()) |
|
274
|
|
|
->where('Url', $url) |
|
|
|
|
|
|
275
|
|
|
->where('Active', 1) |
|
276
|
|
|
->first(); |
|
277
|
|
|
|
|
278
|
|
|
// If only one argument is passed - return null, otherwise bool |
|
279
|
|
|
return func_num_args() > 2 ? $return !== null : $return; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Set additional material field value by field identifier |
|
284
|
|
|
* @param string $fieldID Field identifier |
|
285
|
|
|
* @param string $value Value to be stored |
|
286
|
|
|
* @param string $locale Locale identifier |
|
287
|
|
|
*/ |
|
288
|
|
|
public function setFieldByID($fieldID, $value, $locale = DEFAULT_LOCALE) |
|
289
|
|
|
{ |
|
290
|
|
|
// TODO: This should be removed |
|
291
|
|
|
/** @var QueryInterface $query This should be removed to use $this->database*/ |
|
292
|
|
|
$query = dbQuery(); |
|
|
|
|
|
|
293
|
|
|
|
|
294
|
|
|
/** @var Field $fieldRecord Try to find this additional field */ |
|
295
|
|
|
$fieldRecord = null; |
|
296
|
|
|
if (Field::byID($query, $fieldID, $fieldRecord)) { |
|
|
|
|
|
|
297
|
|
|
/** @var MaterialField[] $materialFieldRecord Try to find additional field value */ |
|
298
|
|
|
$materialFieldRecord = null; |
|
299
|
|
|
if (!MaterialField::byFieldIDAndMaterialID($query, $this->id, $fieldRecord->id, $materialFieldRecord)) { |
|
300
|
|
|
// Create new additional field value record if it does not exists |
|
301
|
|
|
$materialFieldRecord = new MaterialField(); |
|
302
|
|
|
$materialFieldRecord->FieldID = $fieldRecord->id; |
|
303
|
|
|
$materialFieldRecord->MaterialID = $this->id; |
|
304
|
|
|
$materialFieldRecord->Active = 1; |
|
|
|
|
|
|
305
|
|
|
$materialFieldRecord->locale = $locale; |
|
306
|
|
|
} else { // Get first record(actually it should be only one) |
|
307
|
|
|
$materialFieldRecord = array_shift($materialFieldRecord); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
// At this point we already have database record instance |
|
311
|
|
|
$valueFieldName = $fieldRecord->valueFieldName(); |
|
312
|
|
|
$materialFieldRecord->$valueFieldName = $value; |
|
313
|
|
|
$materialFieldRecord->save(); |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* Get select additional field text value. |
|
319
|
|
|
* |
|
320
|
|
|
* @param string $fieldID Field identifier |
|
321
|
|
|
* @return string Select field text |
|
322
|
|
|
*/ |
|
323
|
|
|
public function selectText($fieldID) |
|
324
|
|
|
{ |
|
325
|
|
|
/** @var Field $field */ |
|
326
|
|
|
$field = null; |
|
327
|
|
|
if (Field::byID(new Query('\samsoncms\api\Field', $this->database), $fieldID, $fieldID)) { |
|
|
|
|
|
|
328
|
|
|
// If this entity has this field set |
|
329
|
|
|
if (isset($this[$field->Name]{0})) { |
|
330
|
|
|
return $field->options($this[$field->Name]); |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
// Value not set |
|
335
|
|
|
return ''; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* Get collection of images for material by gallery additional field selector. If none is passed |
|
340
|
|
|
* all images from gallery table would be returned for this material entity. |
|
341
|
|
|
* |
|
342
|
|
|
* @param string|null $fieldSelector Additional field selector value |
|
343
|
|
|
* @param string $selector Additional field field name to search for |
|
344
|
|
|
* @return \samsonframework\orm\RecordInterface[] Collection of images in this gallery additional field for material |
|
345
|
|
|
*/ |
|
346
|
|
|
public function &gallery($fieldSelector = null, $selector = 'FieldID') |
|
347
|
|
|
{ |
|
348
|
|
|
/** @var \samsonframework\orm\RecordInterface[] $images Get material images for this gallery */ |
|
349
|
|
|
$images = array(); |
|
350
|
|
|
|
|
351
|
|
|
// Create query |
|
352
|
|
|
$query = new dbQuery(); |
|
353
|
|
|
|
|
354
|
|
|
$query->entity(CMS::MATERIAL_FIELD_RELATION_ENTITY); |
|
355
|
|
|
|
|
356
|
|
|
/* @var Field Get field object if we need to search it by other fields */ |
|
357
|
|
|
$field = null; |
|
358
|
|
|
if ($selector != 'FieldID' && Field::oneByColumn($query, $selector, $fieldSelector)) { |
|
359
|
|
|
$fieldSelector = $field->id; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
// Add field filter if present |
|
363
|
|
|
if (isset($fieldSelector)) { |
|
364
|
|
|
$query->where("FieldID", $fieldSelector); |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
/** @var \samson\activerecord\materialfield $dbMaterialField Find material field gallery record */ |
|
368
|
|
|
$dbMaterialField = null; |
|
369
|
|
|
if ($query->where('MaterialID', $this->id)->first($dbMaterialField)) { |
|
370
|
|
|
// Get material images for this materialfield |
|
371
|
|
|
$images = $query->entity('samson\activerecord\gallery') |
|
372
|
|
|
->where('materialFieldId', $dbMaterialField->id) |
|
373
|
|
|
->exec(); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
return $images; |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* Create copy of current object. |
|
381
|
|
|
* |
|
382
|
|
|
* @param mixed $clone Material for cloning |
|
383
|
|
|
* @param array $excludedFields excluded from materialfield fields identifiers |
|
384
|
|
|
* @returns void |
|
385
|
|
|
*/ |
|
386
|
|
|
public function ©(& $clone = null, $excludedFields = array()) |
|
387
|
|
|
{ |
|
388
|
|
|
// Create new instance by copying |
|
389
|
|
|
$clone = parent::copy($clone); |
|
390
|
|
|
|
|
391
|
|
|
/** @var \samson\activerecord\structurematerial[] $objects Create structure material relations */ |
|
392
|
|
|
$objects = array(); |
|
393
|
|
View Code Duplication |
if (dbQuery('structurematerial')->cond('MaterialID', $this->MaterialID)->exec($objects)) { |
|
|
|
|
|
|
394
|
|
|
foreach ($objects as $cmsNavigation) { |
|
395
|
|
|
/** @var \samson\activerecord\Record $copy */ |
|
396
|
|
|
$copy = $cmsNavigation->copy(); |
|
397
|
|
|
$copy->MaterialID = $clone->id; |
|
398
|
|
|
$copy->save(); |
|
399
|
|
|
} |
|
400
|
|
|
} |
|
401
|
|
|
/** @var \samson\activerecord\materialfield[] $objects Create material field relations */ |
|
402
|
|
|
$objects = array(); |
|
403
|
|
|
if (dbQuery('materialfield')->cond('MaterialID', $this->MaterialID)->exec($objects)) { |
|
|
|
|
|
|
404
|
|
|
foreach ($objects as $pMaterialField) { |
|
405
|
|
|
// Check if field is NOT excluded from copying |
|
406
|
|
|
if (!in_array($pMaterialField->FieldID, $excludedFields)) { |
|
407
|
|
|
/** @var \samson\activerecord\dbRecord $copy Copy instance */ |
|
408
|
|
|
$copy = $pMaterialField->copy(); |
|
409
|
|
|
$copy->MaterialID = $clone->id; |
|
|
|
|
|
|
410
|
|
|
$copy->save(); |
|
411
|
|
|
} |
|
412
|
|
|
} |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
/** @var \samson\activerecord\gallery[] $objects Create gallery field relations */ |
|
416
|
|
|
$objects = array(); |
|
417
|
|
View Code Duplication |
if (dbQuery('gallery')->cond('MaterialID', $this->MaterialID)->exec($objects)) { |
|
|
|
|
|
|
418
|
|
|
foreach ($objects as $cmsGallery) { |
|
419
|
|
|
/** @var \samson\activerecord\Record $copy */ |
|
420
|
|
|
$copy = $cmsGallery->copy(); |
|
421
|
|
|
$copy->MaterialID = $clone->id; |
|
422
|
|
|
$copy->save(); |
|
423
|
|
|
} |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
return $clone; |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
|
|
/** |
|
430
|
|
|
* Function to retrieve this material table by specified field |
|
431
|
|
|
* @param string $tableSelector Selector to identify table structure |
|
432
|
|
|
* @param string $selector Database field by which search is performed |
|
433
|
|
|
* @param array $tableColumns Columns names list |
|
434
|
|
|
* @param string $externalHandler External handler to perform some extra code |
|
435
|
|
|
* @param array $params External handler params |
|
436
|
|
|
* @return array Collection of collections of table cells, represented as materialfield objects |
|
437
|
|
|
*/ |
|
438
|
|
|
public function getTable($tableSelector, $selector = 'StructureID', &$tableColumns = null, $externalHandler = null, $params = array()) |
|
439
|
|
|
{ |
|
440
|
|
|
/** @var array $resultTable Collection of collections of field cells */ |
|
441
|
|
|
$resultTable = array(); |
|
442
|
|
|
/** @var array $dbTableFieldsIds Array of table structure column identifiers */ |
|
443
|
|
|
$dbTableFieldsIds = array(); |
|
444
|
|
|
|
|
445
|
|
|
// Get structure object if we need to search it by other fields |
|
446
|
|
|
if ($selector != 'StructureID') { |
|
447
|
|
|
$structure = dbQuery('structure')->cond($selector, $tableSelector)->first(); |
|
|
|
|
|
|
448
|
|
|
$tableSelector = $structure->id; |
|
449
|
|
|
} |
|
450
|
|
|
|
|
451
|
|
|
/** If this table has columns */ |
|
452
|
|
|
if (dbQuery('structurefield') |
|
|
|
|
|
|
453
|
|
|
->cond("StructureID", $tableSelector) |
|
454
|
|
|
->fields('FieldID', $dbTableFieldsIds) |
|
|
|
|
|
|
455
|
|
|
) { |
|
456
|
|
|
// Get localized and not localized fields |
|
457
|
|
|
$localizedFields = array(); |
|
458
|
|
|
$unlocalizedFields = array(); |
|
459
|
|
|
/** @var \samson\cms\CMSField $dbTableField Table column */ |
|
460
|
|
|
foreach (dbQuery('field')->order_by('priority')->cond('FieldID', $dbTableFieldsIds)->exec() as $field) { |
|
|
|
|
|
|
461
|
|
|
/** Add table columns names */ |
|
462
|
|
|
$tableColumns[] = $field->Name; |
|
463
|
|
|
if ($field->local == 1) { |
|
464
|
|
|
$localizedFields[] = $field->id; |
|
465
|
|
|
} else { |
|
466
|
|
|
$unlocalizedFields[] = $field->id; |
|
467
|
|
|
} |
|
468
|
|
|
} |
|
469
|
|
|
|
|
470
|
|
|
// Query to get table rows(table materials) |
|
471
|
|
|
$tableQuery = dbQuery('material') |
|
|
|
|
|
|
472
|
|
|
->cond('parent_id', $this->MaterialID) |
|
473
|
|
|
->cond('Active', '1') |
|
474
|
|
|
->join('structurematerial') |
|
475
|
|
|
->cond('structurematerial_StructureID', $tableSelector) |
|
476
|
|
|
->order_by('priority'); |
|
477
|
|
|
|
|
478
|
|
|
// Call user function if exists |
|
479
|
|
|
if (is_callable($externalHandler)) { |
|
480
|
|
|
// Give it query as parameter |
|
481
|
|
|
call_user_func_array($externalHandler, array_merge(array(&$tableQuery), $params)); |
|
482
|
|
|
} |
|
483
|
|
|
|
|
484
|
|
|
// Get table row materials |
|
485
|
|
|
$tableMaterialIds = array(); |
|
486
|
|
|
if ($tableQuery->fields('MaterialID', $tableMaterialIds)) { |
|
487
|
|
|
// Create field condition |
|
488
|
|
|
$localizationFieldCond = new Condition('or'); |
|
489
|
|
|
|
|
490
|
|
|
// Create localized condition |
|
491
|
|
|
if (sizeof($localizedFields)) { |
|
492
|
|
|
$localizedFieldCond = new Condition('and'); |
|
493
|
|
|
$localizedFieldCond->add('materialfield_FieldID', $localizedFields) |
|
494
|
|
|
->add('materialfield_locale', locale()); |
|
495
|
|
|
// Add this condition to condition group |
|
496
|
|
|
$localizationFieldCond->add($localizedFieldCond); |
|
|
|
|
|
|
497
|
|
|
} |
|
498
|
|
|
|
|
499
|
|
|
// Create not localized condition |
|
500
|
|
|
if (sizeof($unlocalizedFields)) { |
|
501
|
|
|
$localizationFieldCond->add('materialfield_FieldID', $unlocalizedFields); |
|
502
|
|
|
} |
|
503
|
|
|
|
|
504
|
|
|
// Create db query |
|
505
|
|
|
$materialFieldQuery = dbQuery('materialfield') |
|
|
|
|
|
|
506
|
|
|
->cond('MaterialID', $tableMaterialIds) |
|
|
|
|
|
|
507
|
|
|
->cond($localizationFieldCond); |
|
|
|
|
|
|
508
|
|
|
|
|
509
|
|
|
// Flip field identifiers as keys |
|
510
|
|
|
$tableColumnIds = array_flip($dbTableFieldsIds); |
|
511
|
|
|
$resultTable = array_flip($tableMaterialIds); |
|
512
|
|
|
|
|
513
|
|
|
/** @var \samson\activerecord\material $dbTableRow Material object (table row) */ |
|
514
|
|
|
foreach ($materialFieldQuery->exec() as $mf) { |
|
515
|
|
|
if (!is_array($resultTable[$mf['MaterialID']])) { |
|
516
|
|
|
$resultTable[$mf['MaterialID']] = array(); |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
$resultTable[$mf['MaterialID']][$tableColumnIds[$mf->FieldID]] = |
|
520
|
|
|
!empty($mf->Value) ? $mf->Value : (!empty($mf->numeric_value) ? $mf->numeric_value : $mf->key_value); |
|
521
|
|
|
} |
|
522
|
|
|
} |
|
523
|
|
|
} |
|
524
|
|
|
|
|
525
|
|
|
return array_values($resultTable); |
|
526
|
|
|
} |
|
527
|
|
|
} |
|
528
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: