Completed
Push — master ( bbb4f4...ddc2b8 )
by Michael
13s
created

XoopstubeTree::makeMySelBox()   D

Complexity

Conditions 9
Paths 112

Size

Total Lines 39
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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