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