Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cCategoryHelper
  • cFrontendHelper
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * This file contains the category helper class.
  4:  *
  5:  * @package Core
  6:  * @subpackage Frontend_Util
  7:  * @author Dominik Ziegler
  8:  * @copyright four for business AG <www.4fb.de>
  9:  * @license http://www.contenido.org/license/LIZENZ.txt
 10:  * @link http://www.4fb.de
 11:  * @link http://www.contenido.org
 12:  */
 13: 
 14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 15: 
 16: /**
 17:  * This class contains functions for the category helper class in CONTENIDO.
 18:  *
 19:  * @package Core
 20:  * @subpackage Frontend_Util
 21:  */
 22: class cCategoryHelper {
 23: 
 24:     /**
 25:      * Instance of the helper class.
 26:      *
 27:      * @var cCategoryHelper
 28:      */
 29:     private static $_instance = NULL;
 30: 
 31:     /**
 32:      * Local stored language ID
 33:      *
 34:      * @var int language ID
 35:      */
 36:     protected $_languageId = 0;
 37: 
 38:     /**
 39:      * Local stored client ID
 40:      *
 41:      * @var int client ID
 42:      */
 43:     protected $_clientId = 0;
 44: 
 45:     /**
 46:      * Local cache of category levels.
 47:      *
 48:      * @var array
 49:      */
 50:     protected $_levelCache = array();
 51: 
 52:     /**
 53:      * Auth object to use.
 54:      *
 55:      * @var cAuth
 56:      */
 57:     protected $_auth = NULL;
 58: 
 59:     /**
 60:      * Array with current frontend user groups.
 61:      *
 62:      * @var array
 63:      */
 64:     protected $_feGroups = array();
 65: 
 66:     /**
 67:      * Object for frontend permission collection.
 68:      *
 69:      * @var cApiFrontendPermissionCollection
 70:      */
 71:     protected $_fePermColl = NULL;
 72: 
 73:     /**
 74:      * Returns the instance of this class.
 75:      *
 76:      * @return cCategoryHelper
 77:      */
 78:     public static function getInstance() {
 79:         if (self::$_instance === NULL) {
 80:             self::$_instance = new self();
 81:         }
 82: 
 83:         return self::$_instance;
 84:     }
 85: 
 86:     /**
 87:      * Constructor to create an instance of this class.
 88:      */
 89:     protected function __construct() {
 90:     }
 91: 
 92:     /**
 93:      * Sets an auth object to use on category access check.
 94:      *
 95:      * @param cAuth $auth
 96:      *         auth object
 97:      */
 98:     public function setAuth($auth) {
 99:         $this->_auth = $auth;
100: 
101:         $feUser = new cApiFrontendUser($auth->auth['uid']);
102:         if ($feUser->isLoaded() === true) {
103:             $this->_feGroups = $feUser->getGroupsForUser();
104:         }
105: 
106:         $this->_fePermColl = new cApiFrontendPermissionCollection();
107:     }
108: 
109:     /**
110:      * Returns the local stored client ID
111:      *
112:      * @throws cInvalidArgumentException if no active client ID specified or
113:      *         found
114:      * @return int
115:      *         client ID
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:      * Sets the client ID to store it locally in the class.
132:      *
133:      * @param int $clientId [optional]
134:      *         client ID
135:      */
136:     public function setClientId($clientId = 0) {
137:         $this->_clientId = (int) $clientId;
138:     }
139: 
140:     /**
141:      * Returns the local stored language ID
142:      *
143:      * @throws cInvalidArgumentException
144:      *         if no active language ID specified or found
145:      * @return int
146:      *         language ID
147:      */
148:     public function getLanguageId() {
149:         if ($this->_languageId == 0) {
150:             $languageId = cRegistry::getLanguageId();
151:             if ($languageId == 0) {
152:                 throw new cInvalidArgumentException('No active language ID specified or found.');
153:             }
154: 
155:             return $languageId;
156:         }
157: 
158:         return $this->_languageId;
159:     }
160: 
161:     /**
162:      * Sets the language ID to store it locally in the class.
163:      *
164:      * @param int $languageId [optional]
165:      *         language ID
166:      */
167:     public function setLanguageId($languageId = 0) {
168:         $this->_languageId = (int) $languageId;
169:     }
170: 
171:     /**
172:      * Return the ID of the top most category based on a given category ID.
173:      *
174:      * @param int $categoryId
175:      *         Base category ID to search on
176:      * @return int
177:      *         Top most category ID
178:      */
179:     public function getTopMostCategoryId($categoryId) {
180:         $category = new cApiCategory($categoryId);
181: 
182:         if ($category->get('parentid') == 0) {
183:             $topMostCategoryId = $categoryId;
184:         } else {
185:             $topMostCategoryId = $this->getTopMostCategoryId($category->get('parentid'));
186:         }
187: 
188:         return $topMostCategoryId;
189:     }
190: 
191:     /**
192:      * Returns an array with ordered cApiCategoryLanguage objects e.g.
193:      * for a breadcrumb.
194:      *
195:      * @param int $categoryId
196:      *         Last category ID in list.
197:      * @param int $startingLevel [optional, default: 1]
198:      *         Define here, at which level the list should start.
199:      * @param int $maxDepth [optional, default: 20]
200:      *         Amount of the max depth of categories.
201:      * @return array
202:      *         Array with cApiCategoryLanguage objects
203:      */
204:     public function getCategoryPath($categoryId, $startingLevel = 1, $maxDepth = 20) {
205:         $clientId = $this->getClientId();
206:         $languageId = $this->getLanguageId();
207: 
208:         $categories = array();
209: 
210:         $categoryLanguage = new cApiCategoryLanguage();
211:         $categoryLanguage->loadByCategoryIdAndLanguageId($categoryId, $languageId);
212: 
213:         if ($this->hasCategoryAccess($categoryLanguage) === true) {
214:             $categories[] = $categoryLanguage;
215:         }
216: 
217:         $parentCategoryIds = $this->getParentCategoryIds($categoryId, $maxDepth);
218:         foreach ($parentCategoryIds as $parentCategoryId) {
219:             $categoryLanguage = new cApiCategoryLanguage();
220:             $categoryLanguage->loadByCategoryIdAndLanguageId($parentCategoryId, $languageId);
221: 
222:             if ($this->hasCategoryAccess($categoryLanguage) === true) {
223:                 $categories[] = $categoryLanguage;
224:             }
225:         }
226: 
227:         for ($removeCount = 2; $removeCount <= $startingLevel; $removeCount++) {
228:             array_pop($categories);
229:         }
230: 
231:         return array_reverse($categories);
232:     }
233: 
234:     /**
235:      * Fetch all parent category IDs of a given category.
236:      *
237:      * @param int $categoryId
238:      *         Base category to search on.
239:      * @param int $maxDepth [optional, default: 20]
240:      *         Amount of the max depth of categories.
241:      * @return array
242:      *         Array with parent category IDs.
243:      */
244:     public function getParentCategoryIds($categoryId, $maxDepth = 20) {
245:         $categoryIds = array();
246: 
247:         $nextCategoryId = $categoryId;
248: 
249:         $categoryCount = 1;
250:         while ($nextCategoryId != 0 && $categoryCount < $maxDepth) {
251:             $category = new cApiCategory($nextCategoryId);
252: 
253:             $nextCategoryId = $category->get('parentid');
254:             if ($nextCategoryId != 0) {
255:                 $categoryIds[] = $nextCategoryId;
256:             }
257:             $categoryCount++;
258:         }
259: 
260:         return $categoryIds;
261:     }
262: 
263:     /**
264:      * Fetchs the level of a category by a given category ID.
265:      *
266:      * @param int $categoryId
267:      *         Category ID to fetch the level of.
268:      * @return int
269:      *         category level
270:      */
271:     public function getCategoryLevel($categoryId) {
272:         if (isset($this->_levelCache[$categoryId]) === false) {
273:             $categoryTree = new cApiCategoryTree();
274:             $categoryTree->loadBy("idcat", $categoryId);
275: 
276:             if ($categoryTree->isLoaded() === false) {
277:                 return -1;
278:             }
279: 
280:             $level = $categoryTree->get('level');
281: 
282:             $this->_levelCache[$categoryId] = $level;
283:         }
284: 
285:         return $this->_levelCache[$categoryId];
286:     }
287: 
288:     /**
289:      * Return the subcategories of the given category ID.
290:      * TODO: Use Generic DB instead of SQL queries
291:      *
292:      * @param int $categoryId
293:      *         ID of the category to load
294:      * @param int $depth
295:      *         the maximum depth
296:      * @return array
297:      *         array with subcategories
298:      */
299:     public function getSubCategories($categoryId, $depth) {
300:         if ((int) $categoryId <= 0 || (int) $depth < 0) {
301:             return array();
302:         }
303:         $depth = (int) $depth;
304: 
305:         $cfg = cRegistry::getConfig();
306: 
307:         $categories = array();
308: 
309:         $clientId = $this->getClientId();
310:         $languageId = $this->getLanguageId();
311: 
312:         $selectFields = "cat_tree.idcat, cat_tree.level";
313: 
314:         $useAuthorization = ($this->_auth !== NULL);
315: 
316:         if ($useAuthorization == true) {
317:             $selectFields .= ", cat_lang.public, cat_lang.idcatlang";
318:         }
319: 
320:         $sqlSnippetPublic = "cat_lang.public = 1 AND";
321:         if ($useAuthorization == true) {
322:             $sqlSnippetPublic = "";
323:         }
324: 
325:         $sql = 'SELECT
326:                     ' . $selectFields . '
327:                 FROM
328:                     ' . $cfg['tab']['cat_tree'] . ' AS cat_tree,
329:                     ' . $cfg['tab']['cat'] . ' AS cat,
330:                     ' . $cfg['tab']['cat_lang'] . ' AS cat_lang
331:                 WHERE
332:                     cat_tree.idcat    = cat.idcat AND
333:                     cat.idcat    = cat_lang.idcat AND
334:                     cat.idclient = ' . $clientId . ' AND
335:                     cat_lang.idlang   = ' . $languageId . ' AND
336:                     cat_lang.visible  = 1 AND ' . $sqlSnippetPublic . '
337:                     cat.parentid = ' . $categoryId . '
338:                 ORDER BY
339:                     cat_tree.idtree';
340: 
341:         $db = cRegistry::getDb();
342:         $db->query($sql);
343: 
344:         while ($db->nextRecord()) {
345:             $catId = (int) $db->f('idcat');
346:             $catLevel = (int) $db->f('level');
347: 
348:             if ($depth > 0 && ($depth > ($catLevel))) {
349:                 $subCategories = $this->getSubCategories($catId, $depth);
350:             } else {
351:                 $subCategories = array();
352:             }
353:             $categoryLanguage = new cApiCategoryLanguage();
354:             $categoryLanguage->loadByCategoryIdAndLanguageId($catId, $languageId);
355: 
356:             $category = array();
357:             $category['item'] = $categoryLanguage;
358:             $category['idcat'] = $catId;
359:             $category['level'] = $catLevel;
360:             $category['subcats'] = $subCategories;
361: 
362:             $this->_levelCache[$catId] = $catLevel;
363: 
364:             if ($this->hasCategoryAccess($categoryLanguage) === true) {
365:                 $categories[] = $category;
366:             }
367:         }
368: 
369:         return $categories;
370:     }
371: 
372:     /**
373:      * Checks if set auth object has access to the specific category.
374:      *
375:      * @param cApiCategoryLanguage $categoryLanguage
376:      *         category language object
377:      * @return bool
378:      *         result of access check
379:      */
380:     public function hasCategoryAccess(cApiCategoryLanguage $categoryLanguage) {
381:         $useAuthorization = ($this->_auth !== NULL && $this->_fePermColl !== NULL);
382: 
383:         if ($useAuthorization === false) {
384:             return true;
385:         }
386: 
387:         $perm = cRegistry::getPerm();
388: 
389:         if (intval($categoryLanguage->getField('public')) == 1) {
390:             return true;
391:         }
392: 
393:         $clientId = $this->getClientId();
394:         $languageId = $this->getLanguageId();
395: 
396:         if ($perm->have_perm_client_lang($clientId, $languageId) == true) {
397:             return true;
398:         }
399: 
400:         foreach ($this->_feGroups as $feGroup) {
401:             if ($this->_fePermColl->checkPerm($feGroup, 'category', 'access', $categoryLanguage->getField('idcatlang'), true)) {
402:                 return true;
403:             }
404:         }
405: 
406:         return false;
407:     }
408: }
CMS CONTENIDO 4.9.11 API documentation generated by ApiGen 2.8.0