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
    • NavigationMain
    • 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:      * Instance of the helper class.
 27:      * @var cCategoryHelper
 28:      */
 29:     static private $_instance = NULL;
 30: 
 31:     /**
 32:      * Local stored language ID
 33:      * @var    int    language ID
 34:      */
 35:     protected $_languageId = 0;
 36: 
 37:     /**
 38:      * Local stored client ID
 39:      * @var    int    client ID
 40:      */
 41:     protected $_clientId = 0;
 42: 
 43:     /**
 44:      * Local cache of category levels.
 45:      * @var    array
 46:      */
 47:     protected $_levelCache = array();
 48: 
 49:     /**
 50:      * Auth object to use.
 51:      * @var    cAuth
 52:      */
 53:     protected $_auth = NULL;
 54: 
 55:     /**
 56:      * Array with current frontend user groups.
 57:      * @var    array
 58:      */
 59:     protected $_feGroups = array();
 60: 
 61:     /**
 62:      * Object for frontend permission collection.
 63:      * @var    cApiFrontendPermissionCollection
 64:      */
 65:     protected $_fePermColl = NULL;
 66: 
 67:     /**
 68:      * Returns the instance of this class.
 69:      * @return    cCategoryHelper
 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:      * Constructor of the class.
 81:      */
 82:     protected function __construct() {
 83:     }
 84: 
 85:     /**
 86:      * Sets an auth object to use on category access check.
 87:      * @param    cAuth    $auth    auth object
 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:      * Sets the client ID to store it locally in the class.
102:      * @param    int    $clientId    client ID
103:      */
104:     public function setClientId($clientId = 0) {
105:         $this->_clientId = (int) $clientId;
106:     }
107: 
108:     /**
109:      * Returns the local stored client ID
110:      * @return    int    client ID
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:      * Sets the language ID to store it locally in the class.
127:      * @param    int    $languageId    language ID
128:      */
129:     public function setLanguageId($languageId = 0) {
130:         $this->_languageId = (int) $languageId;
131:     }
132: 
133:     /**
134:      * Returns the local stored language ID
135:      * @return    int    language ID
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:      * Return the ID of the top most category based on a given category ID.
152:      * @param    int    $categoryId    Base category ID to search on
153:      * @return    int    Top most category ID
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:      * Returns an array with ordered cApiCategoryLanguage objects e.g. for a breadcrumb.
169:      * @param    int    $categoryId    Last category ID in list.
170:      * @param    int    $startingLevel    Define here, at which level the list should start. (optional, default: 1)
171:      * @param    int    $maxDepth    Amount of the max depth of categories. (optional, default: 20)
172:      * @return    array    Array with cApiCategoryLanguage objects
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:      * Fetch all parent category IDs of a given category.
206:      * @param    int    $categoryId    Base category to search on.
207:      * @param    int    $maxDepth    Amount of the max depth of categories. (optional, default: 20)
208:      * @return    array    Array with parent category IDs.
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:      * Fetchs the level of a category by a given category ID.
231:      * @param    int    $categoryId    Category ID to fetch the level of.
232:      * @return    int    category level
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:      * Return the subcategories of the given category ID.
253:      * TODO: Use Generic DB instead of SQL queries
254:      * @param    int    $categoryId    ID of the category to load
255:      * @param    int    $depth    the maximum depth
256:      * @return    array    array with subcategories
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:      * Checks if set auth object has access to the specific category.
331:      * @param    cApiCategoryLanguage    $categoryLanguage    category language object
332:      * @return    bool    result of access check
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: }
CMS CONTENIDO 4.9.0 API documentation generated by ApiGen 2.8.0