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 cCategoryHelper {
25:
26: 27: 28: 29: 30:
31: private static $_instance = NULL;
32:
33: 34: 35: 36: 37:
38: protected $_languageId = 0;
39:
40: 41: 42: 43: 44:
45: protected $_clientId = 0;
46:
47: 48: 49: 50: 51:
52: protected $_levelCache = array();
53:
54: 55: 56: 57: 58:
59: protected $_auth = NULL;
60:
61: 62: 63: 64: 65:
66: protected $_feGroups = array();
67:
68: 69: 70: 71: 72:
73: protected $_fePermColl = NULL;
74:
75: 76: 77: 78: 79:
80: public static function getInstance() {
81: if (self::$_instance === NULL) {
82: self::$_instance = new self();
83: }
84:
85: return self::$_instance;
86: }
87:
88: 89: 90:
91: protected function __construct() {
92: }
93:
94: 95: 96: 97: 98:
99: public function setAuth($auth) {
100: $this->_auth = $auth;
101:
102: $feUser = new cApiFrontendUser($auth->auth['uid']);
103: if ($feUser->isLoaded() === true) {
104: $this->_feGroups = $feUser->getGroupsForUser();
105: }
106:
107: $this->_fePermColl = new cApiFrontendPermissionCollection();
108: }
109:
110: 111: 112: 113: 114: 115: 116:
117: public function getClientId() {
118: if ($this->_clientId == 0) {
119: $clientId = cRegistry::getClientId();
120: if ($clientId == 0) {
121: throw new cInvalidArgumentException('No active client ID specified or found.');
122: }
123:
124: return $clientId;
125: }
126:
127: return $this->_clientId;
128: }
129:
130: 131: 132: 133: 134:
135: public function setClientId($clientId = 0) {
136: $this->_clientId = (int) $clientId;
137: }
138:
139: 140: 141: 142: 143: 144: 145:
146: public function getLanguageId() {
147: if ($this->_languageId == 0) {
148: $languageId = cRegistry::getLanguageId();
149: if ($languageId == 0) {
150: throw new cInvalidArgumentException('No active language ID specified or found.');
151: }
152:
153: return $languageId;
154: }
155:
156: return $this->_languageId;
157: }
158:
159: 160: 161: 162: 163:
164: public function setLanguageId($languageId = 0) {
165: $this->_languageId = (int) $languageId;
166: }
167:
168: 169: 170: 171: 172: 173:
174: public function getTopMostCategoryId($categoryId) {
175: $category = new cApiCategory($categoryId);
176:
177: if ($category->get('parentid') == 0) {
178: $topMostCategoryId = $categoryId;
179: } else {
180: $topMostCategoryId = $this->getTopMostCategoryId($category->get('parentid'));
181: }
182:
183: return $topMostCategoryId;
184: }
185:
186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196:
197: public function getCategoryPath($categoryId, $startingLevel = 1, $maxDepth = 20) {
198: $clientId = $this->getClientId();
199: $languageId = $this->getLanguageId();
200:
201: $categories = array();
202:
203: $categoryLanguage = new cApiCategoryLanguage();
204: $categoryLanguage->loadByCategoryIdAndLanguageId($categoryId, $languageId);
205:
206: if ($this->hasCategoryAccess($categoryLanguage) === true) {
207: $categories[] = $categoryLanguage;
208: }
209:
210: $parentCategoryIds = $this->getParentCategoryIds($categoryId, $maxDepth);
211: foreach ($parentCategoryIds as $parentCategoryId) {
212: $categoryLanguage = new cApiCategoryLanguage();
213: $categoryLanguage->loadByCategoryIdAndLanguageId($parentCategoryId, $languageId);
214:
215: if ($this->hasCategoryAccess($categoryLanguage) === true) {
216: $categories[] = $categoryLanguage;
217: }
218: }
219:
220: for ($removeCount = 2; $removeCount <= $startingLevel; $removeCount++) {
221: array_pop($categories);
222: }
223:
224: return array_reverse($categories);
225: }
226:
227: 228: 229: 230: 231: 232: 233: 234:
235: public function getParentCategoryIds($categoryId, $maxDepth = 20) {
236: $categoryIds = array();
237:
238: $nextCategoryId = $categoryId;
239:
240: $categoryCount = 1;
241: while ($nextCategoryId != 0 && $categoryCount < $maxDepth) {
242: $category = new cApiCategory($nextCategoryId);
243:
244: $nextCategoryId = $category->get('parentid');
245: if ($nextCategoryId != 0) {
246: $categoryIds[] = $nextCategoryId;
247: }
248: $categoryCount++;
249: }
250:
251: return $categoryIds;
252: }
253:
254: 255: 256: 257: 258: 259:
260: public function getCategoryLevel($categoryId) {
261: if (isset($this->_levelCache[$categoryId]) === false) {
262: $categoryTree = new cApiCategoryTree();
263: $categoryTree->loadBy("idcat", $categoryId);
264:
265: if ($categoryTree->isLoaded() === false) {
266: return -1;
267: }
268:
269: $level = $categoryTree->get('level');
270:
271: $this->_levelCache[$categoryId] = $level;
272: }
273:
274: return $this->_levelCache[$categoryId];
275: }
276:
277: 278: 279: 280: 281: 282: 283: 284:
285: public function getSubCategories($categoryId, $depth) {
286: if ((int) $categoryId <= 0 || (int) $depth < 0) {
287: return array();
288: }
289:
290: $cfg = cRegistry::getConfig();
291:
292: $categories = array();
293:
294: $clientId = $this->getClientId();
295: $languageId = $this->getLanguageId();
296:
297: $selectFields = "cat_tree.idcat, cat_tree.level";
298:
299: $useAuthorization = ($this->_auth !== NULL);
300:
301: if ($useAuthorization == true) {
302: $selectFields .= ", cat_lang.public, cat_lang.idcatlang";
303: }
304:
305: $sqlSnippetPublic = "cat_lang.public = 1 AND";
306: if ($useAuthorization == true) {
307: $sqlSnippetPublic = "";
308: }
309:
310: $sql = 'SELECT
311: ' . $selectFields . '
312: FROM
313: ' . $cfg['tab']['cat_tree'] . ' AS cat_tree,
314: ' . $cfg['tab']['cat'] . ' AS cat,
315: ' . $cfg['tab']['cat_lang'] . ' AS cat_lang
316: WHERE
317: cat_tree.idcat = cat.idcat AND
318: cat.idcat = cat_lang.idcat AND
319: cat.idclient = ' . $clientId . ' AND
320: cat_lang.idlang = ' . $languageId . ' AND
321: cat_lang.visible = 1 AND ' . $sqlSnippetPublic . '
322: cat.parentid = ' . $categoryId . '
323: ORDER BY
324: cat_tree.idtree';
325:
326: $db = cRegistry::getDb();
327: $db->query($sql);
328:
329: $this->_subCategories = array();
330:
331: while ($db->nextRecord()) {
332: $catId = (int) $db->f('idcat');
333: $catLevel = (int) $db->f('level');
334:
335: if ($depth > 0 && $catLevel > $depth) {
336: break;
337: }
338:
339: $subCategories = $this->getSubCategories($catId, $depth);
340: $categoryLanguage = new cApiCategoryLanguage();
341: $categoryLanguage->loadByCategoryIdAndLanguageId($catId, $languageId);
342:
343: $category = array();
344: $category['item'] = $categoryLanguage;
345: $category['idcat'] = $catId;
346: $category['level'] = $catLevel;
347: $category['subcats'] = $subCategories;
348:
349: $this->_levelCache[$catId] = $catLevel;
350:
351: if ($this->hasCategoryAccess($categoryLanguage) === true) {
352: $categories[] = $category;
353: }
354: }
355:
356: return $categories;
357: }
358:
359: 360: 361: 362: 363: 364:
365: public function hasCategoryAccess(cApiCategoryLanguage $categoryLanguage) {
366: $useAuthorization = ($this->_auth !== NULL && $this->_fePermColl !== NULL);
367:
368: if ($useAuthorization === false) {
369: return true;
370: }
371:
372: $perm = cRegistry::getPerm();
373:
374: if (intval($categoryLanguage->getField('public')) == 1) {
375: return true;
376: }
377:
378: $clientId = $this->getClientId();
379: $languageId = $this->getLanguageId();
380:
381: if ($perm->have_perm_client_lang($clientId, $languageId) == true) {
382: return true;
383: }
384:
385: foreach ($this->_feGroups as $feGroup) {
386: if ($this->_fePermColl->checkPerm($feGroup, 'category', 'access', $categoryLanguage->getField('idcatlang'), true)) {
387: return true;
388: }
389: }
390:
391: return false;
392: }
393: }