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