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:
21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37:
38: function tplEditTemplate($changelayout, $idtpl, $name, $description, $idlay, $c, $default) {
39: global $db, $sess, $auth, $client, $cfg;
40:
41: $author = (string) $auth->auth['uname'];
42:
43: $template = new cApiTemplate();
44:
45: $template->loadByMany(array('idclient' => $client, 'idtpl' => $idtpl));
46:
47: if ($template->isLoaded() && $template->get('idtpl') != $idtpl) {
48: cRegistry::addErrorMessage(i18n("Template name already exists"));
49: return -1;
50: }
51:
52: if (true === cRegistry::getConfigValue('simulate_magic_quotes')) {
53: $name = stripslashes($name);
54: $description = stripslashes($description);
55: }
56:
57: if (!$idtpl) {
58:
59: $templateColl = new cApiTemplateCollection();
60: $template = $templateColl->create($client, $idlay, 0, $name, $description, 1, 0, 0);
61: $idtpl = $template->get('idtpl');
62:
63:
64: $templateConfColl = new cApiTemplateConfigurationCollection();
65: $templateConf = $templateConfColl->create($idtpl);
66: $idtplcfg = $templateConf->get('idtplcfg');
67:
68:
69: $template->set('idtplcfg', $idtplcfg);
70: $template->store();
71:
72:
73: cInclude('includes', 'functions.rights.php');
74: createRightsForElement('tpl', $idtpl);
75: } else {
76:
77:
78: $lastmodified = date('Y-m-d H:i:s');
79:
80:
81: $template = new cApiTemplate($idtpl);
82: $template->set('name', $name);
83: $template->set('description', $description);
84: $template->set('idlay', $idlay);
85: $template->set('author', $author);
86: $template->set('lastmodified', $lastmodified);
87: $template->store();
88:
89:
90: $containerColl = new cApiContainerCollection();
91: $containerColl->clearAssignments($idtpl);
92:
93: if (is_array($c) && (int)$changelayout !== 1) {
94: foreach ($c as $idcontainer => $dummyval) {
95: $containerColl2 = new cApiContainerCollection();
96: $containerColl2->create($idtpl, $idcontainer, $c[$idcontainer]);
97: }
98: }
99:
100:
101: conGenerateCodeForAllArtsUsingTemplate($idtpl);
102: }
103:
104: if ($default == 1) {
105: $sql = "UPDATE " . $cfg["tab"]["tpl"] . " SET defaulttemplate = 0 WHERE idclient = " . cSecurity::toInteger($client) . " AND idtpl != " . cSecurity::toInteger($template->get('idtpl'));
106: $db->query($sql);
107:
108: $template->set('defaulttemplate', 1);
109: $template->store();
110: } else {
111: $template->set('defaulttemplate', 0);
112: $template->store();
113: }
114:
115:
116:
117:
118:
119:
120:
121: return $idtpl;
122: }
123:
124: 125: 126: 127: 128: 129: 130: 131: 132:
133: function tplDeleteTemplate($idtpl) {
134:
135: global $db, $client, $lang, $cfg, $area_tree, $perm;
136:
137: $sql = "DELETE FROM " . $cfg["tab"]["tpl"] . " WHERE idtpl = '" . cSecurity::toInteger($idtpl) . "'";
138: $db->query($sql);
139:
140:
141:
142: $sql = "DELETE FROM " . $cfg["tab"]["container"] . " WHERE idtpl = '" . cSecurity::toInteger($idtpl) . "'";
143: $db->query($sql);
144:
145: $idsToDelete = array();
146: $sql = "SELECT idtplcfg FROM " . $cfg["tab"]["tpl_conf"] . " WHERE idtpl = '" . cSecurity::toInteger($idtpl) . "'";
147: $db->query($sql);
148: while ($db->nextRecord()) {
149: $idsToDelete[] = $db->f("idtplcfg");
150: }
151:
152: foreach ($idsToDelete as $id) {
153: $sql = "DELETE FROM " . $cfg["tab"]["tpl_conf"] . " WHERE idtplcfg = '" . cSecurity::toInteger($id) . "'";
154: $db->query($sql);
155:
156: $sql = "DELETE FROM " . $cfg["tab"]["container_conf"] . " WHERE idtplcfg = '" . cSecurity::toInteger($id) . "'";
157: $db->query($sql);
158: }
159:
160: cInclude("includes", "functions.rights.php");
161: deleteRightsForElement("tpl", $idtpl);
162: }
163:
164: 165: 166: 167: 168: 169: 170: 171: 172: 173:
174: function tplBrowseLayoutForContainers($idlay) {
175: global $db, $cfg, $containerinf, $lang;
176:
177: $layoutInFile = new cLayoutHandler($idlay, '', $cfg, $lang);
178: $code = $layoutInFile->getLayoutCode();
179:
180: $containerNumbers = array();
181: $returnStr = '';
182:
183: preg_match_all("/CMS_CONTAINER\[([0-9]*)\]/", $code, $containerMatches);
184: $posBody = cString::findFirstPosCI($code, '<body>');
185: $codeBeforeHeader = cString::getPartOfString($code, 0, $posBody);
186:
187: foreach ($containerMatches[1] as $value) {
188: if (preg_match("/CMS_CONTAINER\[$value\]/", $codeBeforeHeader)) {
189: $containerinf[$idlay][$value]["is_body"] = false;
190: } else {
191: $containerinf[$idlay][$value]["is_body"] = true;
192: }
193: }
194:
195: if (is_array($containerinf[$idlay])) {
196: foreach ($containerinf[$idlay] as $key => $value) {
197: $containerMatches[1][] = $key;
198: }
199: }
200:
201: foreach ($containerMatches[1] as $value) {
202: if (!in_array($value, $containerNumbers)) {
203: $containerNumbers[] = $value;
204: }
205: }
206: asort($containerNumbers);
207:
208: $returnStr = implode('&', $containerNumbers);
209:
210: return $returnStr;
211: }
212:
213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223:
224: function tplGetContainerNumbersInLayout($idlay) {
225: $containerNumbers = array();
226:
227: tplPreparseLayout($idlay);
228: $containerNumbersStr = tplBrowseLayoutForContainers($idlay);
229: if (!empty($containerNumbersStr)) {
230: $containerNumbers = explode('&', $containerNumbersStr);
231: }
232:
233: return $containerNumbers;
234: }
235:
236: 237: 238: 239: 240: 241: 242: 243: 244: 245:
246: function tplGetContainerName($idlay, $container) {
247: global $db, $cfg, $containerinf;
248:
249: if (is_array($containerinf[$idlay])) {
250: if (array_key_exists($container, $containerinf[$idlay])) {
251: return $containerinf[$idlay][$container]["name"];
252: }
253: }
254: }
255:
256: 257: 258: 259: 260: 261: 262: 263: 264: 265:
266: function tplGetContainerMode($idlay, $container) {
267: global $containerinf;
268:
269: if (is_array($containerinf[$idlay])) {
270: if (array_key_exists($container, $containerinf[$idlay])) {
271: return $containerinf[$idlay][$container]["mode"];
272: }
273: }
274: }
275:
276: 277: 278: 279: 280: 281: 282: 283: 284: 285:
286: function tplGetContainerTypes($idlay, $container) {
287: global $containerinf;
288:
289: if (is_array($containerinf[$idlay])) {
290: if (array_key_exists($container, $containerinf[$idlay])) {
291: if ($containerinf[$idlay][$container]["types"] != "") {
292: $list = explode(",", $containerinf[$idlay][$container]["types"]);
293:
294: foreach ($list as $key => $value) {
295: $list[$key] = trim($value);
296: }
297: return $list;
298: }
299: }
300: }
301:
302: return [];
303: }
304:
305: 306: 307: 308: 309: 310: 311: 312: 313: 314:
315: function tplGetContainerDefault($idlay, $container) {
316: global $containerinf;
317:
318: if (is_array($containerinf[$idlay])) {
319: if (array_key_exists($container, $containerinf[$idlay])) {
320: return $containerinf[$idlay][$container]["default"];
321: }
322: }
323: }
324:
325: 326: 327: 328: 329: 330: 331: 332:
333: function tplPreparseLayout($idlay) {
334: global $db, $cfg, $containerinf, $lang;
335:
336: $layoutInFile = new cLayoutHandler($idlay, "", $cfg, $lang);
337: $code = $layoutInFile->getLayoutCode();
338:
339: $parser = new HtmlParser($code);
340: $bIsBody = false;
341:
342: while ($parser->parse()) {
343: if (cString::toLowerCase($parser->getNodeName()) == 'body') {
344: $bIsBody = true;
345: }
346:
347: if ($parser->getNodeName() == "container" && $parser->getNodeType() == HtmlParser::NODE_TYPE_ELEMENT) {
348: $idcontainer = $parser->getNodeAttributes('id');
349:
350: $mode = $parser->getNodeAttributes('mode');
351:
352: if ($mode == "") {
353: $mode = "optional";
354: }
355:
356: $containerinf[$idlay][$idcontainer]["name"] = $parser->getNodeAttributes('name');
357: $containerinf[$idlay][$idcontainer]["mode"] = $mode;
358: $containerinf[$idlay][$idcontainer]["default"] = $parser->getNodeAttributes('default');
359: $containerinf[$idlay][$idcontainer]["types"] = $parser->getNodeAttributes('types');
360: $containerinf[$idlay][$idcontainer]["is_body"] = $bIsBody;
361: }
362: }
363: }
364:
365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376:
377: function tplDuplicateTemplate($idtpl) {
378: global $db, $client, $lang, $cfg, $sess, $auth;
379:
380: $idtpl = cSecurity::toInteger($idtpl);
381: $template = new cApiTemplate($idtpl);
382:
383: $newidtplcfg = 0;
384: $idtplcfg = cSecurity::toInteger($template->get('idtplcfg'));
385: if ($idtplcfg) {
386:
387: $templateConfigColl = new cApiTemplateConfigurationCollection();
388: $templateConfig = $templateConfigColl->create(0);
389: $newidtplcfg = cSecurity::toInteger($templateConfig->get('idtplcfg'));
390: }
391:
392:
393: $templateColl = new cApiTemplateCollection();
394: $newTemplate = $templateColl->copyItem($template, array(
395: 'idtplcfg' => $newidtplcfg,
396: 'name' => sprintf(i18n("%s (Copy)"), $template->get('name')),
397: 'author' => cSecurity::toString($auth->auth['uname']),
398: 'created' => date('Y-m-d H:i:s'),
399: 'lastmodified' => date('Y-m-d H:i:s'),
400: 'defaulttemplate' => 0
401: ));
402: $newidtpl = cSecurity::toInteger($newTemplate->get('idtpl'));
403:
404:
405: if ($idtplcfg) {
406: $templateConfig->set('idtpl', $newidtpl);
407: $templateConfig->store();
408: }
409:
410:
411: $containerColl = new cApiContainerCollection();
412: $containerColl->select('idtpl = ' . $idtpl . ' ORDER BY number');
413: while (($container = $containerColl->next()) !== false) {
414: $containerColl2 = new cApiContainerCollection();
415: $containerColl2->copyItem($container, array('idtpl' => $newidtpl));
416: }
417:
418:
419: if ($idtplcfg) {
420: $containerConfigColl = new cApiContainerConfigurationCollection();
421: $containerConfigColl->select('idtplcfg = ' . $idtplcfg . ' ORDER BY number');
422: while (($containerConfig = $containerConfigColl->next()) !== false) {
423: $containerConfigColl2 = new cApiContainerConfigurationCollection();
424: $containerConfigColl2->copyItem($containerConfig, array('idtplcfg' => $newidtplcfg));
425: }
426: }
427:
428: cInclude('includes', 'functions.rights.php');
429: copyRightsForElement('tpl', $idtpl, $newidtpl);
430:
431: return $newidtpl;
432: }
433:
434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444:
445: function tplIsTemplateInUse($idtpl) {
446: global $cfg, $client, $lang;
447:
448: $db = cRegistry::getDb();
449:
450: $sql = "SELECT
451: b.idcatlang, b.name, b.idlang, b.idcat
452: FROM
453: " . $cfg["tab"]["cat"] . " AS a,
454: " . $cfg["tab"]["cat_lang"] . " AS b
455: WHERE
456: a.idclient = '" . cSecurity::toInteger($client) . "' AND
457: a.idcat = b.idcat AND
458: b.idtplcfg IN (SELECT idtplcfg FROM " . $cfg["tab"]["tpl_conf"] . " WHERE idtpl = '" . $idtpl . "')
459: ORDER BY b.idlang ASC, b.name ASC ";
460: $db->query($sql);
461: if ($db->numRows() > 0) {
462: return true;
463: }
464:
465:
466: $sql = "SELECT
467: b.idartlang, b.title, b.idlang, b.idart
468: FROM
469: " . $cfg["tab"]["art"] . " AS a,
470: " . $cfg["tab"]["art_lang"] . " AS b
471: WHERE
472: a.idclient = '" . cSecurity::toInteger($client) . "' AND
473: a.idart = b.idart AND
474: b.idtplcfg IN (SELECT idtplcfg FROM " . $cfg["tab"]["tpl_conf"] . " WHERE idtpl = '" . $idtpl . "')
475: ORDER BY b.idlang ASC, b.title ASC ";
476:
477: $db->query($sql);
478:
479: if ($db->numRows() > 0) {
480: return true;
481: }
482:
483: return false;
484: }
485:
486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496:
497: function tplGetInUsedData($idtpl) {
498: global $cfg, $client, $lang;
499:
500: $db = cRegistry::getDb();
501:
502: $aUsedData = array();
503:
504:
505: $sql = "SELECT
506: b.idcatlang, b.name, b.idlang, b.idcat
507: FROM
508: " . $cfg["tab"]["cat"] . " AS a,
509: " . $cfg["tab"]["cat_lang"] . " AS b
510: WHERE
511: a.idclient = '" . cSecurity::toInteger($client) . "' AND
512: a.idcat = b.idcat AND
513: b.idtplcfg IN (SELECT idtplcfg FROM " . $cfg["tab"]["tpl_conf"] . " WHERE idtpl = '" . $idtpl . "')
514: ORDER BY b.idlang ASC, b.name ASC ";
515: $db->query($sql);
516: if ($db->numRows() > 0) {
517: while ($db->nextRecord()) {
518: $aUsedData['cat'][] = array(
519: 'name' => $db->f('name'),
520: 'lang' => $db->f('idlang'),
521: 'idcat' => $db->f('idcat'),
522: );
523: }
524: }
525:
526:
527: $sql = "SELECT
528: b.idartlang, b.title, b.idlang, b.idart
529: FROM
530: " . $cfg["tab"]["art"] . " AS a,
531: " . $cfg["tab"]["art_lang"] . " AS b
532: WHERE
533: a.idclient = '" . cSecurity::toInteger($client) . "' AND
534: a.idart = b.idart AND
535: b.idtplcfg IN (SELECT idtplcfg FROM " . $cfg["tab"]["tpl_conf"] . " WHERE idtpl = '" . $idtpl . "')
536: ORDER BY b.idlang ASC, b.title ASC ";
537:
538: $db->query($sql);
539:
540: if ($db->numRows() > 0) {
541: while ($db->nextRecord()) {
542: $aUsedData['art'][] = array(
543: 'title' => $db->f('title'),
544: 'lang' => $db->f('idlang'),
545: 'idart' => $db->f('idart'),
546: );
547: }
548: }
549:
550: return $aUsedData;
551: }
552:
553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565:
566: function tplcfgDuplicate($idtplcfg) {
567: global $auth;
568:
569: $templateConfig = new cApiTemplateConfiguration(cSecurity::toInteger($idtplcfg));
570: if (!$templateConfig->isLoaded()) {
571: return 0;
572: }
573:
574:
575: $templateConfigColl = new cApiTemplateConfigurationCollection();
576: $newTemplateConfig = $templateConfigColl->copyItem($templateConfig, array(
577: 'author' => (string) $auth->auth['uname'],
578: 'created' => date('Y-m-d H:i:s'),
579: 'lastmodified' => date('Y-m-d H:i:s'),
580: ));
581: $newidtplcfg = $newTemplateConfig->get('idtplcfg');
582:
583:
584: if ($idtplcfg) {
585: $containerConfigColl = new cApiContainerConfigurationCollection();
586: $containerConfigColl->select('idtplcfg = ' . $idtplcfg . ' ORDER BY number');
587: while (($containerConfig = $containerConfigColl->next()) !== false) {
588: $containerConfigColl2 = new cApiContainerConfigurationCollection();
589: $containerConfigColl2->copyItem($containerConfig, array('idtplcfg' => $newidtplcfg));
590: }
591: }
592:
593: return $newidtplcfg;
594: }
595:
596: 597: 598: 599: 600: 601: 602: 603: 604: 605: 606: 607: 608: 609: 610:
611: function tplAutoFillModules($idtpl) {
612: global $cfg, $db_autofill, $containerinf, $_autoFillcontainerCache;
613:
614: if (!is_object($db_autofill)) {
615: $db_autofill = cRegistry::getDb();
616: }
617:
618: $sql = "SELECT idlay FROM " . $cfg["tab"]["tpl"] . " WHERE idtpl = '" . cSecurity::toInteger($idtpl) . "'";
619: $db_autofill->query($sql);
620:
621: if (!$db_autofill->nextRecord()) {
622: return false;
623: }
624:
625: $idlay = $db_autofill->f("idlay");
626:
627: if (!(is_array($containerinf) && array_key_exists($idlay, $containerinf) && array_key_exists($idlay, $_autoFillcontainerCache))) {
628: $_autoFillcontainerCache[$idlay] = tplGetContainerNumbersInLayout($idlay);
629: }
630:
631: $containerNumbers = $_autoFillcontainerCache[$idlay];
632:
633: $db = cRegistry::getDb();
634:
635: foreach ($containerNumbers as $containerNr) {
636: $currContainerInfo = $containerinf[$idlay][$containerNr];
637:
638: switch ($currContainerInfo["mode"]) {
639:
640: case "fixed":
641: if ($currContainerInfo["default"] != "") {
642: $sql = "SELECT idmod FROM " . $cfg["tab"]["mod"] . " WHERE name = '" . $db->escape($currContainerInfo["default"]) . "'";
643: $db_autofill->query($sql);
644:
645: if ($db_autofill->nextRecord()) {
646: $idmod = $db_autofill->f("idmod");
647:
648: $sql = "SELECT idcontainer FROM " . $cfg["tab"]["container"] . " WHERE idtpl = '" . cSecurity::toInteger($idtpl) . "' AND number = '" . cSecurity::toInteger($containerNr) . "'";
649: $db_autofill->query($sql);
650:
651: if ($db_autofill->nextRecord()) {
652: $sql = "UPDATE " . $cfg["tab"]["container"] .
653: " SET idmod = '" . cSecurity::toInteger($idmod) . "' WHERE idtpl = '" . cSecurity::toInteger($idtpl) . "'" .
654: " AND number = '" . cSecurity::toInteger($containerNr) . "' AND " .
655: " idcontainer = '" . cSecurity::toInteger($db_autofill->f("idcontainer")) . "'";
656: $db_autofill->query($sql);
657: } else {
658: $sql = "INSERT INTO " . $cfg["tab"]["container"] . " (idtpl, number, idmod) " .
659: " VALUES ('$idtpl', '$containerNr', '$idmod')";
660: $db_autofill->query($sql);
661: }
662: }
663: }
664:
665: case "mandatory":
666:
667: if ($currContainerInfo["default"] != "") {
668: $sql = "SELECT idmod FROM " . $cfg["tab"]["mod"] . " WHERE name = '" . $db->escape($currContainerInfo["default"]) . "'";
669: $db_autofill->query($sql);
670:
671: if ($db_autofill->nextRecord()) {
672: $idmod = $db_autofill->f("idmod");
673:
674: $sql = "SELECT idcontainer FROM " . $cfg["tab"]["container"] . " WHERE idtpl = '" . cSecurity::toInteger($idtpl) . "' AND number = '" . cSecurity::toInteger($containerNr) . "'";
675: $db_autofill->query($sql);
676:
677: if ($db_autofill->nextRecord()) {
678:
679: } else {
680: $sql = "INSERT INTO " . $cfg["tab"]["container"] . " (idtpl, number, idmod) " .
681: " VALUES ('" . cSecurity::toInteger($idtpl) . "', '" . cSecurity::toInteger($containerNr) . "', '" . cSecurity::toInteger($idmod) . "')";
682: $db_autofill->query($sql);
683: }
684: }
685: }
686: }
687: }
688: }
689:
690: 691: 692: 693: 694: 695: 696: 697: 698: 699: 700: 701: 702:
703: function tplProcessSendContainerConfiguration($idtpl, $idtplcfg, array $postData) {
704:
705: $containerColl = new cApiContainerCollection();
706: $containerConfColl = new cApiContainerConfigurationCollection();
707: $containerData = array();
708:
709:
710: $containerNumbers = $containerColl->getNumbersByTemplate($idtpl);
711: foreach ($containerNumbers as $number) {
712: $CiCMS_VAR = 'C' . $number . 'CMS_VAR';
713:
714: if (!isset($containerData[$number])) {
715: $containerData[$number] = '';
716: }
717: if (isset($postData[$CiCMS_VAR]) && is_array($postData[$CiCMS_VAR])) {
718: foreach ($postData[$CiCMS_VAR] as $key => $value) {
719: $containerData[$number] = cApiContainerConfiguration::addContainerValue($containerData[$number], $key, $value);
720: }
721: }
722: }
723:
724:
725: if (count($containerData) > 0) {
726:
727: $containerConfColl->deleteBy('idtplcfg', (int) $idtplcfg);
728:
729:
730: foreach ($containerData as $col => $val) {
731: $containerConfColl->create($idtplcfg, $col, $val);
732: }
733: }
734:
735: }
736: