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: global $db, $db2, $sess, $cfg, $code, $cfgClient, $encoding;
319:
320:
321:
322: global $edit, $editLink, $belang;
323:
324: $idcat = $this->_idcat;
325: $idart = $this->_idart;
326: $lang = $this->_lang;
327: $client = $this->_client;
328: $idartlang = $this->_idartlang;
329:
330: if (!is_object($db2)) {
331: $db2 = cRegistry::getDb();
332: }
333:
334:
335: $match = array();
336: $keycode = array();
337:
338:
339:
340: $a_content = $contentList;
341:
342:
343: $_typeList = array();
344: $oTypeColl = new cApiTypeCollection();
345: $oTypeColl->select();
346: while (false !== ($oType = $oTypeColl->next())) {
347: $_typeList[] = $oType->toObject();
348: }
349:
350:
351: foreach ($_typeList as $_typeItem) {
352: $key = strtolower($_typeItem->type);
353: $type = $_typeItem->type;
354:
355:
356:
357: $tmp = preg_match_all('/(' . $type . '\[+(\d)+\])/i', $this->_layoutCode, $match);
358:
359: $a_[$key] = $match[0];
360:
361: $success = array_walk($a_[$key], 'cString::extractNumber');
362:
363: $search = array();
364: $replacements = array();
365:
366: $typeClassName = $this->_getContentTypeClassName($type);
367: $typeCodeFile = $this->_getContentTypeCodeFilePathName($type);
368:
369: foreach ($a_[$key] as $val) {
370: if (class_exists($typeClassName)) {
371:
372: $tmp = $a_content[$_typeItem->type][$val];
373: $cTypeObject = new $typeClassName($tmp, $val, $a_content);
374: global $edit;
375:
376: if (cRegistry::isBackendEditMode()) {
377:
378: $tmp = $cTypeObject->generateEditCode();
379:
380:
381:
382: } else {
383: $tmp = $cTypeObject->generateViewCode();
384: }
385: } else if (cFileHandler::exists($typeCodeFile)) {
386:
387: include($typeCodeFile);
388: }
389:
390: $search[$val] = sprintf('%s[%s]', $type, $val);
391: $replacements[$val] = $tmp;
392: $keycode[$type][$val] = $tmp;
393: }
394: $this->_layoutCode = str_ireplace($search, $replacements, $this->_layoutCode);
395: }
396: }
397:
398: 399: 400:
401: abstract protected function _processCodeTitleTag();
402:
403: 404: 405:
406: abstract protected function _processCodeMetaTags();
407:
408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419:
420: protected function _processCmsValueTags($containerNumber, $containerCfg) {
421: $containerCfgList = array();
422:
423: $containerCfg = preg_replace('/(&\$)/', '', $containerCfg);
424: parse_str($containerCfg, $containerCfgList);
425:
426:
427:
428:
429:
430:
431:
432:
433:
434: $CiCMS_Var = '$C' . $containerNumber . 'CMS_VALUE';
435: $CiCMS_Values = array();
436:
437: foreach ($containerCfgList as $key3 => $value3) {
438:
439: $tmp = conHtmlSpecialChars($value3);
440: $tmp = str_replace('\\', '\\\\', $tmp);
441: $CiCMS_Values[] = $CiCMS_Var . '[' . $key3 . '] = "' . $tmp . '"; ';
442: $this->_moduleCode = str_replace("\$CMS_VALUE[$key3]", $tmp, $this->_moduleCode);
443: $this->_moduleCode = str_replace("CMS_VALUE[$key3]", $tmp, $this->_moduleCode);
444: }
445:
446: $this->_moduleCode = str_replace("CMS_VALUE", $CiCMS_Var, $this->_moduleCode);
447: $this->_moduleCode = str_replace("\$" . $CiCMS_Var, $CiCMS_Var, $this->_moduleCode);
448: $this->_moduleCode = preg_replace("/(CMS_VALUE\[)([0-9]*)(\])/i", '', $this->_moduleCode);
449:
450: return implode("\n", $CiCMS_Values);
451: }
452:
453: 454: 455: 456: 457: 458: 459: 460: 461:
462: protected function _processFrontendDebug($containerNumber, array $module) {
463: global $containerinf;
464:
465: $data = $this->_getTemplateData();
466:
467: if (empty($this->_feDebugOptions)) {
468: return;
469: }
470:
471: $sFeDebug = '';
472: if ($this->_feDebugOptions['container_display'] == true) {
473: $this->_modulePrefix[] = 'if ($frontend_debug[\'container_display\']) echo "<!-- START CONTAINER ' . $containerinf[$data['idlay']][$containerNumber]['name'] . ' (' . $containerNumber . ') -->";';
474: }
475:
476: if ($this->_feDebugOptions['module_display'] == true) {
477: $this->_modulePrefix[] = 'if ($frontend_debug[\'module_display\']) echo "<!-- START MODULE ' . $module['name'] . ' (' . $module['idmod'] . ') -->";';
478: }
479:
480: if ($this->_feDebugOptions['module_timing'] == true) {
481: $this->_modulePrefix[] = '$modTime' . $containerNumber . ' = -getmicrotime(true);';
482: $this->_moduleSuffix[] = '$modTime' . $containerNumber . ' += getmicrotime(true);';
483: }
484:
485: if ($this->_feDebugOptions['module_display'] == true) {
486: $this->_moduleSuffix[] = 'if ($frontend_debug[\'module_display\']) echo "<!-- END MODULE ' . $module['name'] . ' (' . $module['idmod'] . ')";';
487: if ($this->_feDebugOptions['module_timing'] == true) {
488: $this->_moduleSuffix[] = 'if ($frontend_debug[\'module_timing\']) echo(" AFTER " . $modTime' . $containerNumber . ');';
489: }
490: $this->_moduleSuffix[] = 'if ($frontend_debug[\'module_display\']) echo " -->";';
491: }
492: if ($this->_feDebugOptions['container_display'] == true) {
493: $this->_moduleSuffix[] = 'if ($frontend_debug[\'container_display\']) echo "<!-- END CONTAINER ' . $containerinf[$data['idlay']][$containerNumber]['name'] . ' (' . $containerNumber . ') -->";';
494: }
495: }
496:
497: 498: 499: 500: 501: 502: 503:
504: protected function _processCmsContainer($containerNumber) {
505: $cmsContainer = "CMS_CONTAINER[$containerNumber]";
506:
507:
508:
509: $this->_layoutCode = preg_replace("/<container( +)id=\\\"$containerNumber\\\"(.*)>(.*)<\/container>/Uis", $cmsContainer, $this->_layoutCode);
510: $this->_layoutCode = preg_replace("/<container( +)id=\\\"$containerNumber\\\"(.*)\/>/i", $cmsContainer, $this->_layoutCode);
511:
512:
513:
514: $modulePrefix = trim(implode("\n", $this->_modulePrefix));
515: if (!empty($modulePrefix)) {
516: $modulePrefix = "<?php\n" . $modulePrefix . "\n?>";
517: }
518: $moduleSuffix = trim(implode("\n", $this->_moduleSuffix));
519: if (!empty($moduleSuffix)) {
520: $moduleSuffix = "<?php\n" . $moduleSuffix . "\n?>";
521: }
522: $moduleOutput = $modulePrefix . $this->_moduleCode . $moduleSuffix;
523:
524:
525: $this->_layoutCode = str_ireplace($cmsContainer, $moduleOutput, $this->_layoutCode);
526:
527: }
528:
529: 530: 531: 532: 533: 534: 535: 536:
537: protected function _getUsedCmsTypesData($editable = true, $version = NULL) {
538: global $cfg;
539:
540: $return = array();
541:
542:
543: if ($version == NULL) {
544: $sql = "SELECT * FROM `%s` AS A, `%s` AS B, `%s` AS C
545: WHERE A.idtype = C.idtype AND A.idartlang = B.idartlang AND B.idart = %d AND B.idlang = %d";
546: $sql = $this->_db->prepare(
547: $sql,
548: $cfg['tab']['content'],
549: $cfg['tab']['art_lang'],
550: $cfg['tab']['type'],
551: $this->_idart,
552: $this->_lang
553: );
554: } else if (is_numeric($version)) {
555: $sql = 'SELECT b.type as type, a.typeid as typeid, a.value as value
556: FROM `%s` AS a
557: INNER JOIN `%s` as b
558: ON b.idtype = a.idtype
559: WHERE (a.idtype, a.typeid, a.version) IN
560: (SELECT idtype, typeid, max(version)
561: FROM %s
562: WHERE idartlang = %d AND version <= %d
563: GROUP BY idtype, typeid)
564: AND a.idartlang = %d
565: AND (a.deleted < 1 OR a.deleted IS NULL)
566: ORDER BY a.idtype, a.typeid;';
567: $sql = $this->_db->prepare(
568: $sql,
569: $cfg['tab']['content_version'],
570: $cfg['tab']['type'],
571: $cfg['tab']['content_version'],
572: $this->_idartlang,
573: $version,
574: $this->_idartlang
575: );
576: }
577:
578: $this->_db->query($sql);
579: while ($this->_db->nextRecord()) {
580: $return[$this->_db->f('type')][$this->_db->f('typeid')] = $this->_db->f('value');
581: }
582:
583: return $return;
584: }
585:
586: 587: 588:
589: protected function _resetModule() {
590: $this->_modulePrefix = array();
591: $this->_moduleCode = '';
592: $this->_moduleSuffix = array();
593: }
594:
595: 596: 597: 598: 599: 600: 601: 602:
603: protected function _getContentTypeClassName($type) {
604: $typeClassName = 'cContentType' . ucfirst(strtolower(str_replace('CMS_', '', $type)));
605:
606: return $typeClassName;
607: }
608:
609: 610: 611: 612: 613: 614: 615: 616: 617: 618:
619: protected function _getContentTypeCodeFilePathName($type) {
620: global $cfg;
621: $typeCodeFile = cRegistry::getBackendPath() . $cfg['path']['includes'] . 'type/code/include.' . $type . '.code.php';
622:
623: return $typeCodeFile;
624: }
625:
626: 627: 628: 629: 630: 631: 632:
633: protected function getArtLangObject() {
634: return $this->_oArtLang;
635: }
636: }
637: