1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Smartfaq; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Module: XoopsTube |
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
|
|
|
* |
12
|
|
|
* |
13
|
|
|
* @category Module |
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.0 or later (https://www.gnu.org/licenses/gpl-2.0.html) |
18
|
|
|
* @link https://xoops.org/ |
19
|
|
|
* @since 1.0.6 |
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
|
|
|
*/ |
29
|
|
|
class Tree |
30
|
|
|
{ |
31
|
|
|
public $table; //table with parent-child structure |
32
|
|
|
public $id; //name of unique id for records in table $table |
33
|
|
|
public $pid; // name of parent id used in table $table |
34
|
|
|
public $order; //specifies the order of query results |
35
|
|
|
public $title; // name of a field in table $table which will be used when selection box and paths are generated |
36
|
|
|
public $db; |
37
|
|
|
//constructor of class XoopsTree |
38
|
|
|
//sets the names of table, unique id, and parend id |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param $tableName |
42
|
|
|
* @param $idName |
43
|
|
|
* @param $pidName |
44
|
|
|
*/ |
45
|
|
|
public function __construct($tableName, $idName, $pidName) |
46
|
|
|
{ |
47
|
|
|
$this->db = \XoopsDatabaseFactory::getDatabaseConnection(); |
48
|
|
|
$this->table = $tableName; |
49
|
|
|
$this->id = $idName; |
50
|
|
|
$this->pid = $pidName; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// returns an array of first child objects for a given id($selectId) |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $selectId |
57
|
|
|
* @param string $order |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function getFirstChild($selectId, $order = '') |
62
|
|
|
{ |
63
|
|
|
$selectId = (int)$selectId; |
64
|
|
|
$arr = []; |
65
|
|
|
$sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $selectId . ' '; |
66
|
|
|
if ('' !== $order) { |
67
|
|
|
$sql .= " ORDER BY $order"; |
68
|
|
|
} |
69
|
|
|
$result = $this->db->query($sql); |
70
|
|
|
if ($this->db->isResultSet($result)) { |
71
|
|
|
while (false !== ($myrow = $this->db->fetchArray($result))) { |
72
|
|
|
$arr[] = $myrow; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $arr; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// returns an array of all FIRST child ids of a given id($selectId) |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param $selectId |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function getFirstChildId($selectId) |
87
|
|
|
{ |
88
|
|
|
$selectId = (int)$selectId; |
89
|
|
|
$idarray = []; |
90
|
|
|
$result = $this->db->query('SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $selectId); |
91
|
|
|
$count = $this->db->getRowsNum($result); |
92
|
|
|
if (0 == $count) { |
93
|
|
|
return $idarray; |
94
|
|
|
} |
95
|
|
|
while ([$id] = $this->db->fetchRow($result)) { |
96
|
|
|
$idarray[] = $id; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $idarray; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
//returns an array of ALL child ids for a given id($selectId) |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $selectId |
106
|
|
|
* @param string $order |
107
|
|
|
* |
108
|
|
|
* @param array $idarray |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function getAllChildId($selectId, $order = '', array $idarray = []) |
112
|
|
|
{ |
113
|
|
|
$selectId = (int)$selectId; |
114
|
|
|
$sql = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $selectId; |
115
|
|
|
if ('' !== $order) { |
116
|
|
|
$sql .= " ORDER BY $order"; |
117
|
|
|
} |
118
|
|
|
$result = $this->db->query($sql); |
119
|
|
|
$count = $this->db->getRowsNum($result); |
120
|
|
|
if (0 == $count) { |
121
|
|
|
return $idarray; |
122
|
|
|
} |
123
|
|
|
while ([$r_id] = $this->db->fetchRow($result)) { |
124
|
|
|
$idarray[] = $r_id; |
125
|
|
|
$idarray = $this->getAllChildId($r_id, $order, $idarray); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $idarray; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
//returns an array of ALL parent ids for a given id($selectId) |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param $selectId |
135
|
|
|
* @param string $order |
136
|
|
|
* |
137
|
|
|
* @param array $idarray |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
public function getAllParentId($selectId, $order = '', array $idarray = []) |
141
|
|
|
{ |
142
|
|
|
$selectId = (int)$selectId; |
143
|
|
|
$sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $selectId; |
144
|
|
|
if ('' !== $order) { |
145
|
|
|
$sql .= " ORDER BY $order"; |
146
|
|
|
} |
147
|
|
|
$result = $this->db->query($sql); |
148
|
|
|
[$r_id] = $this->db->fetchRow($result); |
149
|
|
|
if (0 == $r_id) { |
150
|
|
|
return $idarray; |
151
|
|
|
} |
152
|
|
|
$idarray[] = $r_id; |
153
|
|
|
$idarray = $this->getAllParentId($r_id, $order, $idarray); |
154
|
|
|
|
155
|
|
|
return $idarray; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
//generates path from the root id to a given id($selectId) |
159
|
|
|
// the path is delimetered with "/" |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param $selectId |
163
|
|
|
* @param $title |
164
|
|
|
* @param string $path |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function getPathFromId($selectId, $title, $path = '') |
169
|
|
|
{ |
170
|
|
|
$selectId = (int)$selectId; |
171
|
|
|
$result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$selectId"); |
172
|
|
|
if (0 == $this->db->getRowsNum($result)) { |
173
|
|
|
return $path; |
174
|
|
|
} |
175
|
|
|
[$parentid, $name] = $this->db->fetchRow($result); |
176
|
|
|
$myts = \MyTextSanitizer::getInstance(); |
|
|
|
|
177
|
|
|
$name = \htmlspecialchars($name, ENT_QUOTES | ENT_HTML5); |
178
|
|
|
$path = '/' . $name . $path; |
179
|
|
|
if (0 == $parentid) { |
180
|
|
|
return $path; |
181
|
|
|
} |
182
|
|
|
$path = $this->getPathFromId($parentid, $title, $path); |
183
|
|
|
|
184
|
|
|
return $path; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
//makes a nicely ordered selection box |
188
|
|
|
//$preset_id is used to specify a preselected item |
189
|
|
|
//set $none to 1 to add an option with value 0 |
190
|
|
|
|
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 = ''): void |
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 ([$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'] . ' ' . \htmlspecialchars($option[$title], ENT_QUOTES | ENT_HTML5); |
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
|
|
|
/** |
242
|
|
|
* @param $selectId |
243
|
|
|
* @param $title |
244
|
|
|
* @param $funcURL |
245
|
|
|
* @param string $path |
246
|
|
|
* |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
public function getNicePathFromId($selectId, $title, $funcURL, $path = '') |
250
|
|
|
{ |
251
|
|
|
$path = !empty($path) ? $path : ''; |
252
|
|
|
$selectId = (int)$selectId; |
253
|
|
|
$sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$selectId"; |
254
|
|
|
$result = $this->db->query($sql); |
255
|
|
|
if (0 == $this->db->getRowsNum($result)) { |
256
|
|
|
return $path; |
257
|
|
|
} |
258
|
|
|
[$parentid, $name] = $this->db->fetchRow($result); |
259
|
|
|
$myts = \MyTextSanitizer::getInstance(); |
|
|
|
|
260
|
|
|
$name = \htmlspecialchars($name, ENT_QUOTES | ENT_HTML5); |
261
|
|
|
$path = "<li><a href='" . $funcURL . '&' . $this->id . '=' . $selectId . "'>" . $name . '</a></li>' . $path; |
262
|
|
|
if (0 == $parentid) { |
263
|
|
|
return $path; |
264
|
|
|
} |
265
|
|
|
$path = $this->getNicePathFromId($parentid, $title, $funcURL, $path); |
266
|
|
|
|
267
|
|
|
return $path; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
//generates id path from the root id to a given id |
271
|
|
|
// the path is delimetered with "/" |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param $selectId |
275
|
|
|
* @param string $path |
276
|
|
|
* |
277
|
|
|
* @return string |
278
|
|
|
*/ |
279
|
|
|
public function getIdPathFromId($selectId, $path = '') |
280
|
|
|
{ |
281
|
|
|
$selectId = (int)$selectId; |
282
|
|
|
$result = $this->db->query('SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$selectId"); |
283
|
|
|
if (0 == $this->db->getRowsNum($result)) { |
284
|
|
|
return $path; |
285
|
|
|
} |
286
|
|
|
[$parentid] = $this->db->fetchRow($result); |
287
|
|
|
$path = '/' . $selectId . $path; |
288
|
|
|
if (0 == $parentid) { |
289
|
|
|
return $path; |
290
|
|
|
} |
291
|
|
|
$path = $this->getIdPathFromId($parentid, $path); |
292
|
|
|
|
293
|
|
|
return $path; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Enter description here... |
298
|
|
|
* |
299
|
|
|
* @param int $selectId |
300
|
|
|
* @param string $order |
301
|
|
|
* |
302
|
|
|
* @param array $parray |
303
|
|
|
* @return array |
304
|
|
|
*/ |
305
|
|
|
public function getAllChild($selectId = 0, $order = '', array $parray = []) |
306
|
|
|
{ |
307
|
|
|
$selectId = (int)$selectId; |
308
|
|
|
$sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $selectId . ' '; |
309
|
|
|
if ('' !== $order) { |
310
|
|
|
$sql .= " ORDER BY $order"; |
311
|
|
|
} |
312
|
|
|
$result = $this->db->query($sql); |
313
|
|
|
$count = $this->db->getRowsNum($result); |
314
|
|
|
if (0 == $count) { |
315
|
|
|
return $parray; |
316
|
|
|
} |
317
|
|
|
while (false !== ($row = $this->db->fetchArray($result))) { |
318
|
|
|
$parray[] = $row; |
319
|
|
|
$parray = $this->getAllChild($row[$this->id], $order, $parray); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
return $parray; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Enter description here... |
327
|
|
|
* |
328
|
|
|
* @param int $selectId |
329
|
|
|
* @param string $order |
330
|
|
|
* @param array $parray |
331
|
|
|
* @param string $r_prefix |
332
|
|
|
* |
333
|
|
|
* @return array |
334
|
|
|
*/ |
335
|
|
|
public function getChildTreeArray($selectId = 0, $order = '', array $parray = [], $r_prefix = '') |
336
|
|
|
{ |
337
|
|
|
$selectId = (int)$selectId; |
338
|
|
|
$sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $selectId . ' '; |
339
|
|
|
if ('' !== $order) { |
340
|
|
|
$sql .= " ORDER BY $order"; |
341
|
|
|
} |
342
|
|
|
$result = $this->db->query($sql); |
343
|
|
|
$count = $this->db->getRowsNum($result); |
344
|
|
|
if (0 == $count) { |
345
|
|
|
return $parray; |
346
|
|
|
} |
347
|
|
|
while (false !== ($row = $this->db->fetchArray($result))) { |
348
|
|
|
$row['prefix'] = $r_prefix . '.'; |
349
|
|
|
$parray[] = $row; |
350
|
|
|
$parray = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
return $parray; |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|