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: 19: 20: 21: 22: 23:
24: class cApiCategoryCollection extends ItemCollection {
25:
26: 27: 28: 29: 30: 31:
32: public function __construct($select = false) {
33: global $cfg;
34: parent::__construct($cfg['tab']['cat'], 'idcat');
35: $this->_setItemClass('cApiCategory');
36:
37:
38: $this->_setJoinPartner('cApiClientCollection');
39:
40: if ($select !== false) {
41: $this->select($select);
42: }
43: }
44:
45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57:
58: public function create($idclient, $parentid = 0, $preid = 0, $postid = 0, $status = 0, $author = '', $created = '', $lastmodified = '') {
59: global $auth;
60:
61: if (empty($author)) {
62: $author = $auth->auth['uname'];
63: }
64: if (empty($created)) {
65: $created = date('Y-m-d H:i:s');
66: }
67: if (empty($lastmodified)) {
68: $lastmodified = date('Y-m-d H:i:s');
69: }
70:
71: $oItem = parent::createNewItem();
72:
73: $oItem->set('idclient', $idclient);
74: $oItem->set('parentid', $parentid);
75: $oItem->set('preid', $preid);
76: $oItem->set('postid', $postid);
77: $oItem->set('status', $status);
78: $oItem->set('author', $author);
79: $oItem->set('created', $created);
80: $oItem->set('lastmodified', $lastmodified);
81: $oItem->store();
82:
83: return $oItem;
84: }
85:
86: 87: 88: 89: 90: 91: 92: 93:
94: public function fetchLastCategoryTree($idclient) {
95: $where = 'parentid=0 AND postid=0 AND idclient=' . (int) $idclient;
96: $this->select($where);
97: return $this->next();
98: }
99:
100: 101: 102: 103: 104: 105:
106: public function getCategoryIdsByClient($idclient) {
107: $list = array();
108: $sql = 'SELECT idcat FROM `%s` WHERE idclient=%d';
109: $this->db->query($sql, $this->table, $idclient);
110: while ($this->db->nextRecord()) {
111: $list[] = $this->db->f('idcat');
112: }
113: return $list;
114: }
115:
116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131:
132: public function getNextPostCategoryId($idcat) {
133: $sql = "SELECT idcat FROM `%s` WHERE preid = %d";
134: $this->db->query($sql, $this->table, $idcat);
135: if ($this->db->nextRecord()) {
136:
137: $idcat = $this->db->f('idcat');
138: $sql = "SELECT parentid FROM `%s` WHERE idcat = %d";
139: $this->db->query($sql, $this->table, $idcat);
140: if ($this->db->nextRecord()) {
141:
142: $parentid = (int) $this->db->f('parentid');
143: return ($parentid != 0) ? $idcat : 0;
144: } else {
145: return 99;
146: }
147: } else {
148:
149: return 0;
150: }
151: }
152:
153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172:
173: public function getParentsNextPostCategoryId($idcat) {
174: $sql = "SELECT parentid FROM `%s` WHERE idcat = %d";
175: $this->db->query($sql, $this->table, $idcat);
176: if ($this->db->nextRecord()) {
177:
178: $idcat = $this->db->f('parentid');
179: if ($idcat != 0) {
180: $sql = "SELECT idcat FROM `%s` WHERE preid = %d";
181: $this->db->query($sql, $this->table, $idcat);
182: if ($this->db->nextRecord()) {
183:
184: $idcat = (int) $this->db->f('idcat');
185: $sql = "SELECT parentid FROM `%s` WHERE idcat = %d";
186: $this->db->query($sql, $this->table, $idcat);
187: if ($this->db->nextRecord()) {
188:
189: $parentid = (int) $this->db->f('parentid');
190: return ($parentid != 0) ? $idcat : 0;
191: } else {
192: return 99;
193: }
194: } else {
195:
196: return $this->getNextBackwardsCategoryId($idcat);
197: }
198: } else {
199: return 0;
200: }
201: } else {
202:
203: return 0;
204: }
205: }
206:
207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227:
228: public function getFirstChildCategoryId($idcat, $idlang = NULL) {
229: global $cfg;
230:
231: $sql = "SELECT idcat FROM `%s` WHERE parentid = %d AND preid = 0";
232: $sql = $this->db->prepare($sql, $this->table, $idcat);
233: $this->db->query($sql);
234: if ($this->db->nextRecord()) {
235: $midcat = (int) $this->db->f('idcat');
236: if (NULL == $idlang) {
237: return $midcat;
238: }
239:
240:
241: $sql = "SELECT idcatlang FROM `%s` WHERE idcat = %d AND idlang = %d";
242: $sql = $this->db->prepare($sql, $cfg['tab']['cat_lang'], $idcat, $idlang);
243: $this->db->query($sql);
244: return ($this->db->nextRecord()) ? $midcat : 0;
245: } else {
246:
247: return 0;
248: }
249: }
250:
251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271:
272: public function getAllChildCategoryIds($idcat, $idlang = NULL) {
273: global $cfg;
274:
275: $aCats = array();
276: $bLoop = true;
277: $db2 = $this->_getSecondDBInstance();
278:
279: $sql = "SELECT idcat FROM `%s` WHERE parentid = %d AND preid = 0";
280: $this->db->query($sql, $this->table, $idcat);
281: if ($this->db->nextRecord()) {
282: while ($bLoop) {
283: $midcat = $this->db->f('idcat');
284: if (NULL == $idlang) {
285: $aCats[] = $midcat;
286: } else {
287:
288: $sql = "SELECT idcatlang FROM `%s` WHERE idcat = %d AND idlang = %d";
289: $db2->query($sql, $cfg['tab']['cat_lang'], $midcat, $idlang);
290: if ($db2->nextRecord()) {
291: $aCats[] = $midcat;
292: }
293: }
294:
295: $sql = "SELECT idcat FROM `%s` WHERE parentid = %d AND preid = %d";
296: $this->db->query($sql, $this->table, $idcat, $midcat);
297: if (!$this->db->nextRecord()) {
298: $bLoop = false;
299: }
300: }
301: }
302: return $aCats;
303: }
304:
305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334:
335: public function getAllCategoryIdsRecursive($idcat, $idclient) {
336: global $cfg;
337:
338: $catList = array();
339: $openList = array();
340:
341: $openList[] = $idcat;
342:
343: while (($actId = array_pop($openList)) != NULL) {
344: if (in_array($actId, $catList)) {
345: continue;
346: }
347:
348: $catList[] = $actId;
349:
350: $sql = "SELECT * FROM `:cat_tree` AS A, `:cat` AS B WHERE A.idcat=B.idcat AND B.parentid=:parentid AND idclient=:idclient ORDER BY idtree";
351: $sql = $this->db->prepare($sql, array(
352: 'cat_tree' => $cfg['tab']['cat_tree'],
353: 'cat' => $this->table,
354: 'parentid' => (int) $actId,
355: 'idclient' => (int) $idclient
356: ));
357: $this->db->query($sql);
358:
359: while ($this->db->nextRecord()) {
360: $openList[] = $this->db->f('idcat');
361: }
362: }
363:
364: return $catList;
365: }
366:
367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395:
396: public function getAllCategoryIdsRecursive2($idcat, $idclient) {
397: global $cfg;
398:
399: $aCats = array();
400: $found = false;
401: $curLevel = 0;
402:
403: $sql = "SELECT * FROM `%s` AS a, `%s` AS b WHERE a.idcat = b.idcat AND idclient = %d ORDER BY idtree";
404: $sql = $this->db->prepare($sql, $cfg['tab']['cat_tree'], $cfg['tab']['cat'], $idclient);
405: $this->db->query($sql);
406:
407: while ($this->db->nextRecord()) {
408: if ($found && $this->db->f('level') <= $curLevel) {
409:
410: $found = false;
411: }
412:
413: if ($this->db->f('idcat') == $idcat) {
414: $found = true;
415: $curLevel = $this->db->f('level');
416: }
417:
418: if ($found) {
419: $aCats[] = $this->db->f('idcat');
420: }
421: }
422:
423: return $aCats;
424: }
425: }
426:
427: 428: 429: 430: 431: 432:
433: class cApiCategory extends Item {
434:
435: 436: 437: 438: 439:
440: public function __construct($mId = false) {
441: global $cfg;
442: parent::__construct($cfg['tab']['cat'], 'idcat');
443: $this->setFilters(array(), array());
444:
445: if ($mId !== false) {
446: $this->loadByPrimaryKey($mId);
447: }
448: }
449:
450: 451: 452: 453: 454:
455: public function store() {
456: $this->set('lastmodified', date('Y-m-d H:i:s'));
457: return parent::store();
458: }
459:
460: 461: 462: 463: 464: 465: 466: 467:
468: public function setField($name, $value, $safe = true) {
469: switch ($name) {
470: case 'idcat':
471: case 'idclient':
472: case 'parentid':
473: case 'preid':
474: case 'postid':
475: case 'status':
476: $value = (int) $value;
477: break;
478: }
479:
480: parent::setField($name, $value, $safe);
481: }
482:
483: 484: 485: 486: 487: 488:
489: public function getLink($changeLangId = 0) {
490: if ($this->isLoaded() === false) {
491: return '';
492: }
493:
494: $options = array();
495: $options['idcat'] = $this->get('idcat');
496: $options['lang'] = ($changeLangId == 0) ? cRegistry::getLanguageId() : $changeLangId;
497: if ($changeLangId > 0) {
498: $options['changelang'] = $changeLangId;
499: }
500:
501: return cUri::getInstance()->build($options);
502: }
503: }
504: