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