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