Completed
Push — master ( 6452b0...d576ea )
by Michael
05:58 queued 03:02
created

MyXoopsObjectTree::getByKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
// $Id$
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) XOOPS Project (http://xoops.org)
40
 */
41
class MyXoopsObjectTree
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...
42
{
43
    /**#@+
44
     * @access    private
45
     */
46
    public $_parentId;
47
    public $_myId;
48
    public $_rootId = null;
49
    public $_tree   = array();
50
    public $_objects;
51
    /**#@-*/
52
53
    /**
54
     * Constructor
55
     *
56
     * @param array  $objectArr Array of {@link XoopsObject}s
57
     * @param string $myId      field name of object ID
58
     * @param string $parentId  field name of parent object ID
59
     * @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...
60
     **/
61
    public function __construct(&$objectArr, $myId, $parentId, $rootId = null)
62
    {
63
        //$this->db = xoopsDatabaseFactory::getDatabaseConnection();
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
64
        $this->_objects  =& $objectArr;
65
        $this->_myId     = $myId;
66
        $this->_parentId = $parentId;
67
        if (isset($rootId)) {
68
            $this->_rootId = $rootId;
69
        }
70
        $this->_initialize();
71
    }
72
73
    /**
74
     * Initialize the object
75
     *
76
     * @access    private
77
     **/
78
    public function _initialize()
79
    {
80
        foreach (array_keys($this->_objects) as $i) {
81
            $key1                          = $this->_objects[$i]->getVar($this->_myId);
82
            $this->_tree[$key1]['obj']     = $this->_objects[$i];
83
            $key2                          = $this->_objects[$i]->getVar($this->_parentId);
84
            $this->_tree[$key1]['parent']  = $key2;
85
            $this->_tree[$key2]['child'][] = $key1;
86
            if (isset($this->_rootId)) {
87
                $this->_tree[$key1]['root'] = $this->_objects[$i]->getVar($this->_rootId);
88
            }
89
        }
90
    }
91
92
    /**
93
     * Get the tree
94
     *
95
     * @return array Associative array comprising the tree
96
     **/
97
    public function &getTree()
98
    {
99
        return $this->_tree;
100
    }
101
102
    /**
103
     * returns an object from the tree specified by its id
104
     *
105
     * @param string $key ID of the object to retrieve
106
     *
107
     * @return object Object within the tree
108
     **/
109
    public function &getByKey($key)
110
    {
111
        return $this->_tree[$key]['obj'];
112
    }
113
114
    /**
115
     * returns an array of all the first child object of an object specified by its id
116
     *
117
     * @param string $key ID of the parent object
118
     *
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
     *
139
     * @return array Array of child nodes.
140
     **/
141
    public function getAllChild($key, $ret = array())
142
    {
143
        if (isset($this->_tree[$key]['child'])) {
144
            foreach ($this->_tree[$key]['child'] as $childkey) {
145
                $ret[$childkey] = $this->_tree[$childkey]['obj'];
146
                $children       = $this->getAllChild($childkey, $ret);
147
                foreach (array_keys($children) as $newkey) {
148
                    $ret[$newkey] = $children[$newkey];
149
                }
150
            }
151
        }
152
153
        return $ret;
154
    }
155
156
    /**
157
     * returns an array of all parent objects.
158
     * the key of returned array represents how many levels up from the specified object
159
     *
160
     * @param string $key     ID of the child object
161
     * @param array  $ret     (empty when called from outside) Result from previous recursions
162
     * @param int    $uplevel (empty when called from outside) level of recursion
163
     *
164
     * @return array Array of parent nodes.
165
     **/
166
    public function getAllParent($key, $ret = array(), $uplevel = 1)
167
    {
168
        if (isset($this->_tree[$key]['parent']) && isset($this->_tree[$this->_tree[$key]['parent']]['obj'])) {
169
            $ret[$uplevel] = $this->_tree[$this->_tree[$key]['parent']]['obj'];
170
            $parents       = $this->getAllParent($this->_tree[$key]['parent'], $ret, $uplevel + 1);
171
            foreach (array_keys($parents) as $newkey) {
172
                $ret[$newkey] = $parents[$newkey];
173
            }
174
        }
175
176
        return $ret;
177
    }
178
179
    /**
180
     * Make options for a select box from
181
     *
182
     * @param string $fieldName   Name of the member variable from the
183
     *                            node objects that should be used as the title for the options.
184
     * @param string $selected    Value to display as selected
185
     * @param int    $key         ID of the object to display as the root of select options
186
     * @param string $ret         (reference to a string when called from outside) Result from previous recursions
187
     * @param string $prefix_orig String to indent items at deeper levels
188
     * @param string $prefix_curr String to indent the current item
189
     *
190
     * @return void
191
    @access private
192
     */
193
    public function _makeSelBoxOptions($fieldName, $selected, $key, &$ret, $prefix_orig, $prefix_curr = '')
194
    {
195
        if ($key > 0) {
196
            $value = $this->_tree[$key]['obj']->getVar($this->_myId);
197
            $ret .= '<option value=\'' . $value . '\'';
198
            if ($value == $selected) {
199
                $ret .= ' selected="selected"';
200
            }
201
            $ret .= '>' . $prefix_curr . $this->_tree[$key]['obj']->getVar($fieldName) . '</option>';
202
            $prefix_curr .= $prefix_orig;
203
        }
204 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...
205
            foreach ($this->_tree[$key]['child'] as $childkey) {
206
                $this->_makeSelBoxOptions($fieldName, $selected, $childkey, $ret, $prefix_orig, $prefix_curr);
207
            }
208
        }
209
    }
210
211
    /**
212
     * Make a select box with options from the tree
213
     *
214
     * @param string  $name           Name of the select box
215
     * @param string  $fieldName      Name of the member variable from the node objects that should be used as the title for the options.
216
     * @param string  $prefix         String to indent deeper levels
217
     * @param string  $selected       Value to display as selected
218
     * @param bool    $addEmptyOption Set TRUE to add an empty option with value "0" at the top of the hierarchy
219
     * @param integer $key            ID of the object to display as the root of select options
220
     * @param string  $additional
221
     *
222
     * @return string HTML select box
223
     */
224
    public function makeSelBox($name, $fieldName, $prefix = '-', $selected = '', $addEmptyOption = false, $key = 0, $additional = '')
225
    {
226
        $ret = "<select id='" . $name . "' name='" . $name . "'";
227
        if ($additional != '') {
228
            $ret .= $additional;
229
        }
230
        $ret .= '>';
231
        if (false != $addEmptyOption) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
232
            $ret .= '<option value=\'0\'>----</option>';
233
        }
234
        $this->_makeSelBoxOptions($fieldName, $selected, $key, $ret, $prefix);
235
236
        return $ret . '</select>';
237
    }
238
239
    /**
240
     * Internal function used by makeTreeAsArray
241
     * @param        $fieldName
242
     * @param        $key
243
     * @param        $ret
244
     * @param        $prefix_orig
245
     * @param string $prefix_curr
246
     */
247
    public function _recursiveMakeTreeAsArray($fieldName, $key, &$ret, $prefix_orig, $prefix_curr = '')
248
    {
249
        if ($key > 0) {
250
            $value       = $this->_tree[$key]['obj']->getVar($this->_myId);
251
            $ret[$value] = $prefix_curr . $this->_tree[$key]['obj']->getVar($fieldName);
252
            $prefix_curr .= $prefix_orig;
253
        }
254 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...
255
            foreach ($this->_tree[$key]['child'] as $childkey) {
256
                $this->_recursiveMakeTreeAsArray($fieldName, $childkey, $ret, $prefix_orig, $prefix_curr);
257
            }
258
        }
259
    }
260
261
    /**
262
     * Identical function as makeSelBox but returns an array
263
     *
264
     * @param string  $fieldName Name of the member variable from the node objects that should be used as the title for the options.
265
     * @param string  $prefix    String to indent deeper levels
266
     * @param integer $key       ID of the object to display as the root of select options
267
     * @param null    $empty
268
     *
269
     * @return array key = object ID, value = $fieldName
270
     */
271
    public function makeTreeAsArray($fieldName, $prefix = '-', $key = 0, $empty = null)
272
    {
273
        $ret = array();
274
        if ($empty != null) {
275
            $ret[0] = $empty;
276
        }
277
        $this->_recursiveMakeTreeAsArray($fieldName, $key, $ret, $prefix);
278
279
        return $ret;
280
    }
281
282
    /**
283
     * Enter description here...
284
     *
285
     * @param unknown_type $sel_id
286
     * @param unknown_type $order
287
     * @param unknown_type $parray
288
     * @param unknown_type $r_prefix
289
     *
290
     * @return unknown
291
     */
292
    //      function getChildTreeArray($sel_id = 0, $order = "", $parray = array(), $r_prefix = "")
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% 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...
293
    //      {
294
    //          $sel_id = (int)($sel_id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
295
    //          $sql = "SELECT * FROM " . $this->table . " WHERE " . $this->pid . "=" . $sel_id . "";
296
    //          if ($order != "") {
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
297
    //              $sql .= " ORDER BY $order";
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% 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...
298
    //          }
299
    //          $result = $this->db->query($sql);
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
300
    //          $count = $this->db->getRowsNum($result);
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
301
    //          if ($count == 0) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
302
    //              return $parray;
303
    //          }
304
    //          while ($row = $this->db->fetchArray($result)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
305
    //              $row['prefix'] = $r_prefix . ".";
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
306
    //              array_push($parray, $row);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
307
    //              $parray = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
308
    //          }
309
    //          return $parray;
310
    //      }
311
}
312