1: <?php
   2:    3:    4:    5:    6:    7:    8:    9:   10:   11:   12:   13:   14:   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:   24:   25:   26:   27:   28:   29:   30:   31: 
  32: function strNewTree($catname, $catalias = '', $visible = 0, $public = 1, $iIdtplcfg = 0) {
  33:     global $client, $lang, $perm;
  34: 
  35:     
  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:     
  66:     $oCatColl = new cApiCategoryCollection();
  67:     $oLastCatTree = $oCatColl->fetchLastCategoryTree($client);
  68:     $lastCatTreeId = (is_object($oLastCatTree))? $oLastCatTree->get('idcat') : 0;
  69: 
  70:     
  71:     $oCatColl2 = new cApiCategoryCollection();
  72:     $oNewCat = $oCatColl2->create($client, 0, $lastCatTreeId, 0);
  73:     $newIdcat = $oNewCat->get('idcat');
  74:     $oldPostId = -1;
  75: 
  76:     
  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:     
  96:     $aLanguages = array(
  97:         $lang
  98:     );
  99:     foreach ($aLanguages as $curLang) {
 100:         $name = $catname;
 101:         $urlname = conHtmlSpecialChars(cApiStrCleanURLCharacters($catalias), ENT_QUOTES);
 102: 
 103:         
 104:         $oCatLangColl = new cApiCategoryLanguageCollection();
 105:         $oCatLangColl->create($newIdcat, $curLang, $name, $urlname, '', 0, $visible, $public, 0, '', 0);
 106: 
 107:         
 108:         createRightsForElement('str', $newIdcat, $curLang);
 109:         createRightsForElement('con', $newIdcat, $curLang);
 110:     }
 111: 
 112:     
 113:     strAssignTemplate($newIdcat, $client, $iIdtplcfg);
 114: 
 115:     return $newIdcat;
 116: }
 117: 
 118:  119:  120:  121:  122:  123:  124:  125:  126:  127:  128:  129: 
 130: function strNewCategory($parentid, $catname, $remakeTree = true, $catalias = '', $visible = 0, $public = 1, $iIdtplcfg = 0) {
 131:     global $client, $lang, $perm;
 132: 
 133:     
 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:     
 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:     
 172:     $oCatColl2 = new cApiCategoryCollection();
 173:     $oNewCat = $oCatColl2->create($client, $parentid, $preIdcat, 0);
 174:     $newIdcat = $oNewCat->get('idcat');
 175:     $oldPostId = -1;
 176: 
 177:     
 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:     
 199:     $aLanguages = array(
 200:         $lang
 201:     );
 202:     foreach ($aLanguages as $curLang) {
 203:         $name = $catname;
 204:         $urlname = conHtmlSpecialChars(cApiStrCleanURLCharacters($catalias), ENT_QUOTES);
 205: 
 206:         
 207:         $oCatLangColl = new cApiCategoryLanguageCollection();
 208:         $oCatLangColl->create($newIdcat, $curLang, $name, $urlname, '', 0, $visible, $public, 0, '', 0);
 209: 
 210:         
 211:         copyRightsForElement('str', $parentid, $newIdcat, $curLang);
 212:         copyRightsForElement('con', $parentid, $newIdcat, $curLang);
 213:     }
 214: 
 215:     if ($remakeTree == true) {
 216:         strRemakeTreeTable();
 217:     }
 218: 
 219:     
 220:     strAssignTemplate($newIdcat, $client, $iIdtplcfg);
 221: 
 222:     return $newIdcat;
 223: }
 224: 
 225:  226:  227:  228:  229:  230:  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:  246:  247:  248: 
 249: function strRemakeTreeTable() {
 250:     global $db, $client, $cfg;
 251: 
 252:     
 253:     global $remakeCatTable;
 254:     global $remakeStrTable;
 255: 
 256:     
 257:     $oCatColl = new cApiCategoryCollection();
 258:     $idcats = $oCatColl->getCategoryIdsByClient($client);
 259:     if (0 === count($idcats)) {
 260:         
 261:         return;
 262:     }
 263: 
 264:     $errors = strCheckTreeForErrors();
 265:     if (!($errors === false)) {
 266:         return;
 267:     }
 268: 
 269:     $remakeCatTable = true;
 270:     $remakeStrTable = true;
 271: 
 272:     
 273:     $sql = 'DELETE FROM ' . $cfg['tab']['cat_tree'] . ' WHERE idcat IN (' . implode(', ', $idcats) . ')';
 274:     $db->query($sql);
 275: 
 276:     
 277:     
 278:     
 279:     $sql = 'DELETE FROM ' . $cfg['tab']['cat'] . ' WHERE idcat = 0';
 280:     $db->query($sql);
 281: 
 282:     
 283:     
 284:     
 285:     $sql = 'DELETE FROM ' . $cfg['tab']['cat_lang'] . ' WHERE idcat = 0';
 286:     $db->query($sql);
 287: 
 288:     
 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:     
 301:     $sInsertQuery = "INSERT INTO " . $cfg['tab']['cat_tree'] . " (idcat, level) VALUES ";
 302:     $sInsertQuery = strBuildSqlValues($aCategories[0], $sInsertQuery, $aCategories);
 303:     $sInsertQuery = rtrim($sInsertQuery, " ,");
 304: 
 305:     
 306:     $db->query($sInsertQuery);
 307: }
 308: 
 309:  310:  311:  312:  313:  314:  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:     
 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(); 
 337:     }
 338: 
 339:     while ($curId != 0) {
 340:         $array[] = $arr[$curId];
 341:         $curId = $arr[$curId]['postid'];
 342:     }
 343: 
 344:     return $array;
 345: }
 346: 
 347:  348:  349:  350:  351:  352:  353:  354:  355:  356:  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:  374:  375:  376:  377:  378:  379:  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:  391:  392:  393:  394: 
 395: function strHasArticles($idcat) {
 396:     global $lang;
 397: 
 398:     $oCatArtColl = new cApiCategoryArticleCollection();
 399:     return $oCatArtColl->getHasArticles($idcat, $lang);
 400: }
 401: 
 402:  403:  404:  405:  406:  407: 
 408: function strNextPost($idcat) {
 409:     $oCatColl = new cApiCategoryCollection();
 410:     return $oCatColl->getNextPostCategoryId($idcat);
 411: }
 412: 
 413:  414:  415:  416:  417:  418: 
 419: function strNextBackwards($idcat) {
 420:     $oCatColl = new cApiCategoryCollection();
 421:     return $oCatColl->getParentsNextPostCategoryId($idcat);
 422: }
 423: 
 424:  425:  426:  427:  428:  429:  430:  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:  442:  443:  444:  445:  446:  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:         
 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:         
 470:         
 471:         
 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:  494:  495:  496:  497:  498: 
 499: function strRenameCategoryAlias($idcat, $lang, $newcategoryalias) {
 500:     $oCatLang = new cApiCategoryLanguage();
 501:     if (!$oCatLang->loadByCategoryIdAndLanguageId($idcat, $lang)) {
 502:         
 503:         return;
 504:     }
 505: 
 506:     $oldData = array(
 507:         'idcat' => $oCatLang->get('idcat'),
 508:         'urlname' => $oCatLang->get('urlname')
 509:     );
 510: 
 511:     if (trim($newcategoryalias) == '') {
 512:         
 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:  534:  535:  536:  537:  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:  562:  563:  564:  565:  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:  580:  581:  582:  583: 
 584: function strDeeperCategoriesArray($startIdcat) {
 585:     global $client;
 586: 
 587:     $oCatColl = new cApiCategoryCollection();
 588:     return $oCatColl->getAllCategoryIdsRecursive($startIdcat, $client);
 589: }
 590: 
 591:  592:  593:  594:  595:  596:  597:  598: 
 599: function strDeleteCategory($idcat) {
 600:     global $lang, $lang;
 601: 
 602:     
 603:     global $remakeCatTable, $remakeStrTable;
 604: 
 605:     if (strNextDeeper($idcat)) {
 606:         
 607:         return '0201';
 608:     } elseif (strHasArticles($idcat)) {
 609:         
 610:         return '0202';
 611:     }
 612: 
 613:     cInclude('includes', 'functions.rights.php');
 614: 
 615:     $remakeCatTable = true;
 616:     $remakeStrTable = true;
 617: 
 618:     
 619:     $oCatLang = new cApiCategoryLanguage();
 620:     $oCatLang->loadByCategoryIdAndLanguageId($idcat, $lang);
 621: 
 622:     if ($oCatLang->isLoaded()) {
 623:         
 624:         
 625:         $oTemplateConfigColl = new cApiTemplateConfigurationCollection();
 626:         $oTemplateConfigColl->delete($oCatLang->get('idtplcfg'));
 627: 
 628:         
 629:         $oCatLangColl = new cApiCategoryLanguageCollection();
 630:         $oCatLangColl->delete($oCatLang->get('idcatlang'));
 631:     }
 632: 
 633:     
 634:     $oCatLangColl = new cApiCategoryLanguageCollection();
 635:     $oCatLangColl->select('idcat = ' . (int) $idcat);
 636:     if (($oCatLang = $oCatLangColl->next()) !== false) {
 637:         
 638:         deleteRightsForElement('str', $idcat, $lang);
 639:         deleteRightsForElement('con', $idcat, $lang);
 640:         return;
 641:     }
 642: 
 643:     
 644:     $oCat = new cApiCategory((int) $idcat);
 645:     $preid = (int) $oCat->get('preid');
 646:     $postid = (int) $oCat->get('postid');
 647: 
 648:     
 649:     if ($preid != 0) {
 650:         $oPreCat = new cApiCategory($preid);
 651:         $oPreCat->set('postid', $postid);
 652:         $oPreCat->store();
 653:     }
 654: 
 655:     
 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:     
 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:         
 687:         
 688:         $oTemplateConfigColl = new cApiTemplateConfigurationCollection();
 689:         $oTemplateConfigColl->delete($oCatLang->get('idtplcfg'));
 690:     }
 691: 
 692:     
 693:     $oCatLangColl->resetQuery();
 694:     $oCatLangColl->deleteBy('idcat', (int) $idcat);
 695: 
 696:     
 697:     $oCatTreeColl = new cApiCategoryTreeCollection();
 698:     $oCatTreeColl->deleteBy('idcat', (int) $idcat);
 699: 
 700:     
 701:     deleteRightsForElement('str', $idcat);
 702:     deleteRightsForElement('con', $idcat);
 703: }
 704: 
 705:  706:  707:  708:  709: 
 710: function strMoveUpCategory($idcat) {
 711:     
 712:     global $remakeCatTable, $remakeStrTable;
 713: 
 714:     
 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:         
 722:         return;
 723:     }
 724: 
 725:     $remakeCatTable = true;
 726:     $remakeStrTable = true;
 727: 
 728:     
 729:     $oPreCat = new cApiCategory();
 730:     $oPreCat->loadByPrimaryKey((int) $preid);
 731:     $prePreid = $oPreCat->get('preid');
 732:     $preIdcat = $oPreCat->get('idcat');
 733: 
 734:     
 735:     $oPrePreCat = new cApiCategory();
 736:     if ((int) $prePreid > 0) {
 737:         $oPrePreCat->loadByPrimaryKey((int) $prePreid);
 738:     }
 739: 
 740:     
 741:     $oPostCat = new cApiCategory();
 742:     if ((int) $postid > 0) {
 743:         $oPostCat->loadByPrimaryKey((int) $postid);
 744:     }
 745: 
 746:     $updateCats = array();
 747: 
 748:     
 749:     if ($oPrePreCat->isLoaded()) {
 750:         $oPrePreCat->set('postid', $idcat);
 751:         $updateCats[$prePreid] = $oPrePreCat;
 752:     }
 753: 
 754:     
 755:     $oPreCat->set('preid', $idcat);
 756:     $oPreCat->set('postid', $postid);
 757:     $updateCats[$preid] = $oPreCat;
 758: 
 759:     
 760:     $oCat->set('preid', $prePreid);
 761:     $oCat->set('postid', $preid);
 762:     $updateCats[$idcat] = $oCat;
 763: 
 764:     
 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:  785:  786:  787: 
 788: function strMoveDownCategory($idcat) {
 789:     
 790:     global $remakeCatTable, $remakeStrTable;
 791: 
 792:     
 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:         
 800:         return;
 801:     }
 802: 
 803:     $remakeCatTable = true;
 804:     $remakeStrTable = true;
 805: 
 806:     
 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:     
 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:         
 825:         $oPreCat->set('postid', (int) $postIdcat);
 826:         $updateCats[$preIdcat] = $oPreCat;
 827:     }
 828: 
 829:     
 830:     $oCat->set('preid', $postid);
 831:     $oCat->set('postid', $postPostid);
 832:     $updateCats[$idcat] = $oCat;
 833: 
 834:     
 835:     $oPostCat->set('preid', $preIdcat);
 836:     $oPostCat->set('postid', $idcat);
 837:     $updateCats[$postid] = $oPostCat;
 838: 
 839:     if ($postPostid != 0) {
 840:         
 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:  863:  864:  865:  866:  867:  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:         
 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:         
 892:     }
 893:     
 894:     global $remakeCatTable, $remakeStrTable;
 895: 
 896:     $remakeCatTable = true;
 897:     $remakeStrTable = true;
 898: 
 899:     
 900:     if (is_null($newPostId)) {
 901:         $newPostId = 0;
 902:     }
 903: 
 904:     if ($newParentId == -1) {
 905:         
 906:         $movesubtreeidcat = 0;
 907:     } else if (is_null($newParentId)) {
 908:         
 909:         $movesubtreeidcat = $idcat;
 910:     } else {
 911:         
 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:         
 920:         if ($oldPreId != 0) {
 921:             $oldPreCategory = new cApiCategory($oldPreId);
 922:             $oldPreCategory->set('postid', $oldPostId);
 923:             $updateCats[$oldPreId] = $oldPreCategory;
 924:         }
 925: 
 926:         
 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:         
 938:         if (is_null($newPreId)) {
 939:             
 940:             
 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:         
 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:         
 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:  997:  998:  999: 1000: 1001: 
1002: function strMoveCatTargetallowed($idcat, $source) {
1003:     return ($idcat == $source)? 0 : 1;
1004: }
1005: 
1006: 1007: 1008: 1009: 1010: 1011: 1012: 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:         
1026:         $oCatLang = new cApiCategoryLanguage();
1027:         if ($oCatLang->loadByCategoryIdAndLanguageId($idcat, $targetlang)) {
1028:             return false;
1029:         }
1030: 
1031:         
1032:         $oCatLang = new cApiCategoryLanguage();
1033:         if ($oCatLang->loadByCategoryIdAndLanguageId($idcat, $sourcelang)) {
1034:             $aRs = $oCatLang->toArray();
1035: 
1036:             
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:             
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:             
1054:             cInclude('includes', 'functions.rights.php');
1055:             createRightsForElement('str', $idcat, $targetlang);
1056:             createRightsForElement('con', $idcat, $targetlang);
1057:         }
1058:     }
1059: }
1060: 
1061: 1062: 1063: 1064: 1065: 1066: 1067: 
1068: function strHasStartArticle($idcat, $idlang) {
1069:     $oCatLangColl = new cApiCategoryLanguageCollection();
1070:     return ($oCatLangColl->getStartIdartlangByIdcatAndIdlang($idcat, $idlang) > 0);
1071: }
1072: 
1073: 1074: 1075: 1076: 1077: 1078: 1079: 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:     
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:     
1101:     $oNewCat = new cApiCategory((int) $newidcat);
1102:     $oOldCat = new cApiCategory((int) $idcat);
1103: 
1104:     
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:     
1116:     cApiCecHook::execute('Contenido.Category.strCopyCategory', array(
1117:         'oldcat' => $oOldCat,
1118:         'newcat' => $oNewCat,
1119:         'newcatlang' => $oNewCatLang
1120:     ));
1121: 
1122:     
1123:     if ($oOldCatLang->get('idtplcfg') != 0) {
1124:         
1125:         $oNewCatLang->assignTemplate($oOldCatLang->getTemplate());
1126: 
1127:         
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:     
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: 1161: 1162: 1163: 1164: 1165: 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: 1183: 1184: 1185: 1186: 1187: 
1188: function strAssignTemplate($idcat, $client, $idTplCfg) {
1189:     global $perm;
1190: 
1191:     
1192:     $iIdtplcfg = ($perm->have_perm_area_action('str_tplcfg', 'str_tplcfg'))? (int) $idTplCfg : 0;
1193: 
1194:     $idtpl = NULL;
1195:     if ($iIdtplcfg == 0) {
1196:         
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:         
1203:         $idtpl = $idTplCfg;
1204:     }
1205: 
1206:     if ($idtpl) {
1207:         
1208:         $oCatLangColl = new cApiCategoryLanguageCollection('idcat = ' . (int) $idcat);
1209:         while (($oCatLang = $oCatLangColl->next()) !== false) {
1210:             $oCatLang->assignTemplate($idtpl);
1211:         }
1212:     }
1213: }
1214: 
1215: 1216: 1217: 1218: 1219: 1220: 1221: 1222: 1223: 1224: 1225: 1226: 
1227: function strCheckTreeForErrors($addCats = array(), $ignoreCats = array()) {
1228:     $errorMessages = array();
1229: 
1230:     
1231:     $cats = new cApiCategoryCollection();
1232:     $cats->select("idclient = '" . cSecurity::toInteger(cRegistry::getClientId()) . "'");
1233: 
1234:     $catArray = array();
1235:     
1236:     foreach ($addCats as $addCat) {
1237:         if ($addCat->get('idcat') == 0) {
1238:             continue;
1239:         }
1240:         $catArray[$addCat->get('idcat')] = $addCat;
1241:     }
1242: 
1243:     
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:     
1257:     
1258:     
1259:     
1260:     
1261:     
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:     
1273:     foreach ($parents as $parentId => $parent) {
1274:         
1275:         
1276:         
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:         
1295:         
1296:         $startCat = null;
1297:         foreach ($parent as $cat) {
1298:             if ($cat->get('preid') == 0) {
1299:                 $startCat = $cat;
1300:                 break;
1301:             }
1302:         }
1303:         
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:         
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:             
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:             
1327:             
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:             
1336:             
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:         
1348:         
1349:         $startCat = null;
1350:         foreach ($parent as $cat) {
1351:             if ($cat->get('postid') == 0) {
1352:                 $startCat = $cat;
1353:                 break;
1354:             }
1355:         }
1356:         
1357:         
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:         
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:             
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:             
1381:             
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:     
1391:     
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: