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