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: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41:
42: function strNewTree($catname, $catalias = '', $visible = 0, $public = 1, $iIdtplcfg = 0) {
43: global $client, $lang, $perm;
44:
45:
46: global $remakeCatTable, $remakeStrTable;
47:
48: if (trim($catname) == '') {
49: return;
50: }
51:
52: $catname = stripslashes($catname);
53:
54: $remakeCatTable = true;
55: $remakeStrTable = true;
56:
57: $catalias = trim($catalias);
58: if ($catalias == '') {
59: $catalias = trim($catname);
60: }
61:
62: $client = (int) $client;
63: $lang = (int) $lang;
64:
65: $visible = ($visible == 1) ? 1 : 0;
66: if (!$perm->have_perm_area_action('str', 'str_makevisible')) {
67: $visible = 0;
68: }
69:
70: $public = ($public == 1) ? 1 : 0;
71: if (!$perm->have_perm_area_action('str', 'str_makepublic')) {
72: $public = 1;
73: }
74:
75:
76: $oCatColl = new cApiCategoryCollection();
77: $oLastCatTree = $oCatColl->fetchLastCategoryTree($client);
78: $lastCatTreeId = (is_object($oLastCatTree)) ? $oLastCatTree->get('idcat') : 0;
79:
80:
81: $oCatColl2 = new cApiCategoryCollection();
82: $oNewCat = $oCatColl2->create($client, 0, $lastCatTreeId, 0);
83: $newIdcat = $oNewCat->get('idcat');
84: $oldPostId = -1;
85:
86:
87: if (is_object($oLastCatTree)) {
88: $oldPostId = $oLastCatTree->get('postid');
89: $oLastCatTree->set('postid', $newIdcat);
90: $oLastCatTree->store();
91: }
92:
93: $error = strCheckTreeForErrors();
94: if (!($error === false)) {
95: if ($oldPostId != -1) {
96: $oLastCatTree->set('postid', $oldPostId);
97: $oLastCatTree->store();
98: }
99: $oCatColl->delete($oNewCat->get('idcat'));
100: return;
101: }
102:
103: cInclude('includes', 'functions.rights.php');
104:
105:
106: $aLanguages = array(
107: $lang
108: );
109: foreach ($aLanguages as $curLang) {
110: $name = $catname;
111: $urlname = conHtmlSpecialChars(cString::cleanURLCharacters($catalias), ENT_QUOTES);
112:
113:
114: $oCatLangColl = new cApiCategoryLanguageCollection();
115: $oCatLangColl->create($newIdcat, $curLang, $name, $urlname, '', 0, $visible, $public, 0, '', 0);
116:
117:
118: createRightsForElement('str', $newIdcat, $curLang);
119: createRightsForElement('con', $newIdcat, $curLang);
120: }
121:
122:
123: strAssignTemplate($newIdcat, $client, $iIdtplcfg);
124:
125: return $newIdcat;
126: }
127:
128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152:
153: function strNewCategory($parentid, $catname, $remakeTree = true, $catalias = '', $visible = 0, $public = 1, $iIdtplcfg = 0) {
154: global $client, $lang, $perm;
155:
156:
157: global $remakeCatTable, $remakeStrTable;
158:
159: $parentid = (int) $parentid;
160:
161: if (trim($catname) == '') {
162: return;
163: }
164:
165: $catname = stripslashes($catname);
166:
167: $remakeCatTable = true;
168: $remakeStrTable = true;
169:
170: $catalias = trim($catalias);
171: if ($catalias == '') {
172: $catalias = trim($catname);
173: }
174:
175: $client = (int) $client;
176: $lang = (int) $lang;
177:
178: $visible = ($visible == 1) ? 1 : 0;
179: if (!$perm->have_perm_area_action('str', 'str_makevisible')) {
180: $visible = 0;
181: }
182:
183: $public = ($public == 1) ? 1 : 0;
184: if (!$perm->have_perm_area_action('str', 'str_makepublic')) {
185: $public = 1;
186: }
187:
188:
189: $oCatColl = new cApiCategoryCollection();
190: $oCatColl->select('parentid=' . $parentid . ' AND postid = 0 AND idclient = ' . $client);
191: $oPrevCat = $oCatColl->next();
192: $preIdcat = (is_object($oPrevCat)) ? $oPrevCat->get('idcat') : 0;
193:
194:
195: $oCatColl2 = new cApiCategoryCollection();
196: $oNewCat = $oCatColl2->create($client, $parentid, $preIdcat, 0);
197: $newIdcat = $oNewCat->get('idcat');
198: $oldPostId = -1;
199:
200:
201: if (is_object($oPrevCat)) {
202: $oldPostId = $oPrevCat->get('postid');
203: $oPrevCat->set('postid', $newIdcat);
204: $oPrevCat->set('lastmodified', date('Y-m-d H:i:s'));
205: $oPrevCat->store();
206: }
207:
208: $error = strCheckTreeForErrors();
209:
210: if (!($error === false)) {
211: if ($oldPostId != -1) {
212: $oPrevCat->set('postid', $oldPostId);
213: $oPrevCat->store();
214: }
215: $oCatColl2->delete($oNewCat->get('idcat'));
216: return;
217: }
218:
219: cInclude('includes', 'functions.rights.php');
220:
221:
222: $aLanguages = array(
223: $lang
224: );
225:
226: $catalias = conHtmlSpecialChars(cString::cleanURLCharacters($catalias), ENT_QUOTES);
227:
228:
229: $checkAlias = strCheckAlias($catalias);
230: if ($checkAlias === true) {
231:
232:
233:
234: $catalias = $catalias . $newIdcat;
235: }
236:
237: foreach ($aLanguages as $curLang) {
238: $name = $catname;
239: $urlname = $catalias;
240:
241:
242: $oCatLangColl = new cApiCategoryLanguageCollection();
243: $oCatLangColl->create($newIdcat, $curLang, $name, $urlname, '', 0, $visible, $public, 0, '', 0);
244:
245:
246: copyRightsForElement('str', $parentid, $newIdcat, $curLang);
247: copyRightsForElement('con', $parentid, $newIdcat, $curLang);
248: }
249:
250: if ($remakeTree == true) {
251: strRemakeTreeTable();
252: }
253:
254:
255: strAssignTemplate($newIdcat, $client, $iIdtplcfg);
256:
257: return $newIdcat;
258: }
259:
260: 261: 262: 263: 264: 265: 266: 267: 268:
269: function strCheckAlias($catalias) {
270: $lang = cRegistry::getLanguageId();
271: $catLangColl = new cApiCategoryLanguageCollection();
272: $result = $catLangColl->select("idlang = " . $lang . " AND urlname = '" . cSecurity::escapeString($catalias) . "'");
273: return $result;
274: }
275:
276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286:
287: function strOrderedPostTreeList($idcat, $poststring) {
288: $oCatColl = new cApiCategoryCollection();
289: $oCatColl->select('parentid = 0 AND preid = ' . (int) $idcat . ' AND idcat != 0');
290: if (($oCat = $oCatColl->next()) !== false) {
291: $postIdcat = $oCat->get('idcat');
292: $poststring = $poststring . ',' . $postIdcat;
293: $poststring = strOrderedPostTreeList($postIdcat, $poststring);
294: }
295:
296: return $poststring;
297: }
298:
299: 300: 301: 302: 303: 304: 305: 306:
307: function strRemakeTreeTable() {
308: global $db, $client, $cfg;
309:
310:
311: global $remakeCatTable;
312: global $remakeStrTable;
313:
314:
315: $oCatColl = new cApiCategoryCollection();
316: $idcats = $oCatColl->getCategoryIdsByClient($client);
317: if (0 === count($idcats)) {
318:
319: return;
320: }
321:
322: $errors = strCheckTreeForErrors();
323: if (!($errors === false)) {
324: return;
325: }
326:
327: $remakeCatTable = true;
328: $remakeStrTable = true;
329:
330:
331: $sql = 'DELETE FROM ' . $cfg['tab']['cat_tree'] . ' WHERE idcat IN (' . implode(', ', $idcats) . ')';
332: $db->query($sql);
333:
334:
335:
336:
337: $sql = 'DELETE FROM ' . $cfg['tab']['cat'] . ' WHERE idcat = 0';
338: $db->query($sql);
339:
340:
341:
342:
343: $sql = 'DELETE FROM ' . $cfg['tab']['cat_lang'] . ' WHERE idcat = 0';
344: $db->query($sql);
345:
346:
347: $sql = "SELECT idcat, parentid, preid, postid FROM " . $cfg['tab']['cat'] . " WHERE idclient = " . (int) $client . " ORDER BY parentid ASC, preid ASC, postid ASC";
348: $aCategories = array();
349: $db->query($sql);
350: while ($db->nextRecord()) {
351: $rs = $db->toArray();
352: if (!isset($aCategories[$rs['parentid']])) {
353: $aCategories[$rs['parentid']] = array();
354: }
355: $aCategories[$rs['parentid']][$rs['idcat']] = $rs;
356: }
357:
358:
359: $sInsertQuery = "INSERT INTO " . $cfg['tab']['cat_tree'] . " (idcat, level) VALUES ";
360: $sInsertQuery = strBuildSqlValues($aCategories[0], $sInsertQuery, $aCategories);
361: $sInsertQuery = rtrim($sInsertQuery, " ,");
362:
363:
364: $db->query($sInsertQuery);
365: }
366:
367: 368: 369: 370: 371: 372: 373:
374: function strSortPrePost($arr) {
375: $firstElement = NULL;
376: foreach ($arr as $row) {
377: if ($row['preid'] == 0) {
378: $firstElement = $row['idcat'];
379: }
380: }
381:
382: $curId = $firstElement;
383: $array = array();
384:
385:
386: $fine = false;
387: foreach ($arr as $row) {
388: if ($row['postid'] == 0) {
389: $fine = true;
390: break;
391: }
392: }
393: if (!$fine) {
394: die();
395: }
396:
397: while ($curId != 0) {
398: $array[] = $arr[$curId];
399: $curId = $arr[$curId]['postid'];
400: }
401:
402: return $array;
403: }
404:
405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417:
418: function strBuildSqlValues($aCats, $sInsertQuery, &$aAllCats, $iLevel = 0) {
419: if (is_array($aCats)) {
420: $aCats = strSortPrePost($aCats);
421: foreach ($aCats as $aCat) {
422: $sInsertQuery .= '(' . (int) $aCat['idcat'] . ', ' . (int) $iLevel . '), ';
423: if (isset($aAllCats[$aCat['idcat']]) && is_array($aAllCats[$aCat['idcat']])) {
424: $iSubLevel = $iLevel + 1;
425: $sInsertQuery = strBuildSqlValues($aAllCats[$aCat['idcat']], $sInsertQuery, $aAllCats, $iSubLevel);
426: }
427: }
428: }
429: return $sInsertQuery;
430: }
431:
432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443:
444: function strNextDeeper($idcat, $ignoreLang = false) {
445: $lang = cRegistry::getLanguageId();
446:
447: $languageId = (true == $ignoreLang) ? NULL : $lang;
448: $oCatColl = new cApiCategoryCollection();
449: return $oCatColl->getFirstChildCategoryId($idcat, $languageId);
450: }
451:
452: 453: 454: 455: 456: 457: 458: 459: 460: 461:
462: function strHasArticles($idcat) {
463: $lang = cRegistry::getLanguageId();
464:
465: $oCatArtColl = new cApiCategoryArticleCollection();
466: return $oCatArtColl->getHasArticles($idcat, $lang);
467: }
468:
469: 470: 471: 472: 473: 474: 475: 476: 477: 478:
479: function strNextPost($idcat) {
480: $oCatColl = new cApiCategoryCollection();
481: return $oCatColl->getNextPostCategoryId($idcat);
482: }
483:
484: 485: 486: 487: 488: 489: 490: 491: 492: 493:
494: function strNextBackwards($idcat) {
495: $oCatColl = new cApiCategoryCollection();
496: return $oCatColl->getParentsNextPostCategoryId($idcat);
497: }
498:
499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510:
511: function strNextDeeperAll($idcat, $ignoreLang = false) {
512: global $lang;
513:
514: $languageId = (true == $ignoreLang) ? NULL : $lang;
515: $oCatColl = new cApiCategoryCollection();
516: return $oCatColl->getAllChildCategoryIds($idcat, $languageId);
517: }
518:
519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534:
535: function strRenameCategory($idcat, $lang, $newCategoryName, $newCategoryAlias) {
536: if (trim($newCategoryName) == '') {
537: return;
538: }
539:
540: $oCatLang = new cApiCategoryLanguage();
541: if (!$oCatLang->loadByCategoryIdAndLanguageId($idcat, $lang)) {
542:
543: return;
544: }
545:
546: $oldData = array(
547: 'idcat' => $oCatLang->get('idcat'),
548: 'name' => $oCatLang->get('name'),
549: 'urlname' => $oCatLang->get('urlname')
550: );
551:
552: $name = stripslashes($newCategoryName);
553: $urlName = (trim($newCategoryAlias) != '') ? trim($newCategoryAlias) : $newCategoryName;
554:
555: if (trim($newCategoryAlias) != '') {
556:
557:
558:
559:
560: cInclude('includes', 'functions.pathresolver.php');
561: $client = cRegistry::getClientId();
562: prDeleteCacheFileContent($client, $lang);
563: }
564:
565: $oCatLang->set('name', $name);
566: $oCatLang->set('urlname', $urlName);
567: $oCatLang->set('lastmodified', date('Y-m-d H:i:s'));
568: $oCatLang->store();
569:
570: $newData = array(
571: 'idcat' => $idcat,
572: 'name' => $name,
573: 'urlname' => $urlName
574: );
575:
576: cApiCecHook::execute('Contenido.Category.strRenameCategory', $newData, $oldData);
577: }
578:
579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592:
593: function strRenameCategoryAlias($idcat, $lang, $newcategoryalias) {
594: $oCatLang = new cApiCategoryLanguage();
595: if (!$oCatLang->loadByCategoryIdAndLanguageId($idcat, $lang)) {
596:
597: return;
598: }
599:
600: $oldData = array(
601: 'idcat' => $oCatLang->get('idcat'),
602: 'urlname' => $oCatLang->get('urlname')
603: );
604:
605: if (trim($newcategoryalias) == '') {
606:
607: $newcategoryalias = $oCatLang->get('name');
608: }
609:
610: $oCatLang->set('urlname', $newcategoryalias);
611: $oCatLang->set('lastmodified', date('Y-m-d H:i:s'));
612: $oCatLang->store();
613:
614: cInclude('includes', 'functions.pathresolver.php');
615: $client = cRegistry::getClientId();
616: prDeleteCacheFileContent($client, $lang);
617:
618: $newData = array(
619: 'idcat' => $idcat,
620: 'urlname' => $newcategoryalias
621: );
622:
623: cApiCecHook::execute('Contenido.Category.strRenameCategoryAlias', $newData, $oldData);
624: }
625:
626: 627: 628: 629: 630: 631: 632: 633: 634: 635: 636: 637: 638: 639:
640: function strMakeVisible($idcat, $lang, $visible) {
641: $visible = (int) $visible;
642: $lang = (int) $lang;
643:
644: $categories = strDeeperCategoriesArray($idcat);
645: foreach ($categories as $value) {
646: $oCatLang = new cApiCategoryLanguage();
647: $oCatLang->loadByCategoryIdAndLanguageId($value, $lang);
648: $oCatLang->set('visible', $visible);
649: $oCatLang->set('lastmodified', date('Y-m-d H:i:s'));
650: $oCatLang->store();
651: }
652:
653: if (cRegistry::getConfigValue('pathresolve_heapcache') == true && $visible != 0) {
654: $oPathresolveCacheColl = new cApiPathresolveCacheCollection();
655: $oPathresolveCacheColl->deleteByCategoryAndLanguage($idcat, $lang);
656: }
657: }
658:
659: 660: 661: 662: 663: 664: 665: 666: 667: 668: 669: 670: 671: 672: 673: 674: 675:
676: function strMakePublic($idcat, $lang, $public) {
677:
678: foreach (strDeeperCategoriesArray($idcat) as $tmpIdcat) {
679: $oCatLang = new cApiCategoryLanguage();
680: $oCatLang->loadByCategoryIdAndLanguageId($tmpIdcat, $lang);
681: $oCatLang->set('public', $public);
682: $oCatLang->set('lastmodified', date('Y-m-d H:i:s'));
683: $oCatLang->store();
684: }
685:
686: }
687:
688: 689: 690: 691: 692: 693: 694: 695: 696: 697: 698:
699: function strDeeperCategoriesArray($idcat) {
700: global $client;
701:
702: $coll = new cApiCategoryCollection();
703: $idcats = $coll->getAllCategoryIdsRecursive($idcat, $client);
704:
705: return $idcats;
706: }
707:
708: 709: 710: 711: 712: 713: 714: 715: 716: 717: 718: 719: 720: 721: 722:
723: function strDeleteCategory($idcat) {
724: $lang = cRegistry::getLanguageId();
725:
726:
727: global $remakeCatTable, $remakeStrTable;
728:
729: if (strNextDeeper($idcat)) {
730:
731: return '0201';
732: } elseif (strHasArticles($idcat)) {
733:
734: return '0202';
735: }
736:
737: cInclude('includes', 'functions.rights.php');
738:
739: $remakeCatTable = true;
740: $remakeStrTable = true;
741:
742:
743: $oCatLang = new cApiCategoryLanguage();
744: $oCatLang->loadByCategoryIdAndLanguageId($idcat, $lang);
745:
746: if ($oCatLang->isLoaded()) {
747:
748:
749: $oTemplateConfigColl = new cApiTemplateConfigurationCollection();
750: $oTemplateConfigColl->delete($oCatLang->get('idtplcfg'));
751:
752:
753: $oCatLangColl = new cApiCategoryLanguageCollection();
754: $oCatLangColl->delete($oCatLang->get('idcatlang'));
755: }
756:
757:
758: $oCatLangColl = new cApiCategoryLanguageCollection();
759: $oCatLangColl->select('idcat = ' . (int) $idcat);
760: if (($oCatLang = $oCatLangColl->next()) !== false) {
761:
762: deleteRightsForElement('str', $idcat, $lang);
763: deleteRightsForElement('con', $idcat, $lang);
764: return;
765: }
766:
767:
768: $oCat = new cApiCategory((int) $idcat);
769: $preid = (int) $oCat->get('preid');
770: $postid = (int) $oCat->get('postid');
771:
772:
773: if ($preid != 0) {
774: $oPreCat = new cApiCategory($preid);
775: $oPreCat->set('postid', $postid);
776: $oPreCat->store();
777: }
778:
779:
780: if ($postid != 0) {
781: $oPostCat = new cApiCategory($postid);
782: $oPostCat->set('preid', $preid);
783: $oPostCat->store();
784: }
785:
786: $error = strCheckTreeForErrors(array(), array(
787: $idcat
788: ));
789: if (!($error === false)) {
790: if ($preid != 0) {
791: $oPreCat = new cApiCategory($preid);
792: $oPreCat->set('postid', $idcat);
793: $oPreCat->store();
794: }
795: if ($postid != 0) {
796: $oPostCat = new cApiCategory($postid);
797: $oPostCat->set('preid', $idcat);
798: $oPostCat->store();
799: }
800: return '0600';
801: }
802:
803:
804: $oCatColl = new cApiCategoryCollection();
805: $oCatColl->deleteBy('idcat', (int) $idcat);
806:
807: $oCatLangColl = new cApiCategoryLanguageCollection();
808: $oCatLangColl->select('idcat = ' . (int) $idcat);
809: if (($oCatLang = $oCatLangColl->next()) !== false) {
810:
811:
812: $oTemplateConfigColl = new cApiTemplateConfigurationCollection();
813: $oTemplateConfigColl->delete($oCatLang->get('idtplcfg'));
814: }
815:
816:
817: $oCatLangColl->resetQuery();
818: $oCatLangColl->deleteBy('idcat', (int) $idcat);
819:
820:
821: $oCatTreeColl = new cApiCategoryTreeCollection();
822: $oCatTreeColl->deleteBy('idcat', (int) $idcat);
823:
824:
825: deleteRightsForElement('str', $idcat);
826: deleteRightsForElement('con', $idcat);
827: }
828:
829: 830: 831: 832: 833: 834: 835: 836: 837:
838: function strMoveUpCategory($idcat) {
839:
840: global $remakeCatTable, $remakeStrTable, $notification;
841:
842:
843: $oCat = new cApiCategory();
844: $oCat->loadByPrimaryKey((int) $idcat);
845: $preid = $oCat->get('preid');
846: $postid = $oCat->get('postid');
847:
848: if (0 == $preid) {
849:
850: return;
851: }
852:
853: $remakeCatTable = true;
854: $remakeStrTable = true;
855:
856:
857: $oPreCat = new cApiCategory();
858: $oPreCat->loadByPrimaryKey((int) $preid);
859: $prePreid = $oPreCat->get('preid');
860: $preIdcat = $oPreCat->get('idcat');
861:
862:
863: $oPrePreCat = new cApiCategory();
864: if ((int) $prePreid > 0) {
865: $oPrePreCat->loadByPrimaryKey((int) $prePreid);
866: }
867:
868:
869: $oPostCat = new cApiCategory();
870: if ((int) $postid > 0) {
871: $oPostCat->loadByPrimaryKey((int) $postid);
872: }
873:
874: $updateCats = array();
875:
876:
877: if ($oPrePreCat->isLoaded()) {
878: $oPrePreCat->set('postid', $idcat);
879: $updateCats[$prePreid] = $oPrePreCat;
880: }
881:
882:
883: $oPreCat->set('preid', $idcat);
884: $oPreCat->set('postid', $postid);
885: $updateCats[$preid] = $oPreCat;
886:
887:
888: $oCat->set('preid', $prePreid);
889: $oCat->set('postid', $preid);
890: $updateCats[$idcat] = $oCat;
891:
892:
893: $oPostCat->set('preid', $preIdcat);
894: $updateCats[$postid] = $oPostCat;
895:
896: $error = strCheckTreeForErrors($updateCats);
897: if ($error === false) {
898: foreach ($updateCats as $cat) {
899: $cat->store();
900: }
901: } else {
902: $string = '';
903: foreach ($error as $msg) {
904: $string .= $msg . '<br>';
905: }
906: $notification->displayNotification(cGuiNotification::LEVEL_WARNING, $msg . '<br><br>' . i18n('Something went wrong while trying to perform this operation. Please try again.'));
907: return;
908: }
909: }
910:
911: 912: 913: 914: 915: 916: 917: 918: 919:
920: function strMoveDownCategory($idcat) {
921:
922: global $remakeCatTable, $remakeStrTable, $notification;
923:
924:
925: $oCat = new cApiCategory();
926: $oCat->loadByPrimaryKey((int) $idcat);
927: $preid = $oCat->get('preid');
928: $postid = $oCat->get('postid');
929:
930: if (0 == $postid) {
931:
932: return;
933: }
934:
935: $remakeCatTable = true;
936: $remakeStrTable = true;
937:
938:
939: $oPreCat = new cApiCategory();
940: if ((int) $preid > 0) {
941: $oPreCat->loadByPrimaryKey((int) $preid);
942: $preIdcat = (int) $oPreCat->get('idcat');
943: } else {
944: $preIdcat = 0;
945: }
946:
947:
948: $oPostCat = new cApiCategory();
949: $oPostCat->loadByPrimaryKey((int) $postid);
950: $postIdcat = $oPostCat->get('idcat');
951: $postPostid = $oPostCat->get('postid');
952:
953: $updateCats = array();
954:
955: if ($preIdcat != 0) {
956:
957: $oPreCat->set('postid', (int) $postIdcat);
958: $updateCats[$preIdcat] = $oPreCat;
959: }
960:
961:
962: $oCat->set('preid', $postid);
963: $oCat->set('postid', $postPostid);
964: $updateCats[$idcat] = $oCat;
965:
966:
967: $oPostCat->set('preid', $preIdcat);
968: $oPostCat->set('postid', $idcat);
969: $updateCats[$postid] = $oPostCat;
970:
971: if ($postPostid != 0) {
972:
973: $oPostPostCat = new cApiCategory($postPostid);
974: $oPostPostCat->set('preid', $idcat);
975: $updateCats[$postPostid] = $oPostPostCat;
976: }
977:
978: $error = strCheckTreeForErrors($updateCats);
979: if ($error === false) {
980: foreach ($updateCats as $cat) {
981: $cat->store();
982: }
983: } else {
984: $string = '';
985: foreach ($error as $msg) {
986: $string .= $msg . '<br>';
987: }
988: $notification->displayNotification(cGuiNotification::LEVEL_WARNING, $msg . '<br><br>' . i18n('Something went wrong while trying to perform this operation. Please try again.'));
989: return;
990: }
991: }
992:
993: 994: 995: 996: 997: 998: 999: 1000: 1001: 1002: 1003: 1004: 1005: 1006: 1007: 1008: 1009:
1010: function strMoveSubtree($idcat, $newParentId, $newPreId = NULL, $newPostId = NULL) {
1011: global $movesubtreeidcat, $notification;
1012:
1013: $idlang = cRegistry::getLanguageId();
1014: $cat = new cApiCategoryCollection();
1015: $children = $cat->getAllChildCategoryIds($idcat, $idlang);
1016:
1017: foreach ($children as $category) {
1018:
1019: if ($category == $newParentId) {
1020: return false;
1021: }
1022: }
1023:
1024: if ($idcat == $newParentId) {
1025: return false;
1026: }
1027:
1028: if ($newParentId == 0 && $newPreId == 0) {
1029: return false;
1030: }
1031:
1032:
1033:
1034:
1035: global $remakeCatTable, $remakeStrTable;
1036:
1037: $remakeCatTable = true;
1038: $remakeStrTable = true;
1039:
1040:
1041: if (is_null($newPostId)) {
1042: $newPostId = 0;
1043: }
1044:
1045: if ($newParentId == -1) {
1046:
1047: $movesubtreeidcat = 0;
1048: } else if (is_null($newParentId)) {
1049:
1050: $movesubtreeidcat = $idcat;
1051: } else {
1052:
1053: $category = new cApiCategory($idcat);
1054: $oldPreId = $category->get('preid');
1055: $oldPostId = $category->get('postid');
1056: $oldParentId = $category->get('parentid');
1057:
1058: $updateCats = array();
1059:
1060:
1061: if ($oldPreId != 0) {
1062: $oldPreCategory = new cApiCategory($oldPreId);
1063: $oldPreCategory->set('postid', $oldPostId);
1064: $updateCats[$oldPreId] = $oldPreCategory;
1065: }
1066:
1067:
1068: if ($oldPostId != 0) {
1069: if (isset($updateCats[$oldPostId])) {
1070: $updateCats[$oldPostId]->set('preid', $oldPreId);
1071: } else {
1072: $oldPostCategory = new cApiCategory($oldPostId);
1073: $oldPostCategory->set('preid', $oldPreId);
1074: $updateCats[$oldPostId] = $oldPostCategory;
1075: }
1076: }
1077:
1078:
1079: if (is_null($newPreId)) {
1080:
1081:
1082: $categoryCollection = new cApiCategoryCollection();
1083: $categoryCollection->select("parentid = " . $newParentId . " AND postid = 0");
1084: $newPreCategory = $categoryCollection->next();
1085: if($newPreCategory != null) {
1086: $newPreId = $newPreCategory->get('idcat');
1087: $newPreCategory->set('postid', $idcat);
1088: $updateCats[$newPreId] = $newPreCategory;
1089: }
1090: } else {
1091: if (isset($updateCats[$newPreId])) {
1092: $updateCats[$newPreId]->set('postid', $idcat);
1093: } else {
1094: $newPreCategory = new cApiCategory($newPreId);
1095: $newPreCategory->set('postid', $idcat);
1096: $updateCats[$newPreId] = $newPreCategory;
1097: $newPreId = $newPreCategory->get('idcat');
1098: }
1099: }
1100:
1101:
1102: if ($newPostId != 0) {
1103: if (isset($updateCats[$newPostId])) {
1104: $updateCats[$newPostId]->set('preid', $idcat);
1105: } else {
1106: $newPostCategory = new cApiCategory($newPostId);
1107: $newPostCategory->set('preid', $idcat);
1108: $updateCats[$newPostId] = $newPostCategory;
1109: }
1110: }
1111:
1112:
1113: $category->set('parentid', $newParentId);
1114: $category->set('preid', $newPreId);
1115: $category->set('postid', $newPostId);
1116: $updateCats[$idcat] = $category;
1117:
1118: $error = strCheckTreeForErrors($updateCats);
1119: if ($error === false) {
1120: foreach ($updateCats as $cat) {
1121: $cat->store();
1122: }
1123: } else {
1124: $string = '';
1125: foreach ($error as $msg) {
1126: $string .= $msg . '<br>';
1127: }
1128: $notification->displayNotification(cGuiNotification::LEVEL_WARNING, $msg . '<br><br>' . i18n('Something went wrong while trying to perform this operation. Please try again.'));
1129: return false;
1130: }
1131:
1132: $movesubtreeidcat = 0;
1133: }
1134:
1135: $sess = cRegistry::getSession();
1136: $sess->register('movesubtreeidcat');
1137: $sess->freeze();
1138: }
1139:
1140: 1141: 1142: 1143: 1144: 1145: 1146: 1147: 1148:
1149: function strMoveCatTargetallowed($idcat, $source) {
1150: return ($idcat == $source) ? 0 : 1;
1151: }
1152:
1153: 1154: 1155: 1156: 1157: 1158: 1159: 1160: 1161: 1162: 1163: 1164: 1165: 1166: 1167: 1168: 1169: 1170:
1171: function strSyncCategory($idcatParam, $sourcelang, $targetlang, $bMultiple = false) {
1172: $bMultiple = (bool) $bMultiple;
1173:
1174: $aCatArray = array();
1175: if ($bMultiple == true) {
1176: $aCatArray = strDeeperCategoriesArray($idcatParam);
1177: } else {
1178: $aCatArray[] = $idcatParam;
1179: }
1180:
1181: foreach ($aCatArray as $idcat) {
1182:
1183: $oCatLang = new cApiCategoryLanguage();
1184: if ($oCatLang->loadByCategoryIdAndLanguageId($idcat, $targetlang)) {
1185: return false;
1186: }
1187:
1188:
1189: $oCatLang = new cApiCategoryLanguage();
1190: if ($oCatLang->loadByCategoryIdAndLanguageId($idcat, $sourcelang)) {
1191: $aRs = $oCatLang->toArray();
1192:
1193:
1194: $newidtplcfg = ($aRs['idtplcfg'] != 0) ? tplcfgDuplicate($aRs['idtplcfg']) : 0;
1195:
1196: $visible = 0;
1197: $startidartlang = 0;
1198: $urlpath = '';
1199:
1200: $oCatLangColl = new cApiCategoryLanguageCollection();
1201: $oNewCatLang = $oCatLangColl->create($aRs['idcat'], $targetlang, $aRs['name'], $aRs['urlname'], $urlpath, $newidtplcfg, $visible, $aRs['public'], $aRs['status'], $aRs['author'], $startidartlang, $aRs['created'], $aRs['lastmodified']);
1202:
1203:
1204: $param = $aRs;
1205: $param['idlang'] = $targetlang;
1206: $param['idtplcfg'] = (int) $newidtplcfg;
1207: $param['visible'] = $visible;
1208: cApiCecHook::execute('Contenido.Category.strSyncCategory_Loop', $param);
1209:
1210:
1211: cInclude('includes', 'functions.rights.php');
1212: createRightsForElement('str', $idcat, $targetlang);
1213: createRightsForElement('con', $idcat, $targetlang);
1214: }
1215: }
1216: }
1217:
1218: 1219: 1220: 1221: 1222: 1223: 1224: 1225: 1226: 1227: 1228: 1229:
1230: function strHasStartArticle($idcat, $idlang) {
1231: $oCatLangColl = new cApiCategoryLanguageCollection();
1232: return ($oCatLangColl->getStartIdartlangByIdcatAndIdlang($idcat, $idlang) > 0);
1233: }
1234:
1235: 1236: 1237: 1238: 1239: 1240: 1241: 1242: 1243: 1244: 1245: 1246: 1247: 1248: 1249: 1250: 1251: 1252:
1253: function strCopyCategory($idcat, $destidcat, $remakeTree = true, $bUseCopyLabel = true) {
1254: global $cfg, $lang;
1255:
1256: $newidcat = (int) strNewCategory($destidcat, 'a', $remakeTree);
1257: if ($newidcat == 0) {
1258: return;
1259: }
1260:
1261:
1262: $oOldCatLang = new cApiCategoryLanguage();
1263: if (!$oOldCatLang->loadByCategoryIdAndLanguageId($idcat, $lang)) {
1264: return;
1265: }
1266:
1267: $oNewCatLang = new cApiCategoryLanguage();
1268: if (!$oNewCatLang->loadByCategoryIdAndLanguageId($newidcat, $lang)) {
1269: return;
1270: }
1271:
1272:
1273: $oNewCat = new cApiCategory((int) $newidcat);
1274: $oOldCat = new cApiCategory((int) $idcat);
1275:
1276:
1277: if ($bUseCopyLabel == true) {
1278: $oNewCatLang->set('name', sprintf(i18n('%s (Copy)'), $oOldCatLang->get('name')));
1279: } else {
1280: $oNewCatLang->set('name', $oOldCatLang->get('name'));
1281: }
1282:
1283: $oNewCatLang->set('public', $oOldCatLang->get('public'));
1284: $oNewCatLang->set('visible', 0);
1285: $oNewCatLang->store();
1286:
1287:
1288: cApiCecHook::execute('Contenido.Category.strCopyCategory', array(
1289: 'oldcat' => $oOldCat,
1290: 'newcat' => $oNewCat,
1291: 'newcatlang' => $oNewCatLang
1292: ));
1293:
1294:
1295: if ($oOldCatLang->get('idtplcfg') != 0) {
1296:
1297: $oNewCatLang->assignTemplate($oOldCatLang->getTemplate());
1298:
1299:
1300: $oContainerConfColl = new cApiContainerConfigurationCollection();
1301: $oContainerConfColl->select('idtplcfg = ' . (int) $oOldCatLang->get('idtplcfg'));
1302:
1303: $oNewContainerConfColl = new cApiContainerConfigurationCollection();
1304: while (($oItem = $oContainerConfColl->next()) !== false) {
1305: $oNewContainerConfColl->create($oNewCatLang->get('idtplcfg'), $oItem->get('number'), $oItem->get('container'));
1306: }
1307: }
1308:
1309: $db = cRegistry::getDb();
1310:
1311: $oCatArtColl = new cApiCategoryArticleCollection();
1312:
1313:
1314: $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";
1315: $db->query($sql, $cfg['tab']['cat_art'], $cfg['tab']['art_lang'], $idcat, $lang);
1316:
1317: while ($db->nextRecord()) {
1318: $newidart = (int) conCopyArticle($db->f('idart'), $newidcat, '', $bUseCopyLabel);
1319: if ($db->f('idartlang') == $oOldCatLang->get('startidartlang')) {
1320: $oCatArtColl->resetQuery();
1321: $idcatart = $oCatArtColl->getIdByCategoryIdAndArticleId($newidcat, $newidart);
1322: if ($idcatart) {
1323: conMakeStart($idcatart, 1);
1324: }
1325: }
1326: }
1327:
1328: return $newidcat;
1329: }
1330:
1331: 1332: 1333: 1334: 1335: 1336: 1337: 1338: 1339: 1340: 1341: 1342: 1343: 1344: 1345: 1346:
1347: function strCopyTree($idcat, $destcat, $remakeTree = true, $bUseCopyLabel = true) {
1348: $newidcat = strCopyCategory($idcat, $destcat, false, $bUseCopyLabel);
1349:
1350: $oCatColl = new cApiCategoryCollection();
1351: $aIds = $oCatColl->getIdsByWhereClause('parentid = ' . (int) $idcat);
1352: foreach ($aIds as $id) {
1353: strCopyTree($id, $newidcat, false, $bUseCopyLabel);
1354: }
1355:
1356: if ($remakeTree == true) {
1357: strRemakeTreeTable();
1358: }
1359: }
1360:
1361: 1362: 1363: 1364: 1365: 1366: 1367: 1368: 1369: 1370:
1371: function strAssignTemplate($idcat, $client, $idTplCfg) {
1372: global $perm;
1373:
1374:
1375: $iIdtplcfg = ($perm->have_perm_area_action('str_tplcfg', 'str_tplcfg')) ? (int) $idTplCfg : 0;
1376:
1377: $idtpl = NULL;
1378: if ($iIdtplcfg == 0) {
1379:
1380: $oTemplateColl = new cApiTemplateCollection('defaulttemplate = 1 AND idclient = ' . (int) $client);
1381: if (($oTemplate = $oTemplateColl->next()) !== false) {
1382: $idtpl = $oTemplate->get('idtpl');
1383: }
1384: } else {
1385:
1386: $idtpl = $idTplCfg;
1387: }
1388:
1389: if ($idtpl) {
1390:
1391: $oCatLangColl = new cApiCategoryLanguageCollection('idcat = ' . (int) $idcat);
1392: while (($oCatLang = $oCatLangColl->next()) !== false) {
1393: $oCatLang->assignTemplate($idtpl);
1394: }
1395: }
1396: }
1397:
1398: 1399: 1400: 1401: 1402: 1403: 1404: 1405: 1406: 1407: 1408: 1409: 1410: 1411: 1412: 1413: 1414:
1415: function strCheckTreeForErrors($addCats = array(), $ignoreCats = array()) {
1416: $errorMessages = array();
1417:
1418:
1419: $cats = new cApiCategoryCollection();
1420: $cats->select("idclient = '" . cSecurity::toInteger(cRegistry::getClientId()) . "'");
1421:
1422: $catArray = array();
1423:
1424: foreach ($addCats as $addCat) {
1425: if ($addCat->get('idcat') == 0) {
1426: continue;
1427: }
1428: $catArray[$addCat->get('idcat')] = $addCat;
1429: }
1430:
1431:
1432: while ($cat = $cats->next()) {
1433: if (in_array($cat->get('idcat'), $ignoreCats)) {
1434: continue;
1435: }
1436: if (isset($catArray[$cat->get('idcat')])) {
1437: continue;
1438: }
1439: $catArray[$cat->get('idcat')] = $cat;
1440: }
1441:
1442: ksort($catArray);
1443:
1444:
1445:
1446:
1447:
1448:
1449:
1450: $fine = true;
1451: $parents = array();
1452: foreach ($catArray as $idcat => $cat) {
1453: if (!array_key_exists($cat->get('parentid'), $catArray) && $cat->get('parentid') != 0) {
1454: $fine = false;
1455: $errorMessages[] = sprintf(i18n('Category %s has a parent id (%s) which does not exist!'), $idcat, $cat->get('parentid'));
1456: }
1457: $parents[$cat->get('parentid')][$idcat] = $cat;
1458: }
1459:
1460:
1461: foreach ($parents as $parentId => $parent) {
1462:
1463:
1464:
1465: $preIds = array();
1466: $postIds = array();
1467: foreach ($parent as $idcat => $cat) {
1468: $preId = $cat->get('preid');
1469: $postId = $cat->get('postid');
1470: if (in_array($preId, $preIds)) {
1471: $fine = false;
1472: $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);
1473: }
1474: if (in_array($postId, $postIds)) {
1475: $fine = false;
1476: $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);
1477: }
1478: $preIds[] = $preId;
1479: $postIds[] = $postId;
1480: }
1481:
1482:
1483:
1484: $startCat = null;
1485: foreach ($parent as $cat) {
1486: if ($cat->get('preid') == 0) {
1487: $startCat = $cat;
1488: break;
1489: }
1490: }
1491:
1492: if ($startCat == null) {
1493: $fine = false;
1494: $errorMessages[] = sprintf(i18n('There is no defined start (a category with preid == 0) in %s. Sorting impossible.'), $parentId);
1495: continue;
1496: }
1497:
1498: $actCat = $startCat;
1499: $checkedCats = array();
1500: $checkedCats[] = $startCat->get('idcat');
1501: while ($actCat != null) {
1502: $catId = $actCat->get('idcat');
1503: $postId = $actCat->get('postid');
1504: if ($postId == 0) {
1505: break;
1506: }
1507:
1508: if (!array_key_exists($postId, $parent)) {
1509: $fine = false;
1510: $errorMessages[] = sprintf(i18n('%s has an invalid post-id (%s). The category does not exist in this parent! Sorting impossible.'), $catId, $postId);
1511: break;
1512: }
1513: $actCat = $catArray[$postId];
1514:
1515:
1516: if (in_array($actCat->get('idcat'), $checkedCats)) {
1517: $fine = false;
1518: $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);
1519: break;
1520: }
1521: $checkedCats[] = $actCat->get('idcat');
1522:
1523:
1524:
1525: if (isset($catArray[$parentId])) {
1526: $parentClientId = $catArray[$parentId]->get('idclient');
1527: if ($actCat->get('idclient') != $parentClientId) {
1528: $fine = false;
1529: $errorMessages[] = sprintf(i18n('The category %s has a sub category (%s) that belongs to another client!'), $parentId, $catId);
1530: break;
1531: }
1532: }
1533: }
1534:
1535:
1536:
1537: $startCat = null;
1538: foreach ($parent as $cat) {
1539: if ($cat->get('postid') == 0) {
1540: $startCat = $cat;
1541: break;
1542: }
1543: }
1544:
1545:
1546: if ($startCat == null) {
1547: $fine = false;
1548: $errorMessages[] = sprintf(i18n('There is no defined end (a category with postid == 0) in %s. Sorting impossible.'), $parentId);
1549: continue;
1550: }
1551:
1552: $actCat = $startCat;
1553: $checkedCats = array();
1554: $checkedCats[] = $startCat->get('idcat');
1555: while ($actCat != null) {
1556: $catId = $actCat->get('idcat');
1557: $preId = $actCat->get('preid');
1558: if ($preId == 0) {
1559: break;
1560: }
1561:
1562: if (!array_key_exists($preId, $parent)) {
1563: $fine = false;
1564: $errorMessages[] = sprintf(i18n('%s has an invalid pre-id (%s). The category does not exist in this parent! Sorting impossible.'), $catId, $preId);
1565: break;
1566: }
1567: $actCat = $catArray[$preId];
1568:
1569:
1570: if (in_array($actCat->get('idcat'), $checkedCats)) {
1571: $fine = false;
1572: $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);
1573: break;
1574: }
1575: $checkedCats[] = $actCat->get('idcat');
1576: }
1577: }
1578:
1579:
1580: if (!$fine) {
1581: $messages = array();
1582: foreach ($errorMessages as $errorMessage) {
1583: if (in_array($errorMessage, $messages)) {
1584: continue;
1585: }
1586: $messages[] = $errorMessage;
1587: }
1588: return $messages;
1589: } else {
1590: return false;
1591: }
1592: }
1593: