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: $depth = (int) $depth;
290:
291: $cfg = cRegistry::getConfig();
292:
293: $categories = array();
294:
295: $clientId = $this->getClientId();
296: $languageId = $this->getLanguageId();
297:
298: $selectFields = "cat_tree.idcat, cat_tree.level";
299:
300: $useAuthorization = ($this->_auth !== NULL);
301:
302: if ($useAuthorization == true) {
303: $selectFields .= ", cat_lang.public, cat_lang.idcatlang";
304: }
305:
306: $sqlSnippetPublic = "cat_lang.public = 1 AND";
307: if ($useAuthorization == true) {
308: $sqlSnippetPublic = "";
309: }
310:
311: $sql = 'SELECT
312: ' . $selectFields . '
313: FROM
314: ' . $cfg['tab']['cat_tree'] . ' AS cat_tree,
315: ' . $cfg['tab']['cat'] . ' AS cat,
316: ' . $cfg['tab']['cat_lang'] . ' AS cat_lang
317: WHERE
318: cat_tree.idcat = cat.idcat AND
319: cat.idcat = cat_lang.idcat AND
320: cat.idclient = ' . $clientId . ' AND
321: cat_lang.idlang = ' . $languageId . ' AND
322: cat_lang.visible = 1 AND ' . $sqlSnippetPublic . '
323: cat.parentid = ' . $categoryId . '
324: ORDER BY
325: cat_tree.idtree';
326:
327: $db = cRegistry::getDb();
328: $db->query($sql);
329:
330: while ($db->nextRecord()) {
331: $catId = (int) $db->f('idcat');
332: $catLevel = (int) $db->f('level');
333:
334: if ($depth > 0 && ($depth > ($catLevel))) {
335: $subCategories = $this->getSubCategories($catId, $depth);
336: } else {
337: $subCategories = array();
338: }
339: $categoryLanguage = new cApiCategoryLanguage();
340: $categoryLanguage->loadByCategoryIdAndLanguageId($catId, $languageId);
341:
342: $category = array();
343: $category['item'] = $categoryLanguage;
344: $category['idcat'] = $catId;
345: $category['level'] = $catLevel;
346: $category['subcats'] = $subCategories;
347:
348: $this->_levelCache[$catId] = $catLevel;
349:
350: if ($this->hasCategoryAccess($categoryLanguage) === true) {
351: $categories[] = $category;
352: }
353: }
354:
355: return $categories;
356: }
357:
358: 359: 360: 361: 362: 363:
364: public function hasCategoryAccess(cApiCategoryLanguage $categoryLanguage) {
365: $useAuthorization = ($this->_auth !== NULL && $this->_fePermColl !== NULL);
366:
367: if ($useAuthorization === false) {
368: return true;
369: }
370:
371: $perm = cRegistry::getPerm();
372:
373: if (intval($categoryLanguage->getField('public')) == 1) {
374: return true;
375: }
376:
377: $clientId = $this->getClientId();
378: $languageId = $this->getLanguageId();
379:
380: if ($perm->have_perm_client_lang($clientId, $languageId) == true) {
381: return true;
382: }
383:
384: foreach ($this->_feGroups as $feGroup) {
385: if ($this->_fePermColl->checkPerm($feGroup, 'category', 'access', $categoryLanguage->getField('idcatlang'), true)) {
386: return true;
387: }
388: }
389:
390: return false;
391: }
392: }