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($containerId, $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' . $containerId . 'CMS_VALUE';
408:         $CiCMS_Values = array();
409: 
410:         foreach ($containerCfgList as $key3 => $value3) {
411:             $tmp = $value3;
412:             $tmp = str_replace("\'", "'", $tmp);
413:             $CiCMS_Values[] = $CiCMS_Var . '[' . $key3 . '] = "' . $tmp . '"; ';
414:             $this->_moduleCode = str_replace("\$CMS_VALUE[$key3]", $tmp, $this->_moduleCode);
415:             $this->_moduleCode = str_replace("CMS_VALUE[$key3]", $tmp, $this->_moduleCode);
416:         }
417: 
418:         $this->_moduleCode = str_replace("CMS_VALUE", $CiCMS_Var, $this->_moduleCode);
419:         $this->_moduleCode = str_replace("\$" . $CiCMS_Var, $CiCMS_Var, $this->_moduleCode);
420:         $this->_moduleCode = preg_replace("/(CMS_VALUE\[)([0-9]*)(\])/i", '', $this->_moduleCode);
421: 
422:         return implode("\n", $CiCMS_Values);
423:     }
424: 
425:     426: 427: 428: 429: 430: 431: 432: 
433:     protected function _processFrontendDebug($containerId, array $module) {
434:         global $containerinf;
435: 
436:         $data = $this->_getTemplateData();
437: 
438:         if (empty($this->_feDebugOptions)) {
439:             return;
440:         }
441: 
442:         $sFeDebug = '';
443:         if ($this->_feDebugOptions['container_display'] == true) {
444:             $this->_modulePrefix[] = 'if ($frontend_debug[\'container_display\']) echo "<!-- START CONTAINER ' . $containerinf[$data['idlay']][$containerId]['name'] . ' (' . $containerId . ') -->";';
445:         }
446: 
447:         if ($this->_feDebugOptions['module_display'] == true) {
448:             $this->_modulePrefix[] = 'if ($frontend_debug[\'module_display\']) echo "<!-- START MODULE ' . $module['name'] . ' (' . $module['idmod'] . ') -->";';
449:         }
450: 
451:         if ($this->_feDebugOptions['module_timing'] == true) {
452:             $this->_modulePrefix[] = '$modTime' . $containerId . ' = -getmicrotime(true);';
453:             $this->_moduleSuffix[] = '$modTime' . $containerId . ' += getmicrotime(true);';
454:         }
455: 
456:         if ($this->_feDebugOptions['module_display'] == true) {
457:             $this->_moduleSuffix[] = 'if ($frontend_debug[\'module_display\']) echo "<!-- END MODULE ' . $module['name'] . ' (' . $module['idmod'] . ')";';
458:             if ($this->_feDebugOptions['module_timing'] == true) {
459:                 $this->_moduleSuffix[] = 'if ($frontend_debug[\'module_timing\']) echo(" AFTER " . $modTime' . $containerId . ');';
460:             }
461:             $this->_moduleSuffix[] = 'if ($frontend_debug[\'module_display\']) echo " -->";';
462:         }
463:         if ($this->_feDebugOptions['container_display'] == true) {
464:             $this->_moduleSuffix[] = 'if ($frontend_debug[\'container_display\']) echo "<!-- END CONTAINER ' . $containerinf[$data['idlay']][$containerId]['name'] . ' (' . $containerId . ') -->";';
465:         }
466:     }
467: 
468:     469: 470: 471: 472: 473: 
474:     protected function _processCmsContainer($containerId) {
475:         $cmsContainer = "CMS_CONTAINER[$containerId]";
476: 
477:         
478:         
479:         $this->_layoutCode = preg_replace("/<container( +)id=\\\"$containerId\\\"(.*)>(.*)<\/container>/Uis", $cmsContainer, $this->_layoutCode);
480:         $this->_layoutCode = preg_replace("/<container( +)id=\\\"$containerId\\\"(.*)\/>/i", $cmsContainer, $this->_layoutCode);
481: 
482:         
483:         
484:         
485:         $modulePrefix = trim(implode("\n", $this->_modulePrefix));
486:         if (!empty($modulePrefix)) {
487:             $modulePrefix = "<?php\n" . $modulePrefix . "\n?>";
488:         }
489:         $moduleSuffix = trim(implode("\n", $this->_moduleSuffix));
490:         if (!empty($moduleSuffix)) {
491:             $moduleSuffix = "<?php\n" . $moduleSuffix . "\n?>";
492:         }
493:         $moduleOutput = $modulePrefix . $this->_moduleCode . $moduleSuffix;
494: 
495:         
496:         $this->_layoutCode = str_ireplace($cmsContainer, $moduleOutput, $this->_layoutCode);
497:         
498:     }
499: 
500:     501: 502: 503: 504: 505: 
506:     protected function _getUsedCmsTypesData() {
507:         global $cfg;
508: 
509:         $return = array();
510: 
511:         
512:         $sql = "SELECT * FROM `%s` AS A, `%s` AS B, `%s` AS C
513:                 WHERE A.idtype = C.idtype AND A.idartlang = B.idartlang AND B.idart = %d AND B.idlang = %d";
514:         $sql = $this->_db->prepare($sql, $cfg['tab']['content'], $cfg['tab']['art_lang'], $cfg['tab']['type'], $this->_idart, $this->_lang);
515:         $this->_db->query($sql);
516:         while ($this->_db->nextRecord()) {
517:             $return[$this->_db->f('type')][$this->_db->f('typeid')] = $this->_db->f('value');
518:         }
519: 
520:         return $return;
521:     }
522: 
523:     524: 525: 
526:     protected function _resetModule() {
527:         $this->_modulePrefix = array();
528:         $this->_moduleCode = '';
529:         $this->_moduleSuffix = array();
530:     }
531: 
532:     533: 534: 535: 536: 537: 538: 539: 
540:     protected function _getContentTypeClassName($type) {
541:         $typeClassName = 'cContentType' . ucfirst(strtolower(str_replace('CMS_', '', $type)));
542: 
543:         return $typeClassName;
544:     }
545: 
546:     547: 548: 549: 550: 551: 552: 553: 554: 
555:     protected function _getContentTypeCodeFilePathName($type) {
556:         global $cfg;
557:         $typeCodeFile = cRegistry::getBackendPath() . $cfg['path']['includes'] . 'type/code/include.' . $type . '.code.php';
558: 
559:         return $typeCodeFile;
560:     }
561: 
562:     563: 564: 565: 
566:     protected function getArtLangObject() {
567:         return $this->_oArtLang;
568:     }
569: }
570: