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: public function __construct($select = false) {
32: global $cfg;
33: parent::__construct($cfg['tab']['cat'], 'idcat');
34: $this->_setItemClass('cApiCategory');
35:
36:
37: $this->_setJoinPartner('cApiClientCollection');
38:
39: if ($select !== false) {
40: $this->select($select);
41: }
42: }
43:
44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56:
57: public function create($idclient, $parentid = 0, $preid = 0, $postid = 0, $status = 0, $author = '', $created = '', $lastmodified = '') {
58: global $auth;
59:
60: if (empty($author)) {
61: $author = $auth->auth['uname'];
62: }
63: if (empty($created)) {
64: $created = date('Y-m-d H:i:s');
65: }
66: if (empty($lastmodified)) {
67: $lastmodified = date('Y-m-d H:i:s');
68: }
69:
70: $oItem = parent::createNewItem();
71:
72: $oItem->set('idclient', $idclient);
73: $oItem->set('parentid', $parentid);
74: $oItem->set('preid', $preid);
75: $oItem->set('postid', $postid);
76: $oItem->set('status', $status);
77: $oItem->set('author', $author);
78: $oItem->set('created', $created);
79: $oItem->set('lastmodified', $lastmodified);
80: $oItem->store();
81:
82: return $oItem;
83: }
84:
85: 86: 87: 88: 89: 90: 91: 92:
93: public function fetchLastCategoryTree($idclient) {
94: $where = 'parentid=0 AND postid=0 AND idclient=' . (int) $idclient;
95: $this->select($where);
96: return $this->next();
97: }
98:
99: 100: 101: 102: 103: 104:
105: public function getCategoryIdsByClient($idclient) {
106: $list = array();
107: $sql = 'SELECT idcat FROM `%s` WHERE idclient=%d';
108: $this->db->query($sql, $this->table, $idclient);
109: while ($this->db->nextRecord()) {
110: $list[] = $this->db->f('idcat');
111: }
112: return $list;
113: }
114:
115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130:
131: public function getNextPostCategoryId($idcat) {
132: $sql = "SELECT idcat FROM `%s` WHERE preid = %d";
133: $this->db->query($sql, $this->table, $idcat);
134: if ($this->db->nextRecord()) {
135:
136: $idcat = $this->db->f('idcat');
137: $sql = "SELECT parentid FROM `%s` WHERE idcat = %d";
138: $this->db->query($sql, $this->table, $idcat);
139: if ($this->db->nextRecord()) {
140:
141: $parentid = (int) $this->db->f('parentid');
142: return ($parentid != 0)? $idcat : 0;
143: } else {
144: return 99;
145: }
146: } else {
147:
148: return 0;
149: }
150: }
151:
152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171:
172: public function getParentsNextPostCategoryId($idcat) {
173: $sql = "SELECT parentid FROM `%s` WHERE idcat = %d";
174: $this->db->query($sql, $this->table, $idcat);
175: if ($this->db->nextRecord()) {
176:
177: $idcat = $this->db->f('parentid');
178: if ($idcat != 0) {
179: $sql = "SELECT idcat FROM `%s` WHERE preid = %d";
180: $this->db->query($sql, $this->table, $idcat);
181: if ($this->db->nextRecord()) {
182:
183: $idcat = (int) $this->db->f('idcat');
184: $sql = "SELECT parentid FROM `%s` WHERE idcat = %d";
185: $this->db->query($sql, $this->table, $idcat);
186: if ($this->db->nextRecord()) {
187:
188: $parentid = (int) $this->db->f('parentid');
189: return ($parentid != 0)? $idcat : 0;
190: } else {
191: return 99;
192: }
193: } else {
194:
195: return $this->getNextBackwardsCategoryId($idcat);
196: }
197: } else {
198: return 0;
199: }
200: } else {
201:
202: return 0;
203: }
204: }
205:
206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226:
227: public function getFirstChildCategoryId($idcat, $idlang = null) {
228: global $cfg;
229:
230: $sql = "SELECT idcat FROM `%s` WHERE parentid = %d AND preid = 0";
231: $sql = $this->db->prepare($sql, $this->table, $idcat);
232: $this->db->query($sql);
233: if ($this->db->nextRecord()) {
234: $midcat = (int) $this->db->f('idcat');
235: if (null == $idlang) {
236: return $midcat;
237: }
238:
239:
240: $sql = "SELECT idcatlang FROM `%s` WHERE idcat = %d AND idlang = %d";
241: $sql = $this->db->prepare($sql, $cfg['tab']['cat_lang'], $idcat, $idlang);
242: $this->db->query($sql);
243: return ($this->db->nextRecord())? $midcat : 0;
244: } else {
245:
246: return 0;
247: }
248: }
249:
250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270:
271: public function getAllChildCategoryIds($idcat, $idlang = null) {
272: global $cfg;
273:
274: $aCats = array();
275: $bLoop = true;
276: $db2 = $this->_getSecondDBInstance();
277:
278: $sql = "SELECT idcat FROM `%s` WHERE parentid = %d AND preid = 0";
279: $this->db->query($sql, $this->table, $idcat);
280: if ($this->db->nextRecord()) {
281: while ($bLoop) {
282: $midcat = $this->db->f('idcat');
283: if (null == $idlang) {
284: $aCats[] = $midcat;
285: } else {
286:
287: $sql = "SELECT idcatlang FROM `%s` WHERE idcat = %d AND idlang = %d";
288: $db2->query($sql, $cfg['tab']['cat_lang'], $midcat, $idlang);
289: if ($db2->nextRecord()) {
290: $aCats[] = $midcat;
291: }
292: }
293:
294: $sql = "SELECT idcat FROM `%s` WHERE parentid = %d AND preid = %d";
295: $this->db->query($sql, $this->table, $idcat, $midcat);
296: if (!$this->db->nextRecord()) {
297: $bLoop = false;
298: }
299: }
300: }
301: return $aCats;
302: }
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: public function getAllCategoryIdsRecursive($idcat, $idclient) {
335: global $cfg;
336:
337: $catList = array();
338: $openList = array();
339:
340: $openList[] = $idcat;
341:
342: while (($actId = array_pop($openList)) != null) {
343: if (in_array($actId, $catList)) {
344: continue;
345: }
346:
347: $catList[] = $actId;
348:
349: $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";
350: $sql = $this->db->prepare($sql, array(
351: 'cat_tree' => $cfg['tab']['cat_tree'],
352: 'cat' => $this->table,
353: 'parentid' => (int) $actId,
354: 'idclient' => (int) $idclient
355: ));
356: $this->db->query($sql);
357:
358: while ($this->db->nextRecord()) {
359: $openList[] = $this->db->f('idcat');
360: }
361: }
362:
363: return $catList;
364: }
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: public function getAllCategoryIdsRecursive2($idcat, $idclient) {
396: global $cfg;
397:
398: $aCats = array();
399: $found = false;
400: $curLevel = 0;
401:
402: $sql = "SELECT * FROM `%s` AS a, `%s` AS b WHERE a.idcat = b.idcat AND idclient = %d ORDER BY idtree";
403: $sql = $this->db->prepare($sql, $cfg['tab']['cat_tree'], $cfg['tab']['cat'], $idclient);
404: $this->db->query($sql);
405:
406: while ($this->db->nextRecord()) {
407: if ($found && $this->db->f('level') <= $curLevel) {
408:
409: $found = false;
410: }
411:
412: if ($this->db->f('idcat') == $idcat) {
413: $found = true;
414: $curLevel = $this->db->f('level');
415: }
416:
417: if ($found) {
418: $aCats[] = $this->db->f('idcat');
419: }
420: }
421:
422: return $aCats;
423: }
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: public function setField($name, $value, $safe = true) {
468: switch ($name) {
469: case 'idcat':
470: case 'idclient':
471: case 'parentid':
472: case 'preid':
473: case 'postid':
474: case 'status':
475: $value = (int) $value;
476: break;
477: }
478:
479: parent::setField($name, $value, $safe);
480: }
481:
482: 483: 484: 485: 486:
487: public function getLink($changeLangId = 0) {
488: if ($this->isLoaded() === false) {
489: return '';
490: }
491:
492: $options = array();
493: $options['idcat'] = $this->get('idcat');
494: $options['lang'] = ($changeLangId == 0) ? cRegistry::getLanguageId() : $changeLangId;
495: if ($changeLangId > 0) {
496: $options['changelang'] = $changeLangId;
497: }
498:
499:
500:
501: return cUri::getInstance()->build($options);
502: }
503:
504: }
505: