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: class cTypeGenerator {
 25: 
 26:      27:  28:  29: 
 30:     private $cfg = NULL;
 31: 
 32:      33:  34:  35: 
 36:     private static $db = NULL;
 37: 
 38:      39:  40:  41: 
 42:     private static $a_content = array();
 43: 
 44:      45:  46:  47: 
 48:     private $_idart = NULL;
 49: 
 50:      51:  52:  53: 
 54:     private $_idlang = NULL;
 55: 
 56:      57:  58:  59:  60: 
 61:     public function __construct() {
 62:         $this->_idart = cRegistry::getArticleId(true);
 63:         $this->_idlang = cRegistry::getLanguageId();
 64:         $this->cfg = cRegistry::getConfig();
 65: 
 66:         if (self::$db === NULL) {
 67:             self::$db = cRegistry::getDb();
 68:         }
 69:         if (!isset(self::$a_content[$this->_idart])) {
 70:             $this->fillContent();
 71:         }
 72:     }
 73: 
 74:      75:  76:  77:  78:  79:  80:  81: 
 82:     protected function _getContentTypeClassName($type) {
 83:         $typeClassName = 'cContentType' . ucfirst(cString::toLowerCase(str_replace('CMS_', '', $type)));
 84:         return $typeClassName;
 85:     }
 86: 
 87:      88:  89:  90:  91: 
 92:     public static function getContentTypeClassName($type)  {
 93:         $contentType = cString::getPartOfString($type, 4);
 94:         return 'cContentType' . cString::toUpperCase($contentType[0]) . cString::toLowerCase(cString::getPartOfString($contentType, 1));
 95:     }
 96: 
 97:      98:  99: 100: 101: 102: 103: 104: 105: 106: 
107:     protected function _getContentTypeCodeFilePathName($type) {
108:         global $cfg;
109:         $typeCodeFile = cRegistry::getBackendPath() . $cfg['path']['includes'] . 'type/code/include.' . $type . '.code.php';
110:         return $typeCodeFile;
111:     }
112: 
113:     114: 115: 116: 117: 
118:     private function fillContent() {
119:         self::$a_content[$this->_idart] = array();
120: 
121:         $sql = "SELECT
122:                     *
123:                 FROM
124:                     " . $this->cfg["tab"]["content"] . " AS A,
125:                     " . $this->cfg["tab"]["art_lang"] . " AS B,
126:                     " . $this->cfg["tab"]["type"] . " AS C
127:                 WHERE
128:                     A.idtype    = C.idtype AND
129:                     A.idartlang = B.idartlang AND
130:                     B.idart     = '" . cSecurity::toInteger($this->_idart) . "' AND
131:                     B.idlang    = '" . cSecurity::toInteger($this->_idlang) . "'";
132: 
133:         self::$db->query($sql);
134: 
135:         while (self::$db->next_record()) {
136:             self::$a_content[$this->_idart][self::$db->f("type")][self::$db->f("typeid")] = self::$db->f("value");
137:         }
138:     }
139: 
140:     141: 142: 143: 144: 145: 146: 147: 148: 149: 
150:     private function _processCmsTags($type, $index) {
151:         $oTypeColl = new cApiTypeCollection();
152:         $oTypeColl->select();
153: 
154:         $typeList = array();
155:         while (false !== $oType = $oTypeColl->next()) {
156:             $typeList[] = $oType->toObject();
157:         }
158: 
159:         
160:         foreach ($typeList as $typeItem) {
161: 
162:             if ($type === $typeItem->type) {
163: 
164:                 $items[] = $typeItem->type;
165: 
166:                 $typeClassName = $this->_getContentTypeClassName($typeItem->type);
167:                 $typeCodeFile = $this->_getContentTypeCodeFilePathName($typeItem->type);
168: 
169:                 $cTypeObject = new $typeClassName(self::$a_content[$this->_idart][$typeItem->type][$index], $index, $items);
170:                 if (cRegistry::isBackendEditMode()) {
171:                     $tmp = $cTypeObject->generateEditCode();
172:                 } else {
173:                     $tmp = $cTypeObject->generateViewCode();
174:                 }
175: 
176:                 return $tmp;
177:             }
178:         }
179:     }
180: 
181:     182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 
192:     public function getGeneratedCmsTag($type, $index) {
193:         return $this->_processCmsTags($type, $index);
194:     }
195: }
196: