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:  * @version SVN Revision $Rev:$
  8:  *
  9:  * @author Dominik Ziegler
 10:  * @copyright four for business AG <www.4fb.de>
 11:  * @license http://www.contenido.org/license/LIZENZ.txt
 12:  * @link http://www.4fb.de
 13:  * @link http://www.contenido.org
 14:  */
 15: 
 16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 17: 
 18: /**
 19:  * This class contains functions for the category helper class in CONTENIDO.
 20:  *
 21:  * @package Core
 22:  * @subpackage Frontend_Util
 23:  */
 24: class cCategoryHelper {
 25: 
 26:     /**
 27:      * Instance of the helper class.
 28:      *
 29:      * @var cCategoryHelper
 30:      */
 31:     private static $_instance = NULL;
 32: 
 33:     /**
 34:      * Local stored language ID
 35:      *
 36:      * @var int language ID
 37:      */
 38:     protected $_languageId = 0;
 39: 
 40:     /**
 41:      * Local stored client ID
 42:      *
 43:      * @var int client ID
 44:      */
 45:     protected $_clientId = 0;
 46: 
 47:     /**
 48:      * Local cache of category levels.
 49:      *
 50:      * @var array
 51:      */
 52:     protected $_levelCache = array();
 53: 
 54:     /**
 55:      * Auth object to use.
 56:      *
 57:      * @var cAuth
 58:      */
 59:     protected $_auth = NULL;
 60: 
 61:     /**
 62:      * Array with current frontend user groups.
 63:      *
 64:      * @var array
 65:      */
 66:     protected $_feGroups = array();
 67: 
 68:     /**
 69:      * Object for frontend permission collection.
 70:      *
 71:      * @var cApiFrontendPermissionCollection
 72:      */
 73:     protected $_fePermColl = NULL;
 74: 
 75:     /**
 76:      * Returns the instance of this class.
 77:      *
 78:      * @return cCategoryHelper
 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:      * Constructor of the class.
 90:      */
 91:     protected function __construct() {
 92:     }
 93: 
 94:     /**
 95:      * Sets an auth object to use on category access check.
 96:      *
 97:      * @param cAuth $auth
 98:      *         auth object
 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:      * Returns the local stored client ID
113:      *
114:      * @throws cInvalidArgumentException if no active client ID specified or
115:      *         found
116:      * @return int
117:      *         client ID
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:      * Sets the client ID to store it locally in the class.
134:      *
135:      * @param int $clientId [optional]
136:      *         client ID
137:      */
138:     public function setClientId($clientId = 0) {
139:         $this->_clientId = (int) $clientId;
140:     }
141: 
142:     /**
143:      * Returns the local stored language ID
144:      *
145:      * @throws cInvalidArgumentException
146:      *         if no active language ID specified or found
147:      * @return int
148:      *         language ID
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:      * Sets the language ID to store it locally in the class.
165:      *
166:      * @param int $languageId [optional]
167:      *         language ID
168:      */
169:     public function setLanguageId($languageId = 0) {
170:         $this->_languageId = (int) $languageId;
171:     }
172: 
173:     /**
174:      * Return the ID of the top most category based on a given category ID.
175:      *
176:      * @param int $categoryId
177:      *         Base category ID to search on
178:      * @return int
179:      *         Top most category ID
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:      * Returns an array with ordered cApiCategoryLanguage objects e.g.
195:      * for a breadcrumb.
196:      *
197:      * @param int $categoryId
198:      *         Last category ID in list.
199:      * @param int $startingLevel [optional, default: 1]
200:      *         Define here, at which level the list should start.
201:      * @param int $maxDepth [optional, default: 20]
202:      *         Amount of the max depth of categories.
203:      * @return array
204:      *         Array with cApiCategoryLanguage objects
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:      * Fetch all parent category IDs of a given category.
238:      *
239:      * @param int $categoryId
240:      *         Base category to search on.
241:      * @param int $maxDepth [optional, default: 20]
242:      *         Amount of the max depth of categories.
243:      * @return array
244:      *         Array with parent category IDs.
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:      * Fetchs the level of a category by a given category ID.
267:      *
268:      * @param int $categoryId
269:      *         Category ID to fetch the level of.
270:      * @return int
271:      *         category level
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:      * Return the subcategories of the given category ID.
292:      * TODO: Use Generic DB instead of SQL queries
293:      *
294:      * @param int $categoryId
295:      *         ID of the category to load
296:      * @param int $depth
297:      *         the maximum depth
298:      * @return array
299:      *         array with subcategories
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:      * Checks if set auth object has access to the specific category.
376:      *
377:      * @param cApiCategoryLanguage $categoryLanguage
378:      *         category language object
379:      * @return bool
380:      *         result of access check
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: }
CMS CONTENIDO 4.9.8 API documentation generated by ApiGen 2.8.0