1: <?php
2:
3: /**
4: * This file contains the cContentTypeAbstract class.
5: *
6: * @package Core
7: * @subpackage ContentType
8: * @author Simon Sprankel
9: * @copyright four for business AG <www.4fb.de>
10: * @license http://www.contenido.org/license/LIZENZ.txt
11: * @link http://www.4fb.de
12: * @link http://www.contenido.org
13: */
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: /**
18: * Abstract content type from which every content type should inherit.
19: *
20: * @package Core
21: * @subpackage ContentType
22: */
23: abstract class cContentTypeAbstract {
24:
25: /**
26: * Constant defining that the settings should be interpreted as plaintext.
27: *
28: * @var string
29: */
30: const SETTINGS_TYPE_PLAINTEXT = 'plaintext';
31:
32: /**
33: * Constant defining that the settings should be interpreted as XML.
34: *
35: * @var string
36: */
37: const SETTINGS_TYPE_XML = 'xml';
38:
39: /**
40: * Name of the content type, e.g. 'CMS_TEASER'.
41: *
42: * @var string
43: */
44: protected $_type = '';
45:
46: /**
47: * Prefix of the content type, e.g. 'teaser'.
48: *
49: * @var string
50: */
51: protected $_prefix = 'abstract';
52:
53: /**
54: * Whether the settings should be interpreted as plaintext or XML.
55: *
56: * @var string
57: */
58: protected $_settingsType = self::SETTINGS_TYPE_PLAINTEXT;
59:
60: /**
61: * ID of the content type, e.g. 3 if CMS_TEASER[3] is used.
62: *
63: * @var int
64: */
65: protected $_id;
66:
67: /**
68: * Array containing the values of all content types.
69: *
70: * @var array
71: */
72: protected $_contentTypes;
73:
74: /**
75: * CONTENIDO configuration array
76: *
77: * @var array
78: */
79: protected $_cfg;
80:
81: /**
82: * idartlang of corresponding article
83: *
84: * @var int
85: */
86: protected $_idArtLang;
87:
88: /**
89: * idart of corresponding article
90: *
91: * @var int
92: */
93: protected $_idArt;
94:
95: /**
96: * idcat of corresponding article
97: *
98: * @var int
99: */
100: protected $_idCat;
101:
102: /**
103: * CONTENIDO client id
104: *
105: * @var int
106: */
107: protected $_client;
108:
109: /**
110: * CONTENIDO language id
111: *
112: * @var int
113: */
114: protected $_lang;
115:
116: /**
117: * CONTENIDO session object
118: *
119: * @var cSession
120: */
121: protected $_session;
122:
123: /**
124: * CONTENIDO configuration array for currently active client
125: *
126: * @var array
127: */
128: protected $_cfgClient;
129:
130: /**
131: * Whether to generate XHTML
132: *
133: * @var bool
134: */
135: protected $_useXHTML;
136:
137: /**
138: * The path to the upload directory.
139: *
140: * @var string
141: */
142: protected $_uploadPath;
143:
144: /**
145: * The raw settings from the DB.
146: *
147: * @var string
148: */
149: protected $_rawSettings = array();
150:
151: /**
152: * The parsed settings.
153: *
154: * @var array|string
155: */
156: protected $_settings = array();
157:
158: /**
159: * List of form field names which are used by this content type!
160: *
161: * @var array
162: */
163: protected $_formFields = array();
164:
165: /**
166: * Initialises class attributes with values from cRegistry.
167: *
168: * @param string $rawSettings
169: * the raw settings in an XML structure or as plaintext
170: * @param int $id
171: * ID of the content type, e.g. 3 if CMS_TEASER[3] is used
172: * @param array $contentTypes
173: * array containing the values of all content types
174: */
175: public function __construct($rawSettings, $id, array $contentTypes) {
176:
177: // set props
178: $this->_rawSettings = $rawSettings;
179: $this->_id = $id;
180: $this->_contentTypes = $contentTypes;
181:
182: $this->_idArtLang = cRegistry::getArticleLanguageId();
183: $this->_idArt = cRegistry::getArticleId();
184: $this->_idCat = cRegistry::getCategoryId();
185: $this->_cfg = cRegistry::getConfig();
186: $this->_client = cRegistry::getClientId();
187: $this->_lang = cRegistry::getLanguageId();
188: $this->_cfgClient = cRegistry::getClientConfig();
189: $this->_session = cRegistry::getSession();
190: $this->_useXHTML = cSecurity::toBoolean(getEffectiveSetting('generator', 'xhtml', 'false'));
191: $this->_uploadPath = $this->_cfgClient[$this->_client]['upl']['path'];
192:
193: $this->_readSettings();
194: }
195:
196: /**
197: * Reads all settings from the $_rawSettings attribute (XML or plaintext)
198: * and stores them in the $_settings attribute (associative array or
199: * plaintext).
200: */
201: protected function _readSettings() {
202: // if no settings have been given, do nothing
203: if (empty($this->_rawSettings)) {
204: return;
205: }
206: if ($this->_settingsType === self::SETTINGS_TYPE_XML) {
207: // if the settings should be interpreted as XML, process them
208: // accordingly
209: $this->_settings = cXmlBase::xmlStringToArray($this->_rawSettings);
210: // add the prefix to the settings array keys
211: foreach ($this->_settings as $key => $value) {
212: $this->_settings[$this->_prefix . '_' . $key] = $value;
213: unset($this->_settings[$key]);
214: }
215: } else {
216: // otherwise do not process the raw setting
217: $this->_settings = $this->_rawSettings;
218: }
219: }
220:
221: /**
222: * Function returns current content type configuration as array
223: *
224: * @return array|string
225: */
226: public function getConfiguration() {
227: return $this->_settings;
228: }
229:
230: /**
231: * Stores all values from the $_POST array in the $_settings attribute
232: * (associative array) and saves them in the database (XML).
233: */
234: protected function _storeSettings() {
235: $settingsToStore = '';
236: if ($this->_settingsType === self::SETTINGS_TYPE_XML) {
237: // if the settings should be stored as XML, process them accordingly
238: $settings = array();
239: // update the values in the settings array with the values from the
240: // $_POST array
241: foreach ($this->_formFields as $key) {
242: $keyWithoutPrefix = str_replace($this->_prefix . '_', '', $key);
243: if (isset($_POST[$key])) {
244: $this->_settings[$key] = $_POST[$key];
245: } else if (isset($_POST[$this->_prefix . '_array_' . $keyWithoutPrefix])) {
246: // key is of type prefix_array_field, so interpret value as an array
247: $this->_settings[$key] = explode(',', $_POST[$this->_prefix . '_array_' . $keyWithoutPrefix]);
248: }
249: $settings[$keyWithoutPrefix] = $this->_settings[$key];
250: }
251: $xml = cXmlBase::arrayToXml($settings, NULL, $this->_prefix);
252: $settingsToStore = $xml->asXML();
253: } else {
254: $settingsToStore = $this->_settings;
255: }
256:
257: // store new settings in the database
258: conSaveContentEntry($this->_idArtLang, $this->_type, $this->_id, $settingsToStore);
259: }
260:
261: /**
262: * Since the content type code is evaled by php, the code has to be encoded.
263: *
264: * @param string $code
265: * code to encode
266: * @return string
267: * encoded code
268: */
269: protected function _encodeForOutput($code) {
270: $code = addslashes($code);
271: $code = str_replace("\\'", "'", $code);
272: $code = str_replace('\$', '\\$', $code);
273:
274: return $code;
275: }
276:
277: /**
278: * Builds an array with directory information from the given upload path.
279: *
280: * @SuppressWarnings docBlocks
281: * @param string $uploadPath [optional]
282: * path to upload directory
283: * (default: root upload path of client)
284: * @return array
285: * with directory information (keys: name, path, sub)
286: */
287: public function buildDirectoryList($uploadPath = '') {
288: // make sure the upload path is set and ends with a slash
289: if ($uploadPath === '') {
290: $uploadPath = $this->_uploadPath;
291: }
292: if (substr($uploadPath, -1) !== '/') {
293: $uploadPath .= '/';
294: }
295:
296: $directories = array();
297:
298: if (is_dir($uploadPath)) {
299: if (false !== ($handle = cDirHandler::read($uploadPath, false, true))) {
300: foreach ($handle as $entry) {
301: if (cFileHandler::fileNameBeginsWithDot($entry) === false && is_dir($uploadPath . $entry)) {
302:
303: $directory = array();
304: $directory['name'] = $entry;
305: $directory['path'] = str_replace($this->_uploadPath, '', $uploadPath);
306: $directory['sub'] = $this->buildDirectoryList($uploadPath . $entry);
307: $directories[] = $directory;
308: }
309: }
310: }
311: }
312:
313: usort($directories, function($a, $b) {
314: $a = mb_strtolower($a["name"]);
315: $b = mb_strtolower($b["name"]);
316: if($a < $b) {
317: return -1;
318: } else if($a > $b) {
319: return 1;
320: } else {
321: return 0;
322: }
323: });
324:
325: return $directories;
326: }
327:
328: /**
329: * Generates a directory list from the given directory information (which is
330: * typically built by {@link cContentTypeAbstract::buildDirectoryList}).
331: *
332: * @param array $dirs
333: * directory information
334: * @return string
335: * HTML code showing a directory list
336: */
337: public function generateDirectoryList(array $dirs) {
338: $template = new cTemplate();
339: $i = 1;
340:
341: foreach ($dirs as $dirData) {
342: // set the active class if this is the chosen directory
343: $divClass = ($this->_isActiveDirectory($dirData)) ? 'active' : '';
344: $template->set('d', 'DIVCLASS', $divClass);
345:
346: $template->set('d', 'TITLE', $dirData['path'] . $dirData['name']);
347: $template->set('d', 'DIRNAME', $dirData['name']);
348:
349: $liClasses = array();
350: // check if the directory should be shown expanded or collapsed
351: if ($this->_shouldDirectoryBeExpanded($dirData)) {
352: $template->set('d', 'SUBDIRLIST', $this->generateDirectoryList($dirData['sub']));
353: } else if (isset($dirData['sub']) && count($dirData['sub']) > 0) {
354: $liClasses[] = 'collapsed';
355: $template->set('d', 'SUBDIRLIST', '');
356: } else {
357: $template->set('d', 'SUBDIRLIST', '');
358: }
359: if ($i === count($dirs)) {
360: $liClasses[] = 'last';
361: }
362: $template->set('d', 'LICLASS', implode(' ', $liClasses));
363:
364: $i++;
365: $template->next();
366: }
367:
368: return $template->generate($this->_cfg['path']['contenido'] . 'templates/standard/template.cms_filelist_dirlistitem.html', true);
369: }
370:
371: /**
372: * Checks whether the directory defined by the given directory
373: * information is the currently active directory.
374: * Overwrite in subclasses if you use generateDirectoryList!
375: *
376: * @param array $dirData
377: * directory information
378: * @return bool
379: * whether the directory is the currently active directory
380: */
381: protected function _isActiveDirectory(array $dirData) {
382: return false;
383: }
384:
385: /**
386: * Checks whether the directory defined by the given directory information
387: * should be shown expanded.
388: * Overwrite in subclasses if you use getDirectoryList!
389: *
390: * @param array $dirData
391: * directory information
392: * @return bool
393: * whether the directory should be shown expanded
394: */
395: protected function _shouldDirectoryBeExpanded(array $dirData) {
396: return false;
397: }
398:
399: /**
400: * Checks whether the given $subDir is a subdirectory of the given $dir.
401: *
402: * @param string $subDir
403: * the potential subdirectory
404: * @param string $dir
405: * the parent directory
406: * @return bool
407: * whether the given $subDir is a subdirectory of $dir
408: */
409: protected function _isSubdirectory($subDir, $dir) {
410: $dirArray = explode('/', $dir);
411: $expand = false;
412: $checkDir = '';
413:
414: // construct the whole directory in single steps and check if the given
415: // directory can be found
416: foreach ($dirArray as $dirPart) {
417: $checkDir .= '/' . $dirPart;
418: if ($checkDir === '/' . $subDir) {
419: $expand = true;
420: }
421: }
422:
423: return $expand;
424: }
425:
426: /**
427: * Generates the code which should be shown if this content type is shown in
428: * the frontend.
429: *
430: * @return string
431: * escaped HTML code which sould be shown if content type is shown in frontend
432: */
433: public abstract function generateViewCode();
434:
435: /**
436: * Generates the code which should be shown if this content type is edited.
437: *
438: * @return string
439: * escaped HTML code which should be shown if content type is edited
440: */
441: public abstract function generateEditCode();
442:
443: /**
444: * Checks if this content type can be edited by a WYSIWYG editor
445: *
446: * @return bools
447: */
448: public function isWysiwygCompatible() {
449: return false;
450: }
451:
452: }
453: