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
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • 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
  • 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:      * Initialises class attributes and handles store events.
 40:      *
 41:      * @param string $rawSettings
 42:      *         the raw settings in an XML structure or as plaintext
 43:      * @param int $id
 44:      *         ID of the content type, e.g. 3 if CMS_DATE[3] is used
 45:      * @param array $contentTypes
 46:      *         array containing the values of all content types
 47:      */
 48:     public function __construct($rawSettings, $id, array $contentTypes) {
 49: 
 50:         // set props
 51:         $this->_type = 'CMS_DATE';
 52:         $this->_prefix = 'date';
 53:         $this->_settingsType = self::SETTINGS_TYPE_XML;
 54:         $this->_formFields = array(
 55:             'date_timestamp',
 56:             'date_format'
 57:         );
 58: 
 59:         // call parent constructor
 60:         parent::__construct($rawSettings, $id, $contentTypes);
 61: 
 62:         // set the locale
 63:         $locale = cRegistry::getBackendLanguage();
 64:         if (empty($locale)
 65:         || false === setlocale(LC_TIME, $locale)) {
 66:             $oApiLang = new cApiLanguage(cRegistry::getLanguageId());
 67:             $locale = $oApiLang->getProperty('dateformat', 'locale');
 68:             if (empty($locale)) {
 69:                 $language = $oApiLang->getProperty('language', 'code');
 70:                 $country = $oApiLang->getProperty('country', 'code');
 71: 
 72:                 $locale = $language . '_' . strtoupper($country);
 73:             }
 74:             if (false === empty($locale)) {
 75:                 setlocale(LC_TIME, $locale);
 76:             }
 77:         }
 78: 
 79:         // initialise the date formats
 80:         $this->_dateFormatsPhp = array(
 81:             conHtmlentities('{"dateFormat":"","timeFormat":""}') => '',
 82:             conHtmlentities('{"dateFormat":"d.m.Y","timeFormat":""}') => $this->_formatDate('d.m.Y'),
 83:             conHtmlentities('{"dateFormat":"D, d.m.Y","timeFormat":""}') => $this->_formatDate('D, d.m.Y'),
 84:             conHtmlentities('{"dateFormat":"d. F Y","timeFormat":""}') => $this->_formatDate('d. F Y'),
 85:             conHtmlentities('{"dateFormat":"Y-m-d","timeFormat":""}') => $this->_formatDate('Y-m-d'),
 86:             conHtmlentities('{"dateFormat":"d/F/Y","timeFormat":""}') => $this->_formatDate('d/F/Y'),
 87:             conHtmlentities('{"dateFormat":"d/m/y","timeFormat":""}') => $this->_formatDate('d/m/y'),
 88:             conHtmlentities('{"dateFormat":"F y","timeFormat":""}') => $this->_formatDate('F y'),
 89:             conHtmlentities('{"dateFormat":"F-y","timeFormat":""}') => $this->_formatDate('F-y'),
 90:             conHtmlentities('{"dateFormat":"d.m.Y","timeFormat":"H:i"}') => $this->_formatDate('d.m.Y H:i'),
 91:             conHtmlentities('{"dateFormat":"m.d.Y","timeFormat":"H:i:s"}') => $this->_formatDate('m.d.Y H:i:s'),
 92:             conHtmlentities('{"dateFormat":"","timeFormat":"H:i"}') => $this->_formatDate('H:i'),
 93:             conHtmlentities('{"dateFormat":"","timeFormat":"H:i:s"}') => $this->_formatDate('H:i:s'),
 94:             conHtmlentities('{"dateFormat":"","timeFormat":"h:i A"}') => $this->_formatDate('h:i A'),
 95:             conHtmlentities('{"dateFormat":"","timeFormat":"h:i:s A"}') => $this->_formatDate('h:i:s A')
 96:         );
 97: 
 98:         // add formats from client settings
 99:         $additionalFormats = getEffectiveSettingsByType('cms_date');
100:         foreach ($additionalFormats as $format) {
101:             $formatArray = json_decode($format, true);
102:             // ignore invalid formats
103:             if (empty($formatArray) || count($formatArray) != 2 || !array_key_exists('dateFormat', $formatArray) || !array_key_exists('timeFormat', $formatArray)) {
104:                 cWarning('An invalid date-time-format has been entered in the client settings.');
105:                 continue;
106:             }
107:             $key = conHtmlSpecialChars($format);
108:             $value = implode(' ', $formatArray);
109:             $this->_dateFormatsPhp[$key] = $this->_formatDate($value);
110:         }
111: 
112:         // if form is submitted, store the current date settings
113:         // notice: also check the ID of the content type (there could be more
114:         // than one content type of the same type on the same page!)
115:         if (isset($_POST[$this->_prefix . '_action']) && $_POST[$this->_prefix . '_action'] === 'store' && isset($_POST[$this->_prefix . '_id']) && (int) $_POST[$this->_prefix . '_id'] == $this->_id) {
116:             // convert the given date string into a valid timestamp, so that a
117:             // timestamp is stored
118:             //CON-2049 additional check for base64 strings
119:             if (!empty($_POST['date_format']) && base64_encode(base64_decode($_POST['date_format'])) === $_POST['date_format']) {
120:                 $_POST['date_format'] = stripslashes(base64_decode($_POST['date_format']));
121:             } else { // if no date_format is given, set standard value
122:                 $_POST['date_format'] = '{"dateFormat":"","timeFormat":""}';
123:             }
124: 
125:             $this->_storeSettings();
126:         }
127: 
128:         // CON-2049
129:         // reset specific date variable
130:         // $_POST[$this->_prefix . '_action'] = '';
131:         // $_POST['date_format'] = '';
132:     }
133: 
134:     /**
135:      * Returns the displayed timestamp
136:      *
137:      * @return string
138:      */
139:     public function getDateTimestamp() {
140:         return $this->_settings['date_timestamp'];
141:     }
142: 
143:     /**
144:      * Returns the full PHP style format string
145:      *
146:      * @return string
147:      */
148:     public function getDateFormat() {
149:         $format = $this->_settings['date_format'];
150: 
151:         if (empty($format)) {
152:             $format = '';
153:         } else {
154:             $decoded_array = json_decode($format, true);
155:             if (is_array($decoded_array)) {
156:                 $format = implode(' ', $decoded_array);
157:             } else {
158:                 $format = '';
159:             }
160:         }
161: 
162:         return $format;
163:     }
164: 
165:     /**
166:      * Returns only the time portion of the PHP style format string
167:      *
168:      * @return string
169:      */
170:     public function getTimeFormat() {
171:         $format = $this->_settings['date_format'];
172: 
173:         if (empty($format)) {
174:             $format = '';
175:         } else {
176:             $decoded_array = json_decode($format, true);
177:             if (is_array($decoded_array)) {
178:                 return $decoded_array['timeFormat'];
179:             } else {
180:                 return '';
181:             }
182:         }
183: 
184:         return $format;
185:     }
186: 
187:     /**
188:      * Formats the given timestamp according to the given format.
189:      * Localises the output.
190:      *
191:      * @param string $format
192:      *         the format string in the PHP date format
193:      * @param int $timestamp [optional]
194:      *         the timestamp representing the date which should be formatted
195:      * @return string
196:      *         the formatted, localised date
197:      */
198:     private function _formatDate($format, $timestamp = NULL) {
199:         $result = '';
200:         if ($timestamp === NULL) {
201:             $timestamp = time();
202:         }
203:         $replacements = array(
204:             'd',
205:             'D',
206:             'j',
207:             'l',
208:             'N',
209:             'S',
210:             'w',
211:             'z',
212:             'W',
213:             'F',
214:             'm',
215:             'M',
216:             'n',
217:             't',
218:             'L',
219:             'o',
220:             'Y',
221:             'y',
222:             'a',
223:             'A',
224:             'B',
225:             'g',
226:             'G',
227:             'h',
228:             'H',
229:             'i',
230:             's',
231:             'u',
232:             'e',
233:             'I',
234:             'O',
235:             'P',
236:             'T',
237:             'Z',
238:             'c',
239:             'r',
240:             'U'
241:         );
242:         foreach (str_split($format) as $char) {
243:             if (in_array($char, $replacements)) {
244:                 // replace the format chars with localised values
245:                 switch ($char) {
246:                     case 'D':
247:                         $result .= strftime('%a', $timestamp);
248:                         break;
249:                     case 'l':
250:                         $result .= strftime('%A', $timestamp);
251:                         break;
252:                     case 'F':
253:                         $result .= strftime('%B', $timestamp);
254:                         break;
255:                     case 'M':
256:                         $result .= strftime('%b', $timestamp);
257:                         break;
258:                     default:
259:                         // use the default date() format if no localisation is
260:                         // needed
261:                         $result .= date($char, $timestamp);
262:                         break;
263:                 }
264:             } else {
265:                 // if this is not a format char, just add it to the result
266:                 // string
267:                 $result .= $char;
268:             }
269:         }
270: 
271:         // strftime returns a string in an encoding that is specified by the locale
272:         // use iconv extension to get the content encoding of string
273:         // use mbstring extension to convert encoding to contenido's target encoding
274:         if (extension_loaded('iconv') && extension_loaded('mbstring')) {
275:             $result = mb_convert_encoding($result, cRegistry::getEncoding(), iconv_get_encoding('output_encoding'));
276:             $result = conHtmlentities($result);
277:         }
278: 
279:         // strftime returns a string in an encoding that is specified by the locale
280:         // use iconv extension to get the content encoding of string
281:         // use mbstring extension to convert encoding to contenido's target encoding
282:         if (extension_loaded('iconv') && extension_loaded('mbstring')) {
283:             $result = mb_convert_encoding($result, cRegistry::getEncoding(), iconv_get_encoding('output_encoding'));
284:             $result = conHtmlentities($result);
285:         }
286: 
287:         return $result;
288:     }
289: 
290:     /**
291:      * Generates the code which should be shown if this content type is shown in
292:      * the frontend.
293:      *
294:      * @return string
295:      *         escaped HTML code which sould be shown if content type is shown in frontend
296:      */
297:     public function generateViewCode() {
298:         $format = $this->_settings['date_format'];
299: 
300:         if (empty($format)) {
301:             $format = '';
302:         } else {
303:             $decoded_array = json_decode($format, true);
304:             if (is_array($decoded_array)) {
305:                 $format = implode(' ', $decoded_array);
306:             } else {
307:                 $format = '';
308:             }
309:         }
310:         $timestamp = $this->_settings['date_timestamp'];
311:         if (empty($timestamp)) {
312:             return '';
313:         }
314: 
315:         return $this->_formatDate($format, $timestamp);
316:     }
317: 
318:     /**
319:      * Generates the code which should be shown if this content type is edited.
320:      *
321:      * @return string
322:      *         escaped HTML code which should be shown if content type is edited
323:      */
324:     public function generateEditCode() {
325:         $belang = cRegistry::getBackendLanguage();
326:         $format = 'Y-m-d h:i:sA';
327:         if ($belang == 'de_DE') {
328:             $format = 'd.m.Y H:i:s';
329:         }
330:         $value = date($format, $this->_settings['date_timestamp']);
331:         $code = new cHTMLTextbox('date_timestamp_' . $this->_id, $value, '', '', 'date_timestamp_' . $this->_id, true, '', '', 'date_timestamp');
332:         $code .= $this->_generateJavaScript();
333:         $code .= $this->_generateFormatSelect();
334:         $code .= $this->_generateStoreButton();
335:         $code = new cHTMLDiv($code, 'cms_date', 'cms_' . $this->_prefix . '_' . $this->_id . '_settings');
336: 
337:         return $this->_encodeForOutput($code);
338:     }
339: 
340:     /**
341:      * Generates the JavaScript needed for CMS_DATE.
342:      *
343:      * @return string
344:      *         HTML code which includes the needed JavaScript
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', substr(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' => '2px 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.9.8 API documentation generated by ApiGen 2.8.0