Overview

Packages

  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Datatype
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationMain
    • NavigationTop
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cCodeGeneratorAbstract
  • cCodeGeneratorFactory
  • cCodeGeneratorStandard
  • cContentTypeAbstract
  • cContentTypeAbstractTabbed
  • cContentTypeDate
  • cContentTypeFilelist
  • cContentTypeHead
  • cContentTypeHtml
  • cContentTypeHtmlhead
  • cContentTypeImg
  • cContentTypeImgdescr
  • cContentTypeImgeditor
  • cContentTypeLink
  • cContentTypeLinkdescr
  • cContentTypeLinkeditor
  • cContentTypeLinktarget
  • cContentTypeTeaser
  • cContentTypeText
  • cTypeGenerator
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * This file contains the cContentTypeDate class.
  4:  *
  5:  * @package Core
  6:  * @subpackage ContentType
  7:  * @version SVN Revision $Rev:$
  8:  * @author Bilal Arslan, Timo Trautmann, 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:  * Content type CMS_DATE which allows the editor to select a date from a
 19:  * calendar and a date format. The selected date is then shown in the selected
 20:  * format.
 21:  *
 22:  * @package Core
 23:  * @subpackage ContentType
 24:  */
 25: class cContentTypeDate extends cContentTypeAbstract {
 26: 
 27:     /**
 28:      * The possible PHP date formats in which the selected date can be
 29:      * displayed.
 30:      *
 31:      * @var array
 32:      */
 33:     private $_dateFormatsPhp;
 34: 
 35:     /**
 36:      * Initialises class attributes and handles store events.
 37:      *
 38:      * @param string $rawSettings the raw settings in an XML structure or as
 39:      *            plaintext
 40:      * @param integer $id ID of the content type, e.g. 3 if CMS_DATE[3] is used
 41:      * @param array $contentTypes array containing the values of all content
 42:      *            types
 43:      * @return void
 44:      */
 45:     public function __construct($rawSettings, $id, array $contentTypes) {
 46:         // change attributes from the parent class and call the parent
 47:         // constructor
 48:         $this->_type = 'CMS_DATE';
 49:         $this->_prefix = 'date';
 50:         $this->_settingsType = 'xml';
 51:         $this->_formFields = array(
 52:                 'date_timestamp',
 53:                 'date_format'
 54:         );
 55:         parent::__construct($rawSettings, $id, $contentTypes);
 56: 
 57:         // set the locale
 58:         $belang = cRegistry::getBackendLanguage();
 59:         if (empty($belang)) {
 60:             $language = new cApiLanguage(cRegistry::getLanguageId());
 61:             $locale = $language->getProperty('dateformat', 'locale');
 62:             if (!empty($locale)) {
 63:                 setlocale(LC_TIME, $locale);
 64:             }
 65:         } else {
 66:             setlocale(LC_TIME, $belang);
 67:         }
 68: 
 69:         // initialise the date formats
 70:         $this->_dateFormatsPhp = array(
 71:                 conHtmlSpecialChars('{"dateFormat":"","timeFormat":""}') => '',
 72:                 conHtmlSpecialChars('{"dateFormat":"d.m.Y","timeFormat":""}') => $this->_formatDate('d.m.Y'),
 73:                 conHtmlSpecialChars('{"dateFormat":"D, d.m.Y","timeFormat":""}') => $this->_formatDate('D, d.m.Y'),
 74:                 conHtmlSpecialChars('{"dateFormat":"d. F Y","timeFormat":""}') => $this->_formatDate('d. F Y'),
 75:                 conHtmlSpecialChars('{"dateFormat":"Y-m-d","timeFormat":""}') => $this->_formatDate('Y-m-d'),
 76:                 conHtmlSpecialChars('{"dateFormat":"d/F/Y","timeFormat":""}') => $this->_formatDate('d/F/Y'),
 77:                 conHtmlSpecialChars('{"dateFormat":"d/m/y","timeFormat":""}') => $this->_formatDate('d/m/y'),
 78:                 conHtmlSpecialChars('{"dateFormat":"F y","timeFormat":""}') => $this->_formatDate('F y'),
 79:                 conHtmlSpecialChars('{"dateFormat":"F-y","timeFormat":""}') => $this->_formatDate('F-y'),
 80:                 conHtmlSpecialChars('{"dateFormat":"d.m.Y","timeFormat":"H:i"}') => $this->_formatDate('d.m.Y H:i'),
 81:                 conHtmlSpecialChars('{"dateFormat":"m.d.Y","timeFormat":"H:i:s"}') => $this->_formatDate('m.d.Y H:i:s'),
 82:                 conHtmlSpecialChars('{"dateFormat":"","timeFormat":"H:i"}') => $this->_formatDate('H:i'),
 83:                 conHtmlSpecialChars('{"dateFormat":"","timeFormat":"H:i:s"}') => $this->_formatDate('H:i:s'),
 84:                 conHtmlSpecialChars('{"dateFormat":"","timeFormat":"h:i A"}') => $this->_formatDate('h:i A'),
 85:                 conHtmlSpecialChars('{"dateFormat":"","timeFormat":"h:i:s A"}') => $this->_formatDate('h:i:s A')
 86:         );
 87: 
 88:         // add formats from client settings
 89:         $additionalFormats = getEffectiveSettingsByType('cms_date');
 90:         foreach ($additionalFormats as $format) {
 91:             $formatArray = json_decode($format, true);
 92:             // ignore invalid formats
 93:             if (empty($formatArray) || count($formatArray) != 2 || !array_key_exists('dateFormat', $formatArray) || !array_key_exists('timeFormat', $formatArray)) {
 94:                 cWarning('An invalid date-time-format has been entered in the client settings.');
 95:                 continue;
 96:             }
 97:             $key = conHtmlSpecialChars($format);
 98:             $value = implode(' ', $formatArray);
 99:             $this->_dateFormatsPhp[$key] = $this->_formatDate($value);
100:         }
101: 
102:         // if form is submitted, store the current date settings
103:         // notice: also check the ID of the content type (there could be more
104:         // than one content type of the same type on the same page!)
105:         if (isset($_POST[$this->_prefix . '_action']) && $_POST[$this->_prefix . '_action'] === 'store' && isset($_POST[$this->_prefix . '_id']) && (int) $_POST[$this->_prefix . '_id'] == $this->_id) {
106:             // convert the given date string into a valid timestamp, so that a
107:             // timestamp is stored
108:             $_POST['date_format'] = stripslashes(base64_decode($_POST['date_format']));
109:             if (empty($_POST['date_format'])) {
110:                 $_POST['date_format'] = '{"dateFormat":"","timeFormat":""}';
111:             }
112:             $this->_storeSettings();
113:         }
114:     }
115: 
116:     /**
117:      * Formats the given timestamp according to the given format. Localises the
118:      * output.
119:      *
120:      * @param string $format the format string in the PHP date format
121:      * @param int $timestamp the timestamp representing the date which should be
122:      *            formatted
123:      * @return string the formatted, localised date
124:      */
125:     private function _formatDate($format, $timestamp = null) {
126:         $result = '';
127:         if ($timestamp === null) {
128:             $timestamp = time();
129:         }
130:         $replacements = array(
131:                 'd',
132:                 'D',
133:                 'j',
134:                 'l',
135:                 'N',
136:                 'S',
137:                 'w',
138:                 'z',
139:                 'W',
140:                 'F',
141:                 'm',
142:                 'M',
143:                 'n',
144:                 't',
145:                 'L',
146:                 'o',
147:                 'Y',
148:                 'y',
149:                 'a',
150:                 'A',
151:                 'B',
152:                 'g',
153:                 'G',
154:                 'h',
155:                 'H',
156:                 'i',
157:                 's',
158:                 'u',
159:                 'e',
160:                 'I',
161:                 'O',
162:                 'P',
163:                 'T',
164:                 'Z',
165:                 'c',
166:                 'r',
167:                 'U'
168:         );
169:         foreach (str_split($format) as $char) {
170:             if (in_array($char, $replacements)) {
171:                 // replace the format chars with localised values
172:                 switch ($char) {
173:                     case 'D':
174:                         $result .= strftime('%a', $timestamp);
175:                         break;
176:                     case 'l':
177:                         $result .= strftime('%A', $timestamp);
178:                         break;
179:                     case 'F':
180:                         $result .= strftime('%B', $timestamp);
181:                         break;
182:                     case 'M':
183:                         $result .= strftime('%b', $timestamp);
184:                         break;
185:                     default:
186:                         // use the default date() format if no localisation is
187:                         // needed
188:                         $result .= date($char, $timestamp);
189:                         break;
190:                 }
191:             } else {
192:                 // if this is not a format char, just add it to the result
193:                 // string
194:                 $result .= $char;
195:             }
196:         }
197: 
198:         return $result;
199:     }
200: 
201:     /**
202:      * Generates the code which should be shown if this content type is shown in
203:      * the frontend.
204:      *
205:      * @return string escaped HTML code which sould be shown if content type is
206:      *         shown in frontend
207:      */
208:     public function generateViewCode() {
209:         $format = $this->_settings['date_format'];
210: 
211:         if (empty($format)) {
212:             $format = '';
213:         } else {
214:             $decoded_array = json_decode($format, true);
215:             if (is_array($decoded_array)) {
216:                 $format = implode(' ', $decoded_array);
217:             } else {
218:                 $format = '';
219:             }
220:         }
221:         $timestamp = $this->_settings['date_timestamp'];
222:         if (empty($timestamp)) {
223:             return '';
224:         }
225: 
226:         return $this->_formatDate($format, $timestamp);
227:     }
228: 
229:     /**
230:      * Generates the code which should be shown if this content type is edited.
231:      *
232:      * @return string escaped HTML code which should be shown if content type is
233:      *         edited
234:      */
235:     public function generateEditCode() {
236:         $belang = cRegistry::getBackendLanguage();
237:         $format = 'Y-m-d h:i:sA';
238:         if ($belang == 'de_DE') {
239:             $format = 'd.m.Y H:i:s';
240:         }
241:         $value = date($format, $this->_settings['date_timestamp']);
242:         $code = new cHTMLTextbox('date_timestamp_' . $this->_id, $value, '', '', 'date_timestamp_' . $this->_id, true, '', '', 'date_timestamp');
243:         $code .= $this->_generateJavaScript();
244:         $code .= $this->_generateFormatSelect();
245:         $code .= $this->_generateStoreButton();
246:         $code = new cHTMLDiv($code, 'cms_date', 'cms_' . $this->_prefix . '_' . $this->_id . '_settings');
247: 
248:         return $this->_encodeForOutput($code);
249:     }
250: 
251:     /**
252:      * Generates the JavaScript needed for CMS_DATE.
253:      *
254:      * @return string HTML code which includes the needed JavaScript
255:      */
256:     private function _generateJavaScript() {
257:         $template = new cTemplate();
258:         $pathBackend = $this->_cfg['path']['contenido_fullhtml'];
259: 
260:         $template->set('s', 'PREFIX', $this->_prefix);
261:         $template->set('s', 'ID', $this->_id);
262:         $template->set('s', 'IDARTLANG', $this->_idArtLang);
263:         $template->set('s', 'PATH_BACKEND', $pathBackend);
264:         $template->set('s', 'LANG', substr(cRegistry::getBackendLanguage(), 0, 2));
265:         $template->set('s', 'PATH_TO_CALENDAR_PIC', $pathBackend . $this->_cfg['path']['images'] . 'calendar.gif');
266:         $setting = $this->_settings;
267:         if (array_key_exists('date_format', $setting)) {
268:             $setting['date_format'] = json_decode($setting['date_format'], true);
269:         }
270:         $template->set('s', 'SETTINGS', json_encode($setting));
271:         $template->set('s', 'BELANG', cRegistry::getBackendLanguage());
272: 
273:         return $template->generate($this->_cfg['path']['contenido'] . 'templates/standard/template.cms_date.html', true);
274:     }
275: 
276:     /**
277:      * Generates the save button.
278:      *
279:      * @return string HTML code for the save button
280:      */
281:     private function _generateStoreButton() {
282:         $saveButton = new cHTMLImage($this->_cfg['path']['contenido_fullhtml'] . $this->_cfg['path']['images'] . 'but_ok.gif', 'save_settings');
283: 
284:         return $saveButton->render();
285:     }
286: 
287:     /**
288:      * Generates a select box for defining the format of the date.
289:      *
290:      * @return string the HTML code of the format select box
291:      */
292:     private function _generateFormatSelect() {
293:         $formatSelect = new cHTMLSelectElement($this->_prefix . '_format_select_' . $this->_id, '', $this->_prefix . '_format_select_' . $this->_id);
294:         $formatSelect->appendStyleDefinitions(array(
295:                 'border' => '1px solid #ccc',
296:                 'margin' => '2px 5px 5px'
297:         ));
298:         $formatSelect->autoFill($this->_dateFormatsPhp);
299:         $phpDateFormat = conHtmlSpecialChars($this->_settings[$this->_prefix . '_format']);
300:         $formatSelect->setDefault($phpDateFormat);
301: 
302:         return $formatSelect->render();
303:     }
304: 
305: }
CMS CONTENIDO 4.9.0 API documentation generated by ApiGen 2.8.0