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