1: <?php
   2:    3:    4:    5:    6:    7:    8:    9:   10:   11:   12:   13:   14:   15: 
  16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
  17: 
  18: cInclude('includes', 'functions.con.php');
  19: cInclude('includes', 'functions.database.php');
  20: 
  21:   22:   23:   24:   25:   26:   27:   28:   29:   30: 
  31: function strNewTree($catname, $catalias = '', $visible = 0, $public = 1, $iIdtplcfg = 0) {
  32:     global $client, $lang, $perm;
  33: 
  34:     
  35:     global $remakeCatTable, $remakeStrTable;
  36: 
  37:     if (trim($catname) == '') {
  38:         return;
  39:     }
  40: 
  41:     $catname = stripslashes($catname);
  42: 
  43:     $remakeCatTable = true;
  44:     $remakeStrTable = true;
  45: 
  46:     $catalias = trim($catalias);
  47:     if ($catalias == '') {
  48:         $catalias = trim($catname);
  49:     }
  50: 
  51:     $client = (int) $client;
  52:     $lang = (int) $lang;
  53: 
  54:     $visible = ($visible == 1)? 1 : 0;
  55:     if (!$perm->have_perm_area_action('str', 'str_makevisible')) {
  56:         $visible = 0;
  57:     }
  58: 
  59:     $public = ($public == 1)? 1 : 0;
  60:     if (!$perm->have_perm_area_action('str', 'str_makepublic')) {
  61:         $public = 1;
  62:     }
  63: 
  64:     
  65:     $oCatColl = new cApiCategoryCollection();
  66:     $oLastCatTree = $oCatColl->fetchLastCategoryTree($client);
  67:     $lastCatTreeId = (is_object($oLastCatTree))? $oLastCatTree->get('idcat') : 0;
  68: 
  69:     
  70:     $oCatColl2 = new cApiCategoryCollection();
  71:     $oNewCat = $oCatColl2->create($client, 0, $lastCatTreeId, 0);
  72:     $newIdcat = $oNewCat->get('idcat');
  73: 
  74:     
  75:     if (is_object($oLastCatTree)) {
  76:         $oLastCatTree->set('postid', $newIdcat);
  77:         $oLastCatTree->store();
  78:     }
  79: 
  80:     cInclude('includes', 'functions.rights.php');
  81: 
  82:     
  83:     $aLanguages = array(
  84:         $lang
  85:     );
  86:     foreach ($aLanguages as $curLang) {
  87:         $name = $catname;
  88:         $urlname = conHtmlSpecialChars(cApiStrCleanURLCharacters($catalias), ENT_QUOTES);
  89: 
  90:         
  91:         $oCatLangColl = new cApiCategoryLanguageCollection();
  92:         $oCatLangColl->create($newIdcat, $curLang, $name, $urlname, '', 0, $visible, $public, 0, '', 0);
  93: 
  94:         
  95:         createRightsForElement('str', $newIdcat, $curLang);
  96:         createRightsForElement('con', $newIdcat, $curLang);
  97:     }
  98: 
  99:     
 100:     strAssignTemplate($newIdcat, $client, $iIdtplcfg);
 101: 
 102:     return $newIdcat;
 103: }
 104: 
 105:  106:  107:  108:  109:  110:  111:  112:  113:  114:  115:  116: 
 117: function strNewCategory($parentid, $catname, $remakeTree = true, $catalias = '', $visible = 0, $public = 1, $iIdtplcfg = 0) {
 118:     global $client, $lang, $perm;
 119: 
 120:     
 121:     global $remakeCatTable, $remakeStrTable;
 122: 
 123:     $parentid = (int) $parentid;
 124: 
 125:     if (trim($catname) == '') {
 126:         return;
 127:     }
 128: 
 129:     $catname = stripslashes($catname);
 130: 
 131:     $remakeCatTable = true;
 132:     $remakeStrTable = true;
 133: 
 134:     $catalias = trim($catalias);
 135:     if ($catalias == '') {
 136:         $catalias = trim($catname);
 137:     }
 138: 
 139:     $client = (int) $client;
 140:     $lang = (int) $lang;
 141: 
 142:     $visible = ($visible == 1)? 1 : 0;
 143:     if (!$perm->have_perm_area_action('str', 'str_makevisible')) {
 144:         $visible = 0;
 145:     }
 146: 
 147:     $public = ($public == 1)? 1 : 0;
 148:     if (!$perm->have_perm_area_action('str', 'str_makepublic')) {
 149:         $public = 1;
 150:     }
 151: 
 152:     
 153:     $oCatColl = new cApiCategoryCollection();
 154:     $oCatColl->select('parentid=' . $parentid . ' AND postid = 0 AND idclient = ' . $client);
 155:     $oPrevCat = $oCatColl->next();
 156:     $preIdcat = (is_object($oPrevCat))? $oPrevCat->get('idcat') : 0;
 157: 
 158:     
 159:     $oCatColl2 = new cApiCategoryCollection();
 160:     $oNewCat = $oCatColl2->create($client, $parentid, $preIdcat, 0);
 161:     $newIdcat = $oNewCat->get('idcat');
 162: 
 163:     
 164:     if (is_object($oPrevCat)) {
 165:         $oPrevCat->set('postid', $newIdcat);
 166:         $oPrevCat->set('lastmodified', date('Y-m-d H:i:s'));
 167:         $oPrevCat->store();
 168:     }
 169: 
 170:     cInclude('includes', 'functions.rights.php');
 171: 
 172:     
 173:     $aLanguages = array(
 174:         $lang
 175:     );
 176:     foreach ($aLanguages as $curLang) {
 177:         $name = $catname;
 178:         $urlname = conHtmlSpecialChars(cApiStrCleanURLCharacters($catalias), ENT_QUOTES);
 179: 
 180:         
 181:         $oCatLangColl = new cApiCategoryLanguageCollection();
 182:         $oCatLangColl->create($newIdcat, $curLang, $name, $urlname, '', 0, $visible, $public, 0, '', 0);
 183: 
 184:         
 185:         copyRightsForElement('str', $parentid, $newIdcat, $curLang);
 186:         copyRightsForElement('con', $parentid, $newIdcat, $curLang);
 187:     }
 188: 
 189:     if ($remakeTree == true) {
 190:         strRemakeTreeTable();
 191:     }
 192: 
 193:     
 194:     strAssignTemplate($newIdcat, $client, $iIdtplcfg);
 195: 
 196:     return $newIdcat;
 197: }
 198: 
 199:  200:  201:  202:  203:  204:  205: 
 206: function strOrderedPostTreeList($idcat, $poststring) {
 207:     $oCatColl = new cApiCategoryCollection();
 208:     $oCatColl->select('parentid = 0 AND preid = ' . (int) $idcat . ' AND idcat != 0');
 209:     if (($oCat = $oCatColl->next()) !== false) {
 210:         $postIdcat = $oCat->get('idcat');
 211:         $poststring = $poststring . ',' . $postIdcat;
 212:         $poststring = strOrderedPostTreeList($postIdcat, $poststring);
 213:     }
 214: 
 215:     return $poststring;
 216: }
 217: 
 218:  219:  220:  221:  222: 
 223: function strRemakeTreeTable() {
 224:     global $db, $client, $cfg;
 225: 
 226:     
 227:     global $remakeCatTable;
 228:     global $remakeStrTable;
 229: 
 230:     
 231:     $oCatColl = new cApiCategoryCollection();
 232:     $idcats = $oCatColl->getCategoryIdsByClient($client);
 233:     if (0 === count($idcats)) {
 234:         
 235:         return;
 236:     }
 237: 
 238:     $remakeCatTable = true;
 239:     $remakeStrTable = true;
 240: 
 241:     
 242:     $sql = 'DELETE FROM ' . $cfg['tab']['cat_tree'] . ' WHERE idcat IN (' . implode(', ', $idcats) . ')';
 243:     $db->query($sql);
 244: 
 245:     
 246:     
 247:     
 248:     $sql = 'DELETE FROM ' . $cfg['tab']['cat'] . ' WHERE idcat = 0';
 249:     $db->query($sql);
 250: 
 251:     
 252:     
 253:     
 254:     $sql = 'DELETE FROM ' . $cfg['tab']['cat_lang'] . ' WHERE idcat = 0';
 255:     $db->query($sql);
 256: 
 257:     
 258:     $sql = "SELECT idcat, parentid, preid, postid FROM " . $cfg['tab']['cat'] . " WHERE idclient = " . (int) $client . " ORDER BY parentid ASC, preid ASC, postid ASC";
 259:     $aCategories = array();
 260:     $db->query($sql);
 261:     while ($db->nextRecord()) {
 262:         $rs = $db->toArray();
 263:         if (!isset($aCategories[$rs['parentid']])) {
 264:             $aCategories[$rs['parentid']] = array();
 265:         }
 266:         $aCategories[$rs['parentid']][$rs['idcat']] = $rs;
 267:     }
 268: 
 269:     
 270:     $sInsertQuery = "INSERT INTO " . $cfg['tab']['cat_tree'] . " (idcat, level) VALUES ";
 271:     $sInsertQuery = strBuildSqlValues($aCategories[0], $sInsertQuery, $aCategories);
 272:     $sInsertQuery = rtrim($sInsertQuery, " ,");
 273: 
 274:     
 275:     $db->query($sInsertQuery);
 276: }
 277: 
 278:  279:  280:  281:  282:  283:  284: 
 285: function strSortPrePost($arr) {
 286:     $firstElement = null;
 287:     foreach ($arr as $row) {
 288:         if ($row['preid'] == 0) {
 289:             $firstElement = $row['idcat'];
 290:         }
 291:     }
 292: 
 293:     $curId = $firstElement;
 294:     $array = array();
 295: 
 296:     
 297:     $checkedIds = array();
 298:     foreach ($arr as $row) {
 299:         if (in_array($row['postid'], $checkedIds) || $row['idcat'] == $row['postid']) {
 300:             
 301:             
 302:             continue;
 303:         }
 304:         $checkedIds[] = $row['idcat'];
 305:     }
 306: 
 307:     
 308:     $fine = false;
 309:     foreach ($arr as $row) {
 310:         if ($row['postid'] == 0) {
 311:             $fine = true;
 312:             break;
 313:         }
 314:     }
 315:     if (!$fine) {
 316:         die(i18n("The list of categories is messed up. The order in the list creates an infinite loop. Check you database."));
 317:     }
 318: 
 319:     while ($curId != 0) {
 320:         $array[] = $arr[$curId];
 321:         $curId = $arr[$curId]['postid'];
 322:     }
 323: 
 324:     return $array;
 325: }
 326: 
 327:  328:  329:  330:  331:  332:  333:  334:  335:  336:  337: 
 338: function strBuildSqlValues($aCats, $sInsertQuery, &$aAllCats, $iLevel = 0) {
 339:     if (is_array($aCats)) {
 340:         $aCats = strSortPrePost($aCats);
 341:         foreach ($aCats as $aCat) {
 342:             $sInsertQuery .= '(' . (int) $aCat['idcat'] . ', ' . (int) $iLevel . '), ';
 343:             if (is_array($aAllCats[$aCat['idcat']])) {
 344:                 $iSubLevel = $iLevel + 1;
 345:                 $sInsertQuery = strBuildSqlValues($aAllCats[$aCat['idcat']], $sInsertQuery, $aAllCats, $iSubLevel);
 346:             }
 347:         }
 348:     }
 349:     return $sInsertQuery;
 350: }
 351: 
 352:  353:  354:  355:  356:  357:  358:  359:  360: 
 361: function strNextDeeper($idcat, $ignoreLang = false) {
 362:     global $lang;
 363: 
 364:     $languageId = (true == $ignoreLang)? $lang : null;
 365:     $oCatColl = new cApiCategoryCollection();
 366:     return $oCatColl->getFirstChildCategoryId($idcat, $languageId);
 367: }
 368: 
 369:  370:  371:  372:  373:  374: 
 375: function strHasArticles($idcat) {
 376:     global $lang;
 377: 
 378:     $oCatArtColl = new cApiCategoryArticleCollection();
 379:     return $oCatArtColl->getHasArticles($idcat, $lang);
 380: }
 381: 
 382:  383:  384:  385:  386:  387: 
 388: function strNextPost($idcat) {
 389:     $oCatColl = new cApiCategoryCollection();
 390:     return $oCatColl->getNextPostCategoryId($idcat);
 391: }
 392: 
 393:  394:  395:  396:  397:  398: 
 399: function strNextBackwards($idcat) {
 400:     $oCatColl = new cApiCategoryCollection();
 401:     return $oCatColl->getParentsNextPostCategoryId($idcat);
 402: }
 403: 
 404:  405:  406:  407:  408:  409:  410:  411: 
 412: function strNextDeeperAll($idcat, $ignoreLang = false) {
 413:     global $lang;
 414: 
 415:     $languageId = (true == $ignoreLang)? $lang : null;
 416:     $oCatColl = new cApiCategoryCollection();
 417:     return $oCatColl->getAllChildCategoryIds($idcat, $languageId);
 418: }
 419: 
 420:  421:  422:  423:  424:  425:  426:  427: 
 428: function strRenameCategory($idcat, $lang, $newCategoryName, $newCategoryAlias) {
 429:     if (trim($newCategoryName) == '') {
 430:         return;
 431:     }
 432: 
 433:     $oCatLang = new cApiCategoryLanguage();
 434:     if (!$oCatLang->loadByCategoryIdAndLanguageId($idcat, $lang)) {
 435:         
 436:         return;
 437:     }
 438: 
 439:     $oldData = array(
 440:         'idcat' => $oCatLang->get('idcat'),
 441:         'name' => $oCatLang->get('name'),
 442:         'urlname' => $oCatLang->get('urlname')
 443:     );
 444: 
 445:     $name = stripslashes($newCategoryName);
 446:     $urlName = (trim($newCategoryAlias) != '')? trim($newCategoryAlias) : $newCategoryName;
 447: 
 448:     if (trim($newCategoryAlias) != '') {
 449:         
 450:         
 451:         
 452:         
 453:         cInclude('includes', 'functions.pathresolver.php');
 454:         $client = cRegistry::getClientId();
 455:         prDeleteCacheFileContent($client, $lang);
 456:     }
 457: 
 458:     $oCatLang->set('name', $name);
 459:     $oCatLang->set('urlname', $urlName);
 460:     $oCatLang->set('lastmodified', date('Y-m-d H:i:s'));
 461:     $oCatLang->store();
 462: 
 463:     $newData = array(
 464:         'idcat' => $idcat,
 465:         'name' => $name,
 466:         'urlname' => $urlName
 467:     );
 468: 
 469:     cApiCecHook::execute('Contenido.Category.strRenameCategory', $newData, $oldData);
 470: }
 471: 
 472:  473:  474:  475:  476:  477:  478: 
 479: function strRenameCategoryAlias($idcat, $lang, $newcategoryalias) {
 480:     $oCatLang = new cApiCategoryLanguage();
 481:     if (!$oCatLang->loadByCategoryIdAndLanguageId($idcat, $lang)) {
 482:         
 483:         return;
 484:     }
 485: 
 486:     $oldData = array(
 487:         'idcat' => $oCatLang->get('idcat'),
 488:         'urlname' => $oCatLang->get('urlname')
 489:     );
 490: 
 491:     if (trim($newcategoryalias) == '') {
 492:         
 493:         $newcategoryalias = $oCatLang->get('name');
 494:     }
 495: 
 496:     $oCatLang->set('urlname', $newcategoryalias);
 497:     $oCatLang->set('lastmodified', date('Y-m-d H:i:s'));
 498:     $oCatLang->store();
 499: 
 500:     cInclude('includes', 'functions.pathresolver.php');
 501:     $client = cRegistry::getClientId();
 502:     prDeleteCacheFileContent($client, $lang);
 503: 
 504:     $newData = array(
 505:         'idcat' => $idcat,
 506:         'urlname' => $newcategoryalias
 507:     );
 508: 
 509:     cApiCecHook::execute('Contenido.Category.strRenameCategoryAlias', $newData, $oldData);
 510: }
 511: 
 512:  513:  514:  515:  516:  517:  518: 
 519: function strMakeVisible($idcat, $lang, $visible) {
 520:     global $cfg;
 521: 
 522:     $visible = (int) $visible;
 523:     $lang = (int) $lang;
 524: 
 525:     $categories = strDeeperCategoriesArray($idcat);
 526:     foreach ($categories as $value) {
 527:         $oCatLang = new cApiCategoryLanguage();
 528:         $oCatLang->loadByCategoryIdAndLanguageId($value, $lang);
 529:         $oCatLang->set('visible', $visible);
 530:         $oCatLang->set('lastmodified', date('Y-m-d H:i:s'));
 531:         $oCatLang->store();
 532:     }
 533: 
 534:     if ($cfg['pathresolve_heapcache'] == true && $visible = 0) {
 535:         $oPathresolveCacheColl = new cApiPathresolveCacheCollection();
 536:         $oPathresolveCacheColl->deleteByCategoryAndLanguage($idcat, $lang);
 537:     }
 538: }
 539: 
 540:  541:  542:  543:  544:  545:  546: 
 547: function strMakePublic($idcat, $lang, $public) {
 548:     $categories = strDeeperCategoriesArray($idcat);
 549:     foreach ($categories as $value) {
 550:         $oCatLang = new cApiCategoryLanguage();
 551:         $oCatLang->loadByCategoryIdAndLanguageId($value, $lang);
 552:         $oCatLang->set('public', $public);
 553:         $oCatLang->set('lastmodified', date('Y-m-d H:i:s'));
 554:         $oCatLang->store();
 555:     }
 556: }
 557: 
 558:  559:  560:  561:  562:  563: 
 564: function strDeeperCategoriesArray($startIdcat) {
 565:     global $client;
 566: 
 567:     $oCatColl = new cApiCategoryCollection();
 568:     return $oCatColl->getAllCategoryIdsRecursive($startIdcat, $client);
 569: }
 570: 
 571:  572:  573:  574:  575:  576:  577:  578: 
 579: function strDeleteCategory($idcat) {
 580:     global $lang, $lang;
 581: 
 582:     
 583:     global $remakeCatTable, $remakeStrTable;
 584: 
 585:     if (strNextDeeper($idcat)) {
 586:         
 587:         return '0201';
 588:     } elseif (strHasArticles($idcat)) {
 589:         
 590:         return '0202';
 591:     }
 592: 
 593:     cInclude('includes', 'functions.rights.php');
 594: 
 595:     $remakeCatTable = true;
 596:     $remakeStrTable = true;
 597: 
 598:     
 599:     $oCatLang = new cApiCategoryLanguage();
 600:     $oCatLang->loadByCategoryIdAndLanguageId($idcat, $lang);
 601: 
 602:     if ($oCatLang->isLoaded()) {
 603:         
 604:         
 605:         $oTemplateConfigColl = new cApiTemplateConfigurationCollection();
 606:         $oTemplateConfigColl->delete($oCatLang->get('idtplcfg'));
 607: 
 608:         
 609:         $oCatLangColl = new cApiCategoryLanguageCollection();
 610:         $oCatLangColl->delete($oCatLang->get('idcatlang'));
 611:     }
 612: 
 613:     
 614:     $oCatLangColl = new cApiCategoryLanguageCollection();
 615:     $oCatLangColl->select('idcat = ' . (int) $idcat);
 616:     if (($oCatLang = $oCatLangColl->next()) !== false) {
 617:         
 618:         deleteRightsForElement('str', $idcat, $lang);
 619:         deleteRightsForElement('con', $idcat, $lang);
 620:         return;
 621:     }
 622: 
 623:     
 624:     $oCat = new cApiCategory((int) $idcat);
 625:     $preid = (int) $oCat->get('preid');
 626:     $postid = (int) $oCat->get('postid');
 627: 
 628:     
 629:     if ($preid != 0) {
 630:         $oPreCat = new cApiCategory($preid);
 631:         $oPreCat->set('postid', $postid);
 632:         $oPreCat->store();
 633:     }
 634: 
 635:     
 636:     if ($postid != 0) {
 637:         $oPostCat = new cApiCategory($postid);
 638:         $oPostCat->set('preid', $preid);
 639:         $oPostCat->store();
 640:     }
 641: 
 642:     
 643:     $oCatColl = new cApiCategoryCollection();
 644:     $oCatColl->deleteBy('idcat', (int) $idcat);
 645: 
 646:     $oCatLangColl = new cApiCategoryLanguageCollection();
 647:     $oCatLangColl->select('idcat = ' . (int) $idcat);
 648:     if (($oCatLang = $oCatLangColl->next()) !== false) {
 649:         
 650:         
 651:         $oTemplateConfigColl = new cApiTemplateConfigurationCollection();
 652:         $oTemplateConfigColl->delete($oCatLang->get('idtplcfg'));
 653:     }
 654: 
 655:     
 656:     $oCatLangColl->resetQuery();
 657:     $oCatLangColl->deleteBy('idcat', (int) $idcat);
 658: 
 659:     
 660:     $oCatTreeColl = new cApiCategoryTreeCollection();
 661:     $oCatTreeColl->deleteBy('idcat', (int) $idcat);
 662: 
 663:     
 664:     deleteRightsForElement('str', $idcat);
 665:     deleteRightsForElement('con', $idcat);
 666: }
 667: 
 668:  669:  670:  671:  672: 
 673: function strMoveUpCategory($idcat) {
 674:     
 675:     global $remakeCatTable, $remakeStrTable;
 676: 
 677:     
 678:     $oCat = new cApiCategory();
 679:     $oCat->loadByPrimaryKey((int) $idcat);
 680:     $preid = $oCat->get('preid');
 681:     $postid = $oCat->get('postid');
 682: 
 683:     if (0 == $preid) {
 684:         
 685:         return;
 686:     }
 687: 
 688:     $remakeCatTable = true;
 689:     $remakeStrTable = true;
 690: 
 691:     
 692:     $oPreCat = new cApiCategory();
 693:     $oPreCat->loadByPrimaryKey((int) $preid);
 694:     $prePreid = $oPreCat->get('preid');
 695:     $preIdcat = $oPreCat->get('idcat');
 696: 
 697:     
 698:     $oPrePreCat = new cApiCategory();
 699:     if ((int) $prePreid > 0) {
 700:         $oPrePreCat->loadByPrimaryKey((int) $prePreid);
 701:     }
 702: 
 703:     
 704:     $oPostCat = new cApiCategory();
 705:     if ((int) $postid > 0) {
 706:         $oPostCat->loadByPrimaryKey((int) $postid);
 707:     }
 708: 
 709:     
 710:     if ($oPrePreCat->isLoaded()) {
 711:         $oPrePreCat->set('postid', $idcat);
 712:         $oPrePreCat->store();
 713:     }
 714: 
 715:     
 716:     $oPreCat->set('preid', $idcat);
 717:     $oPreCat->set('postid', $postid);
 718:     $oPreCat->store();
 719: 
 720:     
 721:     $oCat->set('preid', $prePreid);
 722:     $oCat->set('postid', $preid);
 723:     $oCat->store();
 724: 
 725:     
 726:     $oPostCat->set('preid', $preIdcat);
 727:     $oPostCat->store();
 728: }
 729: 
 730:  731:  732:  733:  734: 
 735: function strMoveDownCategory($idcat) {
 736:     
 737:     global $remakeCatTable, $remakeStrTable;
 738: 
 739:     
 740:     $oCat = new cApiCategory();
 741:     $oCat->loadByPrimaryKey((int) $idcat);
 742:     $preid = $oCat->get('preid');
 743:     $postid = $oCat->get('postid');
 744: 
 745:     if (0 == $postid) {
 746:         
 747:         return;
 748:     }
 749: 
 750:     $remakeCatTable = true;
 751:     $remakeStrTable = true;
 752: 
 753:     
 754:     $oPreCat = new cApiCategory();
 755:     if ((int) $preid > 0) {
 756:         $oPreCat->loadByPrimaryKey((int) $preid);
 757:         $preIdcat = (int) $oPreCat->get('idcat');
 758:     } else {
 759:         $preIdcat = 0;
 760:     }
 761: 
 762:     
 763:     $oPostCat = new cApiCategory();
 764:     $oPostCat->loadByPrimaryKey((int) $postid);
 765:     $postIdcat = $oPostCat->get('idcat');
 766:     $postPostid = $oPostCat->get('postid');
 767: 
 768:     if ($preIdcat != 0) {
 769:         
 770:         $oPreCat->set('postid', (int) $postIdcat);
 771:         $oPreCat->store();
 772:     }
 773: 
 774:     
 775:     $oCat->set('preid', $postid);
 776:     $oCat->set('postid', $postPostid);
 777:     $oCat->store();
 778: 
 779:     
 780:     $oPostCat->set('preid', $preIdcat);
 781:     $oPostCat->set('postid', $idcat);
 782:     $oPostCat->store();
 783: 
 784:     if ($postPostid != 0) {
 785:         
 786:         $oPostPostCat = new cApiCategory($postPostid);
 787:         $oPostPostCat->set('preid', $idcat);
 788:         $oPostPostCat->store();
 789:     }
 790: }
 791: 
 792:  793:  794:  795:  796:  797:  798:  799: 
 800: function strMoveSubtree($idcat, $newParentId, $newPreId = null, $newPostId = null) {
 801:     global $movesubtreeidcat;
 802: 
 803:     $idlang = cRegistry::getLanguageId();
 804:     $cat = new cApiCategoryCollection();
 805:     $children = $cat->getAllChildCategoryIds($idcat, $idlang);
 806: 
 807:     foreach ($children as $category) {
 808:         
 809:         if ($category == $newParentId) {
 810:             return false;
 811:         }
 812:     }
 813: 
 814:     if ($idcat == $newParentId) {
 815:         return false;
 816:     }
 817: 
 818:     if ($newParentId == 0 && $newPreId == 0) {
 819:         return false;
 820:     }
 821:     if (!isset($newPostId)) {
 822:         return false;
 823:     }
 824:     
 825:     global $remakeCatTable, $remakeStrTable;
 826: 
 827:     $remakeCatTable = true;
 828:     $remakeStrTable = true;
 829: 
 830:     
 831:     if (is_null($newPostId)) {
 832:         $newPostId = 0;
 833:     }
 834: 
 835:     if ($newParentId == -1) {
 836:         
 837:         $movesubtreeidcat = 0;
 838:     } else if (is_null($newParentId)) {
 839:         
 840:         $movesubtreeidcat = $idcat;
 841:     } else {
 842:         
 843:         $category = new cApiCategory($idcat);
 844:         $oldPreId = $category->get('preid');
 845:         $oldPostId = $category->get('postid');
 846: 
 847:         
 848:         if ($oldPreId != 0) {
 849:             $oldPreCategory = new cApiCategory($oldPreId);
 850:             $oldPreCategory->set('postid', $oldPostId);
 851:             $oldPreCategory->store();
 852:         }
 853: 
 854:         
 855:         if ($oldPostId != 0) {
 856:             $oldPostCategory = new cApiCategory($oldPostId);
 857:             $oldPostCategory->set('preid', $oldPreId);
 858:             $oldPostCategory->store();
 859:         }
 860: 
 861:         
 862:         if (is_null($newPreId)) {
 863:             
 864:             
 865:             $categoryCollection = new cApiCategoryCollection();
 866:             $categoryCollection->select("parentid = " . $newParentId . " AND postid = 0");
 867:             $newPreCategory = $categoryCollection->next();
 868:         } else {
 869:             $newPreCategory = new cApiCategory($newPreId);
 870:         }
 871:         if ($newPreCategory) {
 872:             $newPreCategory->set('postid', $idcat);
 873:             $newPreCategory->store();
 874:             $newPreId = $newPreCategory->get('idcat');
 875:         } else {
 876:             $newPreId = 0;
 877:         }
 878: 
 879:         
 880:         if ($newPostId != 0) {
 881:             $newPostCategory = new cApiCategory($newPostId);
 882:             $newPostCategory->set('preid', $idcat);
 883:             $newPostCategory->store();
 884:         }
 885: 
 886:         
 887:         $category->set('parentid', $newParentId);
 888:         $category->set('preid', $newPreId);
 889:         $category->set('postid', $newPostId);
 890:         $category->store();
 891: 
 892:         $movesubtreeidcat = 0;
 893:     }
 894: 
 895:     $sess = cRegistry::getSession();
 896:     $sess->register('movesubtreeidcat');
 897:     $sess->freeze();
 898: }
 899: 
 900:  901:  902:  903:  904:  905:  906: 
 907: function strMoveCatTargetallowed($idcat, $source) {
 908:     return ($idcat == $source)? 0 : 1;
 909: }
 910: 
 911:  912:  913:  914:  915:  916:  917:  918: 
 919: function strSyncCategory($idcatParam, $sourcelang, $targetlang, $bMultiple = false) {
 920:     $bMultiple = (bool) $bMultiple;
 921: 
 922:     $aCatArray = array();
 923:     if ($bMultiple == true) {
 924:         $aCatArray = strDeeperCategoriesArray($idcatParam);
 925:     } else {
 926:         $aCatArray[] = $idcatParam;
 927:     }
 928: 
 929:     foreach ($aCatArray as $idcat) {
 930:         
 931:         $oCatLang = new cApiCategoryLanguage();
 932:         if ($oCatLang->loadByCategoryIdAndLanguageId($idcat, $targetlang)) {
 933:             return false;
 934:         }
 935: 
 936:         
 937:         $oCatLang = new cApiCategoryLanguage();
 938:         if ($oCatLang->loadByCategoryIdAndLanguageId($idcat, $sourcelang)) {
 939:             $aRs = $oCatLang->toArray();
 940: 
 941:             
 942:             $newidtplcfg = ($aRs['idtplcfg'] != 0)? tplcfgDuplicate($aRs['idtplcfg']) : 0;
 943: 
 944:             $visible = 0;
 945:             $startidartlang = 0;
 946:             $urlpath = '';
 947: 
 948:             $oCatLangColl = new cApiCategoryLanguageCollection();
 949:             $oNewCatLang = $oCatLangColl->create($aRs['idcat'], $targetlang, $aRs['name'], $aRs['urlname'], $urlpath, $newidtplcfg, $visible, $aRs['public'], $aRs['status'], $aRs['author'], $startidartlang, $aRs['created'], $aRs['lastmodified']);
 950: 
 951:             
 952:             $param = $aRs;
 953:             $param['idlang'] = $targetlang;
 954:             $param['idtplcfg'] = (int) $newidtplcfg;
 955:             $param['visible'] = $visible;
 956:             cApiCecHook::execute('Contenido.Category.strSyncCategory_Loop', $param);
 957: 
 958:             
 959:             cInclude('includes', 'functions.rights.php');
 960:             createRightsForElement('str', $idcat, $targetlang);
 961:             createRightsForElement('con', $idcat, $targetlang);
 962:         }
 963:     }
 964: }
 965: 
 966:  967:  968:  969:  970:  971:  972: 
 973: function strHasStartArticle($idcat, $idlang) {
 974:     $oCatLangColl = new cApiCategoryLanguageCollection();
 975:     return ($oCatLangColl->getStartIdartlangByIdcatAndIdlang($idcat, $idlang) > 0);
 976: }
 977: 
 978:  979:  980:  981:  982:  983:  984:  985: 
 986: function strCopyCategory($idcat, $destidcat, $remakeTree = true, $bUseCopyLabel = true) {
 987:     global $cfg, $lang;
 988: 
 989:     $newidcat = (int) strNewCategory($destidcat, 'a', $remakeTree);
 990:     if ($newidcat == 0) {
 991:         return;
 992:     }
 993: 
 994:     
 995:     $oOldCatLang = new cApiCategoryLanguage();
 996:     if (!$oOldCatLang->loadByCategoryIdAndLanguageId($idcat, $lang)) {
 997:         return;
 998:     }
 999: 
1000:     $oNewCatLang = new cApiCategoryLanguage();
1001:     if (!$oNewCatLang->loadByCategoryIdAndLanguageId($newidcat, $lang)) {
1002:         return;
1003:     }
1004: 
1005:     
1006:     $oNewCat = new cApiCategory((int) $newidcat);
1007:     $oOldCat = new cApiCategory((int) $idcat);
1008: 
1009:     
1010:     if ($bUseCopyLabel == true) {
1011:         $oNewCatLang->set('name', sprintf(i18n('%s (Copy)'), $oOldCatLang->get('name')));
1012:     } else {
1013:         $oNewCatLang->set('name', $oOldCatLang->get('name'));
1014:     }
1015: 
1016:     $oNewCatLang->set('public', $oOldCatLang->get('public'));
1017:     $oNewCatLang->set('visible', 0);
1018:     $oNewCatLang->store();
1019: 
1020:     
1021:     cApiCecHook::execute('Contenido.Category.strCopyCategory', array(
1022:         'oldcat' => $oOldCat,
1023:         'newcat' => $oNewCat,
1024:         'newcatlang' => $oNewCatLang
1025:     ));
1026: 
1027:     
1028:     if ($oOldCatLang->get('idtplcfg') != 0) {
1029:         
1030:         $oNewCatLang->assignTemplate($oOldCatLang->getTemplate());
1031: 
1032:         
1033:         $oContainerConfColl = new cApiContainerConfigurationCollection();
1034:         $oContainerConfColl->select('idtplcfg = ' . (int) $oOldCatLang->get('idtplcfg'));
1035: 
1036:         $oNewContainerConfColl = new cApiContainerConfigurationCollection();
1037:         while (($oItem = $oContainerConfColl->next()) !== false) {
1038:             $oNewContainerConfColl->create($oNewCatLang->get('idtplcfg'), $oItem->get('number'), $oItem->get('container'));
1039:         }
1040:     }
1041: 
1042:     $db = cRegistry::getDb();
1043: 
1044:     $oCatArtColl = new cApiCategoryArticleCollection();
1045: 
1046:     
1047:     $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";
1048:     $db->query($sql, $cfg['tab']['cat_art'], $cfg['tab']['art_lang'], $idcat, $lang);
1049: 
1050:     while ($db->nextRecord()) {
1051:         $newidart = (int) conCopyArticle($db->f('idart'), $newidcat, '', $bUseCopyLabel);
1052:         if ($db->f('idartlang') == $oOldCatLang->get('startidartlang')) {
1053:             $oCatArtColl->resetQuery();
1054:             $idcatart = $oCatArtColl->getIdByCategoryIdAndArticleId($newidcat, $newidart);
1055:             if ($idcatart) {
1056:                 conMakeStart($idcatart, 1);
1057:             }
1058:         }
1059:     }
1060: 
1061:     return $newidcat;
1062: }
1063: 
1064: 1065: 1066: 1067: 1068: 1069: 1070: 1071: 
1072: function strCopyTree($idcat, $destcat, $remakeTree = true, $bUseCopyLabel = true) {
1073:     $newidcat = strCopyCategory($idcat, $destcat, false, $bUseCopyLabel);
1074: 
1075:     $oCatColl = new cApiCategoryCollection();
1076:     $aIds = $oCatColl->getIdsByWhereClause('parentid = ' . (int) $idcat);
1077:     foreach ($aIds as $id) {
1078:         strCopyTree($id, $newidcat, false, $bUseCopyLabel);
1079:     }
1080: 
1081:     if ($remakeTree == true) {
1082:         strRemakeTreeTable();
1083:     }
1084: }
1085: 
1086: 1087: 1088: 1089: 1090: 1091: 1092: 
1093: function strAssignTemplate($idcat, $client, $idTplCfg) {
1094:     global $perm;
1095: 
1096:     
1097:     $iIdtplcfg = ($perm->have_perm_area_action('str_tplcfg', 'str_tplcfg'))? (int) $idTplCfg : 0;
1098: 
1099:     $idtpl = null;
1100:     if ($iIdtplcfg == 0) {
1101:         
1102:         $oTemplateColl = new cApiTemplateCollection('defaulttemplate = 1 AND idclient = ' . (int) $client);
1103:         if (($oTemplate = $oTemplateColl->next()) !== false) {
1104:             $idtpl = $oTemplate->get('idtpl');
1105:         }
1106:     } else {
1107:         
1108:         $idtpl = $idTplCfg;
1109:     }
1110: 
1111:     if ($idtpl) {
1112:         
1113:         $oCatLangColl = new cApiCategoryLanguageCollection('idcat = ' . (int) $idcat);
1114:         while (($oCatLang = $oCatLangColl->next()) !== false) {
1115:             $oCatLang->assignTemplate($idtpl);
1116:         }
1117:     }
1118: }
1119: