XoopsTree::getNicePathFromId()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 6
nop 4
dl 0
loc 19
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Wfdownloads\Common;
4
5
/**
6
 * XOOPS tree handler
7
 *
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 *
15
 * @copyright       XOOPS Project (https://xoops.org)
16
 * @license         GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 * @package         kernel
18
 * @since           2.0.0
19
 * @author          Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
20
 * @version         $Id: xoopstree.php 12863 2014-11-22 04:11:17Z beckmi $
21
 */
22
23
/**
24
 * Abstract base class for forms
25
 *
26
 * @author     Kazumi Ono <[email protected]>
27
 * @author     John Neill <[email protected]>
28
 * @copyright  copyright (c) XOOPS.org
29
 * @package    kernel
30
 * @subpackage XoopsTree
31
 * @access     public
32
 */
33
class XoopsTree
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
    //constructor of class XoopsTree
42
    //sets the names of table, unique id, and parend id
43
44
    /**
45
     * @param $table_name
46
     * @param $id_name
47
     * @param $pid_name
48
     */
49
    public function __construct($table_name, $id_name, $pid_name)
50
    {
51
        //        $GLOBALS['xoopsLogger']->addDeprecated("Class '" . __CLASS__ . "' is deprecated, check 'XoopsObjectTree' in tree.php");
52
        $this->db    = \XoopsDatabaseFactory::getDatabaseConnection();
53
        $this->table = $table_name;
54
        $this->id    = $id_name;
55
        $this->pid   = $pid_name;
56
    }
57
58
    // returns an array of first child objects for a given id($sel_id)
59
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    = [];
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 ($result) {
76
            while (false !== ($myrow = $this->db->fetchArray($result))) {
77
                $arr[] = $myrow;
78
            }
79
        }
80
        return $arr;
81
    }
82
83
    // returns an array of all FIRST child ids of a given id($sel_id)
84
85
    /**
86
     * @param $sel_id
87
     *
88
     * @return array
89
     */
90
    public function getFirstChildId($sel_id)
91
    {
92
        $sel_id  = (int)$sel_id;
93
        $idarray = [];
94
        $result  = $this->db->query('SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '');
95
        $count   = $this->db->getRowsNum($result);
96
        if (0 == $count) {
97
            return $idarray;
98
        }
99
        while (list($id) = $this->db->fetchRow($result)) {
100
            $idarray[] = $id;
101
        }
102
103
        return $idarray;
104
    }
105
106
    //returns an array of ALL child ids for a given id($sel_id)
107
108
    /**
109
     * @param        $sel_id
110
     * @param string $order
111
     * @param array  $idarray
112
     *
113
     * @return array
114
     */
115
    public function getAllChildId($sel_id, $order = '', $idarray = [])
116
    {
117
        $sel_id = (int)$sel_id;
118
        $sql    = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
119
        if ('' != $order) {
120
            $sql .= " ORDER BY $order";
121
        }
122
        $result = $this->db->query($sql);
123
        $count  = $this->db->getRowsNum($result);
124
        if (0 == $count) {
125
            return $idarray;
126
        }
127
        while (list($r_id) = $this->db->fetchRow($result)) {
128
            $idarray[] = $r_id;
129
            $idarray   = $this->getAllChildId($r_id, $order, $idarray);
130
        }
131
132
        return $idarray;
133
    }
134
135
    //returns an array of ALL parent ids for a given id($sel_id)
136
137
    /**
138
     * @param        $sel_id
139
     * @param string $order
140
     * @param array  $idarray
141
     *
142
     * @return array
143
     */
144
    public function getAllParentId($sel_id, $order = '', $idarray = [])
145
    {
146
        $sel_id = (int)$sel_id;
147
        $sql    = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . '';
148
        if ('' != $order) {
149
            $sql .= " ORDER BY $order";
150
        }
151
        $result = $this->db->query($sql);
152
        [$r_id] = $this->db->fetchRow($result);
153
        if (0 == $r_id) {
154
            return $idarray;
155
        }
156
        $idarray[] = $r_id;
157
        $idarray   = $this->getAllParentId($r_id, $order, $idarray);
158
159
        return $idarray;
160
    }
161
162
    //generates path from the root id to a given id($sel_id)
163
    // the path is delimetered with "/"
164
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 = (int)$sel_id;
175
        $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
176
        if (0 == $this->db->getRowsNum($result)) {
177
            return $path;
178
        }
179
        [$parentid, $name] = $this->db->fetchRow($result);
180
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
181
        $name = htmlspecialchars($name, ENT_QUOTES | ENT_HTML5);
182
        $path = '/' . $name . $path . '';
183
        if (0 == $parentid) {
184
            return $path;
185
        }
186
        $path = $this->getPathFromId($parentid, $title, $path);
187
188
        return $path;
189
    }
190
191
    //makes a nicely ordered selection box
192
    //$preset_id is used to specify a preselected item
193
    //set $none to 1 to add a option with value 0
194
195
    /**
196
     * @param        $title
197
     * @param string $order
198
     * @param int    $preset_id
199
     * @param int    $none
200
     * @param string $sel_name
201
     * @param string $onchange
202
     */
203
    public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '')
204
    {
205
        if ('' == $sel_name) {
206
            $sel_name = $this->id;
207
        }
208
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
209
        echo "<select name='" . $sel_name . "'";
210
        if ('' != $onchange) {
211
            echo " onchange='" . $onchange . "'";
212
        }
213
        echo ">\n";
214
        $sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0';
215
        if ('' != $order) {
216
            $sql .= " ORDER BY $order";
217
        }
218
        $result = $this->db->query($sql);
219
        if ($none) {
220
            echo "<option value='0'>----</option>\n";
221
        }
222
        while (list($catid, $name) = $this->db->fetchRow($result)) {
223
            $sel = '';
224
            if ($catid == $preset_id) {
225
                $sel = ' selected';
226
            }
227
            echo "<option value='$catid'$sel>$name</option>\n";
228
            $sel = '';
229
            $arr = $this->getChildTreeArray($catid, $order);
230
            foreach ($arr as $option) {
231
                $option['prefix'] = \str_replace('.', '--', $option['prefix']);
232
                $catpath          = $option['prefix'] . '&nbsp;' . htmlspecialchars($option[$title], ENT_QUOTES | ENT_HTML5);
233
                if ($option[$this->id] == $preset_id) {
234
                    $sel = ' selected';
235
                }
236
                echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n";
237
                $sel = '';
238
            }
239
        }
240
        echo "</select>\n";
241
    }
242
243
    //generates nicely formatted linked path from the root id to a given id
244
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 (0 == $this->db->getRowsNum($result)) {
260
            return $path;
261
        }
262
        [$parentid, $name] = $this->db->fetchRow($result);
263
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
264
        $name = htmlspecialchars($name, ENT_QUOTES | ENT_HTML5);
265
        $path = "<a href='" . $funcURL . '&amp;' . $this->id . '=' . $sel_id . "'>" . $name . '</a>' . $path . '';
266
        if (0 == $parentid) {
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
    /**
278
     * @param        $sel_id
279
     * @param string $path
280
     *
281
     * @return string
282
     */
283
    public function getIdPathFromId($sel_id, $path = '')
284
    {
285
        $sel_id = (int)$sel_id;
286
        $result = $this->db->query('SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
287
        if (0 == $this->db->getRowsNum($result)) {
288
            return $path;
289
        }
290
        [$parentid] = $this->db->fetchRow($result);
291
        $path = '/' . $sel_id . $path . '';
292
        if (0 == $parentid) {
293
            return $path;
294
        }
295
        $path = $this->getIdPathFromId($parentid, $path);
296
297
        return $path;
298
    }
299
300
    /**
301
     * Enter description here...
302
     *
303
     * @param int|mixed    $sel_id
304
     * @param string|mixed $order
305
     * @param array|mixed  $parray
306
     *
307
     * @return mixed
308
     */
309
    public function getAllChild($sel_id = 0, $order = '', $parray = [])
310
    {
311
        $sel_id = (int)$sel_id;
312
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
313
        if ('' != $order) {
314
            $sql .= " ORDER BY $order";
315
        }
316
        $result = $this->db->query($sql);
317
        $count  = $this->db->getRowsNum($result);
318
        if (0 == $count) {
319
            return $parray;
320
        }
321
        while (false !== ($row = $this->db->fetchArray($result))) {
322
            $parray[] = $row;
323
            $parray   = $this->getAllChild($row[$this->id], $order, $parray);
324
        }
325
326
        return $parray;
327
    }
328
329
    /**
330
     * Enter description here...
331
     *
332
     * @param int|mixed    $sel_id
333
     * @param string|mixed $order
334
     * @param array|mixed  $parray
335
     * @param string|mixed $r_prefix
336
     *
337
     * @return mixed
338
     */
339
    public function getChildTreeArray($sel_id = 0, $order = '', $parray = [], $r_prefix = '')
340
    {
341
        $sel_id = (int)$sel_id;
342
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
343
        if ('' != $order) {
344
            $sql .= " ORDER BY $order";
345
        }
346
        $result = $this->db->query($sql);
347
        $count  = $this->db->getRowsNum($result);
348
        if (0 == $count) {
349
            return $parray;
350
        }
351
        while (false !== ($row = $this->db->fetchArray($result))) {
352
            $row['prefix'] = $r_prefix . '.';
353
            $parray[]      = $row;
354
            $parray        = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']);
355
        }
356
357
        return $parray;
358
    }
359
}
360