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) ? NULL : $lang;
 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) ? NULL : $lang;
 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:             if($newPreCategory != null) {
 945:                 $newPreId = $newPreCategory->get('idcat');
 946:                 $newPreCategory->set('postid', $idcat);
 947:                 $updateCats[$newPreId] = $newPreCategory;
 948:             }
 949:         } else {
 950:             if (isset($updateCats[$newPreId])) {
 951:                 $updateCats[$newPreId]->set('postid', $idcat);
 952:             } else {
 953:                 $newPreCategory = new cApiCategory($newPreId);
 954:                 $newPreCategory->set('postid', $idcat);
 955:                 $updateCats[$newPreId] = $newPreCategory;
 956:                 $newPreId = $newPreCategory->get('idcat');
 957:             }
 958:         }
 959: 
 960:         
 961:         if ($newPostId != 0) {
 962:             if (isset($updateCats[$newPostId])) {
 963:                 $updateCats[$newPostId]->set('preid', $idcat);
 964:             } else {
 965:                 $newPostCategory = new cApiCategory($newPostId);
 966:                 $newPostCategory->set('preid', $idcat);
 967:                 $updateCats[$newPostId] = $newPostCategory;
 968:             }
 969:         }
 970: 
 971:         
 972:         $category->set('parentid', $newParentId);
 973:         $category->set('preid', $newPreId);
 974:         $category->set('postid', $newPostId);
 975:         $updateCats[$idcat] = $category;
 976: 
 977:         $error = strCheckTreeForErrors($updateCats);
 978:         if ($error === false) {
 979:             foreach ($updateCats as $cat) {
 980:                 $cat->store();
 981:             }
 982:         } else {
 983:             $string = '';
 984:             foreach ($error as $msg) {
 985:                 $string .= $msg . '<br>';
 986:             }
 987:             $notification->displayNotification(cGuiNotification::LEVEL_WARNING, $msg . '<br><br>' . i18n('Something went wrong while trying to perform this operation. Please try again.'));
 988:             return false;
 989:         }
 990: 
 991:         $movesubtreeidcat = 0;
 992:     }
 993: 
 994:     $sess = cRegistry::getSession();
 995:     $sess->register('movesubtreeidcat');
 996:     $sess->freeze();
 997: }
 998: 
 999: 1000: 1001: 1002: 1003: 1004: 1005: 
1006: function strMoveCatTargetallowed($idcat, $source) {
1007:     return ($idcat == $source) ? 0 : 1;
1008: }
1009: 
1010: 1011: 1012: 1013: 1014: 1015: 1016: 1017: 
1018: function strSyncCategory($idcatParam, $sourcelang, $targetlang, $bMultiple = false) {
1019:     $bMultiple = (bool) $bMultiple;
1020: 
1021:     $aCatArray = array();
1022:     if ($bMultiple == true) {
1023:         $aCatArray = strDeeperCategoriesArray($idcatParam);
1024:     } else {
1025:         $aCatArray[] = $idcatParam;
1026:     }
1027: 
1028:     foreach ($aCatArray as $idcat) {
1029:         
1030:         $oCatLang = new cApiCategoryLanguage();
1031:         if ($oCatLang->loadByCategoryIdAndLanguageId($idcat, $targetlang)) {
1032:             return false;
1033:         }
1034: 
1035:         
1036:         $oCatLang = new cApiCategoryLanguage();
1037:         if ($oCatLang->loadByCategoryIdAndLanguageId($idcat, $sourcelang)) {
1038:             $aRs = $oCatLang->toArray();
1039: 
1040:             
1041:             $newidtplcfg = ($aRs['idtplcfg'] != 0) ? tplcfgDuplicate($aRs['idtplcfg']) : 0;
1042: 
1043:             $visible = 0;
1044:             $startidartlang = 0;
1045:             $urlpath = '';
1046: 
1047:             $oCatLangColl = new cApiCategoryLanguageCollection();
1048:             $oNewCatLang = $oCatLangColl->create($aRs['idcat'], $targetlang, $aRs['name'], $aRs['urlname'], $urlpath, $newidtplcfg, $visible, $aRs['public'], $aRs['status'], $aRs['author'], $startidartlang, $aRs['created'], $aRs['lastmodified']);
1049: 
1050:             
1051:             $param = $aRs;
1052:             $param['idlang'] = $targetlang;
1053:             $param['idtplcfg'] = (int) $newidtplcfg;
1054:             $param['visible'] = $visible;
1055:             cApiCecHook::execute('Contenido.Category.strSyncCategory_Loop', $param);
1056: 
1057:             
1058:             cInclude('includes', 'functions.rights.php');
1059:             createRightsForElement('str', $idcat, $targetlang);
1060:             createRightsForElement('con', $idcat, $targetlang);
1061:         }
1062:     }
1063: }
1064: 
1065: 1066: 1067: 1068: 1069: 1070: 1071: 
1072: function strHasStartArticle($idcat, $idlang) {
1073:     $oCatLangColl = new cApiCategoryLanguageCollection();
1074:     return ($oCatLangColl->getStartIdartlangByIdcatAndIdlang($idcat, $idlang) > 0);
1075: }
1076: 
1077: 1078: 1079: 1080: 1081: 1082: 1083: 1084: 
1085: function strCopyCategory($idcat, $destidcat, $remakeTree = true, $bUseCopyLabel = true) {
1086:     global $cfg, $lang;
1087: 
1088:     $newidcat = (int) strNewCategory($destidcat, 'a', $remakeTree);
1089:     if ($newidcat == 0) {
1090:         return;
1091:     }
1092: 
1093:     
1094:     $oOldCatLang = new cApiCategoryLanguage();
1095:     if (!$oOldCatLang->loadByCategoryIdAndLanguageId($idcat, $lang)) {
1096:         return;
1097:     }
1098: 
1099:     $oNewCatLang = new cApiCategoryLanguage();
1100:     if (!$oNewCatLang->loadByCategoryIdAndLanguageId($newidcat, $lang)) {
1101:         return;
1102:     }
1103: 
1104:     
1105:     $oNewCat = new cApiCategory((int) $newidcat);
1106:     $oOldCat = new cApiCategory((int) $idcat);
1107: 
1108:     
1109:     if ($bUseCopyLabel == true) {
1110:         $oNewCatLang->set('name', sprintf(i18n('%s (Copy)'), $oOldCatLang->get('name')));
1111:     } else {
1112:         $oNewCatLang->set('name', $oOldCatLang->get('name'));
1113:     }
1114: 
1115:     $oNewCatLang->set('public', $oOldCatLang->get('public'));
1116:     $oNewCatLang->set('visible', 0);
1117:     $oNewCatLang->store();
1118: 
1119:     
1120:     cApiCecHook::execute('Contenido.Category.strCopyCategory', array(
1121:         'oldcat' => $oOldCat,
1122:         'newcat' => $oNewCat,
1123:         'newcatlang' => $oNewCatLang
1124:     ));
1125: 
1126:     
1127:     if ($oOldCatLang->get('idtplcfg') != 0) {
1128:         
1129:         $oNewCatLang->assignTemplate($oOldCatLang->getTemplate());
1130: 
1131:         
1132:         $oContainerConfColl = new cApiContainerConfigurationCollection();
1133:         $oContainerConfColl->select('idtplcfg = ' . (int) $oOldCatLang->get('idtplcfg'));
1134: 
1135:         $oNewContainerConfColl = new cApiContainerConfigurationCollection();
1136:         while (($oItem = $oContainerConfColl->next()) !== false) {
1137:             $oNewContainerConfColl->create($oNewCatLang->get('idtplcfg'), $oItem->get('number'), $oItem->get('container'));
1138:         }
1139:     }
1140: 
1141:     $db = cRegistry::getDb();
1142: 
1143:     $oCatArtColl = new cApiCategoryArticleCollection();
1144: 
1145:     
1146:     $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";
1147:     $db->query($sql, $cfg['tab']['cat_art'], $cfg['tab']['art_lang'], $idcat, $lang);
1148: 
1149:     while ($db->nextRecord()) {
1150:         $newidart = (int) conCopyArticle($db->f('idart'), $newidcat, '', $bUseCopyLabel);
1151:         if ($db->f('idartlang') == $oOldCatLang->get('startidartlang')) {
1152:             $oCatArtColl->resetQuery();
1153:             $idcatart = $oCatArtColl->getIdByCategoryIdAndArticleId($newidcat, $newidart);
1154:             if ($idcatart) {
1155:                 conMakeStart($idcatart, 1);
1156:             }
1157:         }
1158:     }
1159: 
1160:     return $newidcat;
1161: }
1162: 
1163: 1164: 1165: 1166: 1167: 1168: 1169: 1170: 
1171: function strCopyTree($idcat, $destcat, $remakeTree = true, $bUseCopyLabel = true) {
1172:     $newidcat = strCopyCategory($idcat, $destcat, false, $bUseCopyLabel);
1173: 
1174:     $oCatColl = new cApiCategoryCollection();
1175:     $aIds = $oCatColl->getIdsByWhereClause('parentid = ' . (int) $idcat);
1176:     foreach ($aIds as $id) {
1177:         strCopyTree($id, $newidcat, false, $bUseCopyLabel);
1178:     }
1179: 
1180:     if ($remakeTree == true) {
1181:         strRemakeTreeTable();
1182:     }
1183: }
1184: 
1185: 1186: 1187: 1188: 1189: 1190: 1191: 
1192: function strAssignTemplate($idcat, $client, $idTplCfg) {
1193:     global $perm;
1194: 
1195:     
1196:     $iIdtplcfg = ($perm->have_perm_area_action('str_tplcfg', 'str_tplcfg')) ? (int) $idTplCfg : 0;
1197: 
1198:     $idtpl = NULL;
1199:     if ($iIdtplcfg == 0) {
1200:         
1201:         $oTemplateColl = new cApiTemplateCollection('defaulttemplate = 1 AND idclient = ' . (int) $client);
1202:         if (($oTemplate = $oTemplateColl->next()) !== false) {
1203:             $idtpl = $oTemplate->get('idtpl');
1204:         }
1205:     } else {
1206:         
1207:         $idtpl = $idTplCfg;
1208:     }
1209: 
1210:     if ($idtpl) {
1211:         
1212:         $oCatLangColl = new cApiCategoryLanguageCollection('idcat = ' . (int) $idcat);
1213:         while (($oCatLang = $oCatLangColl->next()) !== false) {
1214:             $oCatLang->assignTemplate($idtpl);
1215:         }
1216:     }
1217: }
1218: 
1219: 1220: 1221: 1222: 1223: 1224: 1225: 1226: 1227: 1228: 1229: 1230: 
1231: function strCheckTreeForErrors($addCats = array(), $ignoreCats = array()) {
1232:     $errorMessages = array();
1233: 
1234:     
1235:     $cats = new cApiCategoryCollection();
1236:     $cats->select("idclient = '" . cSecurity::toInteger(cRegistry::getClientId()) . "'");
1237: 
1238:     $catArray = array();
1239:     
1240:     foreach ($addCats as $addCat) {
1241:         if ($addCat->get('idcat') == 0) {
1242:             continue;
1243:         }
1244:         $catArray[$addCat->get('idcat')] = $addCat;
1245:     }
1246: 
1247:     
1248:     while ($cat = $cats->next()) {
1249:         if (in_array($cat->get('idcat'), $ignoreCats)) {
1250:             continue;
1251:         }
1252:         if (isset($catArray[$cat->get('idcat')])) {
1253:             continue;
1254:         }
1255:         $catArray[$cat->get('idcat')] = $cat;
1256:     }
1257: 
1258:     ksort($catArray);
1259: 
1260:     
1261:     
1262:     
1263:     
1264:     
1265:     
1266:     $fine = true;
1267:     $parents = array();
1268:     foreach ($catArray as $idcat => $cat) {
1269:         if (!array_key_exists($cat->get('parentid'), $catArray) && $cat->get('parentid') != 0) {
1270:             $fine = false;
1271:             $errorMessages[] = sprintf(i18n('Category %s has a parent id (%s) which does not exist!'), $idcat, $cat->get('parentid'));
1272:         }
1273:         $parents[$cat->get('parentid')][$idcat] = $cat;
1274:     }
1275: 
1276:     
1277:     foreach ($parents as $parentId => $parent) {
1278:         
1279:         
1280:         
1281:         $preIds = array();
1282:         $postIds = array();
1283:         foreach ($parent as $idcat => $cat) {
1284:             $preId = $cat->get('preid');
1285:             $postId = $cat->get('postid');
1286:             if (in_array($preId, $preIds)) {
1287:                 $fine = false;
1288:                 $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);
1289:             }
1290:             if (in_array($postId, $postIds)) {
1291:                 $fine = false;
1292:                 $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);
1293:             }
1294:             $preIds[] = $preId;
1295:             $postIds[] = $postId;
1296:         }
1297: 
1298:         
1299:         
1300:         $startCat = null;
1301:         foreach ($parent as $cat) {
1302:             if ($cat->get('preid') == 0) {
1303:                 $startCat = $cat;
1304:                 break;
1305:             }
1306:         }
1307:         
1308:         if ($startCat == null) {
1309:             $fine = false;
1310:             $errorMessages[] = sprintf(i18n('There is no defined start (a category with preid == 0) in %s. Sorting impossible.'), $parentId);
1311:             continue;
1312:         }
1313:         
1314:         $actCat = $startCat;
1315:         $checkedCats = array();
1316:         $checkedCats[] = $startCat->get('idcat');
1317:         while ($actCat != null) {
1318:             $catId = $actCat->get('idcat');
1319:             $postId = $actCat->get('postid');
1320:             if ($postId == 0) {
1321:                 break;
1322:             }
1323:             
1324:             if (!array_key_exists($postId, $parent)) {
1325:                 $fine = false;
1326:                 $errorMessages[] = sprintf(i18n('%s has an invalid post-id (%s). The category does not exist in this parent! Sorting impossible.'), $catId, $postId);
1327:                 break;
1328:             }
1329:             $actCat = $catArray[$postId];
1330:             
1331:             
1332:             if (in_array($actCat->get('idcat'), $checkedCats)) {
1333:                 $fine = false;
1334:                 $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);
1335:                 break;
1336:             }
1337:             $checkedCats[] = $actCat->get('idcat');
1338: 
1339:             
1340:             
1341:             if (isset($catArray[$parentId])) {
1342:                 $parentClientId = $catArray[$parentId]->get('idclient');
1343:                 if ($actCat->get('idclient') != $parentClientId) {
1344:                     $fine = false;
1345:                     $errorMessages[] = sprintf(i18n('The category %s has a sub category (%s) that belongs to another client!'), $parentId, $catId);
1346:                     break;
1347:                 }
1348:             }
1349:         }
1350: 
1351:         
1352:         
1353:         $startCat = null;
1354:         foreach ($parent as $cat) {
1355:             if ($cat->get('postid') == 0) {
1356:                 $startCat = $cat;
1357:                 break;
1358:             }
1359:         }
1360:         
1361:         
1362:         if ($startCat == null) {
1363:             $fine = false;
1364:             $errorMessages[] = sprintf(i18n('There is no defined end (a category with postid == 0) in %s. Sorting impossible.'), $parentId);
1365:             continue;
1366:         }
1367:         
1368:         $actCat = $startCat;
1369:         $checkedCats = array();
1370:         $checkedCats[] = $startCat->get('idcat');
1371:         while ($actCat != null) {
1372:             $catId = $actCat->get('idcat');
1373:             $preId = $actCat->get('preid');
1374:             if ($preId == 0) {
1375:                 break;
1376:             }
1377:             
1378:             if (!array_key_exists($preId, $parent)) {
1379:                 $fine = false;
1380:                 $errorMessages[] = sprintf(i18n('%s has an invalid pre-id (%s). The category does not exist in this parent! Sorting impossible.'), $catId, $preId);
1381:                 break;
1382:             }
1383:             $actCat = $catArray[$preId];
1384:             
1385:             
1386:             if (in_array($actCat->get('idcat'), $checkedCats)) {
1387:                 $fine = false;
1388:                 $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);
1389:                 break;
1390:             }
1391:             $checkedCats[] = $actCat->get('idcat');
1392:         }
1393:     }
1394:     
1395:     
1396:     if (!$fine) {
1397:         $messages = array();
1398:         foreach ($errorMessages as $errorMessage) {
1399:             if (in_array($errorMessage, $messages)) {
1400:                 continue;
1401:             }
1402:             $messages[] = $errorMessage;
1403:         }
1404:         return $messages;
1405:     } else {
1406:         return false;
1407:     }
1408: }
1409: