Completed
Push — master ( 8ca430...3024c9 )
by Michael
03:12
created

XoopsTubeTree::getAllChildId()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 13

Duplication

Lines 19
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 2
b 0
f 0
nc 6
nop 3
dl 19
loc 19
rs 9.2
1
<?php
2
3
/**
4
 * Module: XoopsTube
5
 *
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * PHP version 5
11
 *
12
 * @category        Module
13
 * @package         Xoopstube
14
 * @author          XOOPS Development Team
15
 * @author          Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
16
 * @copyright       2001-2013 The XOOPS Project
17
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
18
 * @version         $Id$
19
 * @link            http://sourceforge.net/projects/xoops/
20
 * @since           1.0.6
21
 */
22
23
// defined('XOOPS_ROOT_PATH') || die('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...
24
25
/**
26
 * Abstract base class for forms
27
 *
28
 * @author    Kazumi Ono <[email protected]>
29
 * @author    John Neill <[email protected]>
30
 * @copyright copyright (c) XOOPS.org
31
 * @package   xoopstubetree
32
 * @access    public
33
 */
34
class XoopsTubeTree
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...
35
{
36
    public $table; //table with parent-child structure
37
    public $id; //name of unique id for records in table $table
38
    public $pid; // name of parent id used in table $table
39
    public $order; //specifies the order of query results
40
    public $title; // name of a field in table $table which will be used when  selection box and paths are generated
41
    public $db;
42
43
    //constructor of class XoopsTree
44
    //sets the names of table, unique id, and parend id
45
    /**
46
     * @param $tableName
47
     * @param $idName
48
     * @param $pidName
49
     */
50
    public function __construct($tableName, $idName, $pidName)
51
    {
52
        $this->db = & XoopsDatabaseFactory::getDatabaseConnection();;
53
        $this->table = $tableName;
54
        $this->id    = $idName;
55
        $this->pid   = $pidName;
56
    }
57
58
    // returns an array of first child objects for a given id($sel_id)
59
    /**
60
     * @param        $sel_id
61
     * @param string $order
62
     *
63
     * @return array
64
     */
65
    public function getFirstChild($sel_id, $order = "")
66
    {
67
        $sel_id = intval($sel_id);
68
        $arr    = array();
69
        $sql    = "SELECT * FROM " . $this->table . " WHERE " . $this->pid . "=" . $sel_id . "";
70
        if ($order != "") {
71
            $sql .= " ORDER BY $order";
72
        }
73
        $result = $this->db->query($sql);
74
        $count  = $this->db->getRowsNum($result);
75
        if ($count == 0) {
76
            return $arr;
77
        }
78
        while ($myrow = $this->db->fetchArray($result)) {
79
            array_push($arr, $myrow);
80
        }
81
82
        return $arr;
83
    }
84
85
    // returns an array of all FIRST child ids of a given id($sel_id)
86
    /**
87
     * @param $sel_id
88
     *
89
     * @return array
90
     */
91
    public function getFirstChildId($sel_id)
92
    {
93
        $sel_id  = intval($sel_id);
94
        $idarray = array();
95
        $result  = $this->db->query(
96
            "SELECT " . $this->id . " FROM " . $this->table . " WHERE " . $this->pid . "=" . $sel_id . ""
97
        );
98
        $count   = $this->db->getRowsNum($result);
99
        if ($count == 0) {
100
            return $idarray;
101
        }
102
        while (list ($id) = $this->db->fetchRow($result)) {
103
            array_push($idarray, $id);
104
        }
105
106
        return $idarray;
107
    }
108
109
    //returns an array of ALL child ids for a given id($sel_id)
110
    /**
111
     * @param        $sel_id
112
     * @param string $order
113
     * @param array  $idarray
114
     *
115
     * @return array
116
     */
117 View Code Duplication
    public function getAllChildId($sel_id, $order = "", $idarray = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
118
    {
119
        $sel_id = intval($sel_id);
120
        $sql    = "SELECT " . $this->id . " FROM " . $this->table . " WHERE " . $this->pid . "=" . $sel_id . "";
121
        if ($order != "") {
122
            $sql .= " ORDER BY $order";
123
        }
124
        $result = $this->db->query($sql);
125
        $count  = $this->db->getRowsNum($result);
126
        if ($count == 0) {
127
            return $idarray;
128
        }
129
        while (list ($r_id) = $this->db->fetchRow($result)) {
130
            array_push($idarray, $r_id);
131
            $idarray = $this->getAllChildId($r_id, $order, $idarray);
132
        }
133
134
        return $idarray;
135
    }
136
137
    //returns an array of ALL parent ids for a given id($sel_id)
138
    /**
139
     * @param        $sel_id
140
     * @param string $order
141
     * @param array  $idarray
142
     *
143
     * @return array
144
     */
145 View Code Duplication
    public function getAllParentId($sel_id, $order = "", $idarray = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
146
    {
147
        $sel_id = intval($sel_id);
148
        $sql    = "SELECT " . $this->pid . " FROM " . $this->table . " WHERE " . $this->id . "=" . $sel_id . "";
149
        if ($order != "") {
150
            $sql .= " ORDER BY $order";
151
        }
152
        $result = $this->db->query($sql);
153
        list ($r_id) = $this->db->fetchRow($result);
154
        if ($r_id == 0) {
155
            return $idarray;
156
        }
157
        array_push($idarray, $r_id);
158
        $idarray = $this->getAllParentId($r_id, $order, $idarray);
159
160
        return $idarray;
161
    }
162
163
    //generates path from the root id to a given id($sel_id)
164
    // the path is delimetered with "/"
165
    /**
166
     * @param        $sel_id
167
     * @param        $title
168
     * @param string $path
169
     *
170
     * @return string
171
     */
172
    public function getPathFromId($sel_id, $title, $path = "")
173
    {
174
        $sel_id = intval($sel_id);
175
        $result = $this->db->query(
176
            "SELECT " . $this->pid . ", " . $title . " FROM " . $this->table . " WHERE " . $this->id . "=$sel_id"
177
        );
178
        if ($this->db->getRowsNum($result) == 0) {
179
            return $path;
180
        }
181
        list ($parentid, $name) = $this->db->fetchRow($result);
182
        $myts = MyTextSanitizer::getInstance();
183
        $name = $myts->htmlspecialchars($name);
184
        $path = "/" . $name . $path . "";
185
        if ($parentid == 0) {
186
            return $path;
187
        }
188
        $path = $this->getPathFromId($parentid, $title, $path);
189
190
        return $path;
191
    }
192
193
    //makes a nicely ordered selection box
194
    //$preset_id is used to specify a preselected item
195
    //set $none to 1 to add a option with value 0
196
    /**
197
     * @param        $title
198
     * @param string $order
199
     * @param int    $preset_id
200
     * @param int    $none
201
     * @param string $sel_name
202
     * @param string $onchange
203
     */
204
    public function makeMySelBox($title, $order = "", $preset_id = 0, $none = 0, $sel_name = "", $onchange = "")
205
    {
206
        if ($sel_name == "") {
207
            $sel_name = $this->id;
208
        }
209
        $myts = MyTextSanitizer::getInstance();
210
        echo "<select name='" . $sel_name . "'";
211
        if ($onchange != "") {
212
            echo " onchange='" . $onchange . "'";
213
        }
214
        echo ">\n";
215
        $sql = "SELECT " . $this->id . ", " . $title . " FROM " . $this->table . " WHERE " . $this->pid . "=0";
216
        if ($order != "") {
217
            $sql .= " ORDER BY $order";
218
        }
219
        $result = $this->db->query($sql);
220
        if ($none) {
221
            echo "<option value='0'>----</option>\n";
222
        }
223
        while (list ($catid, $name) = $this->db->fetchRow($result)) {
224
            $sel = "";
225
            if ($catid == $preset_id) {
226
                $sel = " selected='selected'";
227
            }
228
            echo "<option value='$catid'$sel>$name</option>\n";
229
            $sel = "";
230
            $arr = $this->getChildTreeArray($catid, $order);
231
            foreach ($arr as $option) {
232
                $option['prefix'] = str_replace(".", "--", $option['prefix']);
233
                $catpath          = $option['prefix'] . "&nbsp;" . $myts->htmlspecialchars($option[$title]);
234
                if ($option[$this->id] == $preset_id) {
235
                    $sel = " selected='selected'";
236
                }
237
                echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n";
238
                $sel = "";
239
            }
240
        }
241
        echo "</select>\n";
242
    }
243
244
    //generates nicely formatted linked path from the root id to a given id
245
    /**
246
     * @param        $sel_id
247
     * @param        $title
248
     * @param        $funcURL
249
     * @param string $path
250
     *
251
     * @return string
252
     */
253
    public function getNicePathFromId($sel_id, $title, $funcURL, $path = "")
254
    {
255
        $path   = !empty($path) ? "&nbsp;:&nbsp;" . $path : $path;
256
        $sel_id = intval($sel_id);
257
        $sql    = "SELECT " . $this->pid . ", " . $title . " FROM " . $this->table . " WHERE " . $this->id . "=$sel_id";
258
        $result = $this->db->query($sql);
259
        if ($this->db->getRowsNum($result) == 0) {
260
            return $path;
261
        }
262
        list ($parentid, $name) = $this->db->fetchRow($result);
263
        $myts = MyTextSanitizer::getInstance();
264
        $name = $myts->htmlspecialchars($name);
265
        $path = "<a href='" . $funcURL . "&amp;" . $this->id . "=" . $sel_id . "'>" . $name . "</a>" . $path . "";
266
        if ($parentid == 0) {
267
            return $path;
268
        }
269
        $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path);
270
271
        return $path;
272
    }
273
274
    //generates id path from the root id to a given id
275
    // the path is delimetered with "/"
276
    /**
277
     * @param        $sel_id
278
     * @param string $path
279
     *
280
     * @return string
281
     */
282
    public function getIdPathFromId($sel_id, $path = "")
283
    {
284
        $sel_id = intval($sel_id);
285
        $result = $this->db->query(
286
            "SELECT " . $this->pid . " FROM " . $this->table . " WHERE " . $this->id . "=$sel_id"
287
        );
288
        if ($this->db->getRowsNum($result) == 0) {
289
            return $path;
290
        }
291
        list ($parentid) = $this->db->fetchRow($result);
292
        $path = "/" . $sel_id . $path . "";
293
        if ($parentid == 0) {
294
            return $path;
295
        }
296
        $path = $this->getIdPathFromId($parentid, $path);
297
298
        return $path;
299
    }
300
301
    /**
302
     * Enter description here...
303
     *
304
     * @param int    $sel_id
305
     * @param string $order
306
     * @param array  $parray
307
     *
308
     * @return array
309
     */
310 View Code Duplication
    public function getAllChild($sel_id = 0, $order = "", $parray = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
311
    {
312
        $sel_id = intval($sel_id);
313
        $sql    = "SELECT * FROM " . $this->table . " WHERE " . $this->pid . "=" . $sel_id . "";
314
        if ($order != "") {
315
            $sql .= " ORDER BY $order";
316
        }
317
        $result = $this->db->query($sql);
318
        $count  = $this->db->getRowsNum($result);
319
        if ($count == 0) {
320
            return $parray;
321
        }
322
        while ($row = $this->db->fetchArray($result)) {
323
            array_push($parray, $row);
324
            $parray = $this->getAllChild($row[$this->id], $order, $parray);
325
        }
326
327
        return $parray;
328
    }
329
330
    /**
331
     * Enter description here...
332
     *
333
     * @param int    $sel_id
334
     * @param string $order
335
     * @param array  $parray
336
     * @param string $r_prefix
337
     *
338
     * @return array
339
     */
340
    public function getChildTreeArray($sel_id = 0, $order = "", $parray = array(), $r_prefix = "")
341
    {
342
        $sel_id = intval($sel_id);
343
        $sql    = "SELECT * FROM " . $this->table . " WHERE " . $this->pid . "=" . $sel_id . "";
344
        if ($order != "") {
345
            $sql .= " ORDER BY $order";
346
        }
347
        $result = $this->db->query($sql);
348
        $count  = $this->db->getRowsNum($result);
349
        if ($count == 0) {
350
            return $parray;
351
        }
352
        while ($row = $this->db->fetchArray($result)) {
353
            $row['prefix'] = $r_prefix . ".";
354
            array_push($parray, $row);
355
            $parray = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']);
356
        }
357
358
        return $parray;
359
    }
360
}
361