Completed
Push — master ( 00e474...9d3fbd )
by Michael
04:26
created

Oledrion_XoopsObjectTree::getAllParent()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 2
nop 3
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
1
<?php
2
//
3
//  ------------------------------------------------------------------------ //
4
//                XOOPS - PHP Content Management System                      //
5
//                  Copyright (c) 2000-2016 XOOPS.org                        //
6
//                       <http://xoops.org/>                             //
7
//  ------------------------------------------------------------------------ //
8
//  This program is free software; you can redistribute it and/or modify     //
9
//  it under the terms of the GNU General Public License as published by     //
10
//  the Free Software Foundation; either version 2 of the License, or        //
11
//  (at your option) any later version.                                      //
12
//                                                                           //
13
//  You may not change or alter any portion of this comment or credits       //
14
//  of supporting developers from this source code or any supporting         //
15
//  source code which is considered copyrighted (c) material of the          //
16
//  original comment or credit authors.                                      //
17
//                                                                           //
18
//  This program is distributed in the hope that it will be useful,          //
19
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21
//  GNU General Public License for more details.                             //
22
//                                                                           //
23
//  You should have received a copy of the GNU General Public License        //
24
//  along with this program; if not, write to the Free Software              //
25
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26
//  ------------------------------------------------------------------------ //
27
// Author: Kazumi Ono (AKA onokazu)                                          //
28
// URL: http://xoops.org/ http://jp.xoops.org/  http://www.myweb.ne.jp/  //
29
// Project: XOOPS Project (http://xoops.org/)                            //
30
// ------------------------------------------------------------------------- //
31
32
/**
33
 * A tree structures with {@link XoopsObject}s as nodes
34
 *
35
 * @package          kernel
36
 * @subpackage       core
37
 *
38
 * @author           Kazumi Ono     <[email protected]>
39
 * @copyright    (c) 2000-2003 XOOPS Project (http://xoops.org)
40
 */
41
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42
43
class Oledrion_XoopsObjectTree
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
44
{
45
    /**#@+
46
     * @access    private
47
     */
48
    public $_parentId;
49
    public $_myId;
50
    public $_rootId = null;
51
    public $_tree   = array();
52
    public $_objects;
53
54
    /**#@-*/
55
56
    /**
57
     * Constructor
58
     *
59
     * @param array  $objectArr Array of {@link XoopsObject}s
60
     * @param string $myId      field name of object ID
61
     * @param string $parentId  field name of parent object ID
62
     * @param string $rootId    field name of root object ID
0 ignored issues
show
Documentation introduced by
Should the type for parameter $rootId not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
63
     **/
64
    public function __construct(&$objectArr, $myId, $parentId, $rootId = null)
65
    {
66
        $this->_objects  =& $objectArr;
67
        $this->_myId     = $myId;
68
        $this->_parentId = $parentId;
69
        if (isset($rootId)) {
70
            $this->_rootId = $rootId;
71
        }
72
        $this->_initialize();
73
    }
74
75
    /**
76
     * Initialize the object
77
     *
78
     * @access    private
79
     **/
80
    public function _initialize()
81
    {
82
        foreach (array_keys($this->_objects) as $i) {
83
            $key1                          = $this->_objects[$i]->getVar($this->_myId);
84
            $this->_tree[$key1]['obj']     = $this->_objects[$i];
85
            $key2                          = $this->_objects[$i]->getVar($this->_parentId);
86
            $this->_tree[$key1]['parent']  = $key2;
87
            $this->_tree[$key2]['child'][] = $key1;
88
            if (isset($this->_rootId)) {
89
                $this->_tree[$key1]['root'] = $this->_objects[$i]->getVar($this->_rootId);
90
            }
91
        }
92
    }
93
94
    /**
95
     * Get the tree
96
     *
97
     * @return array Associative array comprising the tree
98
     **/
99
    public function getTree()
100
    {
101
        return $this->_tree;
102
    }
103
104
    /**
105
     * returns an object from the tree specified by its id
106
     *
107
     * @param  string $key ID of the object to retrieve
108
     * @return object Object within the tree
109
     **/
110
    public function getByKey($key)
111
    {
112
        return $this->_tree[$key]['obj'];
113
    }
114
115
    /**
116
     * returns an array of all the first child object of an object specified by its id
117
     *
118
     * @param  string $key ID of the parent object
119
     * @return array  Array of children of the parent
120
     **/
121
    public function getFirstChild($key)
122
    {
123
        $ret = array();
124
        if (isset($this->_tree[$key]['child'])) {
125
            foreach ($this->_tree[$key]['child'] as $childkey) {
126
                $ret[$childkey] = $this->_tree[$childkey]['obj'];
127
            }
128
        }
129
130
        return $ret;
131
    }
132
133
    /**
134
     * returns an array of all child objects of an object specified by its id
135
     *
136
     * @param  string $key ID of the parent
137
     * @param  array  $ret (Empty when called from client) Array of children from previous recursions.
138
     * @return array  Array of child nodes.
139
     **/
140
    public function getAllChild($key, $ret = array())
141
    {
142
        if (isset($this->_tree[$key]['child'])) {
143
            foreach ($this->_tree[$key]['child'] as $childkey) {
144
                $ret[$childkey] = $this->_tree[$childkey]['obj'];
145
                $children       = $this->getAllChild($childkey, $ret);
146
                foreach (array_keys($children) as $newkey) {
147
                    $ret[$newkey] = $children[$newkey];
148
                }
149
            }
150
        }
151
152
        return $ret;
153
    }
154
155
    /**
156
     * returns an array of all parent objects.
157
     * the key of returned array represents how many levels up from the specified object
158
     *
159
     * @param  string $key     ID of the child object
160
     * @param  array  $ret     (empty when called from outside) Result from previous recursions
161
     * @param  int    $uplevel (empty when called from outside) level of recursion
162
     * @return array  Array of parent nodes.
163
     **/
164
    public function getAllParent($key, $ret = array(), $uplevel = 1)
165
    {
166
        if (isset($this->_tree[$key]['parent']) && isset($this->_tree[$this->_tree[$key]['parent']]['obj'])) {
167
            $ret[$uplevel] = $this->_tree[$this->_tree[$key]['parent']]['obj'];
168
            $parents       = $this->getAllParent($this->_tree[$key]['parent'], $ret, $uplevel + 1);
169
            foreach (array_keys($parents) as $newkey) {
170
                $ret[$newkey] = $parents[$newkey];
171
            }
172
        }
173
174
        return $ret;
175
    }
176
177
    /**
178
     * Make options for a select box from
179
     *
180
     * @param string $fieldName   Name of the member variable from the
181
     *                            node objects that should be used as the title for the options.
182
     * @param string $selected    Value to display as selected
183
     * @param int    $key         ID of the object to display as the root of select options
184
     * @param string $ret         (reference to a string when called from outside) Result from previous recursions
185
     * @param string $prefix_orig String to indent items at deeper levels
186
     * @param string $prefix_curr String to indent the current item
187
     * @access    private
188
     */
189
    public function _makeSelBoxOptions($fieldName, $selected, $key, &$ret, $prefix_orig, $prefix_curr = '')
190
    {
191
        if ($key > 0) {
192
            $value = $this->_tree[$key]['obj']->getVar($this->_myId);
193
            $ret .= '<option value=\'' . $value . '\'';
194
            if ($value == $selected) {
195
                $ret .= ' selected';
196
            }
197
            $ret .= '>' . $prefix_curr . $this->_tree[$key]['obj']->getVar($fieldName) . '</option>';
198
            $prefix_curr .= $prefix_orig;
199
        }
200 View Code Duplication
        if (isset($this->_tree[$key]['child']) && !empty($this->_tree[$key]['child'])) {
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...
201
            foreach ($this->_tree[$key]['child'] as $childkey) {
202
                $this->_makeSelBoxOptions($fieldName, $selected, $childkey, $ret, $prefix_orig, $prefix_curr);
203
            }
204
        }
205
    }
206
207
    /**
208
     * Make a select box with options from the tree
209
     *
210
     * @param  string      $name           Name of the select box
211
     * @param  string      $fieldName      Name of the member variable from the node objects that should be used as the title for the options.
212
     * @param  string      $prefix         String to indent deeper levels
213
     * @param  string      $selected       Value to display as selected
214
     * @param  bool|string $addEmptyOption Set TRUE to add an empty option with value "0" at the top of the hierarchy
215
     * @param  integer     $key            ID of the object to display as the root of select options
216
     * @param  string      $additional
217
     * @return string      HTML select box
218
     */
219
    public function makeSelBox(
220
        $name,
221
        $fieldName,
222
        $prefix = '-',
223
        $selected = '',
224
        $addEmptyOption = '',
225
        $key = 0,
226
        $additional = ''
227
    ) {
228
        $ret = "<select id='" . $name . "' name='" . $name . "'";
229
        if ($additional != '') {
230
            $ret .= $additional;
231
        }
232
        $ret .= '>';
233
        if ($addEmptyOption != '') {
234
            $tmpSelected = '';
235
            if ($selected == 0) {
236
                $tmpSelected = ' selected';
237
            }
238
            $ret .= '<option' . $tmpSelected . ' value=\'0\'>' . $addEmptyOption . '</option>';
239
        }
240
        $this->_makeSelBoxOptions($fieldName, $selected, $key, $ret, $prefix);
241
242
        return $ret . '</select>';
243
    }
244
245
    /**
246
     * Internal function used by makeTreeAsArray
247
     * @param        $fieldName
248
     * @param        $key
249
     * @param        $ret
250
     * @param        $prefix_orig
251
     * @param string $prefix_curr
252
     */
253
    public function _recursiveMakeTreeAsArray($fieldName, $key, &$ret, $prefix_orig, $prefix_curr = '')
254
    {
255
        if ($key > 0) {
256
            $value       = $this->_tree[$key]['obj']->getVar($this->_myId);
257
            $ret[$value] = $prefix_curr . $this->_tree[$key]['obj']->getVar($fieldName);
258
            $prefix_curr .= $prefix_orig;
259
        }
260 View Code Duplication
        if (isset($this->_tree[$key]['child']) && !empty($this->_tree[$key]['child'])) {
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...
261
            foreach ($this->_tree[$key]['child'] as $childkey) {
262
                $this->_recursiveMakeTreeAsArray($fieldName, $childkey, $ret, $prefix_orig, $prefix_curr);
263
            }
264
        }
265
    }
266
267
    /**
268
     * Identical function as makeSelBox but returns an array
269
     *
270
     * @param  string  $fieldName Name of the member variable from the node objects that should be used as the title for the options.
271
     * @param  string  $prefix    String to indent deeper levels
272
     * @param  integer $key       ID of the object to display as the root of select options
273
     * @param  null    $empty
274
     * @return array   key = object ID, value = $fieldName
275
     */
276
    public function makeTreeAsArray($fieldName, $prefix = '-', $key = 0, $empty = null)
277
    {
278
        $ret = array();
279
        if (null !== $empty) {
280
            $ret[0] = $empty;
281
        }
282
        $this->_recursiveMakeTreeAsArray($fieldName, $key, $ret, $prefix);
283
284
        return $ret;
285
    }
286
}
287