Completed
Pull Request — master (#77)
by
unknown
03:36
created

Navigation::fields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
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\structure;
9
use samsonframework\orm\QueryInterface;
10
11
/**
12
 * SamsonCMS Navigation entity
13
 * @author Vitaly Egorov <[email protected]>
14
 * @copyright 2014 SamsonOS
15
 */
16
class Navigation extends structure
17
{
18
    /** Store entity name */
19
    const ENTITY = __CLASS__;
20
21
    /** Entity field names constants for using in code */
22
    const F_PRIMARY = 'StructureID';
23
    const F_IDENTIFIER = 'Url';
24
    const F_NAME = 'Name';
25
    const F_TYPE = 'type';
26
    const F_DELETION = 'Active';
27
    const F_PARENT = 'ParentID';
28
    const F_PRIORITY = 'priority';
29
    const F_CREATED = 'Created';
30
    const F_MODIFIED = 'Modyfied';
31
    const F_DEF_MATERIAL = 'MaterialID';
32
33
    /** @var self[] Collection of child items */
34
    public $children = array();
35
36
    /** @var array WTF?? */
37
    public $parentsnav = array();
38
    /** @var bool WTF??? */
39
    protected $base = false;
40
    /**
41
     * Material query injection
42
     * @param \samson\activerecord\dbQuery $query Query object
43
     */
44
    public function materialsHandlers(&$query)
45
    {
46
        $query->join('gallery');
47
    }
48
    /**
49
     * Get all materials related to current navigation
50
     * @return Material[] Collection of related materials
51
     * @deprecated Will be removed in nex major version
52
     */
53
    public function &materials()
54
    {
55
        /** @var Material[] $materials Get related materials collection */
56
        $materials = array();
57
        // Perform generic material retrieval
58
        if (CMS::getMaterialsByStructures(
0 ignored issues
show
Bug introduced by
The method getMaterialsByStructures() does not seem to exist on object<samsoncms\api\CMS>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
            array($this->id),
60
            $materials,
61
            'samson\cms\CMSMaterial',
62
            null,
63
            array(),
64
            array($this, 'materialsHandlers'))
65
        ) {
66
            // Handle
67
        }
68
        return $materials;
69
    }
70
    /**
71
     * Get all related fields
72
     * @return Field[] Collection of related fields
73
     */
74
    public function &fields()
75
    {
76
        // Prepare db request to get related fields
77
        $fieldIDs = 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...
78
            ->cond('StructureID', $this->id)
79
            ->cond('Active', 1)
80
            ->fields('FieldID');
81
        /** @var \samson\cms\NavigationField[] $fields Get collection of related navigation fields */
82
        $fields = array();
83
        if (sizeof($fieldIDs)) {
84
            dbQuery('samson\cms\Field')->id($fieldIDs)->exec($fields);
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...
85
        }
86
        return $fields;
87
    }
88
    /**
89
     * Get default Material object
90
     * @return \samson\cms\Material|bool Default Material object, otherwise false
91
     */
92
    public function def()
93
    {
94
        // If this naviagtion has default material identifier specified
95
        if (isset($this->MaterialID) && $this->MaterialID > 0) {
96
            // Perform db query to get this material
97
            return dbQuery('samson\cms\Material')->id($this->MaterialID)->first();
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...
98
        }
99
        return false;
100
    }
101
    /**
102
     * Get all children navigation elements default material object.
103
     * This approach increases performance on large navigation tree branches.
104
     * @return Material[] Collection of material objects
105
     */
106
    public function childrenDef()
107
    {
108
        // Gather all default materials
109
        $defaultMaterialIds = array();
110
        foreach ($this->children() as $child) {
111
            $defaultMaterialIds[] = $child->MaterialID;
112
        }
113
        // Perform database query
114
        return dbQuery('samson\cms\CMSMaterial')->cond('MaterialID', $defaultMaterialIds)->exec();
0 ignored issues
show
Documentation introduced by
$defaultMaterialIds 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...
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...
115
    }
116
    // TODO: Functions lower to this line should be rewritten by [email protected]
117
    public function parents(CMSNav & $bound = NULL)
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
118
    {
119
        $parents = array();
120
        $this->base();
121
        if (sizeof($this->parentsnav) > 0) {
122
            $parent = current($this->parentsnav);
123
            $parents[] = $parent;
124
            if (!(isset($bound) && ($bound == $this->parentsnav[0]))) {
125
                $parents = array_merge($parents, $parent->parents($bound));
126
            }
127
        }
128
        //return array_reverse( $parents );
129
        return $parents;
130
    }
131
    public function children()
132
    {
133
        // check? is this objeck full;
134
        $this->base();
135
        return $this->children;
136
    }
137
    public function parent()
138
    {
139
        // check? is this objeck full;
140
        $this->base();
141
        return $this->parent;
0 ignored issues
show
Bug introduced by
The property parent does not seem to exist. Did you mean parentsnav?

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...
142
    }
143
    /**
144
     * WTF?
145
     */
146
    public function prepare()
147
    {
148
        $this->base = true;
149
        if (isset($this->onetomany['_children'])) {
0 ignored issues
show
Bug introduced by
The property onetomany does not seem to exist. Did you mean oneToMany?

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...
150
            foreach ($this->onetomany['_children'] as & $child) {
0 ignored issues
show
Bug introduced by
The property onetomany does not seem to exist. Did you mean oneToMany?

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...
151
                $this->children[$child->id] = &$child;
152
            }
153
            unset($this->onetomany['_children']);
154
        }
155
        if (isset($this->onetomany['_parents'])) {
0 ignored issues
show
Bug introduced by
The property onetomany does not seem to exist. Did you mean oneToMany?

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...
156
            foreach ($this->onetomany['_parents'] as & $parent) {
0 ignored issues
show
Bug introduced by
The property onetomany does not seem to exist. Did you mean oneToMany?

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...
157
                $this->parentsnav[$parent->id] = &$parent;
158
                $this->parent = &$parent;
0 ignored issues
show
Bug introduced by
The property parent does not seem to exist. Did you mean parentsnav?

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...
159
            }
160
            unset($this->onetomany['_parents']);
161
        }
162
    }
163
    /*
164
     * Has object all its relations?
165
     * If not, fill relations.
166
     */
167
    protected function base()
168
    {
169
        if (!$this->base) {
170
            //$classname = ns_classname('cmsnav', 'samson\cms');
171
            $classname = get_class($this);
172
            $cmsnav = null;
173
            if (dbQuery($classname)
174
                ->where('Active', 1)
175
                ->where('StructureID', $this->id)
176
                ->join('children_relations', null, true)
177
                ->join('children', get_class($this))
178
                ->join('parents_relations', null, true)
179
                ->join('parents', get_class($this))
180
                ->exec($cmsnav)
181
            ) {
182
                $cmsnav = array_shift($cmsnav);
183
                if (isset($cmsnav->onetomany['_children'])) {
184
                    $this->onetomany['_children'] = $cmsnav->onetomany['_children'];
0 ignored issues
show
Bug introduced by
The property onetomany does not seem to exist. Did you mean oneToMany?

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...
185
                }
186
                if (isset($cmsnav->onetomany['_parents'])) {
187
                    $this->onetomany['_parents'] = $cmsnav->onetomany['_parents'];
0 ignored issues
show
Bug introduced by
The property onetomany does not seem to exist. Did you mean oneToMany?

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...
188
                }
189
                $this->prepare();
190
            }
191
        }
192
    }
193
    protected function baseChildren()
194
    {
195
        //elapsed('startBaseChildren');
196
        //trace('baseChildren');
197
        $this->base();
198
        //$classname = ns_classname('cmsnav', 'samson\cms');
199
        $classname = get_class($this);
200
        //trace($classname);
201
        $cmsnavs = null;
202
        $children_id = array_keys($this->children);
203
        //elapsed('queryStart');
204
        if (sizeof($children_id)) {
205
            if (dbQuery($classname)
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...
206
                ->cond('Active', 1)
207
                ->cond('StructureID', $children_id)
0 ignored issues
show
Documentation introduced by
$children_id is of type array<integer,integer>, 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...
208
                ->join('children_relations', null, true)
209
                ->join('children', $classname)
210
                ->join('parents_relations', null, true)
211
                ->join('parents', $classname)
212
                ->exec($cmsnavs)
213
            ) {
214
                //elapsed('queryEnd');
215
                $this->children = array();
216
                foreach ($cmsnavs as & $cmsnav) {
217
                    $cmsnav->prepare();
218
                    $this->children[] = &$cmsnav;
219
                }
220
            }
221
        }
222
        //elapsed('endBaseChildren');
223
        return $this->children;
224
    }
225
    public function rewind()
226
    {
227
        $this->base();
228
        reset($this->children);
229
    }
230
    public function next()
231
    {
232
        $this->base();
233
        return next($this->children);
234
    }
235
    public function current()
236
    {
237
        $this->base();
238
        return current($this->children);
239
    }
240
    public function key()
241
    {
242
        $this->base();
243
        return key($this->children);
244
    }
245
    public function valid()
246
    {
247
        $this->base();
248
        $key = key($this->children);
249
        return ($key !== null && $key !== false);
250
    }
251
252
    /**
253
     * Override standard view passing
254
     * @param string $prefix Prefix
255
     * @param array $restricted Collection of ignored entity fields
256
     * @return array Filled collection of key => values for view
257
     */
258
    public function toView($prefix = '', array $restricted = array())
259
    {
260
        return parent::toView(
261
            $prefix,
262
            array_merge(
263
                $restricted,
264
                array('parent', 'parents', 'children'
265
                )
266
            )
267
        );
268
    }
269
}
270