Completed
Push — master ( 83cdf5...cb2b08 )
by Vitaly
02:53
created

Material::selectText()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 14
rs 9.4286
cc 3
eloc 6
nc 3
nop 1
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)) {
0 ignored issues
show
Documentation introduced by
$fieldRecord is of type object<samsoncms\api\Field>, but the function expects a null|object<self>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
            $materials = array();
60
61
            // Get material identifiers by field
62
            $query->entity(CMS::MATERIAL_FIELD_RELATION_ENTITY)
63
                ->where('MaterialID', $materials)
0 ignored issues
show
Documentation introduced by
$materials is of type array, but the function expects a string|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Documentation introduced by
$materialIDs is of type array, but the function expects a string|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Documentation introduced by
$materialIDs is of type array, but the function expects a string|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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)
0 ignored issues
show
Bug introduced by
It seems like $materialIDs defined by parameter $materialIDs on line 125 can also be of type array; however, samsonframework\orm\QueryInterface::where() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
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())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $materialIds defined by null on line 151 can also be of type null; however, samsoncms\api\Material::byIDs() does only seem to accept string|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $materialIds defined by null on line 221 can also be of type null; however, samsoncms\api\Material::byIDs() does only seem to accept string|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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');
0 ignored issues
show
Bug introduced by
It seems like $materialIds defined by null on line 251 can also be of type null; however, samsoncms\api\Material::byIDs() does only seem to accept string|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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)
0 ignored issues
show
Bug introduced by
It seems like $url defined by parameter $url on line 270 can also be of type array; however, samsonframework\orm\QueryInterface::where() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
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();
0 ignored issues
show
Bug introduced by
The call to dbQuery() misses a required argument $class_name.

This check looks for function calls that miss required arguments.

Loading history...
293
294
        /** @var Field $fieldRecord Try to find this additional field */
295
        $fieldRecord = null;
296
        if (Field::byID($query, $fieldID, $fieldRecord)) {
0 ignored issues
show
Documentation introduced by
$fieldRecord is of type object<samsoncms\api\Field>, but the function expects a null|object<self>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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;
0 ignored issues
show
Documentation Bug introduced by
The property $Active was declared of type boolean, but 1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
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)) {
0 ignored issues
show
Documentation introduced by
$fieldID is of type string, but the function expects a null|object<self>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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 &copy(& $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)) {
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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)) {
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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;
0 ignored issues
show
Bug introduced by
The property MaterialID does not seem to exist in samson\activerecord\dbRecord.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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)) {
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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();
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
448
            $tableSelector = $structure->id;
449
        }
450
451
        /** If this table has columns */
452
        if (dbQuery('structurefield')
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
453
            ->cond("StructureID", $tableSelector)
454
            ->fields('FieldID', $dbTableFieldsIds)
0 ignored issues
show
Documentation introduced by
$dbTableFieldsIds is of type array, but the function expects a string|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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) {
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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')
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The call to add() misses a required argument $value.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$localizedFieldCond is of type object<samsonframework\orm\Condition>, but the function expects a string.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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')
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
506
                    ->cond('MaterialID', $tableMaterialIds)
0 ignored issues
show
Documentation introduced by
$tableMaterialIds is of type array, but the function expects a string|null.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
507
                    ->cond($localizationFieldCond);
0 ignored issues
show
Documentation introduced by
$localizationFieldCond is of type object<samsonframework\orm\Condition>, but the function expects a string|object<samson\act...cord\ArgumentInterface>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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