1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22:
23: abstract class cCodeGeneratorAbstract {
24:
25: 26: 27: 28: 29:
30: protected $_db;
31:
32: 33: 34: 35: 36: 37:
38: protected $_feDebugOptions = array();
39:
40: 41: 42: 43: 44:
45: protected $_cssData = '';
46:
47: 48: 49: 50: 51:
52: protected $_jsData = '';
53:
54: 55: 56: 57: 58:
59: protected $_tplName = '';
60:
61: 62: 63: 64: 65:
66: protected $_idcat;
67:
68: 69: 70: 71: 72:
73: protected $_idart;
74:
75: 76: 77: 78: 79:
80: protected $_lang;
81:
82: 83: 84: 85: 86:
87: protected $_client;
88:
89: 90: 91: 92: 93:
94: protected $_layout;
95:
96: 97: 98: 99: 100:
101: protected $_save;
102:
103: 104: 105: 106: 107:
108: protected $_idartlang;
109:
110: 111: 112: 113: 114: 115:
116: protected $_pageTitle;
117:
118: 119: 120: 121: 122: 123:
124: protected $_layoutCode = '';
125:
126: 127: 128: 129: 130:
131: protected $_modulePrefix = array();
132:
133: 134: 135: 136: 137:
138: protected $_moduleCode = '';
139:
140: 141: 142: 143: 144:
145: protected $_moduleSuffix = array();
146:
147: 148: 149: 150: 151:
152: protected $_oArtLang;
153:
154: 155: 156:
157: public function __construct() {
158: $this->_db = cRegistry::getDb();
159: }
160:
161: 162: 163: 164: 165: 166: 167:
168: public function setFrontendDebugOptions(array $debugOptions) {
169: $this->_feDebugOptions = $debugOptions;
170: }
171:
172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193:
194: public function generate(
195: $idcat, $idart, $lang, $client, $layout = false, $save = true,
196: $contype = true, $editable = true, $version = NULL
197: ) {
198:
199: $this->_idcat = (int) $idcat;
200: $this->_idart = (int) $idart;
201: $this->_lang = (int) $lang;
202: $this->_client = (int) $client;
203: $this->_layout = (bool) $layout;
204: $this->_save = (bool) $save;
205:
206: $this->_oArtLang = new cApiArticleLanguage();
207: $this->_oArtLang->loadByArticleAndLanguageId($this->_idart, $this->_lang);
208: if (!$this->_oArtLang->isLoaded()) {
209: throw new cInvalidArgumentException('Couldn\'t load article language for idart=' . $this->_idart . 'AND idlang=' . $this->_lang);
210: }
211:
212: $this->_idartlang = $this->_oArtLang->get('idartlang');
213: $this->_pageTitle = stripslashes($this->_oArtLang->get('pagetitle'));
214:
215: return $this->_generate($contype, $editable, $version);
216: }
217:
218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228:
229: abstract function _generate($contype = true, $editable = true, $version = NULL);
230:
231: 232: 233: 234: 235: 236:
237: protected function _getTemplateConfigurationId() {
238:
239: $idtplcfg = conGetTemplateConfigurationIdForArticle($this->_idart, $this->_idcat, $this->_lang, $this->_client);
240: if (is_numeric($idtplcfg) && $idtplcfg != 0) {
241:
242: cDebug::out("configuration for article found: $idtplcfg<br><br>");
243: } else {
244:
245: $idtplcfg = conGetTemplateConfigurationIdForCategory($this->_idcat, $this->_lang, $this->_client);
246: if (NULL !== $idtplcfg) {
247:
248: cDebug::out("configuration for category found: $idtplcfg<br><br>");
249: }
250: }
251:
252: return (is_numeric($idtplcfg)) ? $idtplcfg : NULL;
253: }
254:
255: 256: 257: 258: 259: 260: 261: 262:
263: abstract protected function _processNoConfigurationError($idcatart);
264:
265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276:
277: protected function _getTemplateData() {
278: global $cfg;
279:
280:
281: $sql = "SELECT
282: a.idlay AS idlay
283: , a.idtpl AS idtpl
284: , a.name AS name
285: FROM
286: `%s` AS a
287: , `%s` AS b
288: WHERE
289: b.idtplcfg = %d
290: AND b.idtpl = a.idtpl
291: ;";
292:
293: $sql = $this->_db->prepare($sql, $cfg['tab']['tpl'], $cfg['tab']['tpl_conf'], $this->_idtplcfg);
294: $this->_db->query($sql);
295: $this->_db->nextRecord();
296: $data = $this->_db->toArray();
297:
298: if ($this->_layout !== false) {
299: $data['idlay'] = $this->_layout;
300: }
301:
302: cDebug::out("Using Layout: $data[idlay] and Template: $data[idtpl] for generation of code.<br><br>");
303:
304: return $data;
305: }
306:
307: 308: 309: 310: 311: 312: 313: 314: 315:
316: protected function _processCmsTags($contentList, $saveKeywords = true, $editable = true) {
317: 318: 319:
320: global $db, $db2, $sess, $cfg, $code, $cfgClient, $encoding;
321:
322:
323:
324: global $edit, $editLink, $belang;
325:
326: $idcat = $this->_idcat;
327: $idart = $this->_idart;
328: $lang = $this->_lang;
329: $client = $this->_client;
330: $idartlang = $this->_idartlang;
331:
332: if (!is_object($db2)) {
333: $db2 = cRegistry::getDb();
334: }
335: 336: 337:
338:
339: $match = array();
340: $keycode = array();
341:
342:
343:
344: $a_content = $contentList;
345:
346: $_typeList = array();
347: $oTypeColl = new cApiTypeCollection();
348: $oTypeColl->select();
349: while (false !== ($oType = $oTypeColl->next())) {
350: $_typeList[] = $oType->toObject();
351: }
352:
353:
354: foreach ($_typeList as $_typeItem) {
355: $key = strtolower($_typeItem->type);
356: $type = $_typeItem->type;
357:
358:
359:
360: $tmp = preg_match_all('/(' . $type . '\[+(\d)+\])/i', $this->_layoutCode, $match);
361:
362: $a_[$key] = $match[0];
363:
364: $success = array_walk($a_[$key], 'cString::extractNumber');
365:
366: $search = array();
367: $replacements = array();
368:
369: $typeClassName = $this->_getContentTypeClassName($type);
370: $typeCodeFile = $this->_getContentTypeCodeFilePathName($type);
371:
372: foreach ($a_[$key] as $val) {
373: if (class_exists($typeClassName)) {
374:
375: $tmp = $a_content[$_typeItem->type][$val];
376: $cTypeObject = new $typeClassName($tmp, $val, $a_content);
377: global $edit;
378:
379: if (cRegistry::isBackendEditMode()) {
380:
381: $tmp = $cTypeObject->generateEditCode();
382:
383:
384:
385: } else {
386: $tmp = $cTypeObject->generateViewCode();
387: }
388: } else if (cFileHandler::exists($typeCodeFile)) {
389:
390: include($typeCodeFile);
391: }
392:
393: $search[$val] = sprintf('%s[%s]', $type, $val);
394: $replacements[$val] = $tmp;
395: $keycode[$type][$val] = $tmp;
396: }
397: $this->_layoutCode = str_ireplace($search, $replacements, $this->_layoutCode);
398: }
399: }
400:
401: 402: 403:
404: abstract protected function _processCodeTitleTag();
405:
406: 407: 408:
409: abstract protected function _processCodeMetaTags();
410:
411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422:
423: protected function _processCmsValueTags($containerNumber, $containerCfg) {
424: $containerCfgList = array();
425:
426: $containerCfg = preg_replace('/(&\$)/', '', $containerCfg);
427: parse_str($containerCfg, $containerCfgList);
428:
429:
430:
431:
432:
433:
434:
435:
436:
437: $CiCMS_Var = '$C' . $containerNumber . 'CMS_VALUE';
438: $CiCMS_Values = array();
439:
440: foreach ($containerCfgList as $key3 => $value3) {
441:
442: $tmp = conHtmlSpecialChars($value3);
443: $tmp = str_replace('\\', '\\\\', $tmp);
444: $CiCMS_Values[] = $CiCMS_Var . '[' . $key3 . '] = "' . $tmp . '"; ';
445: $this->_moduleCode = str_replace("\$CMS_VALUE[$key3]", $tmp, $this->_moduleCode);
446: $this->_moduleCode = str_replace("CMS_VALUE[$key3]", $tmp, $this->_moduleCode);
447: }
448:
449: $this->_moduleCode = str_replace("CMS_VALUE", $CiCMS_Var, $this->_moduleCode);
450: $this->_moduleCode = str_replace("\$" . $CiCMS_Var, $CiCMS_Var, $this->_moduleCode);
451: $this->_moduleCode = preg_replace("/(CMS_VALUE\[)([0-9]*)(\])/i", '', $this->_moduleCode);
452:
453: return implode("\n", $CiCMS_Values);
454: }
455:
456: 457: 458: 459: 460: 461: 462: 463: 464:
465: protected function _processFrontendDebug($containerNumber, array $module) {
466: global $containerinf;
467:
468: $data = $this->_getTemplateData();
469:
470: if (empty($this->_feDebugOptions)) {
471: return;
472: }
473:
474: $sFeDebug = '';
475: if ($this->_feDebugOptions['container_display'] == true) {
476: $this->_modulePrefix[] = 'if ($frontend_debug[\'container_display\']) echo "<!-- START CONTAINER ' . $containerinf[$data['idlay']][$containerNumber]['name'] . ' (' . $containerNumber . ') -->";';
477: }
478:
479: if ($this->_feDebugOptions['module_display'] == true) {
480: $this->_modulePrefix[] = 'if ($frontend_debug[\'module_display\']) echo "<!-- START MODULE ' . $module['name'] . ' (' . $module['idmod'] . ') -->";';
481: }
482:
483: if ($this->_feDebugOptions['module_timing'] == true) {
484: $this->_modulePrefix[] = '$modTime' . $containerNumber . ' = -getmicrotime(true);';
485: $this->_moduleSuffix[] = '$modTime' . $containerNumber . ' += getmicrotime(true);';
486: }
487:
488: if ($this->_feDebugOptions['module_display'] == true) {
489: $this->_moduleSuffix[] = 'if ($frontend_debug[\'module_display\']) echo "<!-- END MODULE ' . $module['name'] . ' (' . $module['idmod'] . ')";';
490: if ($this->_feDebugOptions['module_timing'] == true) {
491: $this->_moduleSuffix[] = 'if ($frontend_debug[\'module_timing\']) echo(" AFTER " . $modTime' . $containerNumber . ');';
492: }
493: $this->_moduleSuffix[] = 'if ($frontend_debug[\'module_display\']) echo " -->";';
494: }
495: if ($this->_feDebugOptions['container_display'] == true) {
496: $this->_moduleSuffix[] = 'if ($frontend_debug[\'container_display\']) echo "<!-- END CONTAINER ' . $containerinf[$data['idlay']][$containerNumber]['name'] . ' (' . $containerNumber . ') -->";';
497: }
498: }
499:
500: 501: 502: 503: 504: 505: 506:
507: protected function _processCmsContainer($containerNumber) {
508: $cmsContainer = "CMS_CONTAINER[$containerNumber]";
509:
510:
511:
512: $this->_layoutCode = preg_replace("/<container( +)id=\\\"$containerNumber\\\"(.*)>(.*)<\/container>/Uis", $cmsContainer, $this->_layoutCode);
513: $this->_layoutCode = preg_replace("/<container( +)id=\\\"$containerNumber\\\"(.*)\/>/i", $cmsContainer, $this->_layoutCode);
514:
515:
516:
517: $modulePrefix = trim(implode("\n", $this->_modulePrefix));
518: if (!empty($modulePrefix)) {
519: $modulePrefix = "<?php\n" . $modulePrefix . "\n?>";
520: }
521: $moduleSuffix = trim(implode("\n", $this->_moduleSuffix));
522: if (!empty($moduleSuffix)) {
523: $moduleSuffix = "<?php\n" . $moduleSuffix . "\n?>";
524: }
525: $moduleOutput = $modulePrefix . $this->_moduleCode . $moduleSuffix;
526:
527:
528: $this->_layoutCode = str_ireplace($cmsContainer, $moduleOutput, $this->_layoutCode);
529:
530: }
531:
532: 533: 534: 535: 536: 537: 538: 539:
540: protected function _getUsedCmsTypesData($editable = true, $version = NULL) {
541: global $cfg;
542:
543: $return = array();
544:
545:
546: if ($version == NULL) {
547: $sql = "SELECT * FROM `%s` AS A, `%s` AS B, `%s` AS C
548: WHERE A.idtype = C.idtype AND A.idartlang = B.idartlang AND B.idart = %d AND B.idlang = %d";
549: $sql = $this->_db->prepare(
550: $sql,
551: $cfg['tab']['content'],
552: $cfg['tab']['art_lang'],
553: $cfg['tab']['type'],
554: $this->_idart,
555: $this->_lang
556: );
557: } else if (is_numeric($version)) {
558: $sql = 'SELECT b.type as type, a.typeid as typeid, a.value as value
559: FROM `%s` AS a
560: INNER JOIN `%s` as b
561: ON b.idtype = a.idtype
562: WHERE (a.idtype, a.typeid, a.version) IN
563: (SELECT idtype, typeid, max(version)
564: FROM %s
565: WHERE idartlang = %d AND version <= %d
566: GROUP BY idtype, typeid)
567: AND a.idartlang = %d
568: AND (a.deleted < 1 OR a.deleted IS NULL)
569: ORDER BY a.idtype, a.typeid;';
570: $sql = $this->_db->prepare(
571: $sql,
572: $cfg['tab']['content_version'],
573: $cfg['tab']['type'],
574: $cfg['tab']['content_version'],
575: $this->_idartlang,
576: $version,
577: $this->_idartlang
578: );
579: }
580:
581: $this->_db->query($sql);
582: while ($this->_db->nextRecord()) {
583: $return[$this->_db->f('type')][$this->_db->f('typeid')] = $this->_db->f('value');
584: }
585:
586: return $return;
587: }
588:
589: 590: 591:
592: protected function _resetModule() {
593: $this->_modulePrefix = array();
594: $this->_moduleCode = '';
595: $this->_moduleSuffix = array();
596: }
597:
598: 599: 600: 601: 602: 603: 604: 605:
606: protected function _getContentTypeClassName($type) {
607: $typeClassName = 'cContentType' . ucfirst(strtolower(str_replace('CMS_', '', $type)));
608:
609: return $typeClassName;
610: }
611:
612: 613: 614: 615: 616: 617: 618: 619: 620: 621:
622: protected function _getContentTypeCodeFilePathName($type) {
623: global $cfg;
624: $typeCodeFile = cRegistry::getBackendPath() . $cfg['path']['includes'] . 'type/code/include.' . $type . '.code.php';
625:
626: return $typeCodeFile;
627: }
628:
629: 630: 631: 632: 633: 634: 635:
636: protected function getArtLangObject() {
637: return $this->_oArtLang;
638: }
639: }
640: