Completed
Push — master ( f08d75...32d600 )
by Vitaly
04:20
created

Material::byNavigationIdAndFieldValue()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 6
Ratio 28.57 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
dl 6
loc 21
c 2
b 1
f 1
rs 9.0534
cc 4
eloc 13
nc 6
nop 5
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 identifiers collection by field identifier and its value.
28
     * Method is optimized for performance.
29
     *
30
     * @param QueryInterface $query Database query instance
31
     * @param string $fieldID Additional field identifier
32
     * @param string $fieldValue Additional field value for searching
33
     * @param array|null $return Variable where request result would be returned
34
     * @param array $materialIDs Collection of material identifiers for filtering query
35
     * @return bool|array True if material entities has been found and $return is passed
36
     *                      or identifiers collection if only two parameters is passed.
37
     */
38
    public static function idsByFieldValue(
39
        QueryInterface $query,
40
        $fieldID,
41
        $fieldValue,
42
        &$return = array(),
43
        $materialIDs = null
44
    ) {
45
        // We need to have field record
46
        if (Field::byID($query, $fieldID, $fieldRecord)) {
47
            $materials = array();
48
49
            // Get material identifiers by field
50
            $query->entity(CMS::MATERIAL_FIELD_RELATION_ENTITY)
51
                ->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...
52
                ->where('Active', 1)
53
                ->where('FieldID', $fieldID)
54
                ->where($fieldRecord->valueFieldName(), $fieldValue);
55
56
            // Add material identifier filter if passed
57
            if (isset($materialIDs)) {
58
                $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...
59
            }
60
61
            // Perform database query and get only material identifiers collection
62
            $return = $query->fields($materials);
0 ignored issues
show
Documentation introduced by
$materials is of type array, 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...
63
        }
64
65
        // If only one argument is passed - return null, otherwise bool
66
        return func_num_args() > 3 ? $return == null : $return;
67
    }
68
69
    /**
70
     * Get current entity identifiers collection by navigation identifier.
71
     *
72
     * @param QueryInterface $query Database query
73
     * @param string $navigationID Navigation identifier
74
     * @param array $return Variable where request result would be returned
75
     * @param array $materialIDs Collection of material identifiers for filtering query
76
     * @return bool|array True if material entities has been found and $return is passed
77
     *                      or collection of identifiers if only two parameters is passed.
78
     */
79 View Code Duplication
    public static function idsByNavigationID(
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...
80
        QueryInterface $query,
81
        $navigationID,
82
        &$return = array(),
83
        $materialIDs = null
84
    ) {
85
        // Prepare query
86
         $query->entity(CMS::MATERIAL_NAVIGATION_RELATION_ENTITY)
87
            ->where('StructureID', $navigationID)
88
            ->where('Active', 1);
89
90
        // Add material identifier filter if passed
91
        if (isset($materialIDs)) {
92
            $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...
93
        }
94
95
        // Perform database query and get only material identifiers collection
96
        $return = $query->fields('MaterialID');
97
98
        // If only one argument is passed - return null, otherwise bool
99
        return func_num_args() > 2 ? $return == null : $return;
100
    }
101
102
    /**
103
     * Get self[] by field identifier and its value.
104
     * Method is optimized for performance.
105
     *
106
     * @param QueryInterface $query Database query instance
107
     * @param string $fieldID Additional field identifier
108
     * @param string $fieldValue Additional field value for searching
109
     * @param self[]|array|null $return Variable where request result would be returned
110
     * @return bool|self[] True if material entities has been found and $return is passed
111
     *                      or self[] if only two parameters is passed.
112
     */
113
    public static function byFieldValue(QueryInterface $query, $fieldID, $fieldValue, &$return = array())
114
    {
115
        /** @var array $materialIds Collection of entity identifiers filtered by additional field */
116
        $materialIds = null;
117 View Code Duplication
        if (static::idsByFieldValue($query, $fieldID, $fieldValue, $materialIds)) {
0 ignored issues
show
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...
118
            // Get material instances
119
            $return = $query->entity(get_called_class())
120
                ->where('MaterialID', $materialIds)
0 ignored issues
show
Bug introduced by
It seems like $materialIds can also be of type array; however, samsonframework\orm\QueryInterface::where() does only seem to accept string|null, 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...
121
                ->exec();
122
        }
123
124
        // If only one argument is passed - return null, otherwise bool
125
        return func_num_args() > 3 ? $return == null : $return;
126
    }
127
128
    /**
129
     * Get current entity instances collection by navigation identifier.
130
     *
131
     * @param QueryInterface $query Database query
132
     * @param string $navigationID Navigation identifier
133
     * @param self[]|array|null $return Variable where request result would be returned
134
     * @return bool|self[] True if material entities has been found and $return is passed
135
     *                      or self[] if only two parameters is passed.
136
     */
137 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...
138
    {
139
        /** @var array $materialIds Collection of entity identifiers filtered by additional field */
140
        $materialIds = null;
141
        if (static::idsByNavigationID($query, $navigationID, $materialIds)) {
142
            $return = $query->entity(get_called_class())
143
                ->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...
144
                ->where('Active', 1)
145
                ->where('Published', 1)
146
                ->exec();
147
        }
148
149
        // If only one argument is passed - return null, otherwise bool
150
        return func_num_args() > 2 ? $return == null : $return;
151
    }
152
153
    /**
154
     * Get current entity instances collection by navigation identifier.
155
     *
156
     * @param QueryInterface $query Database query
157
     * @param string $navigationID Navigation identifier
158
     * @param string $fieldID Additional field identifier
159
     * @param string $fieldValue Additional field value for searching
160
     * @param self[]|array|null $return Variable where request result would be returned
161
     * @return bool|Material[] True if material entities has been found and $return is passed
162
     *                      or self[] if only two parameters is passed.
163
     */
164
    public static function byNavigationIdAndFieldValue(
165
        QueryInterface $query,
166
        $navigationID,
167
        $fieldID,
168
        $fieldValue,
169
        &$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 View Code Duplication
            if (static::idsByFieldValue($query, $fieldID, $fieldValue, $materialIds, $materialIds)) {
0 ignored issues
show
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...
175
                // Get entities by filtered identifiers
176
                $return = $query->entity(get_called_class())
177
                    ->where('MaterialID', $materialIds)
0 ignored issues
show
Bug introduced by
It seems like $materialIds can also be of type array; however, samsonframework\orm\QueryInterface::where() does only seem to accept string|null, 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...
178
                    ->exec();
179
            }
180
        }
181
182
        // If only one argument is passed - return null, otherwise bool
183
        return func_num_args() > 4 ? $return == null : $return;
184
    }
185
186
    /**
187
     * Get material entities collection by URL(s).
188
     * @param QueryInterface $query Object for performing database queries
189
     * @param array|string $url Material URL or collection of material URLs
190
     * @param self[]|array|null $return Variable where request result would be returned
191
     * @return bool|self[] True if material entities has been found
192
     */
193
    public static function byUrl(QueryInterface $query, $url, & $return = array())
194
    {
195
        // Get field record by identifier column
196
        $return = static::collectionByColumn($query, 'Url', $url);
0 ignored issues
show
Bug introduced by
It seems like $url defined by parameter $url on line 193 can also be of type array; however, samsonframework\orm\Record::collectionByColumn() does only seem to accept string, 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...
197
198
        // If only one argument is passed - return null, otherwise bool
199
        return func_num_args() > 1 ? $return == null : $return;
200
    }
201
202
    /**
203
     * Set additional material field value by field identifier
204
     * @param string $fieldID Field identifier
205
     * @param string $value Value to be stored
206
     * @param string $locale Locale identifier
207
     */
208
    public function setFieldByID($fieldID, $value, $locale = DEFAULT_LOCALE)
209
    {
210
        // TODO: This should be removed
211
        /** @var QueryInterface $query This should be removed to use $this->database*/
212
        $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...
213
214
        /** @var Field $fieldRecord Try to find this additional field */
215
        $fieldRecord = null;
216
        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...
217
            /** @var MaterialField $materialFieldRecord Try to find additional field value */
218
            $materialFieldRecord = null;
219
            if (!MaterialField::byFieldIDAndMaterialID($query, $this->id, $fieldRecord->id, $materialFieldRecord)) {
0 ignored issues
show
Documentation introduced by
$materialFieldRecord is of type object<samsoncms\api\MaterialField>, 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...
220
                // Create new additional field value record if it does not exists
221
                $materialFieldRecord = new MaterialField();
222
                $materialFieldRecord->FieldID = $fieldRecord->id;
0 ignored issues
show
Bug introduced by
The property FieldID does not seem to exist. Did you mean fieldID?

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...
223
                $materialFieldRecord->MaterialID = $this->id;
0 ignored issues
show
Bug introduced by
The property MaterialID does not seem to exist. Did you mean materialID?

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...
224
                $materialFieldRecord->Active = 1;
0 ignored issues
show
Bug introduced by
The property Active does not seem to exist. Did you mean active?

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...
225
                $materialFieldRecord->locale = $locale;
226
            }
227
228
            // Define which field should be filled
229
            switch ($fieldRecord->Type) {
230
                case 1:
231
                    $valueFieldName = 'numeric_value';
232
                    break;
233
                case 2:
234
                    $valueFieldName = 'key_value';
235
                    break;
236
                default:
237
                    $valueFieldName = 'Value';
238
            }
239
240
            // At this point we already have database record instance
241
            $fieldRecord->$valueFieldName = $value;
242
            $fieldRecord->save();
243
        }
244
    }
245
246
    /**
247
     * Get select additional field text value
248
     * TODO: Find where do we use it
249
     * @return string Select field text
250
     */
251
    public function selectText($fieldID)
252
    {
253
        /** @var \samson\activerecord\field $field */
254
        $field = null;
255
        if (dbQuery('field')->id($fieldID)->first($field)) {
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::id() has been deprecated with message: Use direct query with where('PRIMARY_FIELD',...)

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...
256
            // If this entity has this field set
257
            if (isset($this[$field->Name]{0})) {
258
                $types = array();
259
                foreach (explode(',', $field->Value) as $typeValue) {
260
                    $typeValue = explode(':', $typeValue);
261
                    $types[$typeValue[0]] = $typeValue[1];
262
                }
263
                return $types[$this[$field->Name]];
264
            }
265
        }
266
267
        // Value not set
268
        return '';
269
    }
270
271
    /**
272
     * Get collection of images for material by gallery additional field selector. If none is passed
273
     * all images from gallery table would be returned for this material entity.
274
     *
275
     * @param string|null $fieldSelector Additional field selector value
276
     * @param string $selector Additional field field name to search for
277
     * @return \samson\activerecord\gallery[] Collection of images in this gallery additional field for material
278
     */
279
    public function &gallery($fieldSelector = null, $selector = 'FieldID')
280
    {
281
        /** @var \samson\activerecord\gallery[] $images Get material images for this gallery */
282
        $images = array();
283
284
        /* @var \samson\activerecord\field Get field object if we need to search it by other fields */
285
        $field = null;
0 ignored issues
show
Unused Code introduced by
$field is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
286
        if ($selector != 'FieldID') {
287
            $field = dbQuery('field')->cond($selector, $fieldSelector)->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...
288
            $fieldSelector = $field->id;
289
        }
290
291
        // Create query
292
        $query = dbQuery('materialfield');
293
294
        // Add field filter if present
295
        if (isset($fieldSelector)) {
296
            $query->cond("FieldID", $fieldSelector);
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...
297
        }
298
299
        /** @var \samson\activerecord\materialfield $dbMaterialField Find material field gallery record */
300
        $dbMaterialField = null;
301
        if ($query->cond('MaterialID', $this->id)->first($dbMaterialField)) {
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...
302
            // Get material images for this materialfield
303
            if (dbQuery('gallery')->cond('materialFieldId', $dbMaterialField->id)->exec($images)) {
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...
304
305
            }
306
        }
307
308
        return $images;
309
    }
310
311
    /**
312
     * Create copy of current object
313
     * @param mixed $clone Material for cloning
314
     * @param array $excludedFields excluded from materialfield fields identifiers
315
     * @returns void
316
     */
317
    public function &copy(& $clone = null, $excludedFields = array())
318
    {
319
        // Create new instance by copying
320
        $clone = parent::copy($clone);
321
322
        /** @var \samson\activerecord\structurematerial[] $objects Create structure material relations */
323
        $objects = array();
324 View Code Duplication
        if (dbQuery('structurematerial')->cond('MaterialID', $this->MaterialID)->exec($objects)) {
0 ignored issues
show
Bug introduced by
The property MaterialID does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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...
325
            foreach ($objects as $cmsNavigation) {
326
                /** @var \samson\activerecord\Record $copy */
327
                $copy = $cmsNavigation->copy();
328
                $copy->MaterialID = $clone->id;
329
                $copy->save();
330
            }
331
        }
332
        /** @var \samson\activerecord\materialfield[] $objects Create material field relations */
333
        $objects = array();
334
        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...
335
            foreach ($objects as $pMaterialField) {
336
                // Check if field is NOT excluded from copying
337
                if (!in_array($pMaterialField->FieldID, $excludedFields)) {
338
                    /** @var \samson\activerecord\dbRecord $copy Copy instance */
339
                    $copy = $pMaterialField->copy();
340
                    $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...
341
                    $copy->save();
342
                }
343
            }
344
        }
345
346
        /** @var \samson\activerecord\gallery[] $objects Create gallery field relations */
347
        $objects = array();
348 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...
349
            foreach ($objects as $cmsGallery) {
350
                /** @var \samson\activerecord\Record $copy */
351
                $copy = $cmsGallery->copy();
352
                $copy->MaterialID = $clone->id;
353
                $copy->save();
354
            }
355
        }
356
357
        return $clone;
358
    }
359
360
    /**
361
     * Function to retrieve this material table by specified field
362
     * @param string $tableSelector Selector to identify table structure
363
     * @param string $selector Database field by which search is performed
364
     * @param array $tableColumns Columns names list
365
     * @param string $externalHandler External handler to perform some extra code
366
     * @param array $params External handler params
367
     * @return array Collection of collections of table cells, represented as materialfield objects
368
     */
369
    public function getTable($tableSelector, $selector = 'StructureID', &$tableColumns = null, $externalHandler = null, $params = array())
370
    {
371
        /** @var array $resultTable Collection of collections of field cells */
372
        $resultTable = array();
373
        /** @var array $dbTableFieldsIds Array of table structure column identifiers */
374
        $dbTableFieldsIds = array();
375
376
        // Get structure object if we need to search it by other fields
377
        if ($selector != 'StructureID') {
378
            $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...
379
            $tableSelector = $structure->id;
380
        }
381
382
        /** If this table has columns */
383
        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...
384
            ->cond("StructureID", $tableSelector)
385
            ->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...
386
        ) {
387
            // Get localized and not localized fields
388
            $localizedFields = array();
389
            $unlocalizedFields = array();
390
            /** @var \samson\cms\CMSField $dbTableField Table column */
391
            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...
392
                /** Add table columns names */
393
                $tableColumns[] = $field->Name;
394
                if ($field->local == 1) {
395
                    $localizedFields[] = $field->id;
396
                } else {
397
                    $unlocalizedFields[] = $field->id;
398
                }
399
            }
400
401
            // Query to get table rows(table materials)
402
            $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...
403
                ->cond('parent_id', $this->MaterialID)
404
                ->cond('Active', '1')
405
                ->join('structurematerial')
406
                ->cond('structurematerial_StructureID', $tableSelector)
407
                ->order_by('priority');
408
409
            // Call user function if exists
410
            if (is_callable($externalHandler)) {
411
                // Give it query as parameter
412
                call_user_func_array($externalHandler, array_merge(array(&$tableQuery), $params));
413
            }
414
415
            // Get table row materials
416
            $tableMaterialIds = array();
417
            if ($tableQuery->fields('MaterialID', $tableMaterialIds)) {
418
                // Create field condition
419
                $localizationFieldCond = new Condition('or');
420
421
                // Create localized condition
422
                if (sizeof($localizedFields)) {
423
                    $localizedFieldCond = new Condition('and');
424
                    $localizedFieldCond->add('materialfield_FieldID', $localizedFields)
425
                        ->add('materialfield_locale', locale());
426
                    // Add this condition to condition group
427
                    $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...
428
                }
429
430
                // Create not localized condition
431
                if (sizeof($unlocalizedFields)) {
432
                    $localizationFieldCond->add('materialfield_FieldID', $unlocalizedFields);
433
                }
434
435
                // Create db query
436
                $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...
437
                    ->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...
438
                    ->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...
439
440
                // Flip field identifiers as keys
441
                $tableColumnIds = array_flip($dbTableFieldsIds);
442
                $resultTable = array_flip($tableMaterialIds);
443
444
                /** @var \samson\activerecord\material $dbTableRow Material object (table row) */
445
                foreach ($materialFieldQuery->exec() as $mf) {
446
                    if (!is_array($resultTable[$mf['MaterialID']])) {
447
                        $resultTable[$mf['MaterialID']] = array();
448
                    }
449
450
                    $resultTable[$mf['MaterialID']][$tableColumnIds[$mf->FieldID]] =
451
                        !empty($mf->Value) ? $mf->Value : (!empty($mf->numeric_value) ? $mf->numeric_value : $mf->key_value);
452
                }
453
            }
454
        }
455
456
        return array_values($resultTable);
457
    }
458
}
459