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 = (int) $idtpl;
330: $template = new cApiTemplate($idtpl);
331:
332: $newidtplcfg = 0;
333: $idtplcfg = (int) $template->get('idtplcfg');
334: if ($idtplcfg) {
335:
336: $templateConfigColl = new cApiTemplateConfigurationCollection();
337: $templateConfig = $templateConfigColl->create(0);
338: $newidtplcfg = (int) $templateConfig->get('idtplcfg');
339: }
340:
341:
342: $templateColl = new cApiTemplateCollection();
343: $newTemplate = $templateColl->copyItem($template, array(
344: 'name' => sprintf(i18n("%s (Copy)"), $template->get('name')),
345: 'author' => (string) $auth->auth['uname'],
346: 'created' => date('Y-m-d H:i:s'),
347: 'lastmodified' => date('Y-m-d H:i:s'),
348: 'defaulttemplate' => 0
349: ));
350: $newidtpl = (int) $newTemplate->get('idtpl');
351:
352:
353: if ($idtplcfg) {
354: $templateConfig->set('idtpl', $newidtpl);
355: $templateConfig->store();
356: }
357:
358:
359: $containerColl = new cApiContainerCollection();
360: $containerColl->select('idtpl = ' . $idtpl . ' ORDER BY number');
361: while (($container = $containerColl->next()) !== false) {
362: $containerColl2 = new cApiContainerCollection();
363: $containerColl2->copyItem($container, array('idtpl' => $newidtpl));
364: }
365:
366:
367: if ($idtplcfg) {
368: $containerConfigColl = new cApiContainerConfigurationCollection();
369: $containerConfigColl->select('idtplcfg = ' . $idtplcfg . ' ORDER BY number');
370: while (($containerConfig = $containerConfigColl->next()) !== false) {
371: $containerConfigColl2 = new cApiContainerConfigurationCollection();
372: $containerConfigColl2->copyItem($containerConfig, array('idtplcfg' => $newidtplcfg));
373: }
374: }
375:
376: cInclude('includes', 'functions.rights.php');
377: copyRightsForElement('tpl', $idtpl, $newidtpl);
378:
379: return $newidtpl;
380: }
381:
382: 383: 384: 385: 386: 387: 388:
389: function tplIsTemplateInUse($idtpl) {
390: global $cfg, $client, $lang;
391:
392: $db = cRegistry::getDb();
393:
394: $sql = "SELECT
395: b.idcatlang, b.name, b.idlang, b.idcat
396: FROM
397: " . $cfg["tab"]["cat"] . " AS a,
398: " . $cfg["tab"]["cat_lang"] . " AS b
399: WHERE
400: a.idclient = '" . cSecurity::toInteger($client) . "' AND
401: a.idcat = b.idcat AND
402: b.idtplcfg IN (SELECT idtplcfg FROM " . $cfg["tab"]["tpl_conf"] . " WHERE idtpl = '" . $idtpl . "')
403: ORDER BY b.idlang ASC, b.name ASC ";
404: $db->query($sql);
405: if ($db->numRows() > 0) {
406: return true;
407: }
408:
409:
410: $sql = "SELECT
411: b.idartlang, b.title, b.idlang, b.idart
412: FROM
413: " . $cfg["tab"]["art"] . " AS a,
414: " . $cfg["tab"]["art_lang"] . " AS b
415: WHERE
416: a.idclient = '" . cSecurity::toInteger($client) . "' AND
417: a.idart = b.idart AND
418: b.idtplcfg IN (SELECT idtplcfg FROM " . $cfg["tab"]["tpl_conf"] . " WHERE idtpl = '" . $idtpl . "')
419: ORDER BY b.idlang ASC, b.title ASC ";
420:
421: $db->query($sql);
422:
423: if ($db->numRows() > 0) {
424: return true;
425: }
426:
427: return false;
428: }
429:
430: 431: 432: 433: 434: 435: 436:
437: function tplGetInUsedData($idtpl) {
438: global $cfg, $client, $lang;
439:
440: $db = cRegistry::getDb();
441:
442: $aUsedData = array();
443:
444:
445: $sql = "SELECT
446: b.idcatlang, b.name, b.idlang, b.idcat
447: FROM
448: " . $cfg["tab"]["cat"] . " AS a,
449: " . $cfg["tab"]["cat_lang"] . " AS b
450: WHERE
451: a.idclient = '" . cSecurity::toInteger($client) . "' AND
452: a.idcat = b.idcat AND
453: b.idtplcfg IN (SELECT idtplcfg FROM " . $cfg["tab"]["tpl_conf"] . " WHERE idtpl = '" . $idtpl . "')
454: ORDER BY b.idlang ASC, b.name ASC ";
455: $db->query($sql);
456: if ($db->numRows() > 0) {
457: while ($db->nextRecord()) {
458: $aUsedData['cat'][] = array(
459: 'name' => $db->f('name'),
460: 'lang' => $db->f('idlang'),
461: 'idcat' => $db->f('idcat'),
462: );
463: }
464: }
465:
466:
467: $sql = "SELECT
468: b.idartlang, b.title, b.idlang, b.idart
469: FROM
470: " . $cfg["tab"]["art"] . " AS a,
471: " . $cfg["tab"]["art_lang"] . " AS b
472: WHERE
473: a.idclient = '" . cSecurity::toInteger($client) . "' AND
474: a.idart = b.idart AND
475: b.idtplcfg IN (SELECT idtplcfg FROM " . $cfg["tab"]["tpl_conf"] . " WHERE idtpl = '" . $idtpl . "')
476: ORDER BY b.idlang ASC, b.title ASC ";
477:
478: $db->query($sql);
479:
480: if ($db->numRows() > 0) {
481: while ($db->nextRecord()) {
482: $aUsedData['art'][] = array(
483: 'title' => $db->f('title'),
484: 'lang' => $db->f('idlang'),
485: 'idart' => $db->f('idart'),
486: );
487: }
488: }
489:
490: return $aUsedData;
491: }
492:
493: 494: 495: 496: 497: 498:
499: function tplcfgDuplicate($idtplcfg) {
500: $templateConfig = new cApiTemplateConfiguration((int) $idtplcfg);
501: if (!$templateConfig->isLoaded()) {
502: return 0;
503: }
504:
505:
506: $templateConfigColl = new cApiTemplateConfigurationCollection();
507: $newTemplateConfig = $templateConfigColl->copyItem($templateConfig, array(
508: 'author' => (string) $auth->auth['uname'],
509: 'created' => date('Y-m-d H:i:s'),
510: 'lastmodified' => date('Y-m-d H:i:s'),
511: ));
512: $newidtplcfg = $newTemplateConfig->get('idtplcfg');
513:
514:
515: if ($idtplcfg) {
516: $containerConfigColl = new cApiContainerConfigurationCollection();
517: $containerConfigColl->select('idtplcfg = ' . $idtplcfg . ' ORDER BY number');
518: while (($containerConfig = $containerConfigColl->next()) !== false) {
519: $containerConfigColl2 = new cApiContainerConfigurationCollection();
520: $containerConfigColl2->copyItem($containerConfig, array('idtplcfg' => $newidtplcfg));
521: }
522: }
523:
524: return $newidtplcfg;
525: }
526:
527: 528: 529: 530: 531: 532: 533: 534: 535:
536:
537: function tplAutoFillModules($idtpl) {
538: global $cfg, $db_autofill, $containerinf, $_autoFillcontainerCache;
539:
540: if (!is_object($db_autofill)) {
541: $db_autofill = cRegistry::getDb();
542: }
543:
544: $sql = "SELECT idlay FROM " . $cfg["tab"]["tpl"] . " WHERE idtpl = '" . cSecurity::toInteger($idtpl) . "'";
545: $db_autofill->query($sql);
546:
547: if (!$db_autofill->nextRecord()) {
548: return false;
549: }
550:
551: $idlay = $db_autofill->f("idlay");
552:
553: if (!(is_array($containerinf) && array_key_exists($idlay, $containerinf) && array_key_exists($idlay, $_autoFillcontainerCache))) {
554: $_autoFillcontainerCache[$idlay] = tplGetContainerNumbersInLayout($idlay);
555: }
556:
557: $containerNumbers = $_autoFillcontainerCache[$idlay];
558:
559: $db = cRegistry::getDb();
560:
561: foreach ($containerNumbers as $containerNr) {
562: $currContainerInfo = $containerinf[$idlay][$containerNr];
563:
564: switch ($currContainerInfo["mode"]) {
565:
566: case "fixed":
567: if ($currContainerInfo["default"] != "") {
568: $sql = "SELECT idmod FROM " . $cfg["tab"]["mod"] . " WHERE name = '" . $db->escape($currContainerInfo["default"]) . "'";
569: $db_autofill->query($sql);
570:
571: if ($db_autofill->nextRecord()) {
572: $idmod = $db_autofill->f("idmod");
573:
574: $sql = "SELECT idcontainer FROM " . $cfg["tab"]["container"] . " WHERE idtpl = '" . cSecurity::toInteger($idtpl) . "' AND number = '" . cSecurity::toInteger($containerNr) . "'";
575: $db_autofill->query($sql);
576:
577: if ($db_autofill->nextRecord()) {
578: $sql = "UPDATE " . $cfg["tab"]["container"] .
579: " SET idmod = '" . cSecurity::toInteger($idmod) . "' WHERE idtpl = '" . cSecurity::toInteger($idtpl) . "'" .
580: " AND number = '" . cSecurity::toInteger($containerNr) . "' AND " .
581: " idcontainer = '" . cSecurity::toInteger($db_autofill->f("idcontainer")) . "'";
582: $db_autofill->query($sql);
583: } else {
584: $sql = "INSERT INTO " . $cfg["tab"]["container"] . " (idtpl, number, idmod) " .
585: " VALUES ('$idtpl', '$containerNr', '$idmod')";
586: $db_autofill->query($sql);
587: }
588: }
589: }
590:
591: case "mandatory":
592:
593: if ($currContainerInfo["default"] != "") {
594: $sql = "SELECT idmod FROM " . $cfg["tab"]["mod"] . " WHERE name = '" . $db->escape($currContainerInfo["default"]) . "'";
595: $db_autofill->query($sql);
596:
597: if ($db_autofill->nextRecord()) {
598: $idmod = $db_autofill->f("idmod");
599:
600: $sql = "SELECT idcontainer FROM " . $cfg["tab"]["container"] . " WHERE idtpl = '" . cSecurity::toInteger($idtpl) . "' AND number = '" . cSecurity::toInteger($containerNr) . "'";
601: $db_autofill->query($sql);
602:
603: if ($db_autofill->nextRecord()) {
604:
605: } else {
606: $sql = "INSERT INTO " . $cfg["tab"]["container"] . " (idtpl, number, idmod) " .
607: " VALUES ('" . cSecurity::toInteger($idtpl) . "', '" . cSecurity::toInteger($containerNr) . "', '" . cSecurity::toInteger($idmod) . "')";
608: $db_autofill->query($sql);
609: }
610: }
611: }
612: }
613: }
614: }
615:
616: 617: 618: 619: 620: 621: 622:
623: function tplProcessSendContainerConfiguration($idtpl, $idtplcfg, array $postData) {
624:
625: $containerColl = new cApiContainerCollection();
626: $containerConfColl = new cApiContainerConfigurationCollection();
627: $containerData = array();
628:
629:
630: $containerNumbers = $containerColl->getNumbersByTemplate($idtpl);
631: foreach ($containerNumbers as $number) {
632: $CiCMS_VAR = 'C' . $number . 'CMS_VAR';
633:
634: if (isset($postData[$CiCMS_VAR]) && is_array($postData[$CiCMS_VAR])) {
635: if (!isset($containerData[$number])) {
636: $containerData[$number] = '';
637: }
638: foreach ($postData[$CiCMS_VAR] as $key => $value) {
639: $containerData[$number] = cApiContainerConfiguration::addContainerValue($containerData[$number], $key, $value);
640: }
641: }
642: }
643:
644:
645: if (count($containerData) > 0) {
646:
647: $containerConfColl->deleteBy('idtplcfg', (int) $idtplcfg);
648:
649:
650: foreach ($containerData as $col => $val) {
651: $containerConfColl->create($idtplcfg, $col, $val);
652: }
653: }
654:
655: }
656: