Passed
Pull Request — master (#1270)
by Michael
04:56
created

XoopsTree::makeMySelBox()   B

Complexity

Conditions 9
Paths 112

Size

Total Lines 38
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 38
rs 7.9555
c 0
b 0
f 0
cc 9
nc 112
nop 6
1
<?php
2
/**
3
 * XOOPS tree handler
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @since               2.0.0
16
 * @author              Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17
 */
18
19
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20
21
/**
22
 * Abstract base class for forms
23
 *
24
 * @author              Kazumi Ono <[email protected]>
25
 * @author              John Neill <[email protected]>
26
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
27
 * @package             kernel
28
 * @subpackage          XoopsTree
29
 * @access              public
30
 */
31
class XoopsTree
32
{
33
    public $table; //table with parent-child structure
34
    public $id; //name of unique id for records in table $table
35
    public $pid; // name of parent id used in table $table
36
    public $order; //specifies the order of query results
37
    public $title; // name of a field in table $table which will be used when  selection box and paths are generated
38
    /**
39
     * @var \XoopsMySQLDatabase
40
     */
41
    public $db;
42
43
    //constructor of class XoopsTree
44
    //sets the names of table, unique id, and parend id
45
    /**
46
     * @param $table_name
47
     * @param $id_name
48
     * @param $pid_name
49
     */
50
    public function __construct($table_name, $id_name, $pid_name)
51
    {
52
        $GLOBALS['xoopsLogger']->addDeprecated("Class '" . __CLASS__ . "' is deprecated, check 'XoopsObjectTree' in tree.php");
53
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
54
        $this->table = $table_name;
55
        $this->id    = $id_name;
56
        $this->pid   = $pid_name;
57
    }
58
59
    // returns an array of first child objects for a given id($sel_id)
60
    /**
61
     * @param        $sel_id
62
     * @param string $order
63
     *
64
     * @return array
65
     */
66
    public function getFirstChild($sel_id, $order = '')
67
    {
68
        $sel_id = (int)$sel_id;
69
        $arr    = array();
70
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
71
        if ($order != '') {
72
            $sql .= " ORDER BY $order";
73
        }
74
        $result = $this->db->query($sql);
75
        if (!$this->db->isResultSet($result)) {
76
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR);
77
        }
78
        $count  = $this->db->getRowsNum($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

78
        $count  = $this->db->getRowsNum(/** @scrutinizer ignore-type */ $result);
Loading history...
79
        if ($count == 0) {
80
            return $arr;
81
        }
82
        while (false !== ($myrow = $this->db->fetchArray($result))) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

82
        while (false !== ($myrow = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
83
            $arr[] = $myrow;
84
        }
85
86
        return $arr;
87
    }
88
89
    // returns an array of all FIRST child ids of a given id($sel_id)
90
    /**
91
     * @param $sel_id
92
     *
93
     * @return array
94
     */
95
    public function getFirstChildId($sel_id)
96
    {
97
        $sel_id  = (int)$sel_id;
98
        $idarray = array();
99
        $result  = $this->db->query('SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '');
100
        $count   = $this->db->getRowsNum($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

100
        $count   = $this->db->getRowsNum(/** @scrutinizer ignore-type */ $result);
Loading history...
101
        if ($count == 0) {
102
            return $idarray;
103
        }
104
        while (false !== (list($id) = $this->db->fetchRow($result))) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchRow() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

104
        while (false !== (list($id) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result))) {
Loading history...
105
            $idarray[] = $id;
106
        }
107
108
        return $idarray;
109
    }
110
111
    //returns an array of ALL child ids for a given id($sel_id)
112
    /**
113
     * @param        $sel_id
114
     * @param string $order
115
     * @param array  $idarray
116
     *
117
     * @return array
118
     */
119
    public function getAllChildId($sel_id, $order = '', $idarray = array())
120
    {
121
        $sel_id = (int)$sel_id;
122
        $sql    = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
123
        if ($order != '') {
124
            $sql .= " ORDER BY $order";
125
        }
126
        $result = $this->db->query($sql);
127
        $count  = $this->db->getRowsNum($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

127
        $count  = $this->db->getRowsNum(/** @scrutinizer ignore-type */ $result);
Loading history...
128
        if ($count == 0) {
129
            return $idarray;
130
        }
131
        while (false !== (list($r_id) = $this->db->fetchRow($result))) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchRow() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

131
        while (false !== (list($r_id) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result))) {
Loading history...
132
            $idarray[] = $r_id;
133
            $idarray   = $this->getAllChildId($r_id, $order, $idarray);
134
        }
135
136
        return $idarray;
137
    }
138
139
    //returns an array of ALL parent ids for a given id($sel_id)
140
    /**
141
     * @param        $sel_id
142
     * @param string $order
143
     * @param array  $idarray
144
     *
145
     * @return array
146
     */
147
    public function getAllParentId($sel_id, $order = '', $idarray = array())
148
    {
149
        $sel_id = (int)$sel_id;
150
        $sql    = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . '';
151
        if ($order != '') {
152
            $sql .= " ORDER BY $order";
153
        }
154
        $result = $this->db->query($sql);
155
        list($r_id) = $this->db->fetchRow($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchRow() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

155
        list($r_id) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result);
Loading history...
156
        if ($r_id == 0) {
157
            return $idarray;
158
        }
159
        $idarray[] = $r_id;
160
        $idarray   = $this->getAllParentId($r_id, $order, $idarray);
161
162
        return $idarray;
163
    }
164
165
    //generates path from the root id to a given id($sel_id)
166
    // the path is delimetered with "/"
167
    /**
168
     * @param        $sel_id
169
     * @param        $title
170
     * @param string $path
171
     *
172
     * @return string
173
     */
174
    public function getPathFromId($sel_id, $title, $path = '')
175
    {
176
        $sel_id = (int)$sel_id;
177
        $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
178
        if ($this->db->getRowsNum($result) == 0) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

178
        if ($this->db->getRowsNum(/** @scrutinizer ignore-type */ $result) == 0) {
Loading history...
179
            return $path;
180
        }
181
        list($parentid, $name) = $this->db->fetchRow($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchRow() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

181
        list($parentid, $name) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result);
Loading history...
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 (false !== (list($catid, $name) = $this->db->fetchRow($result))) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchRow() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

223
        while (false !== (list($catid, $name) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result))) {
Loading history...
224
            $sel = '';
225
            if ($catid == $preset_id) {
226
                $sel = " 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";
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 = (int)$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) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

259
        if ($this->db->getRowsNum(/** @scrutinizer ignore-type */ $result) == 0) {
Loading history...
260
            return $path;
261
        }
262
        list($parentid, $name) = $this->db->fetchRow($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchRow() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

262
        list($parentid, $name) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result);
Loading history...
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 = (int)$sel_id;
285
        $result = $this->db->query('SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
286
        if ($this->db->getRowsNum($result) == 0) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

286
        if ($this->db->getRowsNum(/** @scrutinizer ignore-type */ $result) == 0) {
Loading history...
287
            return $path;
288
        }
289
        list($parentid) = $this->db->fetchRow($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchRow() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

289
        list($parentid) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result);
Loading history...
290
        $path = '/' . $sel_id . $path . '';
291
        if ($parentid == 0) {
292
            return $path;
293
        }
294
        $path = $this->getIdPathFromId($parentid, $path);
295
296
        return $path;
297
    }
298
299
    /**
300
     * Enter description here...
301
     *
302
     * @param int|mixed    $sel_id
303
     * @param string|mixed $order
304
     * @param array|mixed  $parray
305
     *
306
     * @return mixed
307
     */
308
    public function getAllChild($sel_id = 0, $order = '', $parray = array())
309
    {
310
        $sel_id = (int)$sel_id;
311
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
312
        if ($order != '') {
313
            $sql .= " ORDER BY $order";
314
        }
315
        $result = $this->db->query($sql);
316
        if (!$this->db->isResultSet($result)) {
317
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR);
318
        }
319
        $count  = $this->db->getRowsNum($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

319
        $count  = $this->db->getRowsNum(/** @scrutinizer ignore-type */ $result);
Loading history...
320
        if ($count == 0) {
321
            return $parray;
322
        }
323
        while (false !== ($row = $this->db->fetchArray($result))) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

323
        while (false !== ($row = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
324
            $parray[] = $row;
325
            $parray   = $this->getAllChild($row[$this->id], $order, $parray);
326
        }
327
328
        return $parray;
329
    }
330
331
    /**
332
     * Enter description here...
333
     *
334
     * @param  int|mixed    $sel_id
335
     * @param  string|mixed $order
336
     * @param  array|mixed  $parray
337
     * @param  string|mixed $r_prefix
338
     * @return mixed
339
     */
340
    public function getChildTreeArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '')
341
    {
342
        $sel_id = (int)$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
        if (!$this->db->isResultSet($result)) {
349
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR);
350
        }
351
        $count  = $this->db->getRowsNum($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

351
        $count  = $this->db->getRowsNum(/** @scrutinizer ignore-type */ $result);
Loading history...
352
        if ($count == 0) {
353
            return $parray;
354
        }
355
        while (false !== ($row = $this->db->fetchArray($result))) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

355
        while (false !== ($row = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
356
            $row['prefix'] = $r_prefix . '.';
357
            $parray[]      = $row;
358
            $parray        = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']);
359
        }
360
361
        return $parray;
362
    }
363
}
364