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

  • cAjaxRequest
  • cAutoload
  • cBackend
  • cEffectiveSetting
  • cGuiScrollListAlltranslations
  • cHTMLValidator
  • cMailer
  • cModuleFileTranslation
  • cModuleHandler
  • cModuleSearch
  • cModuleSynchronizer
  • cModuleTemplateHandler
  • CodeMirror
  • cPasswordRequest
  • cPermission
  • cRegistry
  • cSystemPurge
  • cSystemtest
  • cTinymce4Configuration
  • cTinyMCE4Editor
  • cTinyMCEEditor
  • cWYSIWYGEditor
  • FrontendList
  • HtmlParser
  • TODOBackendList
  • TreeItem
  • UploadList
  • UploadSearchResultList

Functions

  • addArtspec
  • addSortImages
  • backToMainArea
  • buildArticleSelect
  • buildCategorySelect
  • buildCategorySelectRights
  • buildHeapTable
  • buildStackString
  • buildTree
  • buildUserOrGroupPermsFromRequest
  • callPluginStore
  • cApiCatGetLevelNode
  • cApiImageCheckCachedImageValidity
  • cApiImageCheckImageEditingPosibility
  • cApiImageGetCacheFileName
  • cApiImageGetTargetDimensions
  • cApiImageIsAnimGif
  • cApiImgScale
  • cApiImgScaleGetMD5CacheFile
  • cApiImgScaleHQ
  • cApiImgScaleImageMagick
  • cApiImgScaleLQ
  • cApiIsImageMagickAvailable
  • cApiStrCleanURLCharacters
  • cApiStrNormalizeLineEndings
  • cApiStrRecodeString
  • cApiStrReplaceDiacritics
  • cApiStrTrimAfterWord
  • cApiStrTrimHard
  • cApiStrTrimSentence
  • cDeprecated
  • cDie
  • cError
  • checkLangInClients
  • checkPathInformation
  • cInclude
  • compareUrlStrings
  • conChangeTemplateForCat
  • conCopyArticle
  • conCopyArtLang
  • conCopyContainerConf
  • conCopyContent
  • conCopyMetaTags
  • conCopyTemplateConfiguration
  • conCreateLocationString
  • conDeeperCategoriesArray
  • conDeleteart
  • conEditArt
  • conEditFirstTime
  • conFetchCategoryTree
  • conFlagOnOffline
  • conGenerateCode
  • conGenerateCodeForAllArts
  • conGenerateCodeForAllArtsInCategory
  • conGenerateCodeForAllartsUsingLayout
  • conGenerateCodeForAllartsUsingMod
  • conGenerateCodeForAllArtsUsingTemplate
  • conGenerateCodeForArtInAllCategories
  • conGenerateCodeForClient
  • conGenerateKeywords
  • conGetAvailableMetaTagTypes
  • conGetCategoryArticleId
  • conGetCategoryAssignments
  • conGetContainerConfiguration
  • conGetContentFromArticle
  • conGetHtmlTranslationTable
  • conGetMetaValue
  • conGetTemplateConfigurationIdForArticle
  • conGetTemplateConfigurationIdForCategory
  • conGetTopmostCat
  • conGetUsedModules
  • conHtmlentities
  • conHtmlEntityDecode
  • conHtmlSpecialChars
  • conIsLocked
  • conLock
  • conLockBulkEditing
  • conMakeArticleIndex
  • conMakeCatOnline
  • conMakeInlineScript
  • conMakeOnline
  • conMakeOnlineBulkEditing
  • conMakePublic
  • conMakeStart
  • conMoveArticles
  • conPhp54Check
  • conRemoveOldCategoryArticle
  • conSaveContentEntry
  • conSetCodeFlag
  • conSetCodeFlagBulkEditing
  • conSetMetaValue
  • conSyncArticle
  • copyRightsForElement
  • createBulkEditingFunction
  • createRandomName
  • createRightsForElement
  • cWarning
  • dbGetColumns
  • dbGetIndexes
  • dbGetPrimaryKeyName
  • dbTableExists
  • dbUpgradeTable
  • defineIfNotDefined
  • deleteArtspec
  • deleteRightsForElement
  • deleteSystemProperty
  • displayDatetime
  • emptyLogFile
  • endAndLogTiming
  • extractNumber
  • generateDisplayFilePath
  • generateJs
  • getAllClientsAndLanguages
  • getArtLang
  • getArtspec
  • getAvailableContentTypes
  • getCanonicalDay
  • getCanonicalMonth
  • getDirectorySize
  • getEffectiveSetting
  • getEffectiveSettingsByType
  • getEncodingByLanguage
  • getFileContents
  • getFileInformation
  • getFileType
  • getGroupOrUserName
  • getIDForArea
  • getJsHelpContext
  • getLanguageNamesByClient
  • getLanguagesByClient
  • getmicrotime
  • getNamedFrame
  • getParam
  • getParentAreaId
  • getSearchResults
  • getStrExpandCollapseButton
  • getSystemProperties
  • getSystemPropertiesByType
  • getSystemProperty
  • getTemplateSelect
  • getUplExpandCollapseButton
  • htmldecode
  • htmlentities_iso88592
  • humanReadableSize
  • includePlugins
  • insertEmptyStrRow
  • ipMatch
  • isAlphanumeric
  • isArchive
  • isArtInMultipleUse
  • isFunctionDisabled
  • isGroup
  • isIPv4
  • isRunningFromWeb
  • isStartArticle
  • isUtf8
  • isValidMail
  • langActivateDeactivateLanguage
  • langDeleteLanguage
  • langEditLanguage
  • langGetTextDirection
  • langNewLanguage
  • langRenameLanguage
  • layDeleteLayout
  • layEditLayout
  • machineReadableSize
  • mailLogBulkEditingFunctions
  • mailLogDecodeAddresses
  • markSubMenuItem
  • mask
  • modDeleteModule
  • modEditModule
  • phpInfoToHtml
  • plugin_include
  • prCreateURLNameLocationString
  • prDeleteCacheFileContent
  • prGetCacheFileContent
  • prResolvePathViaCategoryNames
  • prResolvePathViaURLNames
  • prWriteCacheFileContent
  • putFileContents
  • recursiveCopy
  • removeFileInformation
  • renderBackendBreadcrumb
  • renderLabel
  • renderSelectProperty
  • renderTextProperty
  • saveGroupRights
  • saveRights
  • scanDirectory
  • scanPlugins
  • sendEncodingHeader
  • set_magic_quotes_gpc
  • setArtspecDefault
  • setArtspecOnline
  • setSystemProperty
  • showTree
  • startTiming
  • statCreateLocationString
  • statDisplayTopChooser
  • statDisplayYearlyTopChooser
  • statGetAvailableMonths
  • statGetAvailableYears
  • statResetStatistic
  • statsArchive
  • statsDisplayInfo
  • statsOverviewAll
  • statsOverviewTop
  • statsOverviewTopYear
  • statsOverviewYear
  • strAssignTemplate
  • strBuildSqlValues
  • strCheckTreeForErrors
  • strCopyCategory
  • strCopyTree
  • strDeeperCategoriesArray
  • strDeleteCategory
  • strHasArticles
  • strHasStartArticle
  • strMakePublic
  • strMakeVisible
  • strMoveCatTargetallowed
  • strMoveDownCategory
  • strMoveSubtree
  • strMoveUpCategory
  • strNewCategory
  • strNewTree
  • strNextBackwards
  • strNextDeeper
  • strNextDeeperAll
  • strNextPost
  • strOrderedPostTreeList
  • strRemakeTreeTable
  • strRenameCategory
  • strRenameCategoryAlias
  • strSortPrePost
  • strSyncCategory
  • systemHavePerm
  • tplAutoFillModules
  • tplBrowseLayoutForContainers
  • tplcfgDuplicate
  • tplDeleteTemplate
  • tplDuplicateTemplate
  • tplEditTemplate
  • tplGetContainerDefault
  • tplGetContainerMode
  • tplGetContainerName
  • tplGetContainerNumbersInLayout
  • tplGetContainerTypes
  • tplGetInUsedData
  • tplIsTemplateInUse
  • tplPreparseLayout
  • tplProcessSendContainerConfiguration
  • updateClientCache
  • updateFileInformation
  • uplCreateFriendlyName
  • uplDirectoryListRecursive
  • uplGetDirectoriesToExclude
  • uplGetFileExtension
  • uplGetFileIcon
  • uplGetFileTypeDescription
  • uplGetThumbnail
  • uplHasFiles
  • uplHasSubdirs
  • uplmkdir
  • uplRecursiveDBDirectoryList
  • uplRecursiveDirectoryList
  • uplRenameDirectory
  • uplSearch
  • uplSyncDirectory
  • uplSyncDirectoryDBFS
  • Overview
  • Package
  • Function
  • Tree
  • Deprecated
  • Todo
   1: <?php
   2: 
   3: /**
   4:  * This file contains the CONTENIDO structure/category functions.
   5:  *
   6:  * @package Core
   7:  * @subpackage Backend
   8:  * @version SVN Revision $Rev:$
   9:  *
  10:  * @author Olaf Niemann
  11:  * @author Murat Purc <murat@purc.de>
  12:  * @copyright four for business AG <www.4fb.de>
  13:  * @license http://www.contenido.org/license/LIZENZ.txt
  14:  * @link http://www.4fb.de
  15:  * @link http://www.contenido.org
  16:  */
  17: 
  18: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
  19: 
  20: cInclude('includes', 'functions.con.php');
  21: cInclude('includes', 'functions.database.php');
  22: 
  23: /**
  24:  * Creates a new category tree (root category item).
  25:  *
  26:  * @param string $catname
  27:  *         The category name
  28:  * @param string $catalias
  29:  *         Alias of category
  30:  * @param int $visible
  31:  *         Flag about visible status
  32:  * @param int $public
  33:  *         Flag about public status
  34:  * @param int $iIdtplcfg
  35:  *         Id of template configuration
  36:  * @return int|NULL
  37:  *         of new generated category or nothing on failure
  38:  */
  39: function strNewTree($catname, $catalias = '', $visible = 0, $public = 1, $iIdtplcfg = 0) {
  40:     global $client, $lang, $perm;
  41: 
  42:     // Flag to rebuild the category table
  43:     global $remakeCatTable, $remakeStrTable;
  44: 
  45:     if (trim($catname) == '') {
  46:         return;
  47:     }
  48: 
  49:     $catname = stripslashes($catname);
  50: 
  51:     $remakeCatTable = true;
  52:     $remakeStrTable = true;
  53: 
  54:     $catalias = trim($catalias);
  55:     if ($catalias == '') {
  56:         $catalias = trim($catname);
  57:     }
  58: 
  59:     $client = (int) $client;
  60:     $lang = (int) $lang;
  61: 
  62:     $visible = ($visible == 1) ? 1 : 0;
  63:     if (!$perm->have_perm_area_action('str', 'str_makevisible')) {
  64:         $visible = 0;
  65:     }
  66: 
  67:     $public = ($public == 1) ? 1 : 0;
  68:     if (!$perm->have_perm_area_action('str', 'str_makepublic')) {
  69:         $public = 1;
  70:     }
  71: 
  72:     // Get last category tree
  73:     $oCatColl = new cApiCategoryCollection();
  74:     $oLastCatTree = $oCatColl->fetchLastCategoryTree($client);
  75:     $lastCatTreeId = (is_object($oLastCatTree)) ? $oLastCatTree->get('idcat') : 0;
  76: 
  77:     // Insert new category tree
  78:     $oCatColl2 = new cApiCategoryCollection();
  79:     $oNewCat = $oCatColl2->create($client, 0, $lastCatTreeId, 0);
  80:     $newIdcat = $oNewCat->get('idcat');
  81:     $oldPostId = -1;
  82: 
  83:     // Update last category tree
  84:     if (is_object($oLastCatTree)) {
  85:         $oldPostId = $oLastCatTree->get('postid');
  86:         $oLastCatTree->set('postid', $newIdcat);
  87:         $oLastCatTree->store();
  88:     }
  89: 
  90:     $error = strCheckTreeForErrors();
  91:     if (!($error === false)) {
  92:         if ($oldPostId != -1) {
  93:             $oLastCatTree->set('postid', $oldPostId);
  94:             $oLastCatTree->store();
  95:         }
  96:         $oCatColl->delete($oNewCat->get('idcat'));
  97:         return;
  98:     }
  99: 
 100:     cInclude('includes', 'functions.rights.php');
 101: 
 102:     // Loop through languages
 103:     $aLanguages = array(
 104:         $lang
 105:     );
 106:     foreach ($aLanguages as $curLang) {
 107:         $name = $catname;
 108:         $urlname = conHtmlSpecialChars(cString::cleanURLCharacters($catalias), ENT_QUOTES);
 109: 
 110:         // Insert new category language entry
 111:         $oCatLangColl = new cApiCategoryLanguageCollection();
 112:         $oCatLangColl->create($newIdcat, $curLang, $name, $urlname, '', 0, $visible, $public, 0, '', 0);
 113: 
 114:         // Set correct rights for element
 115:         createRightsForElement('str', $newIdcat, $curLang);
 116:         createRightsForElement('con', $newIdcat, $curLang);
 117:     }
 118: 
 119:     // Assign template
 120:     strAssignTemplate($newIdcat, $client, $iIdtplcfg);
 121: 
 122:     return $newIdcat;
 123: }
 124: 
 125: /**
 126:  * Creates a new category.
 127:  *
 128:  * @param int $parentid
 129:  *         Id of parent category
 130:  * @param string $catname
 131:  *         The category name
 132:  * @param bool $remakeTree
 133:  *         Flag to rebuild category tree structure
 134:  * @param string $catalias
 135:  *         Alias of category
 136:  * @param int $visible
 137:  *         Flag about visible status
 138:  * @param int $public
 139:  *         Flag about public status
 140:  * @param int $iIdtplcfg
 141:  *         Id of template configuration
 142:  * @return int|NULL
 143:  *         of new generated category or nothing on failure
 144:  */
 145: function strNewCategory($parentid, $catname, $remakeTree = true, $catalias = '', $visible = 0, $public = 1, $iIdtplcfg = 0) {
 146:     global $client, $lang, $perm;
 147: 
 148:     // Flag to rebuild the category table
 149:     global $remakeCatTable, $remakeStrTable;
 150: 
 151:     $parentid = (int) $parentid;
 152: 
 153:     if (trim($catname) == '') {
 154:         return;
 155:     }
 156: 
 157:     $catname = stripslashes($catname);
 158: 
 159:     $remakeCatTable = true;
 160:     $remakeStrTable = true;
 161: 
 162:     $catalias = trim($catalias);
 163:     if ($catalias == '') {
 164:         $catalias = trim($catname);
 165:     }
 166: 
 167:     $client = (int) $client;
 168:     $lang = (int) $lang;
 169: 
 170:     $visible = ($visible == 1) ? 1 : 0;
 171:     if (!$perm->have_perm_area_action('str', 'str_makevisible')) {
 172:         $visible = 0;
 173:     }
 174: 
 175:     $public = ($public == 1) ? 1 : 0;
 176:     if (!$perm->have_perm_area_action('str', 'str_makepublic')) {
 177:         $public = 1;
 178:     }
 179: 
 180:     // Get previous category on same level, if exists
 181:     $oCatColl = new cApiCategoryCollection();
 182:     $oCatColl->select('parentid=' . $parentid . ' AND postid = 0 AND idclient = ' . $client);
 183:     $oPrevCat = $oCatColl->next();
 184:     $preIdcat = (is_object($oPrevCat)) ? $oPrevCat->get('idcat') : 0;
 185: 
 186:     // Insert new category tree
 187:     $oCatColl2 = new cApiCategoryCollection();
 188:     $oNewCat = $oCatColl2->create($client, $parentid, $preIdcat, 0);
 189:     $newIdcat = $oNewCat->get('idcat');
 190:     $oldPostId = -1;
 191: 
 192:     // Update previous category, if exists
 193:     if (is_object($oPrevCat)) {
 194:         $oldPostId = $oPrevCat->get('postid');
 195:         $oPrevCat->set('postid', $newIdcat);
 196:         $oPrevCat->set('lastmodified', date('Y-m-d H:i:s'));
 197:         $oPrevCat->store();
 198:     }
 199: 
 200:     $error = strCheckTreeForErrors();
 201: 
 202:     if (!($error === false)) {
 203:         if ($oldPostId != -1) {
 204:             $oPrevCat->set('postid', $oldPostId);
 205:             $oPrevCat->store();
 206:         }
 207:         $oCatColl2->delete($oNewCat->get('idcat'));
 208:         return;
 209:     }
 210: 
 211:     cInclude('includes', 'functions.rights.php');
 212: 
 213:     // Loop through languages
 214:     $aLanguages = array(
 215:         $lang
 216:     );
 217:     foreach ($aLanguages as $curLang) {
 218:         $name = $catname;
 219:         $urlname = conHtmlSpecialChars(cString::cleanURLCharacters($catalias), ENT_QUOTES);
 220: 
 221:         // Insert new category language entry
 222:         $oCatLangColl = new cApiCategoryLanguageCollection();
 223:         $oCatLangColl->create($newIdcat, $curLang, $name, $urlname, '', 0, $visible, $public, 0, '', 0);
 224: 
 225:         // Set correct rights for element
 226:         copyRightsForElement('str', $parentid, $newIdcat, $curLang);
 227:         copyRightsForElement('con', $parentid, $newIdcat, $curLang);
 228:     }
 229: 
 230:     if ($remakeTree == true) {
 231:         strRemakeTreeTable();
 232:     }
 233: 
 234:     // Assign template
 235:     strAssignTemplate($newIdcat, $client, $iIdtplcfg);
 236: 
 237:     return $newIdcat;
 238: }
 239: 
 240: /**
 241:  * Builds ordered post string for a passed category
 242:  *
 243:  * @param int $idcat
 244:  * @param string $poststring
 245:  * @return string
 246:  */
 247: function strOrderedPostTreeList($idcat, $poststring) {
 248:     $oCatColl = new cApiCategoryCollection();
 249:     $oCatColl->select('parentid = 0 AND preid = ' . (int) $idcat . ' AND idcat != 0');
 250:     if (($oCat = $oCatColl->next()) !== false) {
 251:         $postIdcat = $oCat->get('idcat');
 252:         $poststring = $poststring . ',' . $postIdcat;
 253:         $poststring = strOrderedPostTreeList($postIdcat, $poststring);
 254:     }
 255: 
 256:     return $poststring;
 257: }
 258: 
 259: /**
 260:  * Remakes the category tree structure in category tree table.
 261:  *
 262:  * It still uses manually build sql statements due to performance reasons.
 263:  */
 264: function strRemakeTreeTable() {
 265:     global $db, $client, $cfg;
 266: 
 267:     // Flag to rebuild the category table
 268:     global $remakeCatTable;
 269:     global $remakeStrTable;
 270: 
 271:     // Get all category ids
 272:     $oCatColl = new cApiCategoryCollection();
 273:     $idcats = $oCatColl->getCategoryIdsByClient($client);
 274:     if (0 === count($idcats)) {
 275:         // There are no categories to build the tree from!
 276:         return;
 277:     }
 278: 
 279:     $errors = strCheckTreeForErrors();
 280:     if (!($errors === false)) {
 281:         return;
 282:     }
 283: 
 284:     $remakeCatTable = true;
 285:     $remakeStrTable = true;
 286: 
 287:     // Empty category tree table having specific categories
 288:     $sql = 'DELETE FROM ' . $cfg['tab']['cat_tree'] . ' WHERE idcat IN (' . implode(', ', $idcats) . ')';
 289:     $db->query($sql);
 290: 
 291:     // Delete entries from category table having idcat = 0
 292:     // @todo: Check this, how it is possible to have an invalid entry with
 293:     // primary key = 0
 294:     $sql = 'DELETE FROM ' . $cfg['tab']['cat'] . ' WHERE idcat = 0';
 295:     $db->query($sql);
 296: 
 297:     // Delete entries from category language table having idcat = 0
 298:     // @todo: Check this, how it is possible to have an invalid entry with
 299:     // primary key = 0
 300:     $sql = 'DELETE FROM ' . $cfg['tab']['cat_lang'] . ' WHERE idcat = 0';
 301:     $db->query($sql);
 302: 
 303:     // Get all categories by client
 304:     $sql = "SELECT idcat, parentid, preid, postid FROM " . $cfg['tab']['cat'] . " WHERE idclient = " . (int) $client . " ORDER BY parentid ASC, preid ASC, postid ASC";
 305:     $aCategories = array();
 306:     $db->query($sql);
 307:     while ($db->nextRecord()) {
 308:         $rs = $db->toArray();
 309:         if (!isset($aCategories[$rs['parentid']])) {
 310:             $aCategories[$rs['parentid']] = array();
 311:         }
 312:         $aCategories[$rs['parentid']][$rs['idcat']] = $rs;
 313:     }
 314: 
 315:     // Build INSERT statement
 316:     $sInsertQuery = "INSERT INTO " . $cfg['tab']['cat_tree'] . " (idcat, level) VALUES ";
 317:     $sInsertQuery = strBuildSqlValues($aCategories[0], $sInsertQuery, $aCategories);
 318:     $sInsertQuery = rtrim($sInsertQuery, " ,");
 319: 
 320:     // Lock db table and execute INSERT query
 321:     $db->query($sInsertQuery);
 322: }
 323: 
 324: /**
 325:  * Sorts passed assoziative categories array.
 326:  *
 327:  * @todo Check logic, move sorting to db layer, if possible!
 328:  * @param array $arr
 329:  * @return array
 330:  */
 331: function strSortPrePost($arr) {
 332:     $firstElement = NULL;
 333:     foreach ($arr as $row) {
 334:         if ($row['preid'] == 0) {
 335:             $firstElement = $row['idcat'];
 336:         }
 337:     }
 338: 
 339:     $curId = $firstElement;
 340:     $array = array();
 341: 
 342:     // Test for a last element in the category list
 343:     $fine = false;
 344:     foreach ($arr as $row) {
 345:         if ($row['postid'] == 0) {
 346:             $fine = true;
 347:             break;
 348:         }
 349:     }
 350:     if (!$fine) {
 351:         die(); // we already displayed an error message through strCheckTree
 352:     }
 353: 
 354:     while ($curId != 0) {
 355:         $array[] = $arr[$curId];
 356:         $curId = $arr[$curId]['postid'];
 357:     }
 358: 
 359:     return $array;
 360: }
 361: 
 362: /**
 363:  * Builds values part of the SQL used to recreate the category tree table
 364:  *
 365:  * @param array $aCats
 366:  *         Assoziative categories array or something else, but what?
 367:  * @param string $sInsertQuery
 368:  *         The insert statement
 369:  * @param array $aAllCats
 370:  *         Assoziative categories array holding the complete category structure
 371:  * @param int $iLevel
 372:  *         Category level
 373:  * @return string
 374:  */
 375: function strBuildSqlValues($aCats, $sInsertQuery, &$aAllCats, $iLevel = 0) {
 376:     if (is_array($aCats)) {
 377:         $aCats = strSortPrePost($aCats);
 378:         foreach ($aCats as $aCat) {
 379:             $sInsertQuery .= '(' . (int) $aCat['idcat'] . ', ' . (int) $iLevel . '), ';
 380:             if (is_array($aAllCats[$aCat['idcat']])) {
 381:                 $iSubLevel = $iLevel + 1;
 382:                 $sInsertQuery = strBuildSqlValues($aAllCats[$aCat['idcat']], $sInsertQuery, $aAllCats, $iSubLevel);
 383:             }
 384:         }
 385:     }
 386:     return $sInsertQuery;
 387: }
 388: 
 389: /**
 390:  * Returns id of next deeper category.
 391:  *
 392:  * @global int $lang
 393:  * @param int $idcat
 394:  *         Category id to check next deeper item
 395:  * @param bool $ignoreLang
 396:  *         Flag to check for existing entry in category language table
 397:  * @return int
 398:  */
 399: function strNextDeeper($idcat, $ignoreLang = false) {
 400:     global $lang;
 401: 
 402:     $languageId = (true == $ignoreLang) ? NULL : $lang;
 403:     $oCatColl = new cApiCategoryCollection();
 404:     return $oCatColl->getFirstChildCategoryId($idcat, $languageId);
 405: }
 406: 
 407: /**
 408:  * Checks, if passed category contains any articles
 409:  *
 410:  * @param int $idcat
 411:  *         ID of category
 412:  * @return bool
 413:  */
 414: function strHasArticles($idcat) {
 415:     global $lang;
 416: 
 417:     $oCatArtColl = new cApiCategoryArticleCollection();
 418:     return $oCatArtColl->getHasArticles($idcat, $lang);
 419: }
 420: 
 421: /**
 422:  * Returns next post category id
 423:  *
 424:  * @param int $idcat
 425:  *         ID of category
 426:  * @return int
 427:  */
 428: function strNextPost($idcat) {
 429:     $oCatColl = new cApiCategoryCollection();
 430:     return $oCatColl->getNextPostCategoryId($idcat);
 431: }
 432: 
 433: /**
 434:  * Returns next backwards category id
 435:  *
 436:  * @param int $idcat
 437:  *         ID of category
 438:  * @return int
 439:  */
 440: function strNextBackwards($idcat) {
 441:     $oCatColl = new cApiCategoryCollection();
 442:     return $oCatColl->getParentsNextPostCategoryId($idcat);
 443: }
 444: 
 445: /**
 446:  * Returns list of child categories.
 447:  *
 448:  * @global int $lang
 449:  * @param int $idcat
 450:  * @param bool $ignoreLang
 451:  * @return array
 452:  */
 453: function strNextDeeperAll($idcat, $ignoreLang = false) {
 454:     global $lang;
 455: 
 456:     $languageId = (true == $ignoreLang) ? NULL : $lang;
 457:     $oCatColl = new cApiCategoryCollection();
 458:     return $oCatColl->getAllChildCategoryIds($idcat, $languageId);
 459: }
 460: 
 461: /**
 462:  * Renames a category
 463:  *
 464:  * @param int $idcat
 465:  *         Category id
 466:  * @param int $lang
 467:  *         Language id
 468:  * @param string $newcategoryname
 469:  *         New category name
 470:  * @param string $newcategoryalias
 471:  *         New category alias
 472:  */
 473: function strRenameCategory($idcat, $lang, $newCategoryName, $newCategoryAlias) {
 474:     if (trim($newCategoryName) == '') {
 475:         return;
 476:     }
 477: 
 478:     $oCatLang = new cApiCategoryLanguage();
 479:     if (!$oCatLang->loadByCategoryIdAndLanguageId($idcat, $lang)) {
 480:         // Couldn't load category language
 481:         return;
 482:     }
 483: 
 484:     $oldData = array(
 485:         'idcat' => $oCatLang->get('idcat'),
 486:         'name' => $oCatLang->get('name'),
 487:         'urlname' => $oCatLang->get('urlname')
 488:     );
 489: 
 490:     $name = stripslashes($newCategoryName);
 491:     $urlName = (trim($newCategoryAlias) != '') ? trim($newCategoryAlias) : $newCategoryName;
 492: 
 493:     if (trim($newCategoryAlias) != '') {
 494:         // overfluous assignment
 495:         // if ($oCatLang->get('urlname') != $newCategoryAlias) {
 496:         // $urlName = $newCategoryAlias;
 497:         // }
 498:         cInclude('includes', 'functions.pathresolver.php');
 499:         $client = cRegistry::getClientId();
 500:         prDeleteCacheFileContent($client, $lang);
 501:     }
 502: 
 503:     $oCatLang->set('name', $name);
 504:     $oCatLang->set('urlname', $urlName);
 505:     $oCatLang->set('lastmodified', date('Y-m-d H:i:s'));
 506:     $oCatLang->store();
 507: 
 508:     $newData = array(
 509:         'idcat' => $idcat,
 510:         'name' => $name,
 511:         'urlname' => $urlName
 512:     );
 513: 
 514:     cApiCecHook::execute('Contenido.Category.strRenameCategory', $newData, $oldData);
 515: }
 516: 
 517: /**
 518:  * Renames a category alias.
 519:  *
 520:  * @param int $idcat
 521:  *         Category id
 522:  * @param int $lang
 523:  *         Language id
 524:  * @param string $newcategoryalias
 525:  *         New category alias
 526:  */
 527: function strRenameCategoryAlias($idcat, $lang, $newcategoryalias) {
 528:     $oCatLang = new cApiCategoryLanguage();
 529:     if (!$oCatLang->loadByCategoryIdAndLanguageId($idcat, $lang)) {
 530:         // Couldn't load category language
 531:         return;
 532:     }
 533: 
 534:     $oldData = array(
 535:         'idcat' => $oCatLang->get('idcat'),
 536:         'urlname' => $oCatLang->get('urlname')
 537:     );
 538: 
 539:     if (trim($newcategoryalias) == '') {
 540:         // Use categoryname as default -> get it escape it save it as urlname
 541:         $newcategoryalias = $oCatLang->get('name');
 542:     }
 543: 
 544:     $oCatLang->set('urlname', $newcategoryalias);
 545:     $oCatLang->set('lastmodified', date('Y-m-d H:i:s'));
 546:     $oCatLang->store();
 547: 
 548:     cInclude('includes', 'functions.pathresolver.php');
 549:     $client = cRegistry::getClientId();
 550:     prDeleteCacheFileContent($client, $lang);
 551: 
 552:     $newData = array(
 553:         'idcat' => $idcat,
 554:         'urlname' => $newcategoryalias
 555:     );
 556: 
 557:     cApiCecHook::execute('Contenido.Category.strRenameCategoryAlias', $newData, $oldData);
 558: }
 559: 
 560: /**
 561:  * Sets the visible status of the category and its childs
 562:  *
 563:  * @param int $idcat
 564:  *         Category id
 565:  * @param int $lang
 566:  *         Language id
 567:  * @param int $visible
 568:  *         Visible status
 569:  */
 570: function strMakeVisible($idcat, $lang, $visible) {
 571:     global $cfg;
 572: 
 573:     $visible = (int) $visible;
 574:     $lang = (int) $lang;
 575: 
 576:     $categories = strDeeperCategoriesArray($idcat);
 577:     foreach ($categories as $value) {
 578:         $oCatLang = new cApiCategoryLanguage();
 579:         $oCatLang->loadByCategoryIdAndLanguageId($value, $lang);
 580:         $oCatLang->set('visible', $visible);
 581:         $oCatLang->set('lastmodified', date('Y-m-d H:i:s'));
 582:         $oCatLang->store();
 583:     }
 584: 
 585:     if ($cfg['pathresolve_heapcache'] == true && $visible = 0) {
 586:         $oPathresolveCacheColl = new cApiPathresolveCacheCollection();
 587:         $oPathresolveCacheColl->deleteByCategoryAndLanguage($idcat, $lang);
 588:     }
 589: }
 590: 
 591: /**
 592:  * Sets the public status of the given category and its children
 593:  * for the given language.
 594:  *
 595:  * This is almost the same function as conMakePublic.
 596:  *
 597:  * @param int $idcat
 598:  *         category id
 599:  * @param int $lang
 600:  *         language id
 601:  * @param int $public
 602:  *         public status of the article to set
 603:  */
 604: function strMakePublic($idcat, $lang, $public) {
 605: 
 606:     foreach (strDeeperCategoriesArray($idcat) as $tmpIdcat) {
 607:         $oCatLang = new cApiCategoryLanguage();
 608:         $oCatLang->loadByCategoryIdAndLanguageId($tmpIdcat, $lang);
 609:         $oCatLang->set('public', $public);
 610:         $oCatLang->set('lastmodified', date('Y-m-d H:i:s'));
 611:         $oCatLang->store();
 612:     }
 613: 
 614: }
 615: 
 616: /**
 617:  * Return a list of idcats of all scions of given category.
 618:  *
 619:  * @param int $idcat
 620:  *         category ID to start at
 621:  * @return array
 622:  *         idcats of all scions
 623:  */
 624: function strDeeperCategoriesArray($idcat) {
 625:     global $client;
 626: 
 627:     $coll = new cApiCategoryCollection();
 628:     $idcats = $coll->getAllCategoryIdsRecursive($idcat, $client);
 629: 
 630:     return $idcats;
 631: }
 632: 
 633: /**
 634:  * Deletes the category and its template configurations.
 635:  *
 636:  * Only categories having no child categories and having no articles will be
 637:  * deleted!
 638:  *
 639:  * @param int $idcat
 640:  *         Id of category to delete
 641:  * @return void|string
 642:  */
 643: function strDeleteCategory($idcat) {
 644:     global $lang, $lang;
 645: 
 646:     // Flag to rebuild the category table
 647:     global $remakeCatTable, $remakeStrTable;
 648: 
 649:     if (strNextDeeper($idcat)) {
 650:         // Category has subcategories
 651:         return '0201';
 652:     } elseif (strHasArticles($idcat)) {
 653:         // Category has articles
 654:         return '0202';
 655:     }
 656: 
 657:     cInclude('includes', 'functions.rights.php');
 658: 
 659:     $remakeCatTable = true;
 660:     $remakeStrTable = true;
 661: 
 662:     // Load category language
 663:     $oCatLang = new cApiCategoryLanguage();
 664:     $oCatLang->loadByCategoryIdAndLanguageId($idcat, $lang);
 665: 
 666:     if ($oCatLang->isLoaded()) {
 667:         // Delete template configuration (deletes also all container
 668:         // configurations)
 669:         $oTemplateConfigColl = new cApiTemplateConfigurationCollection();
 670:         $oTemplateConfigColl->delete($oCatLang->get('idtplcfg'));
 671: 
 672:         // Delete category language
 673:         $oCatLangColl = new cApiCategoryLanguageCollection();
 674:         $oCatLangColl->delete($oCatLang->get('idcatlang'));
 675:     }
 676: 
 677:     // Are there any additional entries for other languages?
 678:     $oCatLangColl = new cApiCategoryLanguageCollection();
 679:     $oCatLangColl->select('idcat = ' . (int) $idcat);
 680:     if (($oCatLang = $oCatLangColl->next()) !== false) {
 681:         // More languages found, delete rights for current category
 682:         deleteRightsForElement('str', $idcat, $lang);
 683:         deleteRightsForElement('con', $idcat, $lang);
 684:         return;
 685:     }
 686: 
 687:     // Load category
 688:     $oCat = new cApiCategory((int) $idcat);
 689:     $preid = (int) $oCat->get('preid');
 690:     $postid = (int) $oCat->get('postid');
 691: 
 692:     // Update pre cat, set it to new postid
 693:     if ($preid != 0) {
 694:         $oPreCat = new cApiCategory($preid);
 695:         $oPreCat->set('postid', $postid);
 696:         $oPreCat->store();
 697:     }
 698: 
 699:     // Update post cat, set it to new preid
 700:     if ($postid != 0) {
 701:         $oPostCat = new cApiCategory($postid);
 702:         $oPostCat->set('preid', $preid);
 703:         $oPostCat->store();
 704:     }
 705: 
 706:     $error = strCheckTreeForErrors(array(), array(
 707:         $idcat
 708:     ));
 709:     if (!($error === false)) {
 710:         if ($preid != 0) {
 711:             $oPreCat = new cApiCategory($preid);
 712:             $oPreCat->set('postid', $idcat);
 713:             $oPreCat->store();
 714:         }
 715:         if ($postid != 0) {
 716:             $oPostCat = new cApiCategory($postid);
 717:             $oPostCat->set('preid', $idcat);
 718:             $oPostCat->store();
 719:         }
 720:         return '0600';
 721:     }
 722: 
 723:     // Delete category
 724:     $oCatColl = new cApiCategoryCollection();
 725:     $oCatColl->deleteBy('idcat', (int) $idcat);
 726: 
 727:     $oCatLangColl = new cApiCategoryLanguageCollection();
 728:     $oCatLangColl->select('idcat = ' . (int) $idcat);
 729:     if (($oCatLang = $oCatLangColl->next()) !== false) {
 730:         // Delete template configuration (deletes also all container
 731:         // configurations)
 732:         $oTemplateConfigColl = new cApiTemplateConfigurationCollection();
 733:         $oTemplateConfigColl->delete($oCatLang->get('idtplcfg'));
 734:     }
 735: 
 736:     // Delete category language entry by category id
 737:     $oCatLangColl->resetQuery();
 738:     $oCatLangColl->deleteBy('idcat', (int) $idcat);
 739: 
 740:     // Delete category tree entry by category id
 741:     $oCatTreeColl = new cApiCategoryTreeCollection();
 742:     $oCatTreeColl->deleteBy('idcat', (int) $idcat);
 743: 
 744:     // Delete rights for element
 745:     deleteRightsForElement('str', $idcat);
 746:     deleteRightsForElement('con', $idcat);
 747: }
 748: 
 749: /**
 750:  * Moves a category upwards.
 751:  *
 752:  * @param int $idcat
 753:  *         Id of category to move upwards
 754:  */
 755: function strMoveUpCategory($idcat) {
 756:     // Flag to rebuild the category table and initializing notification variable
 757:     global $remakeCatTable, $remakeStrTable, $notification;
 758: 
 759:     // Load current category
 760:     $oCat = new cApiCategory();
 761:     $oCat->loadByPrimaryKey((int) $idcat);
 762:     $preid = $oCat->get('preid');
 763:     $postid = $oCat->get('postid');
 764: 
 765:     if (0 == $preid) {
 766:         // No preid, no way to move up
 767:         return;
 768:     }
 769: 
 770:     $remakeCatTable = true;
 771:     $remakeStrTable = true;
 772: 
 773:     // Load previous category
 774:     $oPreCat = new cApiCategory();
 775:     $oPreCat->loadByPrimaryKey((int) $preid);
 776:     $prePreid = $oPreCat->get('preid');
 777:     $preIdcat = $oPreCat->get('idcat');
 778: 
 779:     // Load category before previous category
 780:     $oPrePreCat = new cApiCategory();
 781:     if ((int) $prePreid > 0) {
 782:         $oPrePreCat->loadByPrimaryKey((int) $prePreid);
 783:     }
 784: 
 785:     // Load post category
 786:     $oPostCat = new cApiCategory();
 787:     if ((int) $postid > 0) {
 788:         $oPostCat->loadByPrimaryKey((int) $postid);
 789:     }
 790: 
 791:     $updateCats = array();
 792: 
 793:     // Update category before previous, if exists
 794:     if ($oPrePreCat->isLoaded()) {
 795:         $oPrePreCat->set('postid', $idcat);
 796:         $updateCats[$prePreid] = $oPrePreCat;
 797:     }
 798: 
 799:     // Update previous category
 800:     $oPreCat->set('preid', $idcat);
 801:     $oPreCat->set('postid', $postid);
 802:     $updateCats[$preid] = $oPreCat;
 803: 
 804:     // Update current category
 805:     $oCat->set('preid', $prePreid);
 806:     $oCat->set('postid', $preid);
 807:     $updateCats[$idcat] = $oCat;
 808: 
 809:     // Update post category, if exists!
 810:     $oPostCat->set('preid', $preIdcat);
 811:     $updateCats[$postid] = $oPostCat;
 812: 
 813:     $error = strCheckTreeForErrors($updateCats);
 814:     if ($error === false) {
 815:         foreach ($updateCats as $cat) {
 816:             $cat->store();
 817:         }
 818:     } else {
 819:         $string = '';
 820:         foreach ($error as $msg) {
 821:             $string .= $msg . '<br>';
 822:         }
 823:         $notification->displayNotification(cGuiNotification::LEVEL_WARNING, $msg . '<br><br>' . i18n('Something went wrong while trying to perform this operation. Please try again.'));
 824:         return;
 825:     }
 826: }
 827: 
 828: /**
 829:  * Moves a category downwards.
 830:  *
 831:  * @param int $idcat
 832:  *         Id of category to move downwards
 833:  */
 834: function strMoveDownCategory($idcat) {
 835:     // Flag to rebuild the category table and initializing notification variable
 836:     global $remakeCatTable, $remakeStrTable, $notification;
 837: 
 838:     // Load current category
 839:     $oCat = new cApiCategory();
 840:     $oCat->loadByPrimaryKey((int) $idcat);
 841:     $preid = $oCat->get('preid');
 842:     $postid = $oCat->get('postid');
 843: 
 844:     if (0 == $postid) {
 845:         // No post, no way to move down
 846:         return;
 847:     }
 848: 
 849:     $remakeCatTable = true;
 850:     $remakeStrTable = true;
 851: 
 852:     // Load previous category
 853:     $oPreCat = new cApiCategory();
 854:     if ((int) $preid > 0) {
 855:         $oPreCat->loadByPrimaryKey((int) $preid);
 856:         $preIdcat = (int) $oPreCat->get('idcat');
 857:     } else {
 858:         $preIdcat = 0;
 859:     }
 860: 
 861:     // Load post category
 862:     $oPostCat = new cApiCategory();
 863:     $oPostCat->loadByPrimaryKey((int) $postid);
 864:     $postIdcat = $oPostCat->get('idcat');
 865:     $postPostid = $oPostCat->get('postid');
 866: 
 867:     $updateCats = array();
 868: 
 869:     if ($preIdcat != 0) {
 870:         // Update previous category, if exists
 871:         $oPreCat->set('postid', (int) $postIdcat);
 872:         $updateCats[$preIdcat] = $oPreCat;
 873:     }
 874: 
 875:     // Update current category
 876:     $oCat->set('preid', $postid);
 877:     $oCat->set('postid', $postPostid);
 878:     $updateCats[$idcat] = $oCat;
 879: 
 880:     // Update post category
 881:     $oPostCat->set('preid', $preIdcat);
 882:     $oPostCat->set('postid', $idcat);
 883:     $updateCats[$postid] = $oPostCat;
 884: 
 885:     if ($postPostid != 0) {
 886:         // Update post post category, if exists
 887:         $oPostPostCat = new cApiCategory($postPostid);
 888:         $oPostPostCat->set('preid', $idcat);
 889:         $updateCats[$postPostid] = $oPostPostCat;
 890:     }
 891: 
 892:     $error = strCheckTreeForErrors($updateCats);
 893:     if ($error === false) {
 894:         foreach ($updateCats as $cat) {
 895:             $cat->store();
 896:         }
 897:     } else {
 898:         $string = '';
 899:         foreach ($error as $msg) {
 900:             $string .= $msg . '<br>';
 901:         }
 902:         $notification->displayNotification(cGuiNotification::LEVEL_WARNING, $msg . '<br><br>' . i18n('Something went wrong while trying to perform this operation. Please try again.'));
 903:         return;
 904:     }
 905: }
 906: 
 907: /**
 908:  * Moves a subtree to another destination.
 909:  *
 910:  * @param int $idcat
 911:  *         Id of category
 912:  * @param int $newParentId
 913:  *         Id of destination parent category
 914:  * @param int $newPreId
 915:  *         Id of new previous category
 916:  * @param int $newPostId
 917:  *         Id of new post category
 918:  * @return bool
 919:  */
 920: function strMoveSubtree($idcat, $newParentId, $newPreId = NULL, $newPostId = NULL) {
 921:     global $movesubtreeidcat, $notification;
 922: 
 923:     $idlang = cRegistry::getLanguageId();
 924:     $cat = new cApiCategoryCollection();
 925:     $children = $cat->getAllChildCategoryIds($idcat, $idlang);
 926: 
 927:     foreach ($children as $category) {
 928:         // avoids to move the main tree node in sub node of the same tree
 929:         if ($category == $newParentId) {
 930:             return false;
 931:         }
 932:     }
 933: 
 934:     if ($idcat == $newParentId) {
 935:         return false;
 936:     }
 937: 
 938:     if ($newParentId == 0 && $newPreId == 0) {
 939:         return false;
 940:     }
 941:     if (!isset($newPostId)) {
 942:         // return false;
 943:     }
 944:     // flag to rebuild the category table
 945:     global $remakeCatTable, $remakeStrTable;
 946: 
 947:     $remakeCatTable = true;
 948:     $remakeStrTable = true;
 949: 
 950:     // check the post ID parameter
 951:     if (is_null($newPostId)) {
 952:         $newPostId = 0;
 953:     }
 954: 
 955:     if ($newParentId == -1) {
 956:         // stop moving the category without actually moving it
 957:         $movesubtreeidcat = 0;
 958:     } else if (is_null($newParentId)) {
 959:         // start moving the category withour moving it yet
 960:         $movesubtreeidcat = $idcat;
 961:     } else {
 962:         // move the category with the ID idcat to the category newParentId
 963:         $category = new cApiCategory($idcat);
 964:         $oldPreId = $category->get('preid');
 965:         $oldPostId = $category->get('postid');
 966:         $oldParentId = $category->get('parentid');
 967: 
 968:         $updateCats = array();
 969: 
 970:         // update old predecessor (pre) category
 971:         if ($oldPreId != 0) {
 972:             $oldPreCategory = new cApiCategory($oldPreId);
 973:             $oldPreCategory->set('postid', $oldPostId);
 974:             $updateCats[$oldPreId] = $oldPreCategory;
 975:         }
 976: 
 977:         // update old follower (post) category
 978:         if ($oldPostId != 0) {
 979:             if (isset($updateCats[$oldPostId])) {
 980:                 $updateCats[$oldPostId]->set('preid', $oldPreId);
 981:             } else {
 982:                 $oldPostCategory = new cApiCategory($oldPostId);
 983:                 $oldPostCategory->set('preid', $oldPreId);
 984:                 $updateCats[$oldPostId] = $oldPostCategory;
 985:             }
 986:         }
 987: 
 988:         // update new predecessor (pre) category
 989:         if (is_null($newPreId)) {
 990:             // if no new pre ID has been given, use the last category in the
 991:             // given parent category
 992:             $categoryCollection = new cApiCategoryCollection();
 993:             $categoryCollection->select("parentid = " . $newParentId . " AND postid = 0");
 994:             $newPreCategory = $categoryCollection->next();
 995:             if($newPreCategory != null) {
 996:                 $newPreId = $newPreCategory->get('idcat');
 997:                 $newPreCategory->set('postid', $idcat);
 998:                 $updateCats[$newPreId] = $newPreCategory;
 999:             }
1000:         } else {
1001:             if (isset($updateCats[$newPreId])) {
1002:                 $updateCats[$newPreId]->set('postid', $idcat);
1003:             } else {
1004:                 $newPreCategory = new cApiCategory($newPreId);
1005:                 $newPreCategory->set('postid', $idcat);
1006:                 $updateCats[$newPreId] = $newPreCategory;
1007:                 $newPreId = $newPreCategory->get('idcat');
1008:             }
1009:         }
1010: 
1011:         // update new follower (post) category
1012:         if ($newPostId != 0) {
1013:             if (isset($updateCats[$newPostId])) {
1014:                 $updateCats[$newPostId]->set('preid', $idcat);
1015:             } else {
1016:                 $newPostCategory = new cApiCategory($newPostId);
1017:                 $newPostCategory->set('preid', $idcat);
1018:                 $updateCats[$newPostId] = $newPostCategory;
1019:             }
1020:         }
1021: 
1022:         // Update current category
1023:         $category->set('parentid', $newParentId);
1024:         $category->set('preid', $newPreId);
1025:         $category->set('postid', $newPostId);
1026:         $updateCats[$idcat] = $category;
1027: 
1028:         $error = strCheckTreeForErrors($updateCats);
1029:         if ($error === false) {
1030:             foreach ($updateCats as $cat) {
1031:                 $cat->store();
1032:             }
1033:         } else {
1034:             $string = '';
1035:             foreach ($error as $msg) {
1036:                 $string .= $msg . '<br>';
1037:             }
1038:             $notification->displayNotification(cGuiNotification::LEVEL_WARNING, $msg . '<br><br>' . i18n('Something went wrong while trying to perform this operation. Please try again.'));
1039:             return false;
1040:         }
1041: 
1042:         $movesubtreeidcat = 0;
1043:     }
1044: 
1045:     $sess = cRegistry::getSession();
1046:     $sess->register('movesubtreeidcat');
1047:     $sess->freeze();
1048: }
1049: 
1050: /**
1051:  * Checks if category is movable.
1052:  *
1053:  * @param int $idcat
1054:  *         Id of category to move
1055:  * @param int $source
1056:  *         Id of source category
1057:  * @return bool
1058:  */
1059: function strMoveCatTargetallowed($idcat, $source) {
1060:     return ($idcat == $source) ? 0 : 1;
1061: }
1062: 
1063: /**
1064:  * Synchronizes a category from one language to another language.
1065:  *
1066:  * @param int $idcatParam
1067:  *         Id of category to synchronize
1068:  * @param int $sourcelang
1069:  *         Id of source language
1070:  * @param int $targetlang
1071:  *         Id of target language
1072:  * @param bool $bMultiple
1073:  *         Flag to synchronize child languages
1074:  * @return boolean
1075:  */
1076: function strSyncCategory($idcatParam, $sourcelang, $targetlang, $bMultiple = false) {
1077:     $bMultiple = (bool) $bMultiple;
1078: 
1079:     $aCatArray = array();
1080:     if ($bMultiple == true) {
1081:         $aCatArray = strDeeperCategoriesArray($idcatParam);
1082:     } else {
1083:         $aCatArray[] = $idcatParam;
1084:     }
1085: 
1086:     foreach ($aCatArray as $idcat) {
1087:         // Check if category for target language already exists
1088:         $oCatLang = new cApiCategoryLanguage();
1089:         if ($oCatLang->loadByCategoryIdAndLanguageId($idcat, $targetlang)) {
1090:             return false;
1091:         }
1092: 
1093:         // Get source category language
1094:         $oCatLang = new cApiCategoryLanguage();
1095:         if ($oCatLang->loadByCategoryIdAndLanguageId($idcat, $sourcelang)) {
1096:             $aRs = $oCatLang->toArray();
1097: 
1098:             // Copy the template configuration, if exists
1099:             $newidtplcfg = ($aRs['idtplcfg'] != 0) ? tplcfgDuplicate($aRs['idtplcfg']) : 0;
1100: 
1101:             $visible = 0;
1102:             $startidartlang = 0;
1103:             $urlpath = '';
1104: 
1105:             $oCatLangColl = new cApiCategoryLanguageCollection();
1106:             $oNewCatLang = $oCatLangColl->create($aRs['idcat'], $targetlang, $aRs['name'], $aRs['urlname'], $urlpath, $newidtplcfg, $visible, $aRs['public'], $aRs['status'], $aRs['author'], $startidartlang, $aRs['created'], $aRs['lastmodified']);
1107: 
1108:             // Execute CEC hook
1109:             $param = $aRs;
1110:             $param['idlang'] = $targetlang;
1111:             $param['idtplcfg'] = (int) $newidtplcfg;
1112:             $param['visible'] = $visible;
1113:             cApiCecHook::execute('Contenido.Category.strSyncCategory_Loop', $param);
1114: 
1115:             // Set correct rights for element
1116:             cInclude('includes', 'functions.rights.php');
1117:             createRightsForElement('str', $idcat, $targetlang);
1118:             createRightsForElement('con', $idcat, $targetlang);
1119:         }
1120:     }
1121: }
1122: 
1123: /**
1124:  * Checks if category has a start article
1125:  *
1126:  * @param int $idcat
1127:  *         Id of category
1128:  * @param int $idlang
1129:  *         The language id
1130:  * @return bool
1131:  */
1132: function strHasStartArticle($idcat, $idlang) {
1133:     $oCatLangColl = new cApiCategoryLanguageCollection();
1134:     return ($oCatLangColl->getStartIdartlangByIdcatAndIdlang($idcat, $idlang) > 0);
1135: }
1136: 
1137: /**
1138:  * Copies the category and it's existing articles into another category.
1139:  *
1140:  * @param int $idcat
1141:  *         Id of category to copy
1142:  * @param int $destidcat
1143:  *         Id of destination category
1144:  * @param bool $remakeTree
1145:  *         Flag to rebuild category tree
1146:  * @param bool $bUseCopyLabel
1147:  *         Flag to add copy label to the new categories
1148:  * @return void|int
1149:  */
1150: function strCopyCategory($idcat, $destidcat, $remakeTree = true, $bUseCopyLabel = true) {
1151:     global $cfg, $lang;
1152: 
1153:     $newidcat = (int) strNewCategory($destidcat, 'a', $remakeTree);
1154:     if ($newidcat == 0) {
1155:         return;
1156:     }
1157: 
1158:     // Load old and new category
1159:     $oOldCatLang = new cApiCategoryLanguage();
1160:     if (!$oOldCatLang->loadByCategoryIdAndLanguageId($idcat, $lang)) {
1161:         return;
1162:     }
1163: 
1164:     $oNewCatLang = new cApiCategoryLanguage();
1165:     if (!$oNewCatLang->loadByCategoryIdAndLanguageId($newidcat, $lang)) {
1166:         return;
1167:     }
1168: 
1169:     // Worker objects
1170:     $oNewCat = new cApiCategory((int) $newidcat);
1171:     $oOldCat = new cApiCategory((int) $idcat);
1172: 
1173:     // Copy properties
1174:     if ($bUseCopyLabel == true) {
1175:         $oNewCatLang->set('name', sprintf(i18n('%s (Copy)'), $oOldCatLang->get('name')));
1176:     } else {
1177:         $oNewCatLang->set('name', $oOldCatLang->get('name'));
1178:     }
1179: 
1180:     $oNewCatLang->set('public', $oOldCatLang->get('public'));
1181:     $oNewCatLang->set('visible', 0);
1182:     $oNewCatLang->store();
1183: 
1184:     // Execute cec hook
1185:     cApiCecHook::execute('Contenido.Category.strCopyCategory', array(
1186:         'oldcat' => $oOldCat,
1187:         'newcat' => $oNewCat,
1188:         'newcatlang' => $oNewCatLang
1189:     ));
1190: 
1191:     // Copy template configuration
1192:     if ($oOldCatLang->get('idtplcfg') != 0) {
1193:         // Create new template configuration
1194:         $oNewCatLang->assignTemplate($oOldCatLang->getTemplate());
1195: 
1196:         // Copy the container configuration
1197:         $oContainerConfColl = new cApiContainerConfigurationCollection();
1198:         $oContainerConfColl->select('idtplcfg = ' . (int) $oOldCatLang->get('idtplcfg'));
1199: 
1200:         $oNewContainerConfColl = new cApiContainerConfigurationCollection();
1201:         while (($oItem = $oContainerConfColl->next()) !== false) {
1202:             $oNewContainerConfColl->create($oNewCatLang->get('idtplcfg'), $oItem->get('number'), $oItem->get('container'));
1203:         }
1204:     }
1205: 
1206:     $db = cRegistry::getDb();
1207: 
1208:     $oCatArtColl = new cApiCategoryArticleCollection();
1209: 
1210:     // Copy all articles
1211:     $sql = "SELECT A.idart, B.idartlang FROM %s AS A, %s AS B WHERE A.idcat = %d AND B.idart = A.idart AND B.idlang = %s";
1212:     $db->query($sql, $cfg['tab']['cat_art'], $cfg['tab']['art_lang'], $idcat, $lang);
1213: 
1214:     while ($db->nextRecord()) {
1215:         $newidart = (int) conCopyArticle($db->f('idart'), $newidcat, '', $bUseCopyLabel);
1216:         if ($db->f('idartlang') == $oOldCatLang->get('startidartlang')) {
1217:             $oCatArtColl->resetQuery();
1218:             $idcatart = $oCatArtColl->getIdByCategoryIdAndArticleId($newidcat, $newidart);
1219:             if ($idcatart) {
1220:                 conMakeStart($idcatart, 1);
1221:             }
1222:         }
1223:     }
1224: 
1225:     return $newidcat;
1226: }
1227: 
1228: /**
1229:  * Copies the categorytree (category and its childs) to an another category.
1230:  *
1231:  * @param int $idcat
1232:  *         Id of category to copy
1233:  * @param int $destcat
1234:  *         Id of destination category
1235:  * @param bool $remakeTree
1236:  *         Flag to rebuild category tree
1237:  * @param bool $bUseCopyLabel
1238:  *         Flag to add copy label to the new categories
1239:  */
1240: function strCopyTree($idcat, $destcat, $remakeTree = true, $bUseCopyLabel = true) {
1241:     $newidcat = strCopyCategory($idcat, $destcat, false, $bUseCopyLabel);
1242: 
1243:     $oCatColl = new cApiCategoryCollection();
1244:     $aIds = $oCatColl->getIdsByWhereClause('parentid = ' . (int) $idcat);
1245:     foreach ($aIds as $id) {
1246:         strCopyTree($id, $newidcat, false, $bUseCopyLabel);
1247:     }
1248: 
1249:     if ($remakeTree == true) {
1250:         strRemakeTreeTable();
1251:     }
1252: }
1253: 
1254: /**
1255:  * Assigns a template to passed category.
1256:  *
1257:  * @param int $idcat
1258:  * @param int $client
1259:  * @param int $idTplCfg
1260:  */
1261: function strAssignTemplate($idcat, $client, $idTplCfg) {
1262:     global $perm;
1263: 
1264:     // Template permission check
1265:     $iIdtplcfg = ($perm->have_perm_area_action('str_tplcfg', 'str_tplcfg')) ? (int) $idTplCfg : 0;
1266: 
1267:     $idtpl = NULL;
1268:     if ($iIdtplcfg == 0) {
1269:         // Get default template
1270:         $oTemplateColl = new cApiTemplateCollection('defaulttemplate = 1 AND idclient = ' . (int) $client);
1271:         if (($oTemplate = $oTemplateColl->next()) !== false) {
1272:             $idtpl = $oTemplate->get('idtpl');
1273:         }
1274:     } else {
1275:         // Use passed template
1276:         $idtpl = $idTplCfg;
1277:     }
1278: 
1279:     if ($idtpl) {
1280:         // Assign template
1281:         $oCatLangColl = new cApiCategoryLanguageCollection('idcat = ' . (int) $idcat);
1282:         while (($oCatLang = $oCatLangColl->next()) !== false) {
1283:             $oCatLang->assignTemplate($idtpl);
1284:         }
1285:     }
1286: }
1287: 
1288: /**
1289:  * Checks the category tree for errors
1290:  * Returns FALSE if there are NO errors.
1291:  * If there are errors, an array with error messages will be returned
1292:  *
1293:  * @param array $addCats
1294:  *         An array of cApiCategory objects which overwrite categories from the database
1295:  * @param array $ignoreCats
1296:  *         An array of idcat's which will be treated like they don't exist in the database
1297:  * @return array|bool
1298:  *         An array of error messages if something is wrong.
1299:  *         If nothing is wrong false will be returned.
1300:  */
1301: function strCheckTreeForErrors($addCats = array(), $ignoreCats = array()) {
1302:     $errorMessages = array();
1303: 
1304:     // Get all categories into memory
1305:     $cats = new cApiCategoryCollection();
1306:     $cats->select("idclient = '" . cSecurity::toInteger(cRegistry::getClientId()) . "'");
1307: 
1308:     $catArray = array();
1309:     // first add the ones from the parameters
1310:     foreach ($addCats as $addCat) {
1311:         if ($addCat->get('idcat') == 0) {
1312:             continue;
1313:         }
1314:         $catArray[$addCat->get('idcat')] = $addCat;
1315:     }
1316: 
1317:     // add every category from the database
1318:     while ($cat = $cats->next()) {
1319:         if (in_array($cat->get('idcat'), $ignoreCats)) {
1320:             continue;
1321:         }
1322:         if (isset($catArray[$cat->get('idcat')])) {
1323:             continue;
1324:         }
1325:         $catArray[$cat->get('idcat')] = $cat;
1326:     }
1327: 
1328:     ksort($catArray);
1329: 
1330:     // build an array with the parentids at the top level and every child
1331:     // category as member
1332:     // aka
1333:     // $parents[parentId][catIdOfChildToParentId] =
1334:     // cApiCategory(catIdOfChildToParent)
1335:     // check if every parent that is mentioned in the database actually exists
1336:     $fine = true;
1337:     $parents = array();
1338:     foreach ($catArray as $idcat => $cat) {
1339:         if (!array_key_exists($cat->get('parentid'), $catArray) && $cat->get('parentid') != 0) {
1340:             $fine = false;
1341:             $errorMessages[] = sprintf(i18n('Category %s has a parent id (%s) which does not exist!'), $idcat, $cat->get('parentid'));
1342:         }
1343:         $parents[$cat->get('parentid')][$idcat] = $cat;
1344:     }
1345: 
1346:     // check for consistency in every parent
1347:     foreach ($parents as $parentId => $parent) {
1348:         // first, check for multiple preids and postids
1349:         // the category tree will miss some categories if multiple categories
1350:         // share preids and/or postids
1351:         $preIds = array();
1352:         $postIds = array();
1353:         foreach ($parent as $idcat => $cat) {
1354:             $preId = $cat->get('preid');
1355:             $postId = $cat->get('postid');
1356:             if (in_array($preId, $preIds)) {
1357:                 $fine = false;
1358:                 $errorMessages[] = sprintf(i18n('There are multiple categories in %s that share the same pre-id (%s - second occurence at %s). Sorting will fail and not all categories will be shown.'), $parentId, $preId, $idcat);
1359:             }
1360:             if (in_array($postId, $postIds)) {
1361:                 $fine = false;
1362:                 $errorMessages[] = sprintf(i18n('There are multiple categories in %s that share the same post-id (%s - second occurence at %s). Sorting will fail and not all categories will be shown.'), $parentId, $postId, $idcat);
1363:             }
1364:             $preIds[] = $preId;
1365:             $postIds[] = $postId;
1366:         }
1367: 
1368:         // check the consistency of the postids
1369:         // find the start
1370:         $startCat = null;
1371:         foreach ($parent as $cat) {
1372:             if ($cat->get('preid') == 0) {
1373:                 $startCat = $cat;
1374:                 break;
1375:             }
1376:         }
1377:         // if not start was found then something is wrong
1378:         if ($startCat == null) {
1379:             $fine = false;
1380:             $errorMessages[] = sprintf(i18n('There is no defined start (a category with preid == 0) in %s. Sorting impossible.'), $parentId);
1381:             continue;
1382:         }
1383:         // loop through the categories using the postid
1384:         $actCat = $startCat;
1385:         $checkedCats = array();
1386:         $checkedCats[] = $startCat->get('idcat');
1387:         while ($actCat != null) {
1388:             $catId = $actCat->get('idcat');
1389:             $postId = $actCat->get('postid');
1390:             if ($postId == 0) {
1391:                 break;
1392:             }
1393:             // check if the postid is actually a child of the parent
1394:             if (!array_key_exists($postId, $parent)) {
1395:                 $fine = false;
1396:                 $errorMessages[] = sprintf(i18n('%s has an invalid post-id (%s). The category does not exist in this parent! Sorting impossible.'), $catId, $postId);
1397:                 break;
1398:             }
1399:             $actCat = $catArray[$postId];
1400:             // check if the postid was seen before. if yes that would mean
1401:             // there's a loop in the tree
1402:             if (in_array($actCat->get('idcat'), $checkedCats)) {
1403:                 $fine = false;
1404:                 $errorMessages[] = sprintf(i18n('The sorting in category %s creates an infinite loop (postid = %s). Sorting the category is impossible! (Cause of failure is near category %s)'), $parentId, $postId, $catId);
1405:                 break;
1406:             }
1407:             $checkedCats[] = $actCat->get('idcat');
1408: 
1409:             // check that all categories in this parent belong to the same
1410:             // client
1411:             if (isset($catArray[$parentId])) {
1412:                 $parentClientId = $catArray[$parentId]->get('idclient');
1413:                 if ($actCat->get('idclient') != $parentClientId) {
1414:                     $fine = false;
1415:                     $errorMessages[] = sprintf(i18n('The category %s has a sub category (%s) that belongs to another client!'), $parentId, $catId);
1416:                     break;
1417:                 }
1418:             }
1419:         }
1420: 
1421:         // check the consistency of the preids
1422:         // find the last element (which is the start of the preids)
1423:         $startCat = null;
1424:         foreach ($parent as $cat) {
1425:             if ($cat->get('postid') == 0) {
1426:                 $startCat = $cat;
1427:                 break;
1428:             }
1429:         }
1430:         // if no end was found => error (this most likely means there's some
1431:         // kind of loop too)
1432:         if ($startCat == null) {
1433:             $fine = false;
1434:             $errorMessages[] = sprintf(i18n('There is no defined end (a category with postid == 0) in %s. Sorting impossible.'), $parentId);
1435:             continue;
1436:         }
1437:         // loop through the categories using the preid
1438:         $actCat = $startCat;
1439:         $checkedCats = array();
1440:         $checkedCats[] = $startCat->get('idcat');
1441:         while ($actCat != null) {
1442:             $catId = $actCat->get('idcat');
1443:             $preId = $actCat->get('preid');
1444:             if ($preId == 0) {
1445:                 break;
1446:             }
1447:             // if the preid isn't a child of the parent => error
1448:             if (!array_key_exists($preId, $parent)) {
1449:                 $fine = false;
1450:                 $errorMessages[] = sprintf(i18n('%s has an invalid pre-id (%s). The category does not exist in this parent! Sorting impossible.'), $catId, $preId);
1451:                 break;
1452:             }
1453:             $actCat = $catArray[$preId];
1454:             // if we've seen this preid before, that means there is some kind of
1455:             // loop => error
1456:             if (in_array($actCat->get('idcat'), $checkedCats)) {
1457:                 $fine = false;
1458:                 $errorMessages[] = sprintf(i18n('The sorting in category %s creates an infinite loop (preid = %s). Sorting the category is impossible! (Cause of failure is near category %s)'), $parentId, $preId, $catId);
1459:                 break;
1460:             }
1461:             $checkedCats[] = $actCat->get('idcat');
1462:         }
1463:     }
1464:     // if everything is fine, return false
1465:     // otherwise return the collected error messages
1466:     if (!$fine) {
1467:         $messages = array();
1468:         foreach ($errorMessages as $errorMessage) {
1469:             if (in_array($errorMessage, $messages)) {
1470:                 continue;
1471:             }
1472:             $messages[] = $errorMessage;
1473:         }
1474:         return $messages;
1475:     } else {
1476:         return false;
1477:     }
1478: }
1479: 
CMS CONTENIDO 4.9.8 API documentation generated by ApiGen 2.8.0