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: 195: 196:
197: public function generate(
198: $idcat, $idart, $lang, $client, $layout = false, $save = true,
199: $contype = true, $editable = true, $version = NULL
200: ) {
201:
202: $this->_idcat = (int) $idcat;
203: $this->_idart = (int) $idart;
204: $this->_lang = (int) $lang;
205: $this->_client = (int) $client;
206: $this->_layout = (bool) $layout;
207: $this->_save = (bool) $save;
208:
209: $this->_oArtLang = new cApiArticleLanguage();
210: $this->_oArtLang->loadByArticleAndLanguageId($this->_idart, $this->_lang);
211: if (!$this->_oArtLang->isLoaded()) {
212: throw new cInvalidArgumentException('Couldn\'t load article language for idart=' . $this->_idart . 'AND idlang=' . $this->_lang);
213: }
214:
215: $this->_idartlang = $this->_oArtLang->get('idartlang');
216: $this->_pageTitle = stripslashes($this->_oArtLang->get('pagetitle'));
217:
218: return $this->_generate($contype, $editable, $version);
219: }
220:
221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231:
232: abstract function _generate($contype = true, $editable = true, $version = NULL);
233:
234: 235: 236: 237: 238: 239: 240: 241:
242: protected function _getTemplateConfigurationId() {
243:
244: $idtplcfg = conGetTemplateConfigurationIdForArticle($this->_idart, $this->_idcat, $this->_lang, $this->_client);
245: if (is_numeric($idtplcfg) && $idtplcfg != 0) {
246:
247: cDebug::out("configuration for article found: $idtplcfg<br><br>");
248: } else {
249:
250: $idtplcfg = conGetTemplateConfigurationIdForCategory($this->_idcat, $this->_lang, $this->_client);
251: if (NULL !== $idtplcfg) {
252:
253: cDebug::out("configuration for category found: $idtplcfg<br><br>");
254: }
255: }
256:
257: return (is_numeric($idtplcfg)) ? $idtplcfg : NULL;
258: }
259:
260: 261: 262: 263: 264: 265: 266: 267:
268: abstract protected function _processNoConfigurationError($idcatart);
269:
270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285:
286: protected function _getTemplateData() {
287: global $cfg;
288:
289:
290: $sql = "SELECT
291: a.idlay AS idlay
292: , a.idtpl AS idtpl
293: , a.name AS name
294: FROM
295: `%s` AS a
296: , `%s` AS b
297: WHERE
298: b.idtplcfg = %d
299: AND b.idtpl = a.idtpl
300: ;";
301:
302: $sql = $this->_db->prepare($sql, $cfg['tab']['tpl'], $cfg['tab']['tpl_conf'], $this->_idtplcfg);
303: $this->_db->query($sql);
304: $this->_db->nextRecord();
305: $data = $this->_db->toArray();
306:
307: if ($this->_layout !== false) {
308: $data['idlay'] = $this->_layout;
309: }
310:
311: cDebug::out("Using Layout: $data[idlay] and Template: $data[idtpl] for generation of code.<br><br>");
312:
313: return $data;
314: }
315:
316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327:
328: protected function _processCmsTags($contentList, $saveKeywords = true, $editable = true) {
329:
330: global $db, $db2, $sess, $cfg, $code, $cfgClient, $encoding;
331:
332:
333:
334: global $edit, $editLink, $belang;
335:
336: $idcat = $this->_idcat;
337: $idart = $this->_idart;
338: $lang = $this->_lang;
339: $client = $this->_client;
340: $idartlang = $this->_idartlang;
341:
342: if (!is_object($db2)) {
343: $db2 = cRegistry::getDb();
344: }
345:
346:
347: $match = array();
348: $keycode = array();
349:
350:
351:
352: $a_content = $contentList;
353:
354:
355: $_typeList = array();
356: $oTypeColl = new cApiTypeCollection();
357: $oTypeColl->select();
358: while (false !== ($oType = $oTypeColl->next())) {
359: $_typeList[] = $oType->toObject();
360: }
361:
362:
363: foreach ($_typeList as $_typeItem) {
364: $key = cString::toLowerCase($_typeItem->type);
365: $type = $_typeItem->type;
366:
367:
368:
369: $tmp = preg_match_all('/(' . $type . '\[+(\d)+\])/i', $this->_layoutCode, $match);
370:
371: $a_[$key] = $match[0];
372:
373: $success = array_walk($a_[$key], 'cString::extractNumber');
374:
375: $search = array();
376: $replacements = array();
377:
378: $typeClassName = $this->_getContentTypeClassName($type);
379: $typeCodeFile = $this->_getContentTypeCodeFilePathName($type);
380:
381: foreach ($a_[$key] as $val) {
382: if (class_exists($typeClassName)) {
383:
384: $tmp = !empty($a_content[$_typeItem->type][$val]) ? $a_content[$_typeItem->type][$val] : '';
385:
386: $cTypeObject = new $typeClassName($tmp, $val, $a_content);
387: global $edit;
388:
389: if (cRegistry::isBackendEditMode()) {
390:
391: $tmp = $cTypeObject->generateEditCode();
392:
393:
394:
395: } else {
396: $tmp = $cTypeObject->generateViewCode();
397: }
398: } else if (cFileHandler::exists($typeCodeFile)) {
399:
400: include($typeCodeFile);
401: }
402:
403: $search[$val] = sprintf('%s[%s]', $type, $val);
404: $replacements[$val] = $tmp;
405: $keycode[$type][$val] = $tmp;
406: }
407: $this->_layoutCode = str_ireplace($search, $replacements, $this->_layoutCode);
408: }
409: }
410:
411: 412: 413:
414: abstract protected function _processCodeTitleTag();
415:
416: 417: 418:
419: abstract protected function _processCodeMetaTags();
420:
421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432:
433: protected function _processCmsValueTags($containerNumber, $containerCfg) {
434: $containerCfgList = array();
435:
436: $containerCfg = preg_replace('/(&\$)/', '', $containerCfg);
437: parse_str($containerCfg, $containerCfgList);
438:
439:
440:
441:
442:
443:
444:
445:
446:
447: $CiCMS_Var = '$C' . $containerNumber . 'CMS_VALUE';
448: $CiCMS_Values = array();
449:
450: foreach ($containerCfgList as $key3 => $value3) {
451:
452: $tmp = conHtmlSpecialChars($value3);
453: $tmp = str_replace('\\', '\\\\', $tmp);
454: $CiCMS_Values[] = $CiCMS_Var . '[' . $key3 . '] = "' . $tmp . '"; ';
455: $this->_moduleCode = str_replace("\$CMS_VALUE[$key3]", $tmp, $this->_moduleCode);
456: $this->_moduleCode = str_replace("CMS_VALUE[$key3]", $tmp, $this->_moduleCode);
457: }
458:
459: $this->_moduleCode = str_replace("CMS_VALUE", $CiCMS_Var, $this->_moduleCode);
460: $this->_moduleCode = str_replace("\$" . $CiCMS_Var, $CiCMS_Var, $this->_moduleCode);
461: $this->_moduleCode = preg_replace("/(CMS_VALUE\[)([0-9]*)(\])/i", '', $this->_moduleCode);
462:
463: return implode("\n", $CiCMS_Values);
464: }
465:
466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477:
478: protected function _processFrontendDebug($containerNumber, array $module) {
479: global $containerinf;
480:
481: $data = $this->_getTemplateData();
482:
483: if (empty($this->_feDebugOptions)) {
484: return;
485: }
486:
487: $sFeDebug = '';
488: if ($this->_feDebugOptions['container_display'] == true) {
489: $this->_modulePrefix[] = 'if ($frontend_debug[\'container_display\']) echo "<!-- START CONTAINER ' . $containerinf[$data['idlay']][$containerNumber]['name'] . ' (' . $containerNumber . ') -->";';
490: }
491:
492: if ($this->_feDebugOptions['module_display'] == true) {
493: $this->_modulePrefix[] = 'if ($frontend_debug[\'module_display\']) echo "<!-- START MODULE ' . $module['name'] . ' (' . $module['idmod'] . ') -->";';
494: }
495:
496: if ($this->_feDebugOptions['module_timing'] == true) {
497: $this->_modulePrefix[] = '$modTime' . $containerNumber . ' = -getmicrotime(true);';
498: $this->_moduleSuffix[] = '$modTime' . $containerNumber . ' += getmicrotime(true);';
499: }
500:
501: if ($this->_feDebugOptions['module_display'] == true) {
502: $this->_moduleSuffix[] = 'if ($frontend_debug[\'module_display\']) echo "<!-- END MODULE ' . $module['name'] . ' (' . $module['idmod'] . ')";';
503: if ($this->_feDebugOptions['module_timing'] == true) {
504: $this->_moduleSuffix[] = 'if ($frontend_debug[\'module_timing\']) echo(" AFTER " . $modTime' . $containerNumber . ');';
505: }
506: $this->_moduleSuffix[] = 'if ($frontend_debug[\'module_display\']) echo " -->";';
507: }
508: if ($this->_feDebugOptions['container_display'] == true) {
509: $this->_moduleSuffix[] = 'if ($frontend_debug[\'container_display\']) echo "<!-- END CONTAINER ' . $containerinf[$data['idlay']][$containerNumber]['name'] . ' (' . $containerNumber . ') -->";';
510: }
511: }
512:
513: 514: 515: 516: 517: 518: 519:
520: protected function _processCmsContainer($containerNumber) {
521: $cmsContainer = "CMS_CONTAINER[$containerNumber]";
522:
523:
524:
525: $this->_layoutCode = preg_replace("/<container( +)id=\\\"$containerNumber\\\"(.*)>(.*)<\/container>/Uis", $cmsContainer, $this->_layoutCode);
526: $this->_layoutCode = preg_replace("/<container( +)id=\\\"$containerNumber\\\"(.*)\/>/i", $cmsContainer, $this->_layoutCode);
527:
528:
529:
530: $modulePrefix = trim(implode("\n", $this->_modulePrefix));
531: if (!empty($modulePrefix)) {
532: $modulePrefix = "<?php\n" . $modulePrefix . "\n?>";
533: }
534: $moduleSuffix = trim(implode("\n", $this->_moduleSuffix));
535: if (!empty($moduleSuffix)) {
536: $moduleSuffix = "<?php\n" . $moduleSuffix . "\n?>";
537: }
538: $moduleOutput = $modulePrefix . $this->_moduleCode . $moduleSuffix;
539:
540:
541: $this->_layoutCode = str_ireplace($cmsContainer, $moduleOutput, $this->_layoutCode);
542:
543: }
544:
545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555:
556: protected function _getUsedCmsTypesData($editable = true, $version = NULL) {
557: global $cfg;
558:
559: $return = array();
560:
561:
562: if ($version == NULL) {
563: $sql = "SELECT * FROM `%s` AS A, `%s` AS B, `%s` AS C
564: WHERE A.idtype = C.idtype AND A.idartlang = B.idartlang AND B.idart = %d AND B.idlang = %d";
565: $sql = $this->_db->prepare(
566: $sql,
567: $cfg['tab']['content'],
568: $cfg['tab']['art_lang'],
569: $cfg['tab']['type'],
570: $this->_idart,
571: $this->_lang
572: );
573: } else if (is_numeric($version)) {
574: $sql = 'SELECT b.type as type, a.typeid as typeid, a.value as value
575: FROM `%s` AS a
576: INNER JOIN `%s` as b
577: ON b.idtype = a.idtype
578: WHERE (a.idtype, a.typeid, a.version) IN
579: (SELECT idtype, typeid, max(version)
580: FROM %s
581: WHERE idartlang = %d AND version <= %d
582: GROUP BY idtype, typeid)
583: AND a.idartlang = %d
584: AND (a.deleted < 1 OR a.deleted IS NULL)
585: ORDER BY a.idtype, a.typeid;';
586: $sql = $this->_db->prepare(
587: $sql,
588: $cfg['tab']['content_version'],
589: $cfg['tab']['type'],
590: $cfg['tab']['content_version'],
591: $this->_idartlang,
592: $version,
593: $this->_idartlang
594: );
595: }
596:
597: $this->_db->query($sql);
598: while ($this->_db->nextRecord()) {
599: $return[$this->_db->f('type')][$this->_db->f('typeid')] = $this->_db->f('value');
600: }
601:
602: return $return;
603: }
604:
605: 606: 607:
608: protected function _resetModule() {
609: $this->_modulePrefix = array();
610: $this->_moduleCode = '';
611: $this->_moduleSuffix = array();
612: }
613:
614: 615: 616: 617: 618: 619: 620: 621:
622: protected function _getContentTypeClassName($type) {
623: $typeClassName = 'cContentType' . ucfirst(cString::toLowerCase(str_replace('CMS_', '', $type)));
624:
625: return $typeClassName;
626: }
627:
628: 629: 630: 631: 632: 633: 634: 635: 636: 637:
638: protected function _getContentTypeCodeFilePathName($type) {
639: global $cfg;
640: $typeCodeFile = cRegistry::getBackendPath() . $cfg['path']['includes'] . 'type/code/include.' . $type . '.code.php';
641:
642: return $typeCodeFile;
643: }
644:
645: 646: 647: 648: 649: 650: 651:
652: protected function getArtLangObject() {
653: return $this->_oArtLang;
654: }
655: }
656: