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:
25: abstract class cCodeGeneratorAbstract {
26:
27: 28: 29: 30: 31:
32: protected $_db;
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: public function __construct() {
157: $this->_db = cRegistry::getDb();
158: }
159:
160: 161: 162: 163: 164: 165:
166: public function setFrontendDebugOptions(array $debugOptions) {
167: $this->_feDebugOptions = $debugOptions;
168: }
169:
170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184:
185: public function generate($idcat, $idart, $lang, $client, $layout = false, $save = true, $contype = true) {
186: $this->_idcat = (int)$idcat;
187: $this->_idart = (int)$idart;
188: $this->_lang = (int)$lang;
189: $this->_client = (int)$client;
190: $this->_layout = (bool)$layout;
191: $this->_save = (bool)$save;
192:
193: $this->oArtLang = new cApiArticleLanguage();
194: $this->oArtLang->loadByArticleAndLanguageId($idart, $lang);
195: if (!$this->oArtLang->isLoaded()) {
196: throw new cInvalidArgumentException('Couldn\'t load article language for idart=' . $idart . 'AND idlang=' . $lang);
197: }
198:
199: $this->_idartlang = $this->oArtLang->get('idartlang');
200: $this->_pageTitle = stripslashes($this->oArtLang->get('pagetitle'));
201:
202: return $this->_generate($contype);
203: }
204:
205: 206: 207: 208: 209: 210:
211: abstract function _generate($contype = true);
212:
213: 214: 215: 216: 217: 218:
219: protected function _getTemplateConfigurationId() {
220:
221: $idtplcfg = conGetTemplateConfigurationIdForArticle($this->_idart, $this->_idcat, $this->_lang, $this->_client);
222: if (is_numeric($idtplcfg) && $idtplcfg != 0) {
223:
224: cDebug::out("configuration for article found: $idtplcfg<br><br>");
225: } else {
226:
227: $idtplcfg = conGetTemplateConfigurationIdForCategory($this->_idcat, $this->_lang, $this->_client);
228: if (NULL !== $idtplcfg) {
229:
230: cDebug::out("configuration for category found: $idtplcfg<br><br>");
231: }
232: }
233:
234: return (is_numeric($idtplcfg)) ? $idtplcfg : NULL;
235: }
236:
237: abstract protected function _processNoConfigurationError($idcatart);
238:
239: 240: 241: 242: 243:
244: protected function _getTemplateData() {
245: global $cfg;
246:
247:
248: $sql = "SELECT a.idlay AS idlay, a.idtpl AS idtpl, a.name AS name
249: FROM `%s` AS a, `%s` AS b WHERE b.idtplcfg = %d AND b.idtpl = a.idtpl;";
250:
251: $sql = $this->_db->prepare($sql, $cfg['tab']['tpl'], $cfg['tab']['tpl_conf'], $this->_idtplcfg);
252: $this->_db->query($sql);
253: $this->_db->nextRecord();
254: $data = $this->_db->toArray();
255:
256: if ($this->_layout !== false) {
257: $data['idlay'] = $this->_layout;
258: }
259:
260: $idlay = $data['idlay'];
261: $idtpl = $data['idtpl'];
262: cDebug::out("Using Layout: $idlay and Template: $idtpl for generation of code.<br><br>");
263:
264: return $data;
265: }
266:
267: 268: 269: 270: 271: 272: 273: 274:
275: protected function _processCmsTags($contentList, $saveKeywords = true) {
276:
277:
278:
279: global $db, $db2, $sess, $cfg, $code, $cfgClient, $encoding;
280:
281:
282:
283: global $edit, $editLink, $belang;
284:
285: $idcat = $this->_idcat;
286: $idart = $this->_idart;
287: $lang = $this->_lang;
288: $client = $this->_client;
289: $idartlang = $this->_idartlang;
290:
291: if (!is_object($db2)) {
292: $db2 = cRegistry::getDb();
293: }
294:
295:
296:
297: $match = array();
298: $keycode = array();
299:
300:
301: $a_content = $contentList;
302:
303:
304: $_typeList = array();
305: $oTypeColl = new cApiTypeCollection();
306: $oTypeColl->select();
307: while ($oType = $oTypeColl->next()) {
308: $_typeList[] = $oType->toObject();
309: }
310:
311:
312: foreach ($_typeList as $_typeItem) {
313: $key = strtolower($_typeItem->type);
314:
315: $type = $_typeItem->type;
316:
317:
318:
319: $tmp = preg_match_all('/(' . $type . '\[+(\d)+\])/i', $this->_layoutCode, $match);
320:
321: $a_[$key] = $match[0];
322:
323: $success = array_walk($a_[$key], 'extractNumber');
324:
325: $search = array();
326: $replacements = array();
327:
328: $typeClassName = $this->_getContentTypeClassName($type);
329: $typeCodeFile = $this->_getContentTypeCodeFilePathName($type);
330:
331: foreach ($a_[$key] as $val) {
332: if (class_exists($typeClassName)) {
333:
334: $tmp = $a_content[$_typeItem->type][$val];
335: $cTypeObject = new $typeClassName($tmp, $val, $a_content);
336: if (cRegistry::isBackendEditMode()) {
337: $tmp = $cTypeObject->generateEditCode();
338: } else {
339: $tmp = $cTypeObject->generateViewCode();
340: }
341: } else if (cFileHandler::exists($typeCodeFile)) {
342:
343: include($typeCodeFile);
344: }
345:
346: $search[$val] = sprintf('%s[%s]', $type, $val);
347: $replacements[$val] = $tmp;
348: $keycode[$type][$val] = $tmp;
349: }
350:
351: $this->_layoutCode = str_ireplace($search, $replacements, $this->_layoutCode);
352: }
353: }
354:
355: 356: 357:
358: abstract protected function _processCodeTitleTag();
359:
360: 361: 362:
363: abstract protected function _processCodeMetaTags();
364:
365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376:
377: protected function _processCmsValueTags($containerId, $containerCfg) {
378: $containerCfgList = array();
379:
380: $containerCfg = preg_replace('/(&\$)/', '', $containerCfg);
381: parse_str($containerCfg, $containerCfgList);
382: 383: 384: 385: 386:
387:
388: $CiCMS_Var = '$C' . $containerId . 'CMS_VALUE';
389: $CiCMS_Values = array();
390:
391: foreach ($containerCfgList as $key3 => $value3) {
392: $tmp = $value3;
393: $tmp = str_replace("\'", "'", $tmp);
394: $CiCMS_Values[] = $CiCMS_Var . '[' . $key3 . '] = "' . $tmp . '"; ';
395: $this->_moduleCode = str_replace("\$CMS_VALUE[$key3]", $tmp, $this->_moduleCode);
396: $this->_moduleCode = str_replace("CMS_VALUE[$key3]", $tmp, $this->_moduleCode);
397: }
398:
399: $this->_moduleCode = str_replace("CMS_VALUE", $CiCMS_Var, $this->_moduleCode);
400: $this->_moduleCode = str_replace("\$" . $CiCMS_Var, $CiCMS_Var, $this->_moduleCode);
401: $this->_moduleCode = preg_replace("/(CMS_VALUE\[)([0-9]*)(\])/i", '', $this->_moduleCode);
402:
403: return implode("\n", $CiCMS_Values);
404: }
405:
406: 407: 408: 409: 410: 411: 412: 413:
414: protected function _processFrontendDebug($containerId, array $module) {
415:
416: if (empty($this->_feDebugOptions)) {
417: return;
418: }
419:
420: $sFeDebug = '';
421: if ($this->_feDebugOptions['container_display'] == true) {
422: $sFeDebug .= "Container: CMS_CONTAINER[$containerId]\\n";
423: }
424: if ($this->_feDebugOptions['module_display'] == true) {
425: $sFeDebug .= "Module: " . $module['name'] . "\\n";
426: }
427:
428: if ($this->_feDebugOptions['module_timing_summary'] == true || $this->_feDebugOptions['module_timing'] == true) {
429: $sFeDebug .= 'Eval-Time: $modTime' . $containerId . "\\n";
430: $this->_modulePrefix[] = '$modStart' . $containerId . ' = getmicrotime(true);';
431: $this->_moduleSuffix[] = '$modEnd' . $containerId . ' = getmicrotime(true);';
432: $this->_moduleSuffix[] = '$modTime' . $containerId . ' = $modEnd' . $containerId . ' - $modStart' . $containerId . ';';
433: }
434:
435: if ($sFeDebug != '') {
436: $this->_modulePrefix[] = 'echo \'<img onclick="javascript:showmod' . $containerId . '();" src="' . cRegistry::getBackendUrl() . 'images/but_preview.gif"><br>\';';
437: }
438:
439: if ($this->_feDebugOptions['module_timing_summary'] == true) {
440: $this->_moduleSuffix[] = 'echo \'<script type="text/javascript">function showmod' . $containerId . '(){window.alert(\\\'\'. "' . addslashes($sFeDebug) . '".\'\\\');} </script>\';';
441: $this->_moduleSuffix[] = '$cModuleTimes["' . $containerId . '"] = $modTime' . $containerId . ';';
442: $this->_moduleSuffix[] = '$cModuleNames["' . $containerId . '"] = "' . addslashes($module['name']) . '";';
443: }
444: }
445:
446: 447: 448: 449: 450: 451:
452: protected function _processCmsContainer($containerId) {
453: $cmsContainer = "CMS_CONTAINER[$containerId]";
454:
455:
456:
457: $this->_layoutCode = preg_replace("/<container( +)id=\\\"$containerId\\\"(.*)>(.*)<\/container>/Uis", $cmsContainer, $this->_layoutCode);
458: $this->_layoutCode = preg_replace("/<container( +)id=\\\"$containerId\\\"(.*)\/>/i", $cmsContainer, $this->_layoutCode);
459:
460:
461:
462: $modulePrefix = trim(implode("\n", $this->_modulePrefix));
463: if (!empty($modulePrefix)) {
464: $modulePrefix = "<?php\n" . $modulePrefix . "\n?>";
465: }
466: $moduleSuffix = trim(implode("\n", $this->_moduleSuffix));
467: if (!empty($moduleSuffix)) {
468: $moduleSuffix = "<?php\n" . $moduleSuffix . "\n?>";
469: }
470: $moduleOutput = $modulePrefix . $this->_moduleCode . $moduleSuffix;
471:
472:
473: $this->_layoutCode = str_ireplace($cmsContainer, $moduleOutput, $this->_layoutCode);
474:
475: }
476:
477: 478: 479: 480: 481: 482:
483: protected function _getUsedCmsTypesData() {
484: global $cfg;
485:
486: $return = array();
487:
488:
489: $sql = "SELECT * FROM `%s` AS A, `%s` AS B, `%s` AS C
490: WHERE A.idtype = C.idtype AND A.idartlang = B.idartlang AND B.idart = %d AND B.idlang = %d";
491: $sql = $this->_db->prepare($sql, $cfg['tab']['content'], $cfg['tab']['art_lang'], $cfg['tab']['type'], $this->_idart, $this->_lang);
492: $this->_db->query($sql);
493: while ($this->_db->nextRecord()) {
494: $return[$this->_db->f('type')][$this->_db->f('typeid')] = $this->_db->f('value');
495: }
496:
497: return $return;
498: }
499:
500: 501: 502:
503: protected function _resetModule() {
504: $this->_modulePrefix = array();
505: $this->_moduleCode = '';
506: $this->_moduleSuffix = array();
507: }
508:
509: 510: 511: 512: 513: 514: 515:
516: protected function _getContentTypeClassName($type) {
517: $typeClassName = 'cContentType' . ucfirst(strtolower(str_replace('CMS_', '', $type)));
518:
519: return $typeClassName;
520: }
521:
522: 523: 524: 525: 526: 527: 528: 529:
530: protected function _getContentTypeCodeFilePathName($type) {
531: global $cfg;
532: $typeCodeFile = cRegistry::getBackendPath() . $cfg['path']['includes'] . 'type/code/include.' . $type . '.code.php';
533:
534: return $typeCodeFile;
535: }
536:
537: 538: 539:
540: protected function getArtLangObject() {
541: return $this->oArtLang;
542: }
543: }
544: