1
|
|
|
<?php |
2
|
|
|
// |
3
|
|
|
// ------------------------------------------------------------------------ // |
4
|
|
|
// XOOPS - PHP Content Management System // |
5
|
|
|
// Copyright (c) 2000-2016 XOOPS.org // |
6
|
|
|
// <http://xoops.org/> // |
7
|
|
|
// ------------------------------------------------------------------------ // |
8
|
|
|
// This program is free software; you can redistribute it and/or modify // |
9
|
|
|
// it under the terms of the GNU General Public License as published by // |
10
|
|
|
// the Free Software Foundation; either version 2 of the License, or // |
11
|
|
|
// (at your option) any later version. // |
12
|
|
|
// // |
13
|
|
|
// You may not change or alter any portion of this comment or credits // |
14
|
|
|
// of supporting developers from this source code or any supporting // |
15
|
|
|
// source code which is considered copyrighted (c) material of the // |
16
|
|
|
// original comment or credit authors. // |
17
|
|
|
// // |
18
|
|
|
// This program is distributed in the hope that it will be useful, // |
19
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
20
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
21
|
|
|
// GNU General Public License for more details. // |
22
|
|
|
// // |
23
|
|
|
// You should have received a copy of the GNU General Public License // |
24
|
|
|
// along with this program; if not, write to the Free Software // |
25
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // |
26
|
|
|
// ------------------------------------------------------------------------ // |
27
|
|
|
// Author: Kazumi Ono (AKA onokazu) // |
28
|
|
|
// URL: http://www.myweb.ne.jp/, http://xoops.org/, http://jp.xoops.org/ // |
29
|
|
|
// Project: XOOPS Project // |
30
|
|
|
// ------------------------------------------------------------------------- // |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class SmartTree |
34
|
|
|
*/ |
35
|
|
|
class smarttree |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
public $table; //table with parent-child structure |
38
|
|
|
public $id; //name of unique id for records in table $table |
39
|
|
|
public $pid; // name of parent id used in table $table |
40
|
|
|
public $order; //specifies the order of query results |
41
|
|
|
public $title; // name of a field in table $table which will be used when selection box and paths are generated |
42
|
|
|
public $db; |
43
|
|
|
|
44
|
|
|
//constructor of class SmartTree |
45
|
|
|
//sets the names of table, unique id, and parend id |
46
|
|
|
/** |
47
|
|
|
* SmartTree constructor. |
48
|
|
|
* @param $table_name |
49
|
|
|
* @param $id_name |
50
|
|
|
* @param $pid_name |
51
|
|
|
*/ |
52
|
|
|
public function __construct($table_name, $id_name, $pid_name) |
53
|
|
|
{ |
54
|
|
|
$this->db = XoopsDatabaseFactory::getDatabaseConnection(); |
55
|
|
|
$this->table = $table_name; |
56
|
|
|
$this->id = $id_name; |
57
|
|
|
$this->pid = $pid_name; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// returns an array of first child objects for a given id($sel_id) |
61
|
|
|
/** |
62
|
|
|
* @param $sel_id |
63
|
|
|
* @param string $order |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public function getFirstChild($sel_id, $order = '') |
67
|
|
|
{ |
68
|
|
|
$arr = array(); |
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
|
|
|
$count = $this->db->getRowsNum($result); |
75
|
|
|
if ($count == 0) { |
76
|
|
|
return $arr; |
77
|
|
|
} |
78
|
|
|
while ($myrow = $this->db->fetchArray($result)) { |
79
|
|
|
array_push($arr, $myrow); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $arr; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// returns an array of all FIRST child ids of a given id($sel_id) |
86
|
|
|
/** |
87
|
|
|
* @param $sel_id |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function getFirstChildId($sel_id) |
91
|
|
|
{ |
92
|
|
|
$idarray = array(); |
93
|
|
|
$result = $this->db->query('SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''); |
94
|
|
|
$count = $this->db->getRowsNum($result); |
95
|
|
|
if ($count == 0) { |
96
|
|
|
return $idarray; |
97
|
|
|
} |
98
|
|
|
while (list($id) = $this->db->fetchRow($result)) { |
99
|
|
|
array_push($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
|
|
|
* @param $sel_id |
108
|
|
|
* @param string $order |
109
|
|
|
* @param array $idarray |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
View Code Duplication |
public function getAllChildId($sel_id, $order = '', $idarray = array()) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$sql = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
115
|
|
|
if ($order != '') { |
116
|
|
|
$sql .= " ORDER BY $order"; |
117
|
|
|
} |
118
|
|
|
$result = $this->db->query($sql); |
119
|
|
|
$count = $this->db->getRowsNum($result); |
120
|
|
|
if ($count == 0) { |
121
|
|
|
return $idarray; |
122
|
|
|
} |
123
|
|
|
while (list($r_id) = $this->db->fetchRow($result)) { |
124
|
|
|
array_push($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($sel_id) |
132
|
|
|
/** |
133
|
|
|
* @param $sel_id |
134
|
|
|
* @param string $order |
135
|
|
|
* @param array $idarray |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function getAllParentId($sel_id, $order = '', $idarray = array()) |
139
|
|
|
{ |
140
|
|
|
$sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . ''; |
141
|
|
|
if ($order != '') { |
142
|
|
|
$sql .= " ORDER BY $order"; |
143
|
|
|
} |
144
|
|
|
$result = $this->db->query($sql); |
145
|
|
|
list($r_id) = $this->db->fetchRow($result); |
146
|
|
|
if ($r_id == 0) { |
147
|
|
|
return $idarray; |
148
|
|
|
} |
149
|
|
|
array_push($idarray, $r_id); |
150
|
|
|
$idarray = $this->getAllParentId($r_id, $order, $idarray); |
151
|
|
|
|
152
|
|
|
return $idarray; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
//generates path from the root id to a given id($sel_id) |
156
|
|
|
// the path is delimetered with "/" |
157
|
|
|
/** |
158
|
|
|
* @param $sel_id |
159
|
|
|
* @param $title |
160
|
|
|
* @param string $path |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function getPathFromId($sel_id, $title, $path = '') |
164
|
|
|
{ |
165
|
|
|
$result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"); |
166
|
|
|
if ($this->db->getRowsNum($result) == 0) { |
167
|
|
|
return $path; |
168
|
|
|
} |
169
|
|
|
list($parentid, $name) = $this->db->fetchRow($result); |
170
|
|
|
$myts = MyTextSanitizer::getInstance(); |
171
|
|
|
$name = $myts->htmlSpecialChars($name); |
172
|
|
|
$path = '/' . $name . $path . ''; |
173
|
|
|
if ($parentid == 0) { |
174
|
|
|
return $path; |
175
|
|
|
} |
176
|
|
|
$path = $this->getPathFromId($parentid, $title, $path); |
177
|
|
|
|
178
|
|
|
return $path; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
//makes a nicely ordered selection box |
182
|
|
|
//$preset_id is used to specify a preselected item |
183
|
|
|
//set $none to 1 to add a option with value 0 |
184
|
|
|
/** |
185
|
|
|
* @param $title |
186
|
|
|
* @param string $order |
187
|
|
|
* @param int $preset_id |
188
|
|
|
* @param int $none |
189
|
|
|
* @param string $sel_name |
190
|
|
|
* @param string $onchange |
191
|
|
|
* @param bool $multiple |
192
|
|
|
*/ |
193
|
|
|
public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '', $multiple = false) |
194
|
|
|
{ |
195
|
|
|
global $myts; |
|
|
|
|
196
|
|
|
if ($sel_name == '') { |
197
|
|
|
$sel_name = $this->id; |
198
|
|
|
} |
199
|
|
|
$myts = MyTextSanitizer::getInstance(); |
200
|
|
|
/* |
201
|
|
|
* Hack by felix<INBOX> for ampersand |
202
|
|
|
* allow multiple select |
203
|
|
|
*/ |
204
|
|
|
//echo "<select name='".$sel_name."'"; |
|
|
|
|
205
|
|
|
if ($multiple) { |
206
|
|
|
echo "<select name='" . $sel_name . "[]' multiple='multiple'"; |
207
|
|
|
} else { |
208
|
|
|
echo "<select name='" . $sel_name . "'"; |
209
|
|
|
} |
210
|
|
|
/* |
211
|
|
|
* End of Hack by felix<INBOX> for ampersand |
212
|
|
|
* allow multiple select |
213
|
|
|
*/ |
214
|
|
|
if ($onchange != '') { |
215
|
|
|
echo " onchange='" . $onchange . "'"; |
216
|
|
|
} |
217
|
|
|
echo ">\n"; |
218
|
|
|
$sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0'; |
219
|
|
|
if ($order != '') { |
220
|
|
|
$sql .= " ORDER BY $order"; |
221
|
|
|
} |
222
|
|
|
$result = $this->db->query($sql); |
223
|
|
|
if ($none) { |
224
|
|
|
echo "<option value='0'>----</option>\n"; |
225
|
|
|
} |
226
|
|
|
while (list($catid, $name) = $this->db->fetchRow($result)) { |
227
|
|
|
$sel = ''; |
228
|
|
|
if ($catid == $preset_id) { |
229
|
|
|
$sel = " selected='selected'"; |
230
|
|
|
} |
231
|
|
|
/* |
232
|
|
|
* Hack by felix<INBOX> for ampersand |
233
|
|
|
* allow multiple select |
234
|
|
|
*/ |
235
|
|
|
if (is_array($preset_id) && in_array($catid, $preset_id)) { |
236
|
|
|
$sel = " selected='selected'"; |
237
|
|
|
} |
238
|
|
|
/* |
239
|
|
|
* End of Hack by felix<INBOX> for ampersand |
240
|
|
|
* allow multiple select |
241
|
|
|
*/ |
242
|
|
|
// MT hack added by hsalazar // |
243
|
|
|
//$name = $myts->formatForML($name); |
|
|
|
|
244
|
|
|
// MT hack added by hsalazar // |
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='selected'"; |
253
|
|
|
} |
254
|
|
|
/* |
255
|
|
|
* Hack by felix<INBOX> for ampersand |
256
|
|
|
* allow multiple select |
257
|
|
|
*/ |
258
|
|
|
if (is_array($preset_id) && in_array($option[$this->id], $preset_id)) { |
259
|
|
|
$sel = " selected='selected'"; |
260
|
|
|
} |
261
|
|
|
/* |
262
|
|
|
* End of Hack by felix<INBOX> for ampersand |
263
|
|
|
* allow multiple select |
264
|
|
|
*/ |
265
|
|
|
|
266
|
|
|
echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n"; |
267
|
|
|
$sel = ''; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
echo "</select>\n"; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
//generates nicely formatted linked path from the root id to a given id |
274
|
|
|
/** |
275
|
|
|
* @param $sel_id |
276
|
|
|
* @param $title |
277
|
|
|
* @param $funcURL |
278
|
|
|
* @param string $path |
279
|
|
|
* @return string |
280
|
|
|
*/ |
281
|
|
|
public function getNicePathFromId($sel_id, $title, $funcURL, $path = '') |
282
|
|
|
{ |
283
|
|
|
$sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"; |
284
|
|
|
$result = $this->db->query($sql); |
285
|
|
|
if ($this->db->getRowsNum($result) == 0) { |
286
|
|
|
return $path; |
287
|
|
|
} |
288
|
|
|
list($parentid, $name) = $this->db->fetchRow($result); |
289
|
|
|
$myts = MyTextSanitizer::getInstance(); |
290
|
|
|
$name = $myts->htmlSpecialChars($name); |
291
|
|
|
$path = "<a href='" . $funcURL . '&' . $this->id . '=' . $sel_id . "'>" . $name . '</a> : ' . $path . ''; |
292
|
|
|
if ($parentid == 0) { |
293
|
|
|
return $path; |
294
|
|
|
} |
295
|
|
|
$path = $this->getNicePathFromId($parentid, $title, $funcURL, $path); |
296
|
|
|
|
297
|
|
|
return $path; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
//generates id path from the root id to a given id |
301
|
|
|
// the path is delimetered with "/" |
302
|
|
|
/** |
303
|
|
|
* @param $sel_id |
304
|
|
|
* @param string $path |
305
|
|
|
* @return string |
306
|
|
|
*/ |
307
|
|
|
public function getIdPathFromId($sel_id, $path = '') |
308
|
|
|
{ |
309
|
|
|
$result = $this->db->query('SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id"); |
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
|
|
|
* @param int $sel_id |
325
|
|
|
* @param string $order |
326
|
|
|
* @param array $parray |
327
|
|
|
* @return array |
328
|
|
|
*/ |
329
|
|
View Code Duplication |
public function getAllChild($sel_id = 0, $order = '', $parray = array()) |
|
|
|
|
330
|
|
|
{ |
331
|
|
|
$sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
332
|
|
|
if ($order != '') { |
333
|
|
|
$sql .= " ORDER BY $order"; |
334
|
|
|
} |
335
|
|
|
$result = $this->db->query($sql); |
336
|
|
|
$count = $this->db->getRowsNum($result); |
337
|
|
|
if ($count == 0) { |
338
|
|
|
return $parray; |
339
|
|
|
} |
340
|
|
|
while ($row = $this->db->fetchArray($result)) { |
341
|
|
|
array_push($parray, $row); |
342
|
|
|
$parray = $this->getAllChild($row[$this->id], $order, $parray); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
return $parray; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* @param int $sel_id |
350
|
|
|
* @param string $order |
351
|
|
|
* @param array $parray |
352
|
|
|
* @param string $r_prefix |
353
|
|
|
* @return array |
354
|
|
|
*/ |
355
|
|
|
public function getChildTreeArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '') |
356
|
|
|
{ |
357
|
|
|
$sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ''; |
358
|
|
|
if ($order != '') { |
359
|
|
|
$sql .= " ORDER BY $order"; |
360
|
|
|
} |
361
|
|
|
$result = $this->db->query($sql); |
362
|
|
|
$count = $this->db->getRowsNum($result); |
363
|
|
|
if ($count == 0) { |
364
|
|
|
return $parray; |
365
|
|
|
} |
366
|
|
|
while ($row = $this->db->fetchArray($result)) { |
367
|
|
|
$row['prefix'] = $r_prefix . '.'; |
368
|
|
|
array_push($parray, $row); |
369
|
|
|
$parray = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
return $parray; |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.