Completed
Push — master ( 454ebd...de22fe )
by Michael
03:58
created

Tree::getAllParentId()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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