ObjectTree   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 284
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 35
eloc 80
c 0
b 0
f 0
dl 0
loc 284
rs 9.6

12 Methods

Rating   Name   Duplication   Size   Complexity  
A makeSelBox() 0 18 2
A __construct() 0 9 2
A getTree() 0 3 1
A makeSelectElement() 0 20 2
A makeSelBoxOptions() 0 14 6
A initialize() 0 10 3
A __get() 0 11 2
A getByKey() 0 3 1
A getAllChild() 0 13 4
A addSelectOptions() 0 11 5
A getFirstChild() 0 10 3
A getAllParent() 0 11 4
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\News;
4
5
/**
6
 * XOOPS tree class
7
 *
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 *
15
 * @copyright       (c) 2000-2020 XOOPS Project (www.xoops.org)
16
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
17
 * @since               2.0.0
18
 * @author              Kazumi Ono (http://www.myweb.ne.jp/, http://jp.xoops.org/)
19
 */
20
21
/**
22
 * A tree structures with {@link XoopsObject}s as nodes
23
 *
24
 * @author     Kazumi Ono <[email protected]>
25
 */
26
class ObjectTree
27
{
28
    /**
29
     * @access private
30
     */
31
    protected $parentId;
32
    protected $myId;
33
    protected $rootId;
34
    protected $tree = [];
35
    protected $objects;
36
37
    /**
38
     * Constructor
39
     *
40
     * @param array  $objectArr Array of {@link XoopsObject}s
41
     * @param string $myId      field name of object ID
42
     * @param string $parentId  field name of parent object ID
43
     * @param string $rootId    field name of root object ID
44
     */
45
    public function __construct($objectArr, $myId, $parentId, $rootId = null)
46
    {
47
        $this->objects  = $objectArr;
48
        $this->myId     = $myId;
49
        $this->parentId = $parentId;
50
        if (isset($rootId)) {
51
            $this->rootId = $rootId;
52
        }
53
        $this->initialize();
54
    }
55
56
    /**
57
     * Initialize the object
58
     *
59
     * @access private
60
     */
61
    protected function initialize(): void
62
    {
63
        foreach (\array_keys($this->objects) as $i) {
64
            $key1                         = $this->objects[$i]->getVar($this->myId);
65
            $this->tree[$key1]['obj']     = $this->objects[$i];
66
            $key2                         = $this->objects[$i]->getVar($this->parentId);
67
            $this->tree[$key1]['parent']  = $key2;
68
            $this->tree[$key2]['child'][] = $key1;
69
            if (isset($this->rootId)) {
70
                $this->tree[$key1]['root'] = $this->objects[$i]->getVar($this->rootId);
71
            }
72
        }
73
    }
74
75
    /**
76
     * Get the tree
77
     *
78
     * @return array Associative array comprising the tree
79
     */
80
    public function &getTree()
81
    {
82
        return $this->tree;
83
    }
84
85
    /**
86
     * returns an object from the tree specified by its id
87
     *
88
     * @param string $key ID of the object to retrieve
89
     * @return object Object within the tree
90
     */
91
    public function &getByKey($key)
92
    {
93
        return $this->tree[$key]['obj'];
94
    }
95
96
    /**
97
     * returns an array of all the first child object of an object specified by its id
98
     *
99
     * @param string $key ID of the parent object
100
     * @return array  Array of children of the parent
101
     */
102
    public function getFirstChild($key)
103
    {
104
        $ret = [];
105
        if (isset($this->tree[$key]['child'])) {
106
            foreach ($this->tree[$key]['child'] as $childKey) {
107
                $ret[$childKey] = $this->tree[$childKey]['obj'];
108
            }
109
        }
110
111
        return $ret;
112
    }
113
114
    /**
115
     * returns an array of all child objects of an object specified by its id
116
     *
117
     * @param string $key ID of the parent
118
     * @param array  $ret (Empty when called from client) Array of children from previous recursions.
119
     * @return array  Array of child nodes.
120
     */
121
    public function getAllChild($key, $ret = [])
122
    {
123
        if (isset($this->tree[$key]['child'])) {
124
            foreach ($this->tree[$key]['child'] as $childKey) {
125
                $ret[$childKey] = $this->tree[$childKey]['obj'];
126
                $children       = $this->getAllChild($childKey, $ret);
127
                foreach (\array_keys($children) as $newKey) {
128
                    $ret[$newKey] = $children[$newKey];
129
                }
130
            }
131
        }
132
133
        return $ret;
134
    }
135
136
    /**
137
     * returns an array of all parent objects.
138
     * the key of returned array represents how many levels up from the specified object
139
     *
140
     * @param string $key     ID of the child object
141
     * @param array  $ret     (empty when called from outside) Result from previous recursions
142
     * @param int    $upLevel (empty when called from outside) level of recursion
143
     * @return array  Array of parent nodes.
144
     */
145
    public function getAllParent($key, $ret = [], $upLevel = 1)
146
    {
147
        if (isset($this->tree[$key]['parent']) && isset($this->tree[$this->tree[$key]['parent']]['obj'])) {
148
            $ret[$upLevel] = $this->tree[$this->tree[$key]['parent']]['obj'];
149
            $parents       = $this->getAllParent($this->tree[$key]['parent'], $ret, $upLevel + 1);
150
            foreach (\array_keys($parents) as $newKey) {
151
                $ret[$newKey] = $parents[$newKey];
152
            }
153
        }
154
155
        return $ret;
156
    }
157
158
    /**
159
     * Make options for a select box from
160
     *
161
     * @param string $fieldName   Name of the member variable from the
162
     *                            node objects that should be used as the title for the options.
163
     * @param string $selected    Value to display as selected
164
     * @param int    $key         ID of the object to display as the root of select options
165
     * @param string $ret         (reference to a string when called from outside) Result from previous recursions
166
     * @param string $prefix_orig String to indent items at deeper levels
167
     * @param string $prefix_curr String to indent the current item
168
     *
169
     * @deprecated since 2.5.9, please use makeSelectElement() functionality
170
     */
171
    protected function makeSelBoxOptions($fieldName, $selected, $key, &$ret, $prefix_orig, $prefix_curr = ''): void
172
    {
173
        if ($key > 0) {
174
            $value = $this->tree[$key]['obj']->getVar($this->myId);
175
            $ret   .= '<option value="' . $value . '"';
176
            if ($value == $selected) {
177
                $ret .= ' selected';
178
            }
179
            $ret         .= '>' . $prefix_curr . $this->tree[$key]['obj']->getVar($fieldName) . '</option>';
180
            $prefix_curr .= $prefix_orig;
181
        }
182
        if (isset($this->tree[$key]['child']) && !empty($this->tree[$key]['child'])) {
183
            foreach ($this->tree[$key]['child'] as $childKey) {
184
                $this->makeSelBoxOptions($fieldName, $selected, $childKey, $ret, $prefix_orig, $prefix_curr);
0 ignored issues
show
Deprecated Code introduced by
The function XoopsModules\News\ObjectTree::makeSelBoxOptions() has been deprecated: since 2.5.9, please use makeSelectElement() functionality ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

184
                /** @scrutinizer ignore-deprecated */ $this->makeSelBoxOptions($fieldName, $selected, $childKey, $ret, $prefix_orig, $prefix_curr);

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

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

Loading history...
185
            }
186
        }
187
    }
188
189
    /**
190
     * Make a select box with options from the tree
191
     *
192
     * @param string $name             Name of the select box
193
     * @param string $fieldName        Name of the member variable from the
194
     *                                 node objects that should be used as the title for the options.
195
     * @param string $prefix           String to indent deeper levels
196
     * @param string $selected         Value to display as selected
197
     * @param bool   $addEmptyOption   Set TRUE to add an empty option with value "0" at the top of the hierarchy
198
     * @param int    $key              ID of the object to display as the root of select options
199
     * @param string $extra
200
     * @return string  HTML select box
201
     *
202
     * @deprecated since 2.5.9, please use makeSelectElement()
203
     */
204
    public function makeSelBox(
205
        $name,
206
        $fieldName,
207
        $prefix = '-',
208
        $selected = '',
209
        $addEmptyOption = false,
210
        $key = 0,
211
        $extra = ''
212
    ) {
213
        $trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 1);
214
        \trigger_error("makeSelBox() is deprecated since 2.5.9, please use makeSelectElement(), accessed from {$trace[0]['file']} line {$trace[0]['line']},");
215
        $ret = '<select name="' . $name . '" id="' . $name . '" ' . $extra . '>';
216
        if ((bool)$addEmptyOption) {
217
            $ret .= '<option value="0"></option>';
218
        }
219
        $this->makeSelBoxOptions($fieldName, $selected, $key, $ret, $prefix);
0 ignored issues
show
Deprecated Code introduced by
The function XoopsModules\News\ObjectTree::makeSelBoxOptions() has been deprecated: since 2.5.9, please use makeSelectElement() functionality ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

219
        /** @scrutinizer ignore-deprecated */ $this->makeSelBoxOptions($fieldName, $selected, $key, $ret, $prefix);

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

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

Loading history...
220
221
        return $ret . '</select>';
222
    }
223
224
    /**
225
     * Make a select box with options from the tree
226
     *
227
     * @param string $name             Name of the select box
228
     * @param string $fieldName        Name of the member variable from the
229
     *                                 node objects that should be used as the title for the options.
230
     * @param string $prefix           String to indent deeper levels
231
     * @param string $selected         Value to display as selected
232
     * @param bool   $addEmptyOption   Set TRUE to add an empty option with value "0" at the top of the hierarchy
233
     * @param int    $key              ID of the object to display as the root of select options
234
     * @param string $extra            extra content to add to the element
235
     * @param string $caption          optional caption for form element
236
     *
237
     * @return \XoopsFormSelect form element
238
     */
239
    public function makeSelectElement(
240
        $name,
241
        $fieldName,
242
        $prefix = '-',
243
        $selected = '',
244
        $addEmptyOption = false,
245
        $key = 0,
246
        $extra = '',
247
        $caption = ''
248
    ) {
249
        \xoops_load('xoopsformselect');
250
        $element = new \XoopsFormSelect($caption, $name, $selected);
251
        $element->setExtra($extra);
252
253
        if ((bool)$addEmptyOption) {
254
            $element->addOption('0', ' ');
255
        }
256
        $this->addSelectOptions($element, $fieldName, $key, $prefix);
257
258
        return $element;
259
    }
260
261
    /**
262
     * Make options for a select box from
263
     *
264
     * @param \XoopsFormSelect $element     form element to receive tree values as options
265
     * @param string           $fieldName   Name of the member variable from the node objects that
266
     *                                      should be used as the title for the options.
267
     * @param int              $key         ID of the object to display as the root of select options
268
     * @param string           $prefix_orig String to indent items at deeper levels
269
     * @param string           $prefix_curr String to indent the current item
270
     *
271
     * @access private
272
     */
273
    protected function addSelectOptions($element, $fieldName, $key, $prefix_orig, $prefix_curr = ''): void
274
    {
275
        if ($key > 0) {
276
            $value = $this->tree[$key]['obj']->getVar($this->myId);
277
            $name  = $prefix_curr . $this->tree[$key]['obj']->getVar($fieldName);
278
            $element->addOption($value, $name);
279
            $prefix_curr .= $prefix_orig;
280
        }
281
        if (isset($this->tree[$key]['child']) && !empty($this->tree[$key]['child'])) {
282
            foreach ($this->tree[$key]['child'] as $childKey) {
283
                $this->addSelectOptions($element, $fieldName, $childKey, $prefix_orig, $prefix_curr);
284
            }
285
        }
286
    }
287
288
    /**
289
     * Magic __get method
290
     *
291
     * Some modules did not respect the leading underscore is private convention and broke
292
     * when code was modernized. This will keep them running for now.
293
     *
294
     * @param string $name  unknown variable name requested
295
     *                      currently only '_tree' is supported
296
     *
297
     * @return mixed value
298
     */
299
    public function __get($name)
300
    {
301
        $trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 1);
302
        if ('_tree' === $name) {
303
            \trigger_error("XoopsObjectTree::\$_tree is deprecated, accessed from {$trace[0]['file']} line {$trace[0]['line']},");
304
305
            return $this->tree;
306
        }
307
        \trigger_error('Undefined property: XoopsObjectTree::$' . $name . " in {$trace[0]['file']} line {$trace[0]['line']}, ", \E_USER_NOTICE);
308
309
        return null;
310
    }
311
}
312