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