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); |
|
|
|
|
79
|
|
|
if ($count == 0) { |
80
|
|
|
return $arr; |
81
|
|
|
} |
82
|
|
|
while (false !== ($myrow = $this->db->fetchArray($result))) { |
|
|
|
|
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
|
|
|
$sql = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
100
|
|
|
$result = $this->db->query($sql); |
101
|
|
|
if (!$this->db->isResultSet($result)) { |
102
|
|
|
\trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
103
|
|
|
} |
104
|
|
|
$count = $this->db->getRowsNum($result); |
|
|
|
|
105
|
|
|
if ($count == 0) { |
106
|
|
|
return $idarray; |
107
|
|
|
} |
108
|
|
|
while (false !== (list($id) = $this->db->fetchRow($result))) { |
|
|
|
|
109
|
|
|
$idarray[] = $id; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $idarray; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
//returns an array of ALL child ids for a given id($sel_id) |
116
|
|
|
/** |
117
|
|
|
* @param $sel_id |
118
|
|
|
* @param string $order |
119
|
|
|
* @param array $idarray |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function getAllChildId($sel_id, $order = '', $idarray = array()) |
124
|
|
|
{ |
125
|
|
|
$sel_id = (int)$sel_id; |
126
|
|
|
$sql = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
127
|
|
|
if ($order != '') { |
128
|
|
|
$sql .= " ORDER BY $order"; |
129
|
|
|
} |
130
|
|
|
$result = $this->db->query($sql); |
131
|
|
|
if (!$this->db->isResultSet($result)) { |
132
|
|
|
\trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
133
|
|
|
} |
134
|
|
|
$count = $this->db->getRowsNum($result); |
|
|
|
|
135
|
|
|
if ($count == 0) { |
136
|
|
|
return $idarray; |
137
|
|
|
} |
138
|
|
|
while (false !== (list($r_id) = $this->db->fetchRow($result))) { |
|
|
|
|
139
|
|
|
$idarray[] = $r_id; |
140
|
|
|
$idarray = $this->getAllChildId($r_id, $order, $idarray); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $idarray; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
//returns an array of ALL parent ids for a given id($sel_id) |
147
|
|
|
/** |
148
|
|
|
* @param $sel_id |
149
|
|
|
* @param string $order |
150
|
|
|
* @param array $idarray |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
public function getAllParentId($sel_id, $order = '', $idarray = array()) |
155
|
|
|
{ |
156
|
|
|
$sel_id = (int)$sel_id; |
157
|
|
|
$sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . ''; |
158
|
|
|
if ($order != '') { |
159
|
|
|
$sql .= " ORDER BY $order"; |
160
|
|
|
} |
161
|
|
|
$result = $this->db->query($sql); |
162
|
|
|
if (!$this->db->isResultSet($result)) { |
163
|
|
|
\trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
164
|
|
|
} |
165
|
|
|
list($r_id) = $this->db->fetchRow($result); |
|
|
|
|
166
|
|
|
if ($r_id == 0) { |
167
|
|
|
return $idarray; |
168
|
|
|
} |
169
|
|
|
$idarray[] = $r_id; |
170
|
|
|
$idarray = $this->getAllParentId($r_id, $order, $idarray); |
171
|
|
|
|
172
|
|
|
return $idarray; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
//generates path from the root id to a given id($sel_id) |
176
|
|
|
// the path is delimetered with "/" |
177
|
|
|
/** |
178
|
|
|
* @param $sel_id |
179
|
|
|
* @param $title |
180
|
|
|
* @param string $path |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
public function getPathFromId($sel_id, $title, $path = '') |
185
|
|
|
{ |
186
|
|
|
$sel_id = (int)$sel_id; |
187
|
|
|
$sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"; |
188
|
|
|
$result = $this->db->query($sql); |
189
|
|
|
if (!$this->db->isResultSet($result)) { |
190
|
|
|
\trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
191
|
|
|
} |
192
|
|
|
if ($this->db->getRowsNum($result) == 0) { |
|
|
|
|
193
|
|
|
return $path; |
194
|
|
|
} |
195
|
|
|
list($parentid, $name) = $this->db->fetchRow($result); |
|
|
|
|
196
|
|
|
$myts = MyTextSanitizer::getInstance(); |
197
|
|
|
$name = $myts->htmlSpecialChars($name); |
198
|
|
|
$path = '/' . $name . $path . ''; |
199
|
|
|
if ($parentid == 0) { |
200
|
|
|
return $path; |
201
|
|
|
} |
202
|
|
|
$path = $this->getPathFromId($parentid, $title, $path); |
203
|
|
|
|
204
|
|
|
return $path; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
//makes a nicely ordered selection box |
208
|
|
|
//$preset_id is used to specify a preselected item |
209
|
|
|
//set $none to 1 to add a option with value 0 |
210
|
|
|
/** |
211
|
|
|
* @param $title |
212
|
|
|
* @param string $order |
213
|
|
|
* @param int $preset_id |
214
|
|
|
* @param int $none |
215
|
|
|
* @param string $sel_name |
216
|
|
|
* @param string $onchange |
217
|
|
|
*/ |
218
|
|
|
public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '') |
219
|
|
|
{ |
220
|
|
|
if ($sel_name == '') { |
221
|
|
|
$sel_name = $this->id; |
222
|
|
|
} |
223
|
|
|
$myts = MyTextSanitizer::getInstance(); |
224
|
|
|
echo "<select name='" . $sel_name . "'"; |
225
|
|
|
if ($onchange != '') { |
226
|
|
|
echo " onchange='" . $onchange . "'"; |
227
|
|
|
} |
228
|
|
|
echo ">\n"; |
229
|
|
|
$sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0'; |
230
|
|
|
if ($order != '') { |
231
|
|
|
$sql .= " ORDER BY $order"; |
232
|
|
|
} |
233
|
|
|
$result = $this->db->query($sql); |
234
|
|
|
if (!$this->db->isResultSet($result)) { |
235
|
|
|
\trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
236
|
|
|
} |
237
|
|
|
if ($none) { |
238
|
|
|
echo "<option value='0'>----</option>\n"; |
239
|
|
|
} |
240
|
|
|
while (false !== (list($catid, $name) = $this->db->fetchRow($result))) { |
|
|
|
|
241
|
|
|
$sel = ''; |
242
|
|
|
if ($catid == $preset_id) { |
243
|
|
|
$sel = " selected"; |
244
|
|
|
} |
245
|
|
|
echo "<option value='$catid'$sel>$name</option>\n"; |
246
|
|
|
$sel = ''; |
247
|
|
|
$arr = $this->getChildTreeArray($catid, $order); |
248
|
|
|
foreach ($arr as $option) { |
249
|
|
|
$option['prefix'] = str_replace('.', '--', $option['prefix']); |
250
|
|
|
$catpath = $option['prefix'] . ' ' . $myts->htmlSpecialChars($option[$title]); |
251
|
|
|
if ($option[$this->id] == $preset_id) { |
252
|
|
|
$sel = " selected"; |
253
|
|
|
} |
254
|
|
|
echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n"; |
255
|
|
|
$sel = ''; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
echo "</select>\n"; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
//generates nicely formatted linked path from the root id to a given id |
262
|
|
|
/** |
263
|
|
|
* @param $sel_id |
264
|
|
|
* @param $title |
265
|
|
|
* @param $funcURL |
266
|
|
|
* @param string $path |
267
|
|
|
* |
268
|
|
|
* @return string |
269
|
|
|
*/ |
270
|
|
|
public function getNicePathFromId($sel_id, $title, $funcURL, $path = '') |
271
|
|
|
{ |
272
|
|
|
$path = !empty($path) ? ' : ' . $path : $path; |
273
|
|
|
$sel_id = (int)$sel_id; |
274
|
|
|
$sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"; |
275
|
|
|
$result = $this->db->query($sql); |
276
|
|
|
if (!$this->db->isResultSet($result)) { |
277
|
|
|
\trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
278
|
|
|
} |
279
|
|
|
if ($this->db->getRowsNum($result) == 0) { |
|
|
|
|
280
|
|
|
return $path; |
281
|
|
|
} |
282
|
|
|
list($parentid, $name) = $this->db->fetchRow($result); |
|
|
|
|
283
|
|
|
$myts = MyTextSanitizer::getInstance(); |
284
|
|
|
$name = $myts->htmlSpecialChars($name); |
285
|
|
|
$path = "<a href='" . $funcURL . '&' . $this->id . '=' . $sel_id . "'>" . $name . '</a>' . $path . ''; |
286
|
|
|
if ($parentid == 0) { |
287
|
|
|
return $path; |
288
|
|
|
} |
289
|
|
|
$path = $this->getNicePathFromId($parentid, $title, $funcURL, $path); |
290
|
|
|
|
291
|
|
|
return $path; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
//generates id path from the root id to a given id |
295
|
|
|
// the path is delimetered with "/" |
296
|
|
|
/** |
297
|
|
|
* @param $sel_id |
298
|
|
|
* @param string $path |
299
|
|
|
* |
300
|
|
|
* @return string |
301
|
|
|
*/ |
302
|
|
|
public function getIdPathFromId($sel_id, $path = '') |
303
|
|
|
{ |
304
|
|
|
$sel_id = (int)$sel_id; |
305
|
|
|
$sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"; |
306
|
|
|
$result = $this->db->query($sql); |
307
|
|
|
if (!$this->db->isResultSet($result)) { |
308
|
|
|
\trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
309
|
|
|
} |
310
|
|
|
if ($this->db->getRowsNum($result) == 0) { |
|
|
|
|
311
|
|
|
return $path; |
312
|
|
|
} |
313
|
|
|
list($parentid) = $this->db->fetchRow($result); |
|
|
|
|
314
|
|
|
$path = '/' . $sel_id . $path . ''; |
315
|
|
|
if ($parentid == 0) { |
316
|
|
|
return $path; |
317
|
|
|
} |
318
|
|
|
$path = $this->getIdPathFromId($parentid, $path); |
319
|
|
|
|
320
|
|
|
return $path; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Enter description here... |
325
|
|
|
* |
326
|
|
|
* @param int|mixed $sel_id |
327
|
|
|
* @param string|mixed $order |
328
|
|
|
* @param array|mixed $parray |
329
|
|
|
* |
330
|
|
|
* @return mixed |
331
|
|
|
*/ |
332
|
|
|
public function getAllChild($sel_id = 0, $order = '', $parray = array()) |
333
|
|
|
{ |
334
|
|
|
$sel_id = (int)$sel_id; |
335
|
|
|
$sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
336
|
|
|
if ($order != '') { |
337
|
|
|
$sql .= " ORDER BY $order"; |
338
|
|
|
} |
339
|
|
|
$result = $this->db->query($sql); |
340
|
|
|
if (!$this->db->isResultSet($result)) { |
341
|
|
|
\trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
342
|
|
|
} |
343
|
|
|
$count = $this->db->getRowsNum($result); |
|
|
|
|
344
|
|
|
if ($count == 0) { |
345
|
|
|
return $parray; |
346
|
|
|
} |
347
|
|
|
while (false !== ($row = $this->db->fetchArray($result))) { |
|
|
|
|
348
|
|
|
$parray[] = $row; |
349
|
|
|
$parray = $this->getAllChild($row[$this->id], $order, $parray); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
return $parray; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Enter description here... |
357
|
|
|
* |
358
|
|
|
* @param int|mixed $sel_id |
359
|
|
|
* @param string|mixed $order |
360
|
|
|
* @param array|mixed $parray |
361
|
|
|
* @param string|mixed $r_prefix |
362
|
|
|
* @return mixed |
363
|
|
|
*/ |
364
|
|
|
public function getChildTreeArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '') |
365
|
|
|
{ |
366
|
|
|
$sel_id = (int)$sel_id; |
367
|
|
|
$sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
368
|
|
|
if ($order != '') { |
369
|
|
|
$sql .= " ORDER BY $order"; |
370
|
|
|
} |
371
|
|
|
$result = $this->db->query($sql); |
372
|
|
|
if (!$this->db->isResultSet($result)) { |
373
|
|
|
\trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR); |
374
|
|
|
} |
375
|
|
|
$count = $this->db->getRowsNum($result); |
|
|
|
|
376
|
|
|
if ($count == 0) { |
377
|
|
|
return $parray; |
378
|
|
|
} |
379
|
|
|
while (false !== ($row = $this->db->fetchArray($result))) { |
|
|
|
|
380
|
|
|
$row['prefix'] = $r_prefix . '.'; |
381
|
|
|
$parray[] = $row; |
382
|
|
|
$parray = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
return $parray; |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
|