LexikonTree::getAllParentId()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
nc 4
nop 3
dl 0
loc 16
rs 9.9
c 1
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Lexikon;
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
 */
21
22
/**
23
 * Abstract base class for forms
24
 *
25
 * @author     Kazumi Ono <[email protected]>
26
 * @author     John Neill <[email protected]>
27
 * @copyright  copyright (c) XOOPS.org
28
 * @package    kernel
29
 * @subpackage LexikonTree
30
 * @access     public
31
 */
32
class LexikonTree
33
{
34
    public $table; //table with parent-child structure
35
    public $id; //name of unique id for records in table $table
36
    public $pid; // name of parent id used in table $table
37
    public $order; //specifies the order of query results
38
    public $title; // name of a field in table $table which will be used when  selection box and paths are generated
39
    public $db;
40
    //constructor of class LexikonTree
41
    //sets the names of table, unique id, and parend id
42
43
    /**
44
     * @param $table_name
45
     * @param $id_name
46
     * @param $pid_name
47
     */
48
    public function __construct($table_name, $id_name, $pid_name)
49
    {
50
        //        $GLOBALS['xoopsLogger']->addDeprecated("Class '" . __CLASS__ . "' is deprecated, check 'XoopsObjectTree' in tree.php");
51
        $this->db    = \XoopsDatabaseFactory::getDatabaseConnection();
52
        $this->table = $table_name;
53
        $this->id    = $id_name;
54
        $this->pid   = $pid_name;
55
    }
56
57
    // returns an array of first child objects for a given id($sel_id)
58
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 = (int)$sel_id;
68
        $arr    = [];
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
        if ($result instanceof \mysqli_result) {
75
            while (false !== ($myrow = $this->db->fetchArray($result))) {
76
                $arr[] = $myrow;
77
            }
78
        }
79
        return $arr;
80
    }
81
82
    // returns an array of all FIRST child ids of a given id($sel_id)
83
84
    /**
85
     * @param $sel_id
86
     *
87
     * @return array
88
     */
89
    public function getFirstChildId($sel_id)
90
    {
91
        $sel_id  = (int)$sel_id;
92
        $idarray = [];
93
        $result  = $this->db->query('SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '');
94
        $count   = $this->db->getRowsNum($result);
95
        if (0 == $count) {
96
            return $idarray;
97
        }
98
        while (list($id) = $this->db->fetchRow($result)) {
99
            $idarray[] = $id;
100
        }
101
102
        return $idarray;
103
    }
104
105
    //returns an array of ALL child ids for a given id($sel_id)
106
107
    /**
108
     * @param        $sel_id
109
     * @param string $order
110
     * @param array  $idarray
111
     *
112
     * @return array
113
     */
114
    public function getAllChildId($sel_id, $order = '', $idarray = [])
115
    {
116
        $sel_id = (int)$sel_id;
117
        $sql    = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
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 (list($r_id) = $this->db->fetchRow($result)) {
127
            $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($sel_id)
135
136
    /**
137
     * @param        $sel_id
138
     * @param string $order
139
     * @param array  $idarray
140
     *
141
     * @return array
142
     */
143
    public function getAllParentId($sel_id, $order = '', $idarray = [])
144
    {
145
        $sel_id = (int)$sel_id;
146
        $sql    = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . '';
147
        if ('' != $order) {
148
            $sql .= " ORDER BY $order";
149
        }
150
        $result = $this->db->query($sql);
151
        [$r_id] = $this->db->fetchRow($result);
152
        if (0 == $r_id) {
153
            return $idarray;
154
        }
155
        $idarray[] = $r_id;
156
        $idarray   = $this->getAllParentId($r_id, $order, $idarray);
157
158
        return $idarray;
159
    }
160
161
    //generates path from the root id to a given id($sel_id)
162
    // the path is delimetered with "/"
163
164
    /**
165
     * @param        $sel_id
166
     * @param        $title
167
     * @param string $path
168
     *
169
     * @return string
170
     */
171
    public function getPathFromId($sel_id, $title, $path = '')
172
    {
173
        $sel_id = (int)$sel_id;
174
        $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
175
        if (0 == $this->db->getRowsNum($result)) {
176
            return $path;
177
        }
178
        [$parentid, $name] = $this->db->fetchRow($result);
179
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
180
        $name = \htmlspecialchars($name, \ENT_QUOTES | \ENT_HTML5);
181
        $path = '/' . $name . $path . '';
182
        if (0 == $parentid) {
183
            return $path;
184
        }
185
        $path = $this->getPathFromId($parentid, $title, $path);
186
187
        return $path;
188
    }
189
190
    //makes a nicely ordered selection box
191
    //$preset_id is used to specify a preselected item
192
    //set $none to 1 to add a option with value 0
193
194
    /**
195
     * @param        $title
196
     * @param string $order
197
     * @param int    $preset_id
198
     * @param int    $none
199
     * @param string $sel_name
200
     * @param string $onchange
201
     */
202
    public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '')
203
    {
204
        if ('' == $sel_name) {
205
            $sel_name = $this->id;
206
        }
207
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
208
        echo "<select name='" . $sel_name . "'";
209
        if ('' != $onchange) {
210
            echo " onchange='" . $onchange . "'";
211
        }
212
        echo ">\n";
213
        $sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0';
214
        if ('' != $order) {
215
            $sql .= " ORDER BY $order";
216
        }
217
        $result = $this->db->query($sql);
218
        if ($none) {
219
            echo "<option value='0'>----</option>\n";
220
        }
221
        while (list($catid, $name) = $this->db->fetchRow($result)) {
222
            $sel = '';
223
            if ($catid == $preset_id) {
224
                $sel = ' selected';
225
            }
226
            echo "<option value='$catid'$sel>$name</option>\n";
227
            $sel = '';
228
            $arr = $this->getChildTreeArray($catid, $order);
229
            foreach ($arr as $option) {
230
                $option['prefix'] = \str_replace('.', '--', $option['prefix']);
231
                $catpath          = $option['prefix'] . '&nbsp;' . \htmlspecialchars($option[$title], \ENT_QUOTES | \ENT_HTML5);
232
                if ($option[$this->id] == $preset_id) {
233
                    $sel = ' selected';
234
                }
235
                echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n";
236
                $sel = '';
237
            }
238
        }
239
        echo "</select>\n";
240
    }
241
242
    //generates nicely formatted linked path from the root id to a given id
243
244
    /**
245
     * @param        $sel_id
246
     * @param        $title
247
     * @param        $funcURL
248
     * @param string $path
249
     *
250
     * @return string
251
     */
252
    public function getNicePathFromId($sel_id, $title, $funcURL, $path = '')
253
    {
254
        $path   = !empty($path) ? '&nbsp;:&nbsp;' . $path : $path;
255
        $sel_id = (int)$sel_id;
256
        $sql    = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id";
257
        $result = $this->db->query($sql);
258
        if (0 == $this->db->getRowsNum($result)) {
259
            return $path;
260
        }
261
        [$parentid, $name] = $this->db->fetchRow($result);
262
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
263
        $name = \htmlspecialchars($name, \ENT_QUOTES | \ENT_HTML5);
264
        $path = "<a href='" . $funcURL . '&amp;' . $this->id . '=' . $sel_id . "'>" . $name . '</a>' . $path . '';
265
        if (0 == $parentid) {
266
            return $path;
267
        }
268
        $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path);
269
270
        return $path;
271
    }
272
273
    //generates id path from the root id to a given id
274
    // the path is delimetered with "/"
275
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 (0 == $this->db->getRowsNum($result)) {
287
            return $path;
288
        }
289
        [$parentid] = $this->db->fetchRow($result);
290
        $path = '/' . $sel_id . $path . '';
291
        if (0 == $parentid) {
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 array|mixed
307
     */
308
    public function getAllChild($sel_id = 0, $order = '', $parray = [])
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
        $count  = $this->db->getRowsNum($result);
317
        if (0 == $count) {
318
            return $parray;
319
        }
320
        while (false !== ($row = $this->db->fetchArray($result))) {
321
            $parray[] = $row;
322
            $parray   = $this->getAllChild($row[$this->id], $order, $parray);
323
        }
324
325
        return $parray;
326
    }
327
328
    /**
329
     * Enter description here...
330
     *
331
     * @param int|mixed    $sel_id
332
     * @param string|mixed $order
333
     * @param array|mixed  $parray
334
     * @param string|mixed $r_prefix
335
     * @return array|mixed
336
     */
337
    public function getChildTreeArray($sel_id = 0, $order = '', $parray = [], $r_prefix = '')
338
    {
339
        $sel_id = (int)$sel_id;
340
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
341
        if ('' != $order) {
342
            $sql .= " ORDER BY $order";
343
        }
344
        $result = $this->db->query($sql);
345
        $count  = $this->db->getRowsNum($result);
346
        if (0 == $count) {
347
            return $parray;
348
        }
349
        while (false !== ($row = $this->db->fetchArray($result))) {
350
            $row['prefix'] = $r_prefix . '.';
351
            $parray[]      = $row;
352
            $parray        = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']);
353
        }
354
355
        return $parray;
356
    }
357
}
358