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: * Constructor to create an instance of this class.
167: *
168: * Initialises class attributes with values from cRegistry.
169: *
170: * @param string $rawSettings
171: * the raw settings in an XML structure or as plaintext
172: * @param int $id
173: * ID of the content type, e.g. 3 if CMS_TEASER[3] is used
174: * @param array $contentTypes
175: * array containing the values of all content types
176: */
177: public function __construct($rawSettings, $id, array $contentTypes) {
178:
179: // set props
180: $this->_rawSettings = $rawSettings;
181: $this->_id = $id;
182: $this->_contentTypes = $contentTypes;
183:
184: $this->_idArtLang = cRegistry::getArticleLanguageId();
185: $this->_idArt = cRegistry::getArticleId();
186: $this->_idCat = cRegistry::getCategoryId();
187: $this->_cfg = cRegistry::getConfig();
188: $this->_client = cRegistry::getClientId();
189: $this->_lang = cRegistry::getLanguageId();
190: $this->_cfgClient = cRegistry::getClientConfig();
191: $this->_session = cRegistry::getSession();
192: $this->_useXHTML = cSecurity::toBoolean(getEffectiveSetting('generator', 'xhtml', 'false'));
193: $this->_uploadPath = $this->_cfgClient[$this->_client]['upl']['path'];
194:
195: $this->_readSettings();
196: }
197:
198: /**
199: * Reads all settings from the $_rawSettings attribute (XML or plaintext)
200: * and stores them in the $_settings attribute (associative array or
201: * plaintext).
202: */
203: protected function _readSettings() {
204: // if no settings have been given, do nothing
205: if (empty($this->_rawSettings)) {
206: return;
207: }
208: if ($this->_settingsType === self::SETTINGS_TYPE_XML) {
209: // if the settings should be interpreted as XML, process them
210: // accordingly
211: $this->_settings = cXmlBase::xmlStringToArray($this->_rawSettings);
212: // add the prefix to the settings array keys
213: foreach ($this->_settings as $key => $value) {
214: $this->_settings[$this->_prefix . '_' . $key] = $value;
215: unset($this->_settings[$key]);
216: }
217: } else {
218: // otherwise do not process the raw setting
219: $this->_settings = $this->_rawSettings;
220: }
221: }
222:
223: /**
224: * Function returns current content type configuration as array
225: *
226: * @return array|string
227: */
228: public function getConfiguration() {
229: return $this->_settings;
230: }
231:
232: /**
233: * Stores all values from the $_POST array in the $_settings attribute
234: * (associative array) and saves them in the database (XML).
235: */
236: protected function _storeSettings() {
237: $settingsToStore = '';
238: if ($this->_settingsType === self::SETTINGS_TYPE_XML) {
239: // if the settings should be stored as XML, process them accordingly
240: $settings = array();
241: // update the values in the settings array with the values from the
242: // $_POST array
243: foreach ($this->_formFields as $key) {
244: $keyWithoutPrefix = str_replace($this->_prefix . '_', '', $key);
245: if (isset($_POST[$key])) {
246: $this->_settings[$key] = $_POST[$key];
247: } else if (isset($_POST[$this->_prefix . '_array_' . $keyWithoutPrefix])) {
248: // key is of type prefix_array_field, so interpret value as an array
249: $this->_settings[$key] = explode(',', $_POST[$this->_prefix . '_array_' . $keyWithoutPrefix]);
250: }
251: $settings[$keyWithoutPrefix] = $this->_settings[$key];
252: }
253: $xml = cXmlBase::arrayToXml($settings, NULL, $this->_prefix);
254: $settingsToStore = $xml->asXML();
255: } else {
256: $settingsToStore = $this->_settings;
257: }
258:
259: // store new settings in the database
260: conSaveContentEntry($this->_idArtLang, $this->_type, $this->_id, $settingsToStore);
261: }
262:
263: /**
264: * Since the content type code is evaled by php, the code has to be encoded.
265: *
266: * @param string $code
267: * code to encode
268: * @return string
269: * encoded code
270: */
271: protected function _encodeForOutput($code) {
272: $code = addslashes($code);
273: $code = str_replace("\\'", "'", $code);
274: $code = str_replace('\$', '\\$', $code);
275:
276: return $code;
277: }
278:
279: /**
280: * Builds an array with directory information from the given upload path.
281: *
282: * @SuppressWarnings docBlocks
283: * @param string $uploadPath [optional]
284: * path to upload directory
285: * (default: root upload path of client)
286: * @return array
287: * with directory information (keys: name, path, sub)
288: */
289: public function buildDirectoryList($uploadPath = '') {
290: // make sure the upload path is set and ends with a slash
291: if ($uploadPath === '') {
292: $uploadPath = $this->_uploadPath;
293: }
294: if (substr($uploadPath, -1) !== '/') {
295: $uploadPath .= '/';
296: }
297:
298: $directories = array();
299:
300: if (is_dir($uploadPath)) {
301: if (false !== ($handle = cDirHandler::read($uploadPath, false, true))) {
302: foreach ($handle as $entry) {
303: if (cFileHandler::fileNameBeginsWithDot($entry) === false && is_dir($uploadPath . $entry)) {
304:
305: $directory = array();
306: $directory['name'] = $entry;
307: $directory['path'] = str_replace($this->_uploadPath, '', $uploadPath);
308: $directory['sub'] = $this->buildDirectoryList($uploadPath . $entry);
309: $directories[] = $directory;
310: }
311: }
312: }
313: }
314:
315: usort($directories, function($a, $b) {
316: $a = mb_strtolower($a["name"]);
317: $b = mb_strtolower($b["name"]);
318: if($a < $b) {
319: return -1;
320: } else if($a > $b) {
321: return 1;
322: } else {
323: return 0;
324: }
325: });
326:
327: return $directories;
328: }
329:
330: /**
331: * Generates a directory list from the given directory information (which is
332: * typically built by {@link cContentTypeAbstract::buildDirectoryList}).
333: *
334: * @param array $dirs
335: * directory information
336: * @return string
337: * HTML code showing a directory list
338: */
339: public function generateDirectoryList(array $dirs) {
340: $template = new cTemplate();
341: $i = 1;
342:
343: foreach ($dirs as $dirData) {
344: // set the active class if this is the chosen directory
345: $divClass = ($this->_isActiveDirectory($dirData)) ? 'active' : '';
346: $template->set('d', 'DIVCLASS', $divClass);
347:
348: $template->set('d', 'TITLE', $dirData['path'] . $dirData['name']);
349: $template->set('d', 'DIRNAME', $dirData['name']);
350:
351: $liClasses = array();
352: // check if the directory should be shown expanded or collapsed
353: if ($this->_shouldDirectoryBeExpanded($dirData)) {
354: $template->set('d', 'SUBDIRLIST', $this->generateDirectoryList($dirData['sub']));
355: } else if (isset($dirData['sub']) && count($dirData['sub']) > 0) {
356: $liClasses[] = 'collapsed';
357: $template->set('d', 'SUBDIRLIST', '');
358: } else {
359: $template->set('d', 'SUBDIRLIST', '');
360: }
361: if ($i === count($dirs)) {
362: $liClasses[] = 'last';
363: }
364: $template->set('d', 'LICLASS', implode(' ', $liClasses));
365:
366: $i++;
367: $template->next();
368: }
369:
370: return $template->generate($this->_cfg['path']['contenido'] . 'templates/standard/template.cms_filelist_dirlistitem.html', true);
371: }
372:
373: /**
374: * Checks whether the directory defined by the given directory
375: * information is the currently active directory.
376: * Overwrite in subclasses if you use generateDirectoryList!
377: *
378: * @param array $dirData
379: * directory information
380: * @return bool
381: * whether the directory is the currently active directory
382: */
383: protected function _isActiveDirectory(array $dirData) {
384: return false;
385: }
386:
387: /**
388: * Checks whether the directory defined by the given directory information
389: * should be shown expanded.
390: * Overwrite in subclasses if you use getDirectoryList!
391: *
392: * @param array $dirData
393: * directory information
394: * @return bool
395: * whether the directory should be shown expanded
396: */
397: protected function _shouldDirectoryBeExpanded(array $dirData) {
398: return false;
399: }
400:
401: /**
402: * Checks whether the given $subDir is a subdirectory of the given $dir.
403: *
404: * @param string $subDir
405: * the potential subdirectory
406: * @param string $dir
407: * the parent directory
408: * @return bool
409: * whether the given $subDir is a subdirectory of $dir
410: */
411: protected function _isSubdirectory($subDir, $dir) {
412: $dirArray = explode('/', $dir);
413: $expand = false;
414: $checkDir = '';
415:
416: // construct the whole directory in single steps and check if the given
417: // directory can be found
418: foreach ($dirArray as $dirPart) {
419: $checkDir .= '/' . $dirPart;
420: if ($checkDir === '/' . $subDir) {
421: $expand = true;
422: }
423: }
424:
425: return $expand;
426: }
427:
428: /**
429: * Generates the code which should be shown if this content type is shown in
430: * the frontend.
431: *
432: * @return string
433: * escaped HTML code which sould be shown if content type is shown in frontend
434: */
435: public abstract function generateViewCode();
436:
437: /**
438: * Generates the code which should be shown if this content type is edited.
439: *
440: * @return string
441: * escaped HTML code which should be shown if content type is edited
442: */
443: public abstract function generateEditCode();
444:
445: /**
446: * Checks if this content type can be edited by a WYSIWYG editor
447: *
448: * @return bool
449: */
450: public function isWysiwygCompatible() {
451: return false;
452: }
453:
454: }
455: