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
  • Smarty
    • Cacher
    • Compiler
    • Config
    • Debug
    • PluginsBlock
    • PluginsFilter
    • PluginsFunction
    • PluginsInternal
    • PluginsModifier
    • PluginsModifierCompiler
    • PluginsShared
    • Security
    • Template
    • TemplateResources
  • Swift
    • ByteStream
    • CharacterStream
    • Encoder
    • Events
    • KeyCache
    • Mailer
    • Mime
    • Plugins
    • Transport

Classes

  • Swift_FailoverTransport
  • Swift_LoadBalancedTransport
  • Swift_MailTransport
  • Swift_Plugins_Loggers_ArrayLogger
  • Swift_Plugins_Loggers_EchoLogger
  • Swift_SendmailTransport
  • Swift_SmtpTransport
  • Swift_Transport_AbstractSmtpTransport
  • Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  • Swift_Transport_Esmtp_Auth_LoginAuthenticator
  • Swift_Transport_Esmtp_Auth_PlainAuthenticator
  • Swift_Transport_Esmtp_AuthHandler
  • Swift_Transport_EsmtpTransport
  • Swift_Transport_FailoverTransport
  • Swift_Transport_LoadBalancedTransport
  • Swift_Transport_MailTransport
  • Swift_Transport_SendmailTransport
  • Swift_Transport_SimpleMailInvoker
  • Swift_Transport_StreamBuffer

Interfaces

  • Swift_Plugins_Logger
  • Swift_Plugins_Pop_Pop3Exception
  • Swift_Transport
  • Swift_Transport_Esmtp_Authenticator
  • Swift_Transport_EsmtpHandler
  • Swift_Transport_IoBuffer
  • Swift_Transport_MailInvoker
  • Swift_Transport_SmtpAgent
  • Swift_TransportException
  • Overview
  • Package
  • Function
  • Todo
  • Download
  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:         $depth = (int) $depth;
290: 
291:         $cfg = cRegistry::getConfig();
292: 
293:         $categories = array();
294: 
295:         $clientId = $this->getClientId();
296:         $languageId = $this->getLanguageId();
297: 
298:         $selectFields = "cat_tree.idcat, cat_tree.level";
299: 
300:         $useAuthorization = ($this->_auth !== NULL);
301: 
302:         if ($useAuthorization == true) {
303:             $selectFields .= ", cat_lang.public, cat_lang.idcatlang";
304:         }
305: 
306:         $sqlSnippetPublic = "cat_lang.public = 1 AND";
307:         if ($useAuthorization == true) {
308:             $sqlSnippetPublic = "";
309:         }
310: 
311:         $sql = 'SELECT
312:                     ' . $selectFields . '
313:                 FROM
314:                     ' . $cfg['tab']['cat_tree'] . ' AS cat_tree,
315:                     ' . $cfg['tab']['cat'] . ' AS cat,
316:                     ' . $cfg['tab']['cat_lang'] . ' AS cat_lang
317:                 WHERE
318:                     cat_tree.idcat    = cat.idcat AND
319:                     cat.idcat    = cat_lang.idcat AND
320:                     cat.idclient = ' . $clientId . ' AND
321:                     cat_lang.idlang   = ' . $languageId . ' AND
322:                     cat_lang.visible  = 1 AND ' . $sqlSnippetPublic . '
323:                     cat.parentid = ' . $categoryId . '
324:                 ORDER BY
325:                     cat_tree.idtree';
326: 
327:         $db = cRegistry::getDb();
328:         $db->query($sql);
329: 
330:         while ($db->nextRecord()) {
331:             $catId = (int) $db->f('idcat');
332:             $catLevel = (int) $db->f('level');
333: 
334:             if ($depth > 0 && ($depth > ($catLevel))) {
335:                 $subCategories = $this->getSubCategories($catId, $depth);
336:             } else {
337:                 $subCategories = array();
338:             }
339:             $categoryLanguage = new cApiCategoryLanguage();
340:             $categoryLanguage->loadByCategoryIdAndLanguageId($catId, $languageId);
341: 
342:             $category = array();
343:             $category['item'] = $categoryLanguage;
344:             $category['idcat'] = $catId;
345:             $category['level'] = $catLevel;
346:             $category['subcats'] = $subCategories;
347: 
348:             $this->_levelCache[$catId] = $catLevel;
349: 
350:             if ($this->hasCategoryAccess($categoryLanguage) === true) {
351:                 $categories[] = $category;
352:             }
353:         }
354: 
355:         return $categories;
356:     }
357: 
358:     /**
359:      * Checks if set auth object has access to the specific category.
360:      *
361:      * @param cApiCategoryLanguage $categoryLanguage category language object
362:      * @return bool result of access check
363:      */
364:     public function hasCategoryAccess(cApiCategoryLanguage $categoryLanguage) {
365:         $useAuthorization = ($this->_auth !== NULL && $this->_fePermColl !== NULL);
366: 
367:         if ($useAuthorization === false) {
368:             return true;
369:         }
370: 
371:         $perm = cRegistry::getPerm();
372: 
373:         if (intval($categoryLanguage->getField('public')) == 1) {
374:             return true;
375:         }
376: 
377:         $clientId = $this->getClientId();
378:         $languageId = $this->getLanguageId();
379: 
380:         if ($perm->have_perm_client_lang($clientId, $languageId) == true) {
381:             return true;
382:         }
383: 
384:         foreach ($this->_feGroups as $feGroup) {
385:             if ($this->_fePermColl->checkPerm($feGroup, 'category', 'access', $categoryLanguage->getField('idcatlang'), true)) {
386:                 return true;
387:             }
388:         }
389: 
390:         return false;
391:     }
392: }
CMS CONTENIDO 4.9.7 API documentation generated by ApiGen