Overview

Packages

  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Datatype
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

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

Functions

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