Overview

Packages

  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Datatype
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
  • 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 auth object
 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:      * Returns the local stored client ID
112:      *
113:      * @throws cInvalidArgumentException if no active client ID specified or
114:      *         found
115:      * @return int 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 client ID
134:      */
135:     public function setClientId($clientId = 0) {
136:         $this->_clientId = (int) $clientId;
137:     }
138: 
139:     /**
140:      * Returns the local stored language ID
141:      *
142:      * @throws cInvalidArgumentException if no active language ID specified or
143:      *         found
144:      * @return int language ID
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:      * Sets the language ID to store it locally in the class.
161:      *
162:      * @param int $languageId language ID
163:      */
164:     public function setLanguageId($languageId = 0) {
165:         $this->_languageId = (int) $languageId;
166:     }
167: 
168:     /**
169:      * Return the ID of the top most category based on a given category ID.
170:      *
171:      * @param int $categoryId Base category ID to search on
172:      * @return int Top most category ID
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:      * Returns an array with ordered cApiCategoryLanguage objects e.g.
188:      * for a breadcrumb.
189:      *
190:      * @param int $categoryId Last category ID in list.
191:      * @param int $startingLevel Define here, at which level the list should
192:      *        start. (optional, default: 1)
193:      * @param int $maxDepth Amount of the max depth of categories. (optional,
194:      *        default: 20)
195:      * @return array Array with cApiCategoryLanguage objects
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:      * Fetch all parent category IDs of a given category.
229:      *
230:      * @param int $categoryId Base category to search on.
231:      * @param int $maxDepth Amount of the max depth of categories. (optional,
232:      *        default: 20)
233:      * @return array Array with parent category IDs.
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:      * Fetchs the level of a category by a given category ID.
256:      *
257:      * @param int $categoryId Category ID to fetch the level of.
258:      * @return int category level
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:      * Return the subcategories of the given category ID.
279:      * TODO: Use Generic DB instead of SQL queries
280:      *
281:      * @param int $categoryId ID of the category to load
282:      * @param int $depth the maximum depth
283:      * @return array array with subcategories
284:      */
285:     public function getSubCategories($categoryId, $depth) {
286:         if ((int) $categoryId <= 0 || (int) $depth < 0) {
287:             return array();
288:         }
289: 
290:         $cfg = cRegistry::getConfig();
291: 
292:         $categories = array();
293: 
294:         $clientId = $this->getClientId();
295:         $languageId = $this->getLanguageId();
296: 
297:         $selectFields = "cat_tree.idcat, cat_tree.level";
298: 
299:         $useAuthorization = ($this->_auth !== NULL);
300: 
301:         if ($useAuthorization == true) {
302:             $selectFields .= ", cat_lang.public, cat_lang.idcatlang";
303:         }
304: 
305:         $sqlSnippetPublic = "cat_lang.public = 1 AND";
306:         if ($useAuthorization == true) {
307:             $sqlSnippetPublic = "";
308:         }
309: 
310:         $sql = 'SELECT
311:                     ' . $selectFields . '
312:                 FROM
313:                     ' . $cfg['tab']['cat_tree'] . ' AS cat_tree,
314:                     ' . $cfg['tab']['cat'] . ' AS cat,
315:                     ' . $cfg['tab']['cat_lang'] . ' AS cat_lang
316:                 WHERE
317:                     cat_tree.idcat    = cat.idcat AND
318:                     cat.idcat    = cat_lang.idcat AND
319:                     cat.idclient = ' . $clientId . ' AND
320:                     cat_lang.idlang   = ' . $languageId . ' AND
321:                     cat_lang.visible  = 1 AND ' . $sqlSnippetPublic . '
322:                     cat.parentid = ' . $categoryId . '
323:                 ORDER BY
324:                     cat_tree.idtree';
325: 
326:         $db = cRegistry::getDb();
327:         $db->query($sql);
328: 
329:         $this->_subCategories = array();
330: 
331:         while ($db->nextRecord()) {
332:             $catId = (int) $db->f('idcat');
333:             $catLevel = (int) $db->f('level');
334: 
335:             if ($depth > 0 && $catLevel > $depth) {
336:                 break;
337:             }
338: 
339:             $subCategories = $this->getSubCategories($catId, $depth);
340:             $categoryLanguage = new cApiCategoryLanguage();
341:             $categoryLanguage->loadByCategoryIdAndLanguageId($catId, $languageId);
342: 
343:             $category = array();
344:             $category['item'] = $categoryLanguage;
345:             $category['idcat'] = $catId;
346:             $category['level'] = $catLevel;
347:             $category['subcats'] = $subCategories;
348: 
349:             $this->_levelCache[$catId] = $catLevel;
350: 
351:             if ($this->hasCategoryAccess($categoryLanguage) === true) {
352:                 $categories[] = $category;
353:             }
354:         }
355: 
356:         return $categories;
357:     }
358: 
359:     /**
360:      * Checks if set auth object has access to the specific category.
361:      *
362:      * @param cApiCategoryLanguage $categoryLanguage category language object
363:      * @return bool result of access check
364:      */
365:     public function hasCategoryAccess(cApiCategoryLanguage $categoryLanguage) {
366:         $useAuthorization = ($this->_auth !== NULL && $this->_fePermColl !== NULL);
367: 
368:         if ($useAuthorization === false) {
369:             return true;
370:         }
371: 
372:         $perm = cRegistry::getPerm();
373: 
374:         if (intval($categoryLanguage->getField('public')) == 1) {
375:             return true;
376:         }
377: 
378:         $clientId = $this->getClientId();
379:         $languageId = $this->getLanguageId();
380: 
381:         if ($perm->have_perm_client_lang($clientId, $languageId) == true) {
382:             return true;
383:         }
384: 
385:         foreach ($this->_feGroups as $feGroup) {
386:             if ($this->_fePermColl->checkPerm($feGroup, 'category', 'access', $categoryLanguage->getField('idcatlang'), true)) {
387:                 return true;
388:             }
389:         }
390: 
391:         return false;
392:     }
393: }
CMS CONTENIDO 4.9.3 API documentation generated by ApiGen 2.8.0