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