1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: |
|
18: |
|
19: | defined('XOOPS_ROOT_PATH') || exit('Restricted access');
|
20: |
|
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: |
|
31: | class XoopsTree
|
32: | {
|
33: | public $table;
|
34: | public $id;
|
35: | public $pid;
|
36: | public $order;
|
37: | public $title;
|
38: | |
39: | |
40: |
|
41: | public $db;
|
42: |
|
43: |
|
44: |
|
45: | |
46: | |
47: | |
48: | |
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: |
|
60: | |
61: | |
62: | |
63: | |
64: | |
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: | throw new \RuntimeException(
|
77: | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR
|
78: | );
|
79: | }
|
80: | $count = $this->db->getRowsNum($result);
|
81: | if ($count == 0) {
|
82: | return $arr;
|
83: | }
|
84: | while (false !== ($myrow = $this->db->fetchArray($result))) {
|
85: | $arr[] = $myrow;
|
86: | }
|
87: |
|
88: | return $arr;
|
89: | }
|
90: |
|
91: |
|
92: | |
93: | |
94: | |
95: | |
96: |
|
97: | public function getFirstChildId($sel_id)
|
98: | {
|
99: | $sel_id = (int)$sel_id;
|
100: | $idarray = array();
|
101: | $sql = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
|
102: | $result = $this->db->query($sql);
|
103: | if (!$this->db->isResultSet($result)) {
|
104: | throw new \RuntimeException(
|
105: | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR
|
106: | );
|
107: | }
|
108: | $count = $this->db->getRowsNum($result);
|
109: | if ($count == 0) {
|
110: | return $idarray;
|
111: | }
|
112: | while (false !== (list($id) = $this->db->fetchRow($result))) {
|
113: | $idarray[] = $id;
|
114: | }
|
115: |
|
116: | return $idarray;
|
117: | }
|
118: |
|
119: |
|
120: | |
121: | |
122: | |
123: | |
124: | |
125: | |
126: |
|
127: | public function getAllChildId($sel_id, $order = '', $idarray = array())
|
128: | {
|
129: | $sel_id = (int)$sel_id;
|
130: | $sql = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
|
131: | if ($order != '') {
|
132: | $sql .= " ORDER BY $order";
|
133: | }
|
134: | $result = $this->db->query($sql);
|
135: | if (!$this->db->isResultSet($result)) {
|
136: | throw new \RuntimeException(
|
137: | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR
|
138: | );
|
139: | }
|
140: | $count = $this->db->getRowsNum($result);
|
141: | if ($count == 0) {
|
142: | return $idarray;
|
143: | }
|
144: | while (false !== (list($r_id) = $this->db->fetchRow($result))) {
|
145: | $idarray[] = $r_id;
|
146: | $idarray = $this->getAllChildId($r_id, $order, $idarray);
|
147: | }
|
148: |
|
149: | return $idarray;
|
150: | }
|
151: |
|
152: |
|
153: |
|
154: | |
155: | |
156: | |
157: | |
158: | |
159: | |
160: |
|
161: | public function getAllParentId($sel_id, $order = '', $idarray = array())
|
162: | {
|
163: | $sel_id = (int)$sel_id;
|
164: | $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . '';
|
165: | if ($order != '') {
|
166: | $sql .= " ORDER BY $order";
|
167: | }
|
168: | $result = $this->db->query($sql);
|
169: | if (!$this->db->isResultSet($result)) {
|
170: | throw new \RuntimeException(
|
171: | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR
|
172: | );
|
173: | }
|
174: | list($r_id) = $this->db->fetchRow($result);
|
175: | $r_id = (int)$r_id;
|
176: | if ($r_id === 0) {
|
177: | return $idarray;
|
178: | }
|
179: | $idarray[] = $r_id;
|
180: | $idarray = $this->getAllParentId($r_id, $order, $idarray);
|
181: |
|
182: | return $idarray;
|
183: | }
|
184: |
|
185: |
|
186: |
|
187: | |
188: | |
189: | |
190: | |
191: | |
192: | |
193: |
|
194: | public function getPathFromId($sel_id, $title, $path = '')
|
195: | {
|
196: | $sel_id = (int)$sel_id;
|
197: | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id";
|
198: | $result = $this->db->query($sql);
|
199: | if (!$this->db->isResultSet($result)) {
|
200: | throw new \RuntimeException(
|
201: | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR
|
202: | );
|
203: | }
|
204: | if ($this->db->getRowsNum($result) == 0) {
|
205: | return $path;
|
206: | }
|
207: | list($parentid, $name) = $this->db->fetchRow($result);
|
208: | $myts = \MyTextSanitizer::getInstance();
|
209: | $parentid = (int)$parentid;
|
210: | $name = $myts->htmlSpecialChars($name);
|
211: | $path = '/' . $name . $path . '';
|
212: | if ($parentid === 0) {
|
213: | return $path;
|
214: | }
|
215: | $path = $this->getPathFromId($parentid, $title, $path);
|
216: |
|
217: | return $path;
|
218: | }
|
219: |
|
220: |
|
221: |
|
222: |
|
223: | |
224: | |
225: | |
226: | |
227: | |
228: | |
229: | |
230: |
|
231: | public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '')
|
232: | {
|
233: | if ($sel_name == '') {
|
234: | $sel_name = $this->id;
|
235: | }
|
236: | $myts = \MyTextSanitizer::getInstance();
|
237: | echo "<select name='" . $sel_name . "'";
|
238: | if ($onchange != '') {
|
239: | echo " onchange='" . $onchange . "'";
|
240: | }
|
241: | echo ">\n";
|
242: | $sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0';
|
243: | if ($order != '') {
|
244: | $sql .= " ORDER BY $order";
|
245: | }
|
246: | $result = $this->db->query($sql);
|
247: | if (!$this->db->isResultSet($result)) {
|
248: | throw new \RuntimeException(
|
249: | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR
|
250: | );
|
251: | }
|
252: | if ($none) {
|
253: | echo "<option value='0'>----</option>\n";
|
254: | }
|
255: | while (false !== (list($catid, $name) = $this->db->fetchRow($result))) {
|
256: | $sel = '';
|
257: | if ($catid == $preset_id) {
|
258: | $sel = " selected";
|
259: | }
|
260: | echo "<option value='$catid'$sel>$name</option>\n";
|
261: | $sel = '';
|
262: | $arr = $this->getChildTreeArray($catid, $order);
|
263: | foreach ($arr as $option) {
|
264: | $option['prefix'] = str_replace('.', '--', $option['prefix']);
|
265: | $catpath = $option['prefix'] . ' ' . $myts->htmlSpecialChars($option[$title]);
|
266: | if ($option[$this->id] == $preset_id) {
|
267: | $sel = " selected";
|
268: | }
|
269: | echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n";
|
270: | $sel = '';
|
271: | }
|
272: | }
|
273: | echo "</select>\n";
|
274: | }
|
275: |
|
276: |
|
277: | |
278: | |
279: | |
280: | |
281: | |
282: | |
283: | |
284: |
|
285: | public function getNicePathFromId($sel_id, $title, $funcURL, $path = '')
|
286: | {
|
287: | $path = !empty($path) ? ' : ' . $path : $path;
|
288: | $sel_id = (int)$sel_id;
|
289: | $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id";
|
290: | $result = $this->db->query($sql);
|
291: | if (!$this->db->isResultSet($result)) {
|
292: | throw new \RuntimeException(
|
293: | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR
|
294: | );
|
295: | }
|
296: | if ($this->db->getRowsNum($result) == 0) {
|
297: | return $path;
|
298: | }
|
299: | list($parentid, $name) = $this->db->fetchRow($result);
|
300: | $myts = \MyTextSanitizer::getInstance();
|
301: | $name = $myts->htmlSpecialChars($name);
|
302: | $parentid = (int)$parentid;
|
303: | $path = "<a href='" . $funcURL . '&' . $this->id . '=' . $sel_id . "'>" . $name . '</a>' . $path . '';
|
304: | if ($parentid === 0) {
|
305: | return $path;
|
306: | }
|
307: | $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path);
|
308: |
|
309: | return $path;
|
310: | }
|
311: |
|
312: |
|
313: |
|
314: | |
315: | |
316: | |
317: | |
318: | |
319: |
|
320: | public function getIdPathFromId($sel_id, $path = '')
|
321: | {
|
322: | $sel_id = (int)$sel_id;
|
323: | $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id";
|
324: | $result = $this->db->query($sql);
|
325: | if (!$this->db->isResultSet($result)) {
|
326: | throw new \RuntimeException(
|
327: | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR
|
328: | );
|
329: | }
|
330: | if ($this->db->getRowsNum($result) == 0) {
|
331: | return $path;
|
332: | }
|
333: | list($parentid) = $this->db->fetchRow($result);
|
334: | $path = '/' . $sel_id . $path . '';
|
335: | $parentid = (int)$parentid;
|
336: | if ($parentid === 0) {
|
337: | return $path;
|
338: | }
|
339: | $path = $this->getIdPathFromId($parentid, $path);
|
340: |
|
341: | return $path;
|
342: | }
|
343: |
|
344: | |
345: | |
346: | |
347: | |
348: | |
349: | |
350: | |
351: | |
352: |
|
353: | public function getAllChild($sel_id = 0, $order = '', $parray = array())
|
354: | {
|
355: | $sel_id = (int)$sel_id;
|
356: | $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
|
357: | if ($order != '') {
|
358: | $sql .= " ORDER BY $order";
|
359: | }
|
360: | $result = $this->db->query($sql);
|
361: | if (!$this->db->isResultSet($result)) {
|
362: | throw new \RuntimeException(
|
363: | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR
|
364: | );
|
365: | }
|
366: | $count = $this->db->getRowsNum($result);
|
367: | if ($count == 0) {
|
368: | return $parray;
|
369: | }
|
370: | while (false !== ($row = $this->db->fetchArray($result))) {
|
371: | $parray[] = $row;
|
372: | $parray = $this->getAllChild($row[$this->id], $order, $parray);
|
373: | }
|
374: |
|
375: | return $parray;
|
376: | }
|
377: |
|
378: | |
379: | |
380: | |
381: | |
382: | |
383: | |
384: | |
385: | |
386: |
|
387: | public function getChildTreeArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '')
|
388: | {
|
389: | $sel_id = (int)$sel_id;
|
390: | $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
|
391: | if ($order != '') {
|
392: | $sql .= " ORDER BY $order";
|
393: | }
|
394: | $result = $this->db->query($sql);
|
395: | if (!$this->db->isResultSet($result)) {
|
396: | throw new \RuntimeException(
|
397: | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR
|
398: | );
|
399: | }
|
400: | $count = $this->db->getRowsNum($result);
|
401: | if ($count == 0) {
|
402: | return $parray;
|
403: | }
|
404: | while (false !== ($row = $this->db->fetchArray($result))) {
|
405: | $row['prefix'] = $r_prefix . '.';
|
406: | $parray[] = $row;
|
407: | $parray = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']);
|
408: | }
|
409: |
|
410: | return $parray;
|
411: | }
|
412: | }
|
413: | |