1: <?php
2: /**
3: * This file contains the cContentTypeAbstract class.
4: *
5: * @package Core
6: * @subpackage ContentType
7: * @version SVN Revision $Rev:$
8: *
9: * @author Simon Sprankel
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
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: * @var string
35: */
36: const SETTINGS_TYPE_XML = 'xml';
37:
38: /**
39: * Name of the content type, e.g.
40: * 'CMS_TEASER'.
41: *
42: * @var string
43: */
44: protected $_type = '';
45:
46: /**
47: * Prefix of the content type, e.g.
48: * 'teaser'.
49: *
50: * @var string
51: */
52: protected $_prefix = 'abstract';
53:
54: /**
55: * Whether the settings should be interpreted as plaintext or XML.
56: *
57: * @var string
58: */
59: protected $_settingsType = self::SETTINGS_TYPE_PLAINTEXT;
60:
61: /**
62: * ID of the content type, e.g.
63: * 3 if CMS_TEASER[3] is used.
64: *
65: * @var integer
66: */
67: protected $_id;
68:
69: /**
70: * Array containing the values of all content types.
71: *
72: * @var array
73: */
74: protected $_contentTypes;
75:
76: /**
77: * CONTENIDO configuration array
78: *
79: * @var array
80: */
81: protected $_cfg;
82:
83: /**
84: * idartlang of corresponding article
85: *
86: * @var integer
87: */
88: protected $_idArtLang;
89:
90: /**
91: * idart of corresponding article
92: *
93: * @var integer
94: */
95: protected $_idArt;
96:
97: /**
98: * idcat of corresponding article
99: *
100: * @var integer
101: */
102: protected $_idCat;
103:
104: /**
105: * CONTENIDO client id
106: *
107: * @var integer
108: */
109: protected $_client;
110:
111: /**
112: * CONTENIDO language id
113: *
114: * @var integer
115: */
116: protected $_lang;
117:
118: /**
119: * CONTENIDO session object
120: *
121: * @var cSession
122: */
123: protected $_session;
124:
125: /**
126: * CONTENIDO configuration array for currently active client
127: *
128: * @var array
129: */
130: protected $_cfgClient;
131:
132: /**
133: * Whether to generate XHTML
134: *
135: * @var boolean
136: */
137: protected $_useXHTML;
138:
139: /**
140: * The path to the upload directory.
141: *
142: * @var string
143: */
144: protected $_uploadPath;
145:
146: /**
147: * The raw settings from the DB.
148: *
149: * @var string
150: */
151: protected $_rawSettings = array();
152:
153: /**
154: * The parsed settings.
155: *
156: * @var array string
157: */
158: protected $_settings = array();
159:
160: /**
161: * List of form field names which are used by this content type!
162: *
163: * @var array
164: */
165: protected $_formFields = array();
166:
167: /**
168: * Initialises class attributes with values from cRegistry.
169: *
170: * @param string $rawSettings the raw settings in an XML structure or as
171: * plaintext
172: * @param int $id ID of the content type, e.g. 3 if CMS_TEASER[3] is
173: * used
174: * @param array $contentTypes array containing the values of all content
175: * types
176: */
177: public function __construct($rawSettings, $id, array $contentTypes) {
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 curren content type configuration as array
223: *
224: * @return array
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
247: // an array
248: $this->_settings[$key] = explode(',', $_POST[$this->_prefix . '_array_' . $keyWithoutPrefix]);
249: }
250: $settings[$keyWithoutPrefix] = $this->_settings[$key];
251: }
252: $xml = cXmlBase::arrayToXml($settings, NULL, $this->_prefix);
253: $settingsToStore = $xml->asXML();
254: } else {
255: $settingsToStore = $this->_settings;
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 code to encode
265: * @return string encoded code
266: */
267: protected function _encodeForOutput($code) {
268: $code = addslashes($code);
269: $code = str_replace("\\'", "'", $code);
270: $code = str_replace('\$', '\\$', $code);
271:
272: return $code;
273: }
274:
275: /**
276: * Builds an array with directory information from the given upload path.
277: *
278: * @param string $uploadPath path to upload directory (optional, default:
279: * root upload path
280: * of client)
281: * @return array with directory information (keys: name, path, sub)
282: */
283: public function buildDirectoryList($uploadPath = '') {
284: // make sure the upload path is set and ends with a slash
285: if ($uploadPath === '') {
286: $uploadPath = $this->_uploadPath;
287: }
288: if (substr($uploadPath, -1) !== '/') {
289: $uploadPath .= '/';
290: }
291:
292: $directories = array();
293:
294: if (is_dir($uploadPath)) {
295: if ($handle = opendir($uploadPath)) {
296: while (($entry = readdir($handle)) !== false) {
297: // ignore .svn directories as well as links to upper dirs
298: if ($entry != '.svn' && $entry != '.' && $entry != '..' && is_dir($uploadPath . $entry)) {
299: $directory = array();
300: $directory['name'] = $entry;
301: $directory['path'] = str_replace($this->_uploadPath, '', $uploadPath);
302: $directory['sub'] = $this->buildDirectoryList($uploadPath . $entry);
303: $directories[] = $directory;
304: }
305: }
306: }
307: closedir($handle);
308: }
309: return $directories;
310: }
311:
312: /**
313: * Generates a directory list from the given directory information (which is
314: * typically built by {@link cContentTypeAbstract::buildDirectoryList}).
315: *
316: * @param array $dirs directory information
317: * @return string HTML code showing a directory list
318: */
319: public function generateDirectoryList(array $dirs) {
320: $template = new cTemplate();
321: $i = 1;
322:
323: foreach ($dirs as $dirData) {
324: // set the active class if this is the chosen directory
325: $divClass = ($this->_isActiveDirectory($dirData)) ? 'active' : '';
326: $template->set('d', 'DIVCLASS', $divClass);
327:
328: $template->set('d', 'TITLE', $dirData['path'] . $dirData['name']);
329: $template->set('d', 'DIRNAME', $dirData['name']);
330:
331: $liClasses = array();
332: // check if the directory should be shown expanded or collapsed
333: if ($this->_shouldDirectoryBeExpanded($dirData)) {
334: $template->set('d', 'SUBDIRLIST', $this->generateDirectoryList($dirData['sub']));
335: } else if (isset($dirData['sub']) && count($dirData['sub']) > 0) {
336: $liClasses[] = 'collapsed';
337: $template->set('d', 'SUBDIRLIST', '');
338: } else {
339: $template->set('d', 'SUBDIRLIST', '');
340: }
341: if ($i === count($dirs)) {
342: $liClasses[] = 'last';
343: }
344: $template->set('d', 'LICLASS', implode(' ', $liClasses));
345:
346: $i++;
347: $template->next();
348: }
349:
350: return $template->generate($this->_cfg['path']['contenido'] . 'templates/standard/template.cms_filelist_dirlistitem.html', true);
351: }
352:
353: /**
354: * Checks whether the directory defined by the given directory
355: * information is the currently active directory.
356: * Overwrite in subclasses if you use generateDirectoryList!
357: *
358: * @param array $dirData directory information
359: * @return boolean whether the directory is the currently active directory
360: */
361: protected function _isActiveDirectory(array $dirData) {
362: return false;
363: }
364:
365: /**
366: * Checks whether the directory defined by the given directory information
367: * should be shown expanded.
368: * Overwrite in subclasses if you use getDirectoryList!
369: *
370: * @param array $dirData directory information
371: * @return boolean whether the directory should be shown expanded
372: */
373: protected function _shouldDirectoryBeExpanded(array $dirData) {
374: return false;
375: }
376:
377: /**
378: * Checks whether the given $subDir is a subdirectory of the given $dir.
379: *
380: * @param string $subDir the potential subdirectory
381: * @param string $dir the parent directory
382: * @return boolean whether the given $subDir is a subdirectory of $dir
383: */
384: protected function _isSubdirectory($subDir, $dir) {
385: $dirArray = explode('/', $dir);
386: $expand = false;
387: $checkDir = '';
388:
389: // construct the whole directory in single steps and check if the given
390: // directory can be found
391: foreach ($dirArray as $dirPart) {
392: $checkDir .= '/' . $dirPart;
393: if ($checkDir === '/' . $subDir) {
394: $expand = true;
395: }
396: }
397:
398: return $expand;
399: }
400:
401: /**
402: * Generates the code which should be shown if this content type is shown in
403: * the frontend.
404: *
405: * @return string escaped HTML code which sould be shown if content type is
406: * shown in frontend
407: */
408: public abstract function generateViewCode();
409:
410: /**
411: * Generates the code which should be shown if this content type is edited.
412: *
413: * @return string escaped HTML code which should be shown if content type is
414: * edited
415: */
416: public abstract function generateEditCode();
417:
418: }