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