1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: plugin_include('repository', 'custom/FrontendNavigation.php');
19:
20: 21: 22: 23: 24: 25:
26: class pApiTree {
27:
28: 29: 30: 31: 32:
33: var $db = NULL;
34:
35: 36: 37:
38: var $table = NULL;
39:
40: 41: 42:
43: var $lang = 1;
44:
45: 46: 47:
48: var $client = 1;
49:
50: 51: 52:
53: var $defaultLang = 1;
54:
55: 56: 57:
58: var $logger = NULL;
59:
60: 61: 62:
63: var $user = NULL;
64:
65: 66: 67:
68: var $treeStatus = array();
69:
70: 71: 72:
73: var $uuid = NULL;
74:
75: 76: 77:
78: var $_arrInFilters = array('htmlspecialchars', 'addslashes');
79:
80: 81: 82:
83: var $_arrOutFilters = array('stripslashes', 'htmldecode');
84:
85: function pApiTree ($uuid) {
86: global $db, $cfg, $lang, $client, $auth;
87:
88: $this->db = cRegistry::getDb();
89: $this->table = $cfg['tab'];
90: $this->lang = $lang;
91: $this->client = $client;
92: $this->bDebug = false;
93:
94: $this->uuid = $uuid;
95:
96: $this->user = new cApiUser($auth->auth["uid"]);
97: $this->loadTreeStatus();
98: }
99:
100: 101: 102: 103: 104: 105: 106:
107: function fetchTree ($parentId = false, $level = 0, $bUseTreeStatus = true) {
108:
109:
110: $sql = "SELECT
111: tree.idpica_alloc, tree.parentid, tree.sortorder
112: FROM
113: " . $this->table['pica_alloc'] . " as tree";
114:
115: if ($parentId === false) {
116: $sql .= " WHERE tree.parentid = '0'";
117: } else {
118: $sql .= " WHERE tree.parentid = " . cSecurity::toInteger($parentId);
119: }
120:
121: $sql .= " ORDER BY sortorder ASC";
122:
123: $this->db->query($sql);
124:
125: $result_tmp = array();
126: while ($this->db->nextRecord()) {
127: $item = $this->_fetchItemNameLang($this->db->f('idpica_alloc'));
128:
129: $itemStatus = 'expanded';
130:
131: if ($bUseTreeStatus)
132: {
133: if (is_array($this->treeStatus) && array_key_exists($this->db->f('idpica_alloc'), $this->treeStatus))
134: {
135: $itemStatus = 'collapsed';
136: }
137: }
138:
139: $rs = array(
140: 'idpica_alloc' => $this->db->f('idpica_alloc'),
141: 'parentid' => ($this->db->f('parentid') == NULL) ? false : $this->db->f('parentid'),
142: 'sortorder' => $this->db->f('sortorder'),
143: 'name' => $this->_outFilter($item['name']),
144: 'idlang' => $item['idlang'],
145: 'level' => $level,
146: 'status' => $itemStatus,
147: 'online' => $item['online']
148: );
149:
150: array_push($result_tmp, $rs);
151: }
152:
153: if (count($result_tmp) > 0) {
154: $result = array();
155: foreach ($result_tmp as $rs) {
156: $children = $this->fetchTree($rs['idpica_alloc'], $level + 1, $bUseTreeStatus);
157: if ($children !== false && $rs['status'] == 'expanded') {
158: $rs['children'] = $children;
159: }
160: array_push($result, $rs);
161: }
162: return $result;
163: } else
164: {
165: return false;
166: }
167: }
168:
169: 170: 171: 172: 173: 174: 175: 176: 177: 178:
179: function fetchTreeIds ($parentId = false, $level = 0, $showOffline = false) {
180:
181:
182: $sql = "SELECT
183: tree.idpica_alloc, tree.parentid, tree.sortorder
184: FROM
185: " . $this->table['pica_alloc'] . " as tree";
186:
187: if ($parentId === false) {
188: $sql .= " WHERE tree.parentid IS NULL";
189: } else {
190: $sql .= " WHERE tree.parentid = " . cSecurity::toInteger($parentId);
191: }
192:
193: $sql .= " ORDER BY sortorder ASC";
194:
195: if ($this->bDebug) {print "<!-- "; print $sql; print " -->";}
196:
197: $this->db->query($sql);
198:
199: $result_tmp = array();
200: while ($this->db->nextRecord()) {
201:
202: $item = $this->_fetchItemNameLang($this->db->f('idpica_alloc'));
203:
204: if ($showOffline OR $item['online'] == 1)
205: {
206: $rs = array(
207: 'idpica_alloc' => $this->db->f('idpica_alloc')
208: );
209:
210: array_push($result_tmp, $rs);
211: }
212: }
213:
214: if (count($result_tmp) > 0) {
215: $result = array();
216: foreach ($result_tmp as $rs) {
217: $children = $this->fetchTreeIds($rs['idpica_alloc'], $level + 1, $showOffline);
218: if ($children !== false) {
219: $rs['children'] = $children;
220: }
221: array_push($result, $rs);
222: }
223: return $result;
224: } else
225: {
226: return false;
227: }
228: }
229:
230: function setTreeStatus($idpica_alloc) {
231: $idpica_alloc = cSecurity::toInteger($idpica_alloc);
232: if (is_array($this->treeStatus) && array_key_exists($idpica_alloc, $this->treeStatus)) {
233: unset($this->treeStatus[$idpica_alloc]);
234: } else {
235: $this->treeStatus[$idpica_alloc] = true;
236: }
237: $this->user->setProperty("expandstate", $this->_uuid, serialize($this->treeStatus));
238: }
239:
240: function loadTreeStatus () {
241: $status = $this->user->getProperty("expandstate", $this->_uuid);
242: if ($status !== false) {
243: $this->treeStatus = unserialize($status);
244: }
245: }
246:
247: function fetchParent ($idpica_alloc) {
248: $sql = "SELECT idpica_alloc FROM ".$this->table['pica_alloc']." WHERE parentId = " . cSecurity::toInteger($idpica_alloc);
249: $this->db->query($sql);
250:
251: if ($this->db->nextRecord()) {
252: return $this->fetchItem($this->db->f('idpica_alloc'));
253: } else {
254: return false;
255: }
256: }
257:
258: function fetchParents () {}
259:
260: function fetchLevel ($parentId = false, $showOffline = false) {
261:
262: $sql = "SELECT
263: tree.idpica_alloc, tree.parentid, tree.sortorder
264: FROM
265: " . $this->table['pica_alloc'] . " as tree
266: LEFT JOIN ".$this->table['pica_lang']." as treelang USING (idpica_alloc)";
267:
268: if ($parentId === false) {
269: $sql .= " WHERE tree.parentid IS NULL";
270: } else {
271: $sql .= " WHERE tree.parentid = " . cSecurity::toInteger($parentId);
272: }
273:
274: if ($showOffline === false) {
275: $sql .= " AND treelang.online = 1";
276: }
277:
278: $sql .= " ORDER BY sortorder ASC";
279:
280: $this->db->query($sql);
281:
282: $result_tmp = array();
283: while ($this->db->nextRecord()) {
284: $item = $this->_fetchItemNameLang($this->db->f('idpica_alloc'));
285:
286: $itemStatus = 'expanded';
287: if (is_array($this->treeStatus) && array_key_exists($this->db->f('idpica_alloc'), $this->treeStatus)) {
288: $itemStatus = 'collapsed';
289: }
290:
291: $rs = array(
292: 'idpica_alloc' => $this->db->f('idpica_alloc'),
293: 'parentid' => ($this->db->f('parentid') == NULL) ? false : $this->db->f('parentid'),
294: 'sortorder' => $this->db->f('sortorder'),
295: 'name' => $this->_outFilter($item['name']),
296: 'idlang' => $item['idlang'],
297: 'level' => 0,
298: 'status' => $itemStatus,
299: 'online' => $item['online']
300: );
301:
302: array_push($result_tmp, $rs);
303: }
304:
305: return $result_tmp;
306: }
307:
308: function storeItem ($treeItem) {
309:
310: if (!$treeItem['idpica_alloc']) {
311:
312: $treeItem['sortorder'] = $this->_fetchMaxOrder($treeItem['parentid']) + 1;
313:
314: if ($treeItem['parentid'] == 'root') {
315: $treeItem['parentid'] = 'NULL';
316: }
317:
318: $treeItem['name'] = $this->_inFilter($treeItem['name']);
319:
320: $sql = "INSERT INTO " . $this->table['pica_alloc'] . "
321: (parentid, sortorder)
322: VALUES
323: (" . cSecurity::toInteger($treeItem['parentid']) . ", " . cSecurity::toInteger($treeItem['sortorder']) . ")";
324: $this->db->query($sql);
325: $treeItem['idpica_alloc'] = $this->db->getLastInsertedId();
326: $sql = "INSERT INTO " . $this->table['pica_lang'] . "
327: (idpica_alloc, idlang, name)
328: VALUES
329: (" . cSecurity::toInteger($treeItem['idpica_alloc']) . ", " . cSecurity::toInteger($this->lang) . ", '" . $this->db->escape($treeItem['name']) . "')";
330: $this->db->query($sql);
331:
332: } else {
333: $treeItem['name'] = $this->_inFilter($treeItem['name']);
334:
335: $sql = "SELECT * FROM " . $this->table['pica_lang'] . " WHERE idpica_alloc = " . cSecurity::toInteger($treeItem['idpica_alloc']) . " AND idlang = " . cSecurity::toInteger($this->lang);
336: $this->db->query($sql);
337:
338: if ($this->db->numRows() > 0) {
339:
340: $sql = "UPDATE " . $this->table['pica_lang'] . " SET name = '" . $this->db->escape($treeItem['name']) . "' WHERE idpica_alloc = " . cSecurity::toInteger($treeItem['idpica_alloc']) . "
341: AND idlang = " . cSecurity::toInteger($this->lang);
342: } else {
343:
344: $sql = "SELECT * FROM " . $this->table['pica_lang'] . " WHERE idpica_alloc = " . $treeItem['idpica_alloc'] . " ORDER BY idlang";
345: $this->db->query($sql);
346:
347: if ($this->db->nextRecord()) {
348: $online_status = $this->db->f('online');
349: } else {
350: $online_status = 0;
351: }
352:
353:
354: $sql = "INSERT INTO " . $this->table['pica_lang'] . "(idpica_alloc, idlang, name, online) VALUES (".cSecurity::toInteger($treeItem['idpica_alloc']).", ".cSecurity::toInteger($this->lang).",
355: '".$this->db->escape($treeItem['name'])."', ".cSecurity::toInteger($online_status).")";
356: }
357:
358: $this->db->query($sql);
359: }
360:
361: return $treeItem;
362: }
363:
364: function setOnline ($idpica_alloc) {
365: $this->_switchOnOffline($idpica_alloc, 1);
366: }
367:
368: function setOffline ($idpica_alloc) {
369: $this->_switchOnOffline($idpica_alloc, 0);
370: }
371:
372: function _switchOnOffline ($idpica_alloc, $status) {
373: $sql = "UPDATE " . $this->table['pica_lang'] . " SET online = " . cSecurity::toInteger($status) . " WHERE idpica_alloc = " . cSecurity::toInteger($idpica_alloc) . "
374: AND idlang = " . cSecurity::toInteger($this->lang);
375: $this->db->query($sql);
376: }
377:
378: function itemMoveUp ($idpica_alloc) {
379: $treeItem = $this->fetchItem($idpica_alloc);
380: $treeItem_old = $treeItem;
381: $treeItem['sortorder']--;
382:
383: if ($treeItem['sortorder'] < $treeItem_old['sortorder']) {
384: if ($treeItem['sortorder'] >= 1) {
385: $this->_decreaseOrder($treeItem['parentid'], $treeItem_old['sortorder']);
386: $this->_increaseOrder($treeItem['parentid'], $treeItem['sortorder']);
387: } else {
388: $treeItem['sortorder'] = $treeItem_old['sortorder'];
389: }
390: }
391:
392: $sql = "UPDATE " . $this->table['pica_alloc'] . " SET sortorder = " . $treeItem['sortorder'] . " WHERE idpica_alloc = " . cSecurity::toInteger($idpica_alloc);
393: $this->db->query($sql);
394: }
395:
396: function itemMoveDown () {}
397:
398: function deleteItem ($idpica_alloc) {
399: $sql = "DELETE FROM " . $this->table['pica_alloc'] . " WHERE idpica_alloc = " . cSecurity::toInteger($idpica_alloc);
400: $this->db->query($sql);
401:
402: $sql = "DELETE FROM " . $this->table['pica_lang'] . " WHERE idpica_alloc = " . cSecurity::toInteger($idpica_alloc);
403: $this->db->query($sql);
404:
405: $sql = "DELETE FROM " . $this->table['pica_alloc_con'] . " WHERE idpica_alloc = " . cSecurity::toInteger($idpica_alloc);
406: $this->db->query($sql);
407: }
408:
409: function fetchItem ($idpica_alloc) {
410: $sql = "SELECT parentid, sortorder FROM " . $this->table['pica_alloc'] . " WHERE idpica_alloc = " . $idpica_alloc;
411: $this->db->query($sql);
412:
413: $item = $this->_fetchItemNameLang($idpica_alloc);
414:
415: if ($this->db->nextRecord()) {
416: $row = array(
417: 'idpica_alloc' => $idpica_alloc,
418: 'parentid' => ($this->db->f('parentid') == NULL) ? false : $this->db->f('parentid'),
419: 'sortorder' => $this->db->f('sortorder'),
420: 'name' => $item['name'],
421: 'idlang' => $item['idlang'],
422: 'online' => $item['online']
423: );
424: return $row;
425: } else {
426: return false;
427: }
428: }
429:
430: function _fetchItemNameLang ($idpica_alloc) {
431: $oDB = cRegistry::getDb();
432:
433: $sSQL = "SELECT name, idlang, online FROM " . $this->table['pica_lang'] . " WHERE idpica_alloc = " . cSecurity::toInteger($idpica_alloc) . " AND idlang = " . cSecurity::toInteger($this->lang);
434: $oDB->query($sSQL);
435:
436: $aResult = array();
437: if ($oDB->nextRecord()) {
438:
439: $aResult['name'] = $this->_outFilter($oDB->f('name'));
440: $aResult['idlang'] = $oDB->f('idlang');
441: $aResult['online'] = $oDB->f('online');
442:
443: } else {
444:
445:
446:
447:
448:
449: $sSQL = "SELECT name, idlang, online FROM " . $this->table['pica_lang'] . " WHERE idpica_alloc = " . cSecurity::toInteger($idpica_alloc) . " ORDER BY idlang";
450: $oDB->query($sSQL);
451:
452: $aNames = array();
453: while ($oDB->nextRecord()) {
454: $sKey = "k" . $oDB->f('idlang');
455:
456: $aNames[$sKey] = array();
457: $aNames[$sKey]['name'] = $this->_outFilter($oDB->f('name'));
458: $aNames[$sKey]['idlang'] = $oDB->f('idlang');
459: $aNames[$sKey]['online'] = $oDB->f('online');
460: }
461:
462: if ($aNames["k" . $this->defaultLang]) {
463:
464: $aResult = $aNames["k" . $this->defaultLang];
465: } else {
466:
467: $aResult = reset($aNames);
468: }
469: }
470: unset($oDB);
471: unset($aNames);
472:
473: return $aResult;
474: }
475:
476: function _fetchMaxOrder ($parentId = false) {
477:
478: if ($parentId == 'root') {
479: $parentId = false;
480: }
481:
482: $sql = "SELECT MAX(sortorder) as max FROM " . $this->table['pica_alloc'];
483: if ($parentId === false) {
484: $sql .= " WHERE parentid = 0";
485: } else {
486: $sql .= " WHERE parentid = " . cSecurity::toInteger($parentId);
487: }
488: $this->db->query($sql);
489: if ($this->db->nextRecord()) {
490: return $this->db->f('max');
491: } else {
492: return 0;
493: }
494: }
495:
496: function _decreaseOrder ($parentId = false, $fromOrder) {
497: $sql = "UPDATE " . $this->table['pica_alloc'] . " SET sortorder = sortorder - 1 WHERE sortorder >= " . cSecurity::toInteger($fromOrder);
498: if ($parentId === false) {
499: $sql .= " AND parentid IS NULL";
500: } else {
501: $sql .= " AND parentid = " . cSecurity::toInteger($parentId);
502: }
503: $this->db->query($sql);
504: }
505:
506: function _increaseOrder ($parentId = false, $fromOrder) {
507: $sql = "UPDATE " . $this->table['pica_alloc'] . " SET sortorder = sortorder + 1 WHERE sortorder >= " . cSecurity::toInteger($fromOrder);
508: if ($parentId === false) {
509: $sql .= " AND parentid IS NULL";
510: } else {
511: $sql .= " AND parentid = " . cSecurity::toInteger($parentId);
512: }
513: $this->db->query($sql);
514: }
515:
516: function setFilters($arrInFilters = array(), $arrOutFilters = array())
517: {
518: $this->_arrInFilters = $arrInFilters;
519: $this->_arrOutFilters = $arrOutFilters;
520: }
521:
522: function _inFilter($data)
523: {
524: foreach ($this->_arrInFilters as $_function)
525: {
526: if (function_exists($_function))
527: {
528: $data = $_function($data);
529: }
530: }
531:
532: return $data;
533: }
534:
535: function _outFilter($data)
536: {
537: foreach ($this->_arrOutFilters as $_function)
538: {
539: if (function_exists($_function))
540: {
541: $data = $_function($data);
542: }
543: }
544:
545: return $data;
546: }
547: }
548:
549: ?>