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:
205:
206: if ($showOffline OR $item['online'] == 1)
207: {
208: $rs = array(
209: 'idpica_alloc' => $this->db->f('idpica_alloc')
210: );
211:
212: array_push($result_tmp, $rs);
213: }
214: }
215:
216: if (count($result_tmp) > 0) {
217: $result = array();
218: foreach ($result_tmp as $rs) {
219: $children = $this->fetchTreeIds($rs['idpica_alloc'], $level + 1, $showOffline);
220: if ($children !== false) {
221: $rs['children'] = $children;
222: }
223: array_push($result, $rs);
224: }
225: return $result;
226: } else
227: {
228: return false;
229: }
230: }
231:
232: function setTreeStatus($idpica_alloc) {
233: if (is_array($this->treeStatus) && array_key_exists($idpica_alloc, $this->treeStatus)) {
234: unset($this->treeStatus[$idpica_alloc]);
235: } else {
236: $this->treeStatus[$idpica_alloc] = true;
237: }
238: $this->user->setProperty("expandstate", $this->_uuid, serialize($this->treeStatus));
239: }
240:
241: function loadTreeStatus () {
242: $status = $this->user->getProperty("expandstate", $this->_uuid);
243: if ($status !== false) {
244: $this->treeStatus = unserialize($status);
245: }
246: }
247:
248: function fetchParent ($idpica_alloc) {
249: $sql = "SELECT idpica_alloc FROM ".$this->table['pica_alloc']." WHERE parentId = " . cSecurity::toInteger($idpica_alloc);
250: $this->db->query($sql);
251:
252: if ($this->db->nextRecord()) {
253: return $this->fetchItem($this->db->f('idpica_alloc'));
254: } else {
255: return false;
256: }
257: }
258:
259: function fetchParents () {}
260:
261: function fetchLevel ($parentId = false, $showOffline = false) {
262:
263: $sql = "SELECT
264: tree.idpica_alloc, tree.parentid, tree.sortorder
265: FROM
266: " . $this->table['pica_alloc'] . " as tree
267: LEFT JOIN ".$this->table['pica_lang']." as treelang USING (idpica_alloc)";
268:
269: if ($parentId === false) {
270: $sql .= " WHERE tree.parentid IS NULL";
271: } else {
272: $sql .= " WHERE tree.parentid = " . cSecurity::toInteger($parentId);
273: }
274:
275: if ($showOffline === false) {
276: $sql .= " AND treelang.online = 1";
277: }
278:
279: $sql .= " ORDER BY sortorder ASC";
280:
281: $this->db->query($sql);
282:
283: $result_tmp = array();
284: while ($this->db->nextRecord()) {
285: $item = $this->_fetchItemNameLang($this->db->f('idpica_alloc'));
286:
287: $itemStatus = 'expanded';
288: if (is_array($this->treeStatus) && array_key_exists($this->db->f('idpica_alloc'), $this->treeStatus)) {
289: $itemStatus = 'collapsed';
290: }
291:
292: $rs = array(
293: 'idpica_alloc' => $this->db->f('idpica_alloc'),
294: 'parentid' => ($this->db->f('parentid') == NULL) ? false : $this->db->f('parentid'),
295: 'sortorder' => $this->db->f('sortorder'),
296: 'name' => $this->_outFilter($item['name']),
297: 'idlang' => $item['idlang'],
298: 'level' => 0,
299: 'status' => $itemStatus,
300: 'online' => $item['online']
301: );
302:
303: array_push($result_tmp, $rs);
304: }
305:
306: return $result_tmp;
307: }
308:
309: function storeItem ($treeItem) {
310:
311: if (!$treeItem['idpica_alloc']) {
312:
313: $treeItem['sortorder'] = $this->_fetchMaxOrder($treeItem['parentid']) + 1;
314:
315: if ($treeItem['parentid'] == 'root') {
316: $treeItem['parentid'] = 'NULL';
317: }
318:
319: $treeItem['name'] = $this->_inFilter($treeItem['name']);
320:
321: $sql = "INSERT INTO " . $this->table['pica_alloc'] . "
322: ( parentid, sortorder)
323: VALUES
324: (" . cSecurity::toInteger($treeItem['parentid']) . ", " . cSecurity::toInteger($treeItem['sortorder']) . ")";
325: $this->db->query($sql);
326: $treeItem['idpica_alloc'] = $this->db->getLastInsertedId($this->table['pica_alloc']);
327: $sql = "INSERT INTO " . $this->table['pica_lang'] . "
328: (idpica_alloc, idlang, name)
329: VALUES
330: (" . cSecurity::toInteger($treeItem['idpica_alloc']) . ", " . cSecurity::toInteger($this->lang) . ", '" . cSecurity::escapeDB($treeItem['name'], $this->db) . "')";
331: $this->db->query($sql);
332:
333: } else {
334: $treeItem['name'] = $this->_inFilter($treeItem['name']);
335:
336: $sql = "SELECT * FROM " . $this->table['pica_lang'] . " WHERE idpica_alloc = " . cSecurity::toInteger($treeItem['idpica_alloc']) . " AND idlang = " . cSecurity::toInteger($this->lang);
337: $this->db->query($sql);
338:
339: if ($this->db->numRows() > 0) {
340:
341: $sql = "UPDATE " . $this->table['pica_lang'] . " SET name = '" . cSecurity::escapeDB($treeItem['name'], $this->db) . "' WHERE idpica_alloc = " . cSecurity::toInteger($treeItem['idpica_alloc']) . "
342: AND idlang = " . cSecurity::toInteger($this->lang);
343: } else {
344:
345: $sql = "SELECT * FROM " . $this->table['pica_lang'] . " WHERE idpica_alloc = " . $treeItem['idpica_alloc'] . " ORDER BY idlang";
346: $this->db->query($sql);
347:
348: if ($this->db->nextRecord()) {
349: $online_status = $this->db->f('online');
350: } else {
351: $online_status = 0;
352: }
353:
354:
355: $sql = "INSERT INTO " . $this->table['pica_lang'] . "(idpica_alloc, idlang, name, online) VALUES ( ".cSecurity::toInteger($treeItem['idpica_alloc']).", ".cSecurity::toInteger($this->lang).",
356: '".cSecurity::escapeDB($treeItem['name'], $this->db)."', ".cSecurity::toInteger($online_status).")";
357: }
358:
359: $this->db->query($sql);
360: }
361:
362: return $treeItem;
363: }
364:
365: function setOnline ($idpica_alloc) {
366: $this->_switchOnOffline($idpica_alloc, 1);
367: }
368:
369: function setOffline ($idpica_alloc) {
370: $this->_switchOnOffline($idpica_alloc, 0);
371: }
372:
373: function _switchOnOffline ($idpica_alloc, $status) {
374: $sql = "UPDATE " . $this->table['pica_lang'] . " SET online = " . cSecurity::toInteger($status) . " WHERE idpica_alloc = " . cSecurity::toInteger($idpica_alloc) . "
375: AND idlang = " . cSecurity::toInteger($this->lang);
376: $this->db->query($sql);
377: }
378:
379: function itemMoveUp ($idpica_alloc) {
380: $treeItem = $this->fetchItem($idpica_alloc);
381: $treeItem_old = $treeItem;
382: $treeItem['sortorder']--;
383:
384: if ($treeItem['sortorder'] < $treeItem_old['sortorder']) {
385: if ($treeItem['sortorder'] >= 1) {
386: $this->_decreaseOrder($treeItem['parentid'], $treeItem_old['sortorder']);
387: $this->_increaseOrder($treeItem['parentid'], $treeItem['sortorder']);
388: } else {
389: $treeItem['sortorder'] = $treeItem_old['sortorder'];
390: }
391: }
392:
393: $sql = "UPDATE " . $this->table['pica_alloc'] . " SET sortorder = " . $treeItem['sortorder'] . " WHERE idpica_alloc = " . cSecurity::toInteger($idpica_alloc);
394: $this->db->query($sql);
395: }
396:
397: function itemMoveDown () {}
398:
399: function deleteItem ($idpica_alloc) {
400: $sql = "DELETE FROM " . $this->table['pica_alloc'] . " WHERE idpica_alloc = " . cSecurity::toInteger($idpica_alloc);
401: $this->db->query($sql);
402:
403: $sql = "DELETE FROM " . $this->table['pica_lang'] . " WHERE idpica_alloc = " . cSecurity::toInteger($idpica_alloc);
404: $this->db->query($sql);
405:
406: $sql = "DELETE FROM " . $this->table['pica_alloc_con'] . " WHERE idpica_alloc = " . cSecurity::toInteger($idpica_alloc);
407: $this->db->query($sql);
408: }
409:
410: function fetchItem ($idpica_alloc) {
411: $sql = "SELECT parentid, sortorder FROM " . $this->table['pica_alloc'] . " WHERE idpica_alloc = " . $idpica_alloc;
412: $this->db->query($sql);
413:
414: $item = $this->_fetchItemNameLang($idpica_alloc);
415:
416: if ($this->db->nextRecord()) {
417: $row = array(
418: 'idpica_alloc' => $idpica_alloc,
419: 'parentid' => ($this->db->f('parentid') == NULL) ? false : $this->db->f('parentid'),
420: 'sortorder' => $this->db->f('sortorder'),
421: 'name' => $item['name'],
422: 'idlang' => $item['idlang'],
423: 'online' => $item['online']
424: );
425: return $row;
426: } else {
427: return false;
428: }
429: }
430:
431: function _fetchItemNameLang ($idpica_alloc) {
432: $oDB = cRegistry::getDb();
433:
434: $sSQL = "SELECT name, idlang, online FROM " . $this->table['pica_lang'] . " WHERE idpica_alloc = " . cSecurity::toInteger($idpica_alloc) . " AND idlang = " . cSecurity::toInteger($this->lang);
435: $oDB->query($sSQL);
436:
437: $aResult = array();
438: if ($oDB->nextRecord()) {
439:
440: $aResult['name'] = $this->_outFilter($oDB->f('name'));
441: $aResult['idlang'] = $oDB->f('idlang');
442: $aResult['online'] = $oDB->f('online');
443:
444: } else {
445:
446:
447:
448:
449:
450: $sSQL = "SELECT name, idlang, online FROM " . $this->table['pica_lang'] . " WHERE idpica_alloc = " . cSecurity::toInteger($idpica_alloc) . " ORDER BY idlang";
451: $oDB->query($sSQL);
452:
453: $aNames = array();
454: while ($oDB->nextRecord()) {
455: $sKey = "k" . $oDB->f('idlang');
456:
457: $aNames[$sKey] = array();
458: $aNames[$sKey]['name'] = $this->_outFilter($oDB->f('name'));
459: $aNames[$sKey]['idlang'] = $oDB->f('idlang');
460: $aNames[$sKey]['online'] = $oDB->f('online');
461: }
462:
463: if ($aNames["k" . $this->defaultLang]) {
464:
465: $aResult = $aNames["k" . $this->defaultLang];
466: } else {
467:
468: $aResult = reset($aNames);
469: }
470: }
471: unset($oDB);
472: unset($aNames);
473:
474: return $aResult;
475: }
476:
477: function _fetchMaxOrder ($parentId = false) {
478:
479: if ($parentId == 'root') {
480: $parentId = false;
481: }
482:
483: $sql = "SELECT MAX(sortorder) as max FROM " . $this->table['pica_alloc'];
484: if ($parentId === false) {
485: $sql .= " WHERE parentid = 0";
486: } else {
487: $sql .= " WHERE parentid = " . cSecurity::toInteger($parentId);
488: }
489: $this->db->query($sql);
490: if ($this->db->nextRecord()) {
491: return $this->db->f('max');
492: } else {
493: return 0;
494: }
495: }
496:
497: function _decreaseOrder ($parentId = false, $fromOrder) {
498: $sql = "UPDATE " . $this->table['pica_alloc'] . " SET sortorder = sortorder - 1 WHERE sortorder >= " . cSecurity::toInteger($fromOrder);
499: if ($parentId === false) {
500: $sql .= " AND parentid IS NULL";
501: } else {
502: $sql .= " AND parentid = " . cSecurity::toInteger($parentId);
503: }
504: $this->db->query($sql);
505: }
506:
507: function _increaseOrder ($parentId = false, $fromOrder) {
508: $sql = "UPDATE " . $this->table['pica_alloc'] . " SET sortorder = sortorder + 1 WHERE sortorder >= " . cSecurity::toInteger($fromOrder);
509: if ($parentId === false) {
510: $sql .= " AND parentid IS NULL";
511: } else {
512: $sql .= " AND parentid = " . cSecurity::toInteger($parentId);
513: }
514: $this->db->query($sql);
515: }
516:
517: function setFilters($arrInFilters = array(), $arrOutFilters = array())
518: {
519: $this->_arrInFilters = $arrInFilters;
520: $this->_arrOutFilters = $arrOutFilters;
521: }
522:
523: function _inFilter($data)
524: {
525: foreach ($this->_arrInFilters as $_function)
526: {
527: if (function_exists($_function))
528: {
529: $data = $_function($data);
530: }
531: }
532:
533: return $data;
534: }
535:
536: function _outFilter($data)
537: {
538: foreach ($this->_arrOutFilters as $_function)
539: {
540: if (function_exists($_function))
541: {
542: $data = $_function($data);
543: }
544: }
545:
546: return $data;
547: }
548: }
549:
550: ?>