Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • PHP
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • 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
  • cContentTypeRaw
  • cContentTypeTeaser
  • cContentTypeText
  • cTypeGenerator
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: /**
  4:  * This file contains the cContentTypeDate class.
  5:  *
  6:  * @package Core
  7:  * @subpackage ContentType
  8:  * @author Bilal Arslan
  9:  * @author Timo Trautmann
 10:  * @author Simon Sprankel
 11:  * @copyright four for business AG <www.4fb.de>
 12:  * @license http://www.contenido.org/license/LIZENZ.txt
 13:  * @link http://www.4fb.de
 14:  * @link http://www.contenido.org
 15:  */
 16: 
 17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 18: 
 19: /**
 20:  * Content type CMS_DATE which allows the editor to select a date from a
 21:  * calendar and a date format.
 22:  * The selected date is then shown in the selected
 23:  * format.
 24:  *
 25:  * @package Core
 26:  * @subpackage ContentType
 27:  */
 28: class cContentTypeDate extends cContentTypeAbstract {
 29: 
 30:     /**
 31:      * The possible PHP date formats in which the selected date can be
 32:      * displayed.
 33:      *
 34:      * @var array
 35:      */
 36:     private $_dateFormatsPhp;
 37: 
 38:     /**
 39:      * Constructor to create an instance of this class.
 40:      *
 41:      * Initialises class attributes and handles store events.
 42:      *
 43:      * @param string $rawSettings
 44:      *         the raw settings in an XML structure or as plaintext
 45:      * @param int    $id
 46:      *         ID of the content type, e.g. 3 if CMS_DATE[3] is used
 47:      * @param array  $contentTypes
 48:      *         array containing the values of all content types
 49:      *
 50:      * @throws cDbException
 51:      * @throws cException
 52:      */
 53:     public function __construct($rawSettings, $id, array $contentTypes) {
 54: 
 55:         // set props
 56:         $this->_type = 'CMS_DATE';
 57:         $this->_prefix = 'date';
 58:         $this->_settingsType = self::SETTINGS_TYPE_XML;
 59:         $this->_formFields = array(
 60:             'date_timestamp',
 61:             'date_format'
 62:         );
 63: 
 64:         // call parent constructor
 65:         parent::__construct($rawSettings, $id, $contentTypes);
 66: 
 67:         // set the locale
 68:         $locale = cRegistry::getBackendLanguage();
 69:         if (empty($locale)
 70:         || false === setlocale(LC_TIME, $locale)) {
 71:             $oApiLang = new cApiLanguage(cRegistry::getLanguageId());
 72:             $locale = $oApiLang->getProperty('dateformat', 'locale');
 73:             if (empty($locale)) {
 74:                 $language = $oApiLang->getProperty('language', 'code');
 75:                 $country = $oApiLang->getProperty('country', 'code');
 76: 
 77:                 $locale = $language . '_' . cString::toUpperCase($country);
 78:             }
 79:             if (false === empty($locale)) {
 80:                 setlocale(LC_TIME, $locale);
 81:             }
 82:         }
 83: 
 84:         // initialise the date formats
 85:         $this->_dateFormatsPhp = array(
 86:             conHtmlentities('{"dateFormat":"","timeFormat":""}') => '',
 87:             conHtmlentities('{"dateFormat":"d.m.Y","timeFormat":""}') => $this->_formatDate('d.m.Y'),
 88:             conHtmlentities('{"dateFormat":"D, d.m.Y","timeFormat":""}') => $this->_formatDate('D, d.m.Y'),
 89:             conHtmlentities('{"dateFormat":"d. F Y","timeFormat":""}') => $this->_formatDate('d. F Y'),
 90:             conHtmlentities('{"dateFormat":"Y-m-d","timeFormat":""}') => $this->_formatDate('Y-m-d'),
 91:             conHtmlentities('{"dateFormat":"d/F/Y","timeFormat":""}') => $this->_formatDate('d/F/Y'),
 92:             conHtmlentities('{"dateFormat":"d/m/y","timeFormat":""}') => $this->_formatDate('d/m/y'),
 93:             conHtmlentities('{"dateFormat":"F y","timeFormat":""}') => $this->_formatDate('F y'),
 94:             conHtmlentities('{"dateFormat":"F-y","timeFormat":""}') => $this->_formatDate('F-y'),
 95:             conHtmlentities('{"dateFormat":"d.m.Y","timeFormat":"H:i"}') => $this->_formatDate('d.m.Y H:i'),
 96:             conHtmlentities('{"dateFormat":"m.d.Y","timeFormat":"H:i:s"}') => $this->_formatDate('m.d.Y H:i:s'),
 97:             conHtmlentities('{"dateFormat":"","timeFormat":"H:i"}') => $this->_formatDate('H:i'),
 98:             conHtmlentities('{"dateFormat":"","timeFormat":"H:i:s"}') => $this->_formatDate('H:i:s'),
 99:             conHtmlentities('{"dateFormat":"","timeFormat":"h:i A"}') => $this->_formatDate('h:i A'),
100:             conHtmlentities('{"dateFormat":"","timeFormat":"h:i:s A"}') => $this->_formatDate('h:i:s A')
101:         );
102: 
103:         // add formats from client settings
104:         $additionalFormats = getEffectiveSettingsByType('cms_date');
105:         foreach ($additionalFormats as $format) {
106:             $formatArray = json_decode($format, true);
107:             // ignore invalid formats
108:             if (empty($formatArray) || count($formatArray) != 2 || !array_key_exists('dateFormat', $formatArray) || !array_key_exists('timeFormat', $formatArray)) {
109:                 cWarning('An invalid date-time-format has been entered in the client settings.');
110:                 continue;
111:             }
112:             $key = conHtmlSpecialChars($format);
113:             $value = implode(' ', $formatArray);
114:             $this->_dateFormatsPhp[$key] = $this->_formatDate($value);
115:         }
116: 
117:         // if form is submitted, store the current date settings
118:         // notice: also check the ID of the content type (there could be more
119:         // than one content type of the same type on the same page!)
120:         if (isset($_POST[$this->_prefix . '_action']) && $_POST[$this->_prefix . '_action'] === 'store' && isset($_POST[$this->_prefix . '_id']) && (int) $_POST[$this->_prefix . '_id'] == $this->_id) {
121:             // convert the given date string into a valid timestamp, so that a
122:             // timestamp is stored
123:             //CON-2049 additional check for base64 strings
124:             if (!empty($_POST['date_format']) && base64_encode(base64_decode($_POST['date_format'])) === $_POST['date_format']) {
125:                 $_POST['date_format'] = stripslashes(base64_decode($_POST['date_format']));
126:             } else { // if no date_format is given, set standard value
127:                 $_POST['date_format'] = '{"dateFormat":"","timeFormat":""}';
128:             }
129: 
130:             $this->_storeSettings();
131:         }
132: 
133:         // CON-2049
134:         // reset specific date variable
135:         // $_POST[$this->_prefix . '_action'] = '';
136:         // $_POST['date_format'] = '';
137:     }
138: 
139:     /**
140:      * Returns the displayed timestamp
141:      *
142:      * @return string
143:      */
144:     public function getDateTimestamp() {
145:         return $this->_settings['date_timestamp'];
146:     }
147: 
148:     /**
149:      * Returns the full PHP style format string
150:      *
151:      * @return string
152:      */
153:     public function getDateFormat() {
154:         $format = $this->_settings['date_format'];
155: 
156:         if (empty($format)) {
157:             $format = '';
158:         } else {
159:             $decoded_array = json_decode($format, true);
160:             if (is_array($decoded_array)) {
161:                 $format = implode(' ', $decoded_array);
162:             } else {
163:                 $format = '';
164:             }
165:         }
166: 
167:         return $format;
168:     }
169: 
170:     /**
171:      * Returns only the time portion of the PHP style format string
172:      *
173:      * @return string
174:      */
175:     public function getTimeFormat() {
176:         $format = $this->_settings['date_format'];
177: 
178:         if (empty($format)) {
179:             $format = '';
180:         } else {
181:             $decoded_array = json_decode($format, true);
182:             if (is_array($decoded_array)) {
183:                 return $decoded_array['timeFormat'];
184:             } else {
185:                 return '';
186:             }
187:         }
188: 
189:         return $format;
190:     }
191: 
192:     /**
193:      * Formats the given timestamp according to the given format.
194:      * Localises the output.
195:      *
196:      * @param string $format
197:      *         the format string in the PHP date format
198:      * @param int $timestamp [optional]
199:      *         the timestamp representing the date which should be formatted
200:      * @return string
201:      *         the formatted, localised date
202:      */
203:     private function _formatDate($format, $timestamp = NULL) {
204:         $result = '';
205:         if ($timestamp === NULL) {
206:             $timestamp = time();
207:         }
208:         $replacements = array(
209:             'd',
210:             'D',
211:             'j',
212:             'l',
213:             'N',
214:             'S',
215:             'w',
216:             'z',
217:             'W',
218:             'F',
219:             'm',
220:             'M',
221:             'n',
222:             't',
223:             'L',
224:             'o',
225:             'Y',
226:             'y',
227:             'a',
228:             'A',
229:             'B',
230:             'g',
231:             'G',
232:             'h',
233:             'H',
234:             'i',
235:             's',
236:             'u',
237:             'e',
238:             'I',
239:             'O',
240:             'P',
241:             'T',
242:             'Z',
243:             'c',
244:             'r',
245:             'U'
246:         );
247:         foreach (str_split($format) as $char) {
248:             if (in_array($char, $replacements)) {
249:                 // replace the format chars with localised values
250:                 switch ($char) {
251:                     case 'D':
252:                         $result .= strftime('%a', $timestamp);
253:                         break;
254:                     case 'l':
255:                         $result .= strftime('%A', $timestamp);
256:                         break;
257:                     case 'F':
258:                         $result .= strftime('%B', $timestamp);
259:                         break;
260:                     case 'M':
261:                         $result .= strftime('%b', $timestamp);
262:                         break;
263:                     default:
264:                         // use the default date() format if no localisation is
265:                         // needed
266:                         $result .= date($char, $timestamp);
267:                         break;
268:                 }
269:             } else {
270:                 // if this is not a format char, just add it to the result
271:                 // string
272:                 $result .= $char;
273:             }
274:         }
275: 
276:         // strftime returns a string in an encoding that is specified by the locale
277:         // use iconv extension to get the content encoding of string
278:         // use mbstring extension to convert encoding to contenido's target encoding
279:         if (extension_loaded('iconv') && extension_loaded('mbstring')) {
280:             $result = mb_convert_encoding($result, cRegistry::getEncoding(), iconv_get_encoding('output_encoding'));
281:             $result = conHtmlentities($result);
282:         }
283: 
284:         return $result;
285:     }
286: 
287:     /**
288:      * Generates the code which should be shown if this content type is shown in
289:      * the frontend.
290:      *
291:      * @return string
292:      *         escaped HTML code which should be shown if content type is shown in frontend
293:      */
294:     public function generateViewCode() {
295:         if (empty($this->_settings['date_timestamp'])) {
296:             return '';
297:         }
298: 
299:         $timestamp = $this->_settings['date_timestamp'];
300: 
301:         if (empty($this->_settings['date_format'])) {
302:             $format = '';
303:         } else {
304:             $format = $this->_settings['date_format'];
305:             $decoded_array = json_decode($format, true);
306:             if (is_array($decoded_array)) {
307:                 $format = implode(' ', $decoded_array);
308:             } else {
309:                 $format = '';
310:             }
311:         }
312: 
313:         return $this->_formatDate($format, $timestamp);
314:     }
315: 
316:     /**
317:      * Generates the code which should be shown if this content type is edited.
318:      *
319:      * @return string
320:      *         escaped HTML code which should be shown if content type is edited
321:      * @throws cInvalidArgumentException
322:      */
323:     public function generateEditCode() {
324:         $belang = cRegistry::getBackendLanguage();
325:         $format = 'Y-m-d h:i:sA';
326:         if ($belang == 'de_DE') {
327:             $format = 'd.m.Y H:i:s';
328:         }
329:         $value = date($format, $this->_settings['date_timestamp']);
330:         $code = new cHTMLTextbox('date_timestamp_' . $this->_id, $value, '', '', 'date_timestamp_' . $this->_id, true, '', '', 'date_timestamp');
331:         $code .= $this->_generateFormatSelect();
332:         $code .= $this->_generateStoreButton();
333:         $code .= $this->_generateJavaScript();
334:         $code = new cHTMLDiv($code, 'cms_date', 'cms_' . $this->_prefix . '_' . $this->_id . '_settings');
335: 
336:         return $this->_encodeForOutput($code);
337:     }
338: 
339:     /**
340:      * Generates the JavaScript needed for CMS_DATE.
341:      *
342:      * @return string
343:      *         HTML code which includes the needed JavaScript
344:      * @throws cInvalidArgumentException
345:      */
346:     private function _generateJavaScript() {
347:         $template = new cTemplate();
348:         $pathBackend = $this->_cfg['path']['contenido_fullhtml'];
349: 
350:         $template->set('s', 'PREFIX', $this->_prefix);
351:         $template->set('s', 'ID', $this->_id);
352:         $template->set('s', 'IDARTLANG', $this->_idArtLang);
353:         $template->set('s', 'LANG', cString::getPartOfString(cRegistry::getBackendLanguage(), 0, 2));
354:         $template->set('s', 'PATH_TO_CALENDAR_PIC', $pathBackend . $this->_cfg['path']['images'] . 'calendar.gif');
355:         $setting = $this->_settings;
356:         if (array_key_exists('date_format', $setting)) {
357:             $setting['date_format'] = json_decode($setting['date_format'], true);
358:         }
359:         $template->set('s', 'SETTINGS', json_encode($setting));
360:         $template->set('s', 'BELANG', cRegistry::getBackendLanguage());
361: 
362:         return $template->generate($this->_cfg['path']['contenido'] . 'templates/standard/template.cms_date.html', true);
363:     }
364: 
365:     /**
366:      * Generates the save button.
367:      *
368:      * @return string
369:      *         HTML code for the save button
370:      */
371:     private function _generateStoreButton() {
372:         $saveButton = new cHTMLImage($this->_cfg['path']['contenido_fullhtml'] . $this->_cfg['path']['images'] . 'but_ok.gif', 'save_settings');
373: 
374:         return $saveButton->render();
375:     }
376: 
377:     /**
378:      * Generates a select box for defining the format of the date.
379:      *
380:      * @return string
381:      *         the HTML code of the format select box
382:      */
383:     private function _generateFormatSelect() {
384:         $formatSelect = new cHTMLSelectElement($this->_prefix . '_format_select_' . $this->_id, '', $this->_prefix . '_format_select_' . $this->_id);
385:         $formatSelect->appendStyleDefinitions(array(
386:             'border' => '1px solid #ccc',
387:             'margin' => '0px 5px 5px'
388:         ));
389:         $formatSelect->autoFill($this->_dateFormatsPhp);
390:         $phpDateFormat = conHtmlSpecialChars($this->_settings[$this->_prefix . '_format']);
391:         $formatSelect->setDefault($phpDateFormat);
392: 
393:         return $formatSelect->render();
394:     }
395: 
396: }
397: 
CMS CONTENIDO 4.10.0 API documentation generated by ApiGen 2.8.0