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: preg_match_all("/CMS_CONTAINER\[([0-9]*)\]/", $code, $a_container);
153: $iPosBody = stripos($code, '<body>');
154: $sCodeBeforeHeader = substr($code, 0, $iPosBody);
155:
156: foreach ($a_container[1] as $value) {
157: if (preg_match("/CMS_CONTAINER\[$value\]/", $sCodeBeforeHeader)) {
158: $containerinf[$idlay][$value]["is_body"] = false;
159: } else {
160: $containerinf[$idlay][$value]["is_body"] = true;
161: }
162: }
163:
164: if (is_array($containerinf[$idlay])) {
165: foreach ($containerinf[$idlay] as $key => $value) {
166: $a_container[1][] = $key;
167: }
168: }
169:
170: $container = Array();
171:
172: foreach ($a_container[1] as $value) {
173: if (!in_array($value, $container)) {
174: $container[] = $value;
175: }
176: }
177:
178: asort($container);
179:
180: if (is_array($container)) {
181: $tmp_returnstring = implode("&", $container);
182: }
183: return $tmp_returnstring;
184: }
185:
186: 187: 188: 189: 190: 191: 192: 193:
194: function tplGetContainerName($idlay, $container) {
195: global $db, $cfg, $containerinf;
196:
197: if (is_array($containerinf[$idlay])) {
198: if (array_key_exists($container, $containerinf[$idlay])) {
199: return $containerinf[$idlay][$container]["name"];
200: }
201: }
202: }
203:
204: 205: 206: 207: 208: 209: 210: 211:
212: function tplGetContainerMode($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]["mode"];
218: }
219: }
220: }
221:
222: 223: 224: 225: 226: 227: 228: 229:
230: function tplGetContainerTypes($idlay, $container) {
231: global $db, $cfg, $containerinf;
232:
233: if (is_array($containerinf[$idlay])) {
234: if (array_key_exists($container, $containerinf[$idlay])) {
235: if ($containerinf[$idlay][$container]["types"] != "") {
236: $list = explode(",", $containerinf[$idlay][$container]["types"]);
237:
238: foreach ($list as $key => $value) {
239: $list[$key] = trim($value);
240: }
241: return $list;
242: }
243: }
244: }
245: }
246:
247: 248: 249: 250: 251: 252: 253: 254:
255: function tplGetContainerDefault($idlay, $container) {
256: global $db, $cfg, $containerinf;
257:
258: if (is_array($containerinf[$idlay])) {
259: if (array_key_exists($container, $containerinf[$idlay])) {
260: return $containerinf[$idlay][$container]["default"];
261: }
262: }
263: }
264:
265: 266: 267: 268: 269:
270: function tplPreparseLayout($idlay) {
271: global $db, $cfg, $containerinf, $lang;
272:
273: $layoutInFile = new cLayoutHandler($idlay, "", $cfg, $lang);
274: $code = $layoutInFile->getLayoutCode();
275:
276: $parser = new HtmlParser($code);
277: $bIsBody = false;
278: while ($parser->parse()) {
279: if (strtolower($parser->iNodeName) == 'body') {
280: $bIsBody = true;
281: }
282:
283: if ($parser->iNodeName == "container" && $parser->iNodeType == HtmlParser::NODE_TYPE_ELEMENT) {
284: $idcontainer = $parser->iNodeAttributes["id"];
285:
286: $mode = $parser->iNodeAttributes["mode"];
287:
288: if ($mode == "") {
289: $mode = "optional";
290: }
291:
292: $containerinf[$idlay][$idcontainer]["name"] = $parser->iNodeAttributes["name"];
293: $containerinf[$idlay][$idcontainer]["mode"] = $mode;
294: $containerinf[$idlay][$idcontainer]["default"] = $parser->iNodeAttributes["default"];
295: $containerinf[$idlay][$idcontainer]["types"] = $parser->iNodeAttributes["types"];
296: $containerinf[$idlay][$idcontainer]["is_body"] = $bIsBody;
297: }
298: }
299: }
300:
301: 302: 303: 304: 305: 306: 307:
308: function tplDuplicateTemplate($idtpl) {
309: global $db, $client, $lang, $cfg, $sess, $auth;
310:
311: $idtpl = (int) $idtpl;
312: $template = new cApiTemplate($idtpl);
313:
314: $newidtplcfg = 0;
315: $idtplcfg = (int) $template->get('idtplcfg');
316: if ($idtplcfg) {
317:
318: $templateConfigColl = new cApiTemplateConfigurationCollection();
319: $templateConfig = $templateConfigColl->create(0);
320: $newidtplcfg = (int) $templateConfig->get('idtplcfg');
321: }
322:
323:
324: $templateColl = new cApiTemplateCollection();
325: $newTemplate = $templateColl->copyItem($template, array(
326: 'name' => sprintf(i18n("%s (Copy)"), $template->get('name')),
327: 'author' => (string) $auth->auth['uname'],
328: 'created' => date('Y-m-d H:i:s'),
329: 'lastmodified' => date('Y-m-d H:i:s'),
330: ));
331: $newidtpl = (int) $newTemplate->get('idtpl');
332:
333:
334: if ($idtplcfg) {
335: $templateConfig->set('idtpl', $newidtpl);
336: $templateConfig->store();
337: }
338:
339:
340: $containerColl = new cApiContainerCollection();
341: $containerColl->select('idtpl = ' . $idtpl . ' ORDER BY number');
342: while (($container = $containerColl->next()) !== false) {
343: $containerColl2 = new cApiContainerCollection();
344: $containerColl2->copyItem($container, array('idtpl' => $newidtpl));
345: }
346:
347:
348: if ($idtplcfg) {
349: $containerConfigColl = new cApiContainerConfigurationCollection();
350: $containerConfigColl->select('idtplcfg = ' . $idtplcfg . ' ORDER BY number');
351: while (($containerConfig = $containerConfigColl->next()) !== false) {
352: $containerConfigColl2 = new cApiContainerConfigurationCollection();
353: $containerConfigColl2->copyItem($containerConfig, array('idtplcfg' => $newidtplcfg));
354: }
355: }
356:
357: cInclude('includes', 'functions.rights.php');
358: copyRightsForElement('tpl', $idtpl, $newidtpl);
359:
360: return $newidtpl;
361: }
362:
363: 364: 365: 366: 367: 368: 369:
370: function tplIsTemplateInUse($idtpl) {
371: global $cfg, $client, $lang;
372:
373: $db = cRegistry::getDb();
374:
375: $sql = "SELECT
376: b.idcatlang, b.name, b.idlang, b.idcat
377: FROM
378: " . $cfg["tab"]["cat"] . " AS a,
379: " . $cfg["tab"]["cat_lang"] . " AS b
380: WHERE
381: a.idclient = '" . cSecurity::toInteger($client) . "' AND
382: a.idcat = b.idcat AND
383: b.idtplcfg IN (SELECT idtplcfg FROM " . $cfg["tab"]["tpl_conf"] . " WHERE idtpl = '" . $idtpl . "')
384: ORDER BY b.idlang ASC, b.name ASC ";
385: $db->query($sql);
386: if ($db->numRows() > 0) {
387: return true;
388: }
389:
390:
391: $sql = "SELECT
392: b.idartlang, b.title, b.idlang, b.idart
393: FROM
394: " . $cfg["tab"]["art"] . " AS a,
395: " . $cfg["tab"]["art_lang"] . " AS b
396: WHERE
397: a.idclient = '" . cSecurity::toInteger($client) . "' AND
398: a.idart = b.idart AND
399: b.idtplcfg IN (SELECT idtplcfg FROM " . $cfg["tab"]["tpl_conf"] . " WHERE idtpl = '" . $idtpl . "')
400: ORDER BY b.idlang ASC, b.title ASC ";
401:
402: $db->query($sql);
403:
404: if ($db->numRows() > 0) {
405: return true;
406: }
407:
408: return false;
409: }
410:
411: 412: 413: 414: 415: 416: 417:
418: function tplGetInUsedData($idtpl) {
419: global $cfg, $client, $lang;
420:
421: $db = cRegistry::getDb();
422:
423: $aUsedData = array();
424:
425:
426: $sql = "SELECT
427: b.idcatlang, b.name, b.idlang, b.idcat
428: FROM
429: " . $cfg["tab"]["cat"] . " AS a,
430: " . $cfg["tab"]["cat_lang"] . " AS b
431: WHERE
432: a.idclient = '" . cSecurity::toInteger($client) . "' AND
433: a.idcat = b.idcat AND
434: b.idtplcfg IN (SELECT idtplcfg FROM " . $cfg["tab"]["tpl_conf"] . " WHERE idtpl = '" . $idtpl . "')
435: ORDER BY b.idlang ASC, b.name ASC ";
436: $db->query($sql);
437: if ($db->numRows() > 0) {
438: while ($db->nextRecord()) {
439: $aUsedData['cat'][] = array(
440: 'name' => $db->f('name'),
441: 'lang' => $db->f('idlang'),
442: 'idcat' => $db->f('idcat'),
443: );
444: }
445: }
446:
447:
448: $sql = "SELECT
449: b.idartlang, b.title, b.idlang, b.idart
450: FROM
451: " . $cfg["tab"]["art"] . " AS a,
452: " . $cfg["tab"]["art_lang"] . " AS b
453: WHERE
454: a.idclient = '" . cSecurity::toInteger($client) . "' AND
455: a.idart = b.idart AND
456: b.idtplcfg IN (SELECT idtplcfg FROM " . $cfg["tab"]["tpl_conf"] . " WHERE idtpl = '" . $idtpl . "')
457: ORDER BY b.idlang ASC, b.title ASC ";
458:
459: $db->query($sql);
460:
461: if ($db->numRows() > 0) {
462: while ($db->nextRecord()) {
463: $aUsedData['art'][] = array(
464: 'title' => $db->f('title'),
465: 'lang' => $db->f('idlang'),
466: 'idart' => $db->f('idart'),
467: );
468: }
469: }
470:
471: return $aUsedData;
472: }
473:
474: 475: 476: 477: 478: 479:
480: function tplcfgDuplicate($idtplcfg) {
481: $templateConfig = new cApiTemplateConfiguration((int) $idtplcfg);
482: if (!$templateConfig->isLoaded()) {
483: return 0;
484: }
485:
486:
487: $templateConfigColl = new cApiTemplateConfigurationCollection();
488: $newTemplateConfig = $templateConfigColl->copyItem($templateConfig, array(
489: 'author' => (string) $auth->auth['uname'],
490: 'created' => date('Y-m-d H:i:s'),
491: 'lastmodified' => date('Y-m-d H:i:s'),
492: ));
493: $newidtplcfg = $newTemplateConfig->get('idtplcfg');
494:
495:
496: if ($idtplcfg) {
497: $containerConfigColl = new cApiContainerConfigurationCollection();
498: $containerConfigColl->select('idtplcfg = ' . $idtplcfg . ' ORDER BY number');
499: while (($containerConfig = $containerConfigColl->next()) !== false) {
500: $containerConfigColl2 = new cApiContainerConfigurationCollection();
501: $containerConfigColl2->copyItem($containerConfig, array('idtplcfg' => $newidtplcfg));
502: }
503: }
504:
505: return $newidtplcfg;
506: }
507:
508: 509: 510: 511: 512: 513: 514: 515: 516:
517:
518: function tplAutoFillModules($idtpl) {
519: global $cfg, $db_autofill, $containerinf, $_autoFillcontainerCache;
520:
521: if (!is_object($db_autofill)) {
522: $db_autofill = cRegistry::getDb();
523: }
524:
525: $sql = "SELECT idlay FROM " . $cfg["tab"]["tpl"] . " WHERE idtpl = '" . cSecurity::toInteger($idtpl) . "'";
526: $db_autofill->query($sql);
527:
528: if (!$db_autofill->nextRecord()) {
529: return false;
530: }
531:
532: $idlay = $db_autofill->f("idlay");
533:
534: if (!(is_array($containerinf) && array_key_exists($idlay, $containerinf) && array_key_exists($idlay, $_autoFillcontainerCache))) {
535: tplPreparseLayout($idlay);
536: $_autoFillcontainerCache[$idlay] = tplBrowseLayoutForContainers($idlay);
537: }
538:
539: $a_container = explode("&", $_autoFillcontainerCache[$idlay]);
540:
541: $db = cRegistry::getDb();
542:
543: foreach ($a_container as $container) {
544: switch ($containerinf[$idlay][$container]["mode"]) {
545:
546: case "fixed":
547: if ($containerinf[$idlay][$container]["default"] != "") {
548: $sql = "SELECT idmod FROM " . $cfg["tab"]["mod"]
549: . " WHERE name = '" .
550: $db->escape($containerinf[$idlay][$container]["default"]) . "'";
551:
552: $db_autofill->query($sql);
553:
554: if ($db_autofill->nextRecord()) {
555: $idmod = $db_autofill->f("idmod");
556:
557: $sql = "SELECT idcontainer FROM " . $cfg["tab"]["container"] . " WHERE idtpl = '" . cSecurity::toInteger($idtpl) . "' AND number = '" . cSecurity::toInteger($container) . "'";
558:
559: $db_autofill->query($sql);
560:
561: if ($db_autofill->nextRecord()) {
562: $sql = "UPDATE " . $cfg["tab"]["container"] .
563: " SET idmod = '" . cSecurity::toInteger($idmod) . "' WHERE idtpl = '" . cSecurity::toInteger($idtpl) . "'" .
564: " AND number = '" . cSecurity::toInteger($container) . "' AND " .
565: " idcontainer = '" . cSecurity::toInteger($db_autofill->f("idcontainer")) . "'";
566: $db_autofill->query($sql);
567: } else {
568: $sql = "INSERT INTO " . $cfg["tab"]["container"] .
569: " (idtpl, number, idmod) " .
570: " VALUES ( " .
571: " '$idtpl', '$container', '$idmod')";
572: $db_autofill->query($sql);
573: }
574: }
575: }
576:
577: case "mandatory":
578:
579: if ($containerinf[$idlay][$container]["default"] != "") {
580: $sql = "SELECT idmod FROM " . $cfg["tab"]["mod"] .
581: " WHERE name = '" .
582: $db->escape($containerinf[$idlay][$container]["default"]) . "'";
583:
584: $db_autofill->query($sql);
585:
586: if ($db_autofill->nextRecord()) {
587: $idmod = $db_autofill->f("idmod");
588:
589: $sql = "SELECT idcontainer, idmod FROM " . $cfg["tab"]["container"]
590: . " WHERE idtpl = '" . cSecurity::toInteger($idtpl) . "' AND number = '" . cSecurity::toInteger($container) . "'";
591:
592: $db_autofill->query($sql);
593:
594: if ($db_autofill->nextRecord()) {
595:
596: } else {
597: $sql = "INSERT INTO " . $cfg["tab"]["container"] .
598: " (idtpl, number, idmod) " .
599: " VALUES ( " .
600: " '" . cSecurity::toInteger($idtpl) . "', '" . cSecurity::toInteger($container) . "', '" . cSecurity::toInteger($idmod) . "')";
601: $db_autofill->query($sql);
602: }
603: }
604: }
605: }
606: }
607: }
608: