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