Completed
Push — master ( fc9b06...645bdc )
by Vitaly
07:44
created

Navigation::base()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.439
cc 5
eloc 17
nc 6
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
10
/**
11
 * SamsonCMS Navigation element
12
 * @author Vitaly Egorov <[email protected]>
13
 * @copyright 2014 SamsonOS
14
 */
15
class Navigation extends structure implements \Iterator
16
{
17
    /** @var string Navigation string identifier */
18
    public $Url;
19
20
    /** @var \samson\cms\Navigation[] Collection of child items */
21
    public $children = array();
22
23
    /** @var array WTF?? */
24
    public $parentsnav = array();
25
26
    /** @var bool WTF??? */
27
    protected $base = false;
28
29
    /**
30
     * Override standard view passing
31
     * @param string $prefix Prefix
32
     * @param array $restricted Collection of ignored entity fields
33
     * @return array Filled collection of key => values for view
34
     */
35
    public function toView($prefix = '', array $restricted = array())
36
    {
37
        return parent::toView($prefix, $restricted = array('parent','parents','children'));
38
    }
39
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
    /**
50
     * Get all related materials
51
     * @return \samson\cms\CMSMaterial[] Collection of related materials
52
     */
53
    public function & materials()
54
    {
55
        /** @var \samson\cms\Material[] $materials Get related materials collection */
56
        $materials = array();
57
        // Perform generic material retrieval
58
        if (CMS::getMaterialsByStructures(
59
            array($this->id),
60
            $materials,
61
            'samson\cms\CMSMaterial',
62
            null,
63
            array(),
64
            array($this, 'materialsHandlers'))) {
1 ignored issue
show
Coding Style introduced by
Multi-line function call not indented correctly; expected 8 spaces but found 12
Loading history...
Coding Style introduced by
Closing parenthesis of a multi-line function call must be on a line by itself
Loading history...
65
            // Handle
66
        }
67
68
        return $materials;
69
    }
70
71
    /**
72
     * Get all related fields
73
     * @return \samson\cms\Field[] Collection of related fields
74
     */
75
    public function &fields()
76
    {
77
        // Prepare db request to get related fields
78
        $fieldIDs = dbQuery('structurefield')
79
            ->cond('StructureID', $this->id)
80
            ->cond('Active', 1)
81
            ->fields('FieldID');
82
83
        /** @var \samson\cms\NavigationField[] $fields Get collection of related navigation fields */
84
        $fields = array();
85
        if (sizeof($fieldIDs)) {
86
            dbQuery('samson\cms\Field')->id($fieldIDs)->exec($fields);
87
        }
88
89
        return $fields;
90
    }
91
92
    /**
93
     * Get default Material object
94
     * @return \samson\cms\Material|bool Default Material object, otherwise false
95
     */
96
    public function def()
97
    {
98
        // If this naviagtion has default material identifier specified
99
        if (isset($this->MaterialID) && $this->MaterialID > 0) {
100
            // Perform db query to get this material
101
            return dbQuery('samson\cms\Material')->id($this->MaterialID)->first();
102
        }
103
104
        return false;
105
    }
106
107
    /**
108
     * Get all children navigation elements default material object.
109
     * This approach increases performance on large navigation tree branches.
110
     * @return Material[] Collection of material objects
111
     */
112
    public function childrenDef()
113
    {
114
        // Gather all default materials
115
        $defaultMaterialIds =  array();
116
117
        foreach ($this->children() as $child) {
118
            $defaultMaterialIds[] = $child->MaterialID;
119
        }
120
121
        // Perform database query
122
        return dbQuery('samson\cms\CMSMaterial')->cond('MaterialID', $defaultMaterialIds)->exec();
123
    }
124
125
    // TODO: Functions lower to this line should be rewritten by [email protected]
126
127
    public function parents( CMSNav & $bound = NULL)
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between opening bracket and type hint "CMSNav"; 1 found
Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
128
    {
129
        $parents = array();
130
        $this->base();
131
        if (sizeof($this->parentsnav)>0) {
132
            $parent = current($this->parentsnav);
133
            $parents[] = $parent;
134
            if( !(isset( $bound ) && ( $bound == $this->parentsnav[0] )) ){
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
Coding Style introduced by
Expected 1 space after closing parenthesis; found ""
Loading history...
135
                $parents = array_merge($parents, $parent->parents($bound));
136
            }
137
        }
138
139
        //return array_reverse( $parents );
140
        return $parents;
141
    }
142
143
    public function children()
144
    {
145
        // check? is this objeck full;
146
        $this->base();
147
        return $this->children;
148
    }
149
150
    public function parent()
151
    {
152
        // check? is this objeck full;
153
        $this->base();
154
        return $this->parent;
155
    }
156
157
    /**
158
     * WTF?
159
     */
160
    public function prepare()
161
    {
162
        $this->base = true;
163
164
        if (isset($this->onetomany['_children'])) {
165
            foreach ($this->onetomany['_children'] as & $child) {
166
                $this->children[$child->id] = & $child;
167
            }
168
            unset($this->onetomany['_children']);
169
        }
170
171
        if (isset($this->onetomany['_parents'])) {
172
            foreach ($this->onetomany['_parents'] as & $parent) {
173
                $this->parentsnav[$parent->id] = & $parent;
174
                $this->parent = & $parent;
175
            }
176
            unset($this->onetomany['_parents']);
177
        }
178
    }
179
180
    /*
181
     * Has object all its relations?
182
     * If not, fill relations.
183
     */
184
    protected function base()
185
    {
186
        if (!$this->base){
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found ""
Loading history...
187
            //$classname = ns_classname('cmsnav', 'samson\cms');
188
            $classname = get_class($this);
189
            $cmsnav = null;
190
            if( dbQuery($classname)
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
191
                ->cond('Active',1)
0 ignored issues
show
Coding Style introduced by
No space found after comma in function call
Loading history...
192
                ->StructureID( $this->id)
0 ignored issues
show
Coding Style introduced by
As per PSR2, there should be no spaces after opening parenthesis of a function call.
Loading history...
193
                ->join('children_relations',null, true)
0 ignored issues
show
Coding Style introduced by
No space found after comma in function call
Loading history...
194
                ->join('children', get_class($this))
195
                ->join('parents_relations', null, true)
196
                ->join('parents', get_class($this))
197
                ->first( $cmsnav )) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, there should be no spaces after opening parenthesis of a function call.
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
Coding Style introduced by
Expected 1 newline after opening brace; 2 found
Loading history...
198
199
                if (isset($cmsnav->onetomany['_children'])) {
200
                    $this->onetomany['_children'] = & $cmsnav->onetomany['_children'];
201
                }
202
203
                if (isset($cmsnav->onetomany['_parents'])) {
204
                    $this->onetomany['_parents'] = & $cmsnav->onetomany['_parents'];
205
                }
206
207
                $this->prepare();
208
            }
209
        }
210
    }
211
212
    protected function baseChildren()
213
    {
214
        //elapsed('startBaseChildren');
215
        //trace('baseChildren');
216
        $this->base();
217
        //$classname = ns_classname('cmsnav', 'samson\cms');
218
        $classname = get_class($this);
219
        //trace($classname);
220
        $cmsnavs = null;
221
        $children_id = array_keys($this->children);
222
        //elapsed('queryStart');
223
        if (sizeof($children_id)){
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found ""
Loading history...
224
            if( dbQuery($classname)
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
225
                ->cond('Active',1)
0 ignored issues
show
Coding Style introduced by
No space found after comma in function call
Loading history...
226
                ->cond('StructureID', $children_id)
227
                ->join('children_relations', null, true)
228
                ->join('children', $classname)
229
                ->join('parents_relations', null, true)
230
                ->join('parents', $classname)
231
                ->exec( $cmsnavs )) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, there should be no spaces after opening parenthesis of a function call.
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
232
                //elapsed('queryEnd');
233
                $this->children = array();
234
                foreach ($cmsnavs as & $cmsnav) {
0 ignored issues
show
Bug introduced by
The expression $cmsnavs of type null is not traversable.
Loading history...
235
                    $cmsnav->prepare();
236
                    $this->children[] = & $cmsnav;
237
                }
238
            }
239
        }
240
        //elapsed('endBaseChildren');
241
        return $this->children;
242
    }
243
244
    public function rewind()
245
    {
246
        $this->base();
247
        reset( $this->children );
0 ignored issues
show
Coding Style introduced by
As per PSR2, there should be no spaces after opening parenthesis of a function call.
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
248
    }
249
250
    public function next()
251
    {
252
        $this->base();
253
        return next( $this->children );
0 ignored issues
show
Coding Style introduced by
As per PSR2, there should be no spaces after opening parenthesis of a function call.
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
254
    }
255
256
    public function current()
257
    {
258
        $this->base();
259
        return current( $this->children );
0 ignored issues
show
Coding Style introduced by
As per PSR2, there should be no spaces after opening parenthesis of a function call.
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
260
    }
261
    public function key()
262
    {
263
        $this->base();
264
        return key( $this->children );
0 ignored issues
show
Coding Style introduced by
As per PSR2, there should be no spaces after opening parenthesis of a function call.
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
265
    }
266
267
    public function valid()
268
    {
269
        $this->base();
270
        $key = key( $this->children );
0 ignored issues
show
Coding Style introduced by
As per PSR2, there should be no spaces after opening parenthesis of a function call.
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
271
        return ($key !== null && $key !== false);
272
    }
273
}
274