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