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