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
    • 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
  • cContentTypeRaw
  • 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.
 20:  * The selected date is then shown in the selected
 21:  * format.
 22:  *
 23:  * @package Core
 24:  * @subpackage ContentType
 25:  */
 26: class cContentTypeDate extends cContentTypeAbstract {
 27: 
 28:     /**
 29:      * The possible PHP date formats in which the selected date can be
 30:      * displayed.
 31:      *
 32:      * @var array
 33:      */
 34:     private $_dateFormatsPhp;
 35: 
 36:     /**
 37:      * Initialises class attributes and handles store events.
 38:      *
 39:      * @param string $rawSettings the raw settings in an XML structure or as
 40:      *        plaintext
 41:      * @param int $id ID of the content type, e.g. 3 if CMS_DATE[3] is used
 42:      * @param array $contentTypes array containing the values of all content
 43:      *        types
 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 = self::SETTINGS_TYPE_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:             if (!empty($_POST['date_format'])) {
109:                 $_POST['date_format'] = stripslashes(base64_decode($_POST['date_format']));
110:             } else { // if no date_format is given, set standard value
111:                 $_POST['date_format'] = '{"dateFormat":"","timeFormat":""}';
112:             }
113:             $this->_storeSettings();
114:         }
115: 
116:         // reset specific date variable
117:         $_POST[$this->_prefix . '_action'] = '';
118:         $_POST['date_format'] = '';
119:     }
120: 
121:     /**
122:      * Returns the displayed timestamp
123:      *
124:      * @return string
125:      */
126:     public function getDateTimestamp() {
127:         return $this->_settings['date_timestamp'];
128:     }
129: 
130:     /**
131:      * Returns the full PHP style format string
132:      *
133:      * @return string
134:      */
135:     public function getDateFormat() {
136:         $format = $this->_settings['date_format'];
137: 
138:         if (empty($format)) {
139:             $format = '';
140:         } else {
141:             $decoded_array = json_decode($format, true);
142:             if (is_array($decoded_array)) {
143:                 $format = implode(' ', $decoded_array);
144:             } else {
145:                 $format = '';
146:             }
147:         }
148: 
149:         return $format;
150:     }
151: 
152:     /**
153:      * Returns only the time portion of the PHP style format string
154:      *
155:      * @return string
156:      */
157:     public function getTimeFormat() {
158:         $format = $this->_settings['date_format'];
159: 
160:         if (empty($format)) {
161:             $format = '';
162:         } else {
163:             $decoded_array = json_decode($format, true);
164:             if (is_array($decoded_array)) {
165:                 return $decoded_array['timeFormat'];
166:             } else {
167:                 return '';
168:             }
169:         }
170: 
171:         return $format;
172:     }
173: 
174:     /**
175:      * Formats the given timestamp according to the given format.
176:      * Localises the
177:      * output.
178:      *
179:      * @param string $format the format string in the PHP date format
180:      * @param int $timestamp the timestamp representing the date which should be
181:      *        formatted
182:      * @return string the formatted, localised date
183:      */
184:     private function _formatDate($format, $timestamp = NULL) {
185:         $result = '';
186:         if ($timestamp === NULL) {
187:             $timestamp = time();
188:         }
189:         $replacements = array(
190:             'd',
191:             'D',
192:             'j',
193:             'l',
194:             'N',
195:             'S',
196:             'w',
197:             'z',
198:             'W',
199:             'F',
200:             'm',
201:             'M',
202:             'n',
203:             't',
204:             'L',
205:             'o',
206:             'Y',
207:             'y',
208:             'a',
209:             'A',
210:             'B',
211:             'g',
212:             'G',
213:             'h',
214:             'H',
215:             'i',
216:             's',
217:             'u',
218:             'e',
219:             'I',
220:             'O',
221:             'P',
222:             'T',
223:             'Z',
224:             'c',
225:             'r',
226:             'U'
227:         );
228:         foreach (str_split($format) as $char) {
229:             if (in_array($char, $replacements)) {
230:                 // replace the format chars with localised values
231:                 switch ($char) {
232:                     case 'D':
233:                         $result .= strftime('%a', $timestamp);
234:                         break;
235:                     case 'l':
236:                         $result .= strftime('%A', $timestamp);
237:                         break;
238:                     case 'F':
239:                         $result .= strftime('%B', $timestamp);
240:                         break;
241:                     case 'M':
242:                         $result .= strftime('%b', $timestamp);
243:                         break;
244:                     default:
245:                         // use the default date() format if no localisation is
246:                         // needed
247:                         $result .= date($char, $timestamp);
248:                         break;
249:                 }
250:             } else {
251:                 // if this is not a format char, just add it to the result
252:                 // string
253:                 $result .= $char;
254:             }
255:         }
256: 
257:         return $result;
258:     }
259: 
260:     /**
261:      * Generates the code which should be shown if this content type is shown in
262:      * the frontend.
263:      *
264:      * @return string escaped HTML code which sould be shown if content type is
265:      *         shown in frontend
266:      */
267:     public function generateViewCode() {
268:         $format = $this->_settings['date_format'];
269: 
270:         if (empty($format)) {
271:             $format = '';
272:         } else {
273:             $decoded_array = json_decode($format, true);
274:             if (is_array($decoded_array)) {
275:                 $format = implode(' ', $decoded_array);
276:             } else {
277:                 $format = '';
278:             }
279:         }
280:         $timestamp = $this->_settings['date_timestamp'];
281:         if (empty($timestamp)) {
282:             return '';
283:         }
284: 
285:         return $this->_formatDate($format, $timestamp);
286:     }
287: 
288:     /**
289:      * Generates the code which should be shown if this content type is edited.
290:      *
291:      * @return string escaped HTML code which should be shown if content type is
292:      *         edited
293:      */
294:     public function generateEditCode() {
295:         $belang = cRegistry::getBackendLanguage();
296:         $format = 'Y-m-d h:i:sA';
297:         if ($belang == 'de_DE') {
298:             $format = 'd.m.Y H:i:s';
299:         }
300:         $value = date($format, $this->_settings['date_timestamp']);
301:         $code = new cHTMLTextbox('date_timestamp_' . $this->_id, $value, '', '', 'date_timestamp_' . $this->_id, true, '', '', 'date_timestamp');
302:         $code .= $this->_generateJavaScript();
303:         $code .= $this->_generateFormatSelect();
304:         $code .= $this->_generateStoreButton();
305:         $code = new cHTMLDiv($code, 'cms_date', 'cms_' . $this->_prefix . '_' . $this->_id . '_settings');
306: 
307:         return $this->_encodeForOutput($code);
308:     }
309: 
310:     /**
311:      * Generates the JavaScript needed for CMS_DATE.
312:      *
313:      * @return string HTML code which includes the needed JavaScript
314:      */
315:     private function _generateJavaScript() {
316:         $template = new cTemplate();
317:         $pathBackend = $this->_cfg['path']['contenido_fullhtml'];
318: 
319:         $template->set('s', 'PREFIX', $this->_prefix);
320:         $template->set('s', 'ID', $this->_id);
321:         $template->set('s', 'IDARTLANG', $this->_idArtLang);
322:         $template->set('s', 'LANG', substr(cRegistry::getBackendLanguage(), 0, 2));
323:         $template->set('s', 'PATH_TO_CALENDAR_PIC', $pathBackend . $this->_cfg['path']['images'] . 'calendar.gif');
324:         $setting = $this->_settings;
325:         if (array_key_exists('date_format', $setting)) {
326:             $setting['date_format'] = json_decode($setting['date_format'], true);
327:         }
328:         $template->set('s', 'SETTINGS', json_encode($setting));
329:         $template->set('s', 'BELANG', cRegistry::getBackendLanguage());
330: 
331:         return $template->generate($this->_cfg['path']['contenido'] . 'templates/standard/template.cms_date.html', true);
332:     }
333: 
334:     /**
335:      * Generates the save button.
336:      *
337:      * @return string HTML code for the save button
338:      */
339:     private function _generateStoreButton() {
340:         $saveButton = new cHTMLImage($this->_cfg['path']['contenido_fullhtml'] . $this->_cfg['path']['images'] . 'but_ok.gif', 'save_settings');
341: 
342:         return $saveButton->render();
343:     }
344: 
345:     /**
346:      * Generates a select box for defining the format of the date.
347:      *
348:      * @return string the HTML code of the format select box
349:      */
350:     private function _generateFormatSelect() {
351:         $formatSelect = new cHTMLSelectElement($this->_prefix . '_format_select_' . $this->_id, '', $this->_prefix . '_format_select_' . $this->_id);
352:         $formatSelect->appendStyleDefinitions(array(
353:             'border' => '1px solid #ccc',
354:             'margin' => '2px 5px 5px'
355:         ));
356:         $formatSelect->autoFill($this->_dateFormatsPhp);
357:         $phpDateFormat = conHtmlSpecialChars($this->_settings[$this->_prefix . '_format']);
358:         $formatSelect->setDefault($phpDateFormat);
359: 
360:         return $formatSelect->render();
361:     }
362: 
363: }
CMS CONTENIDO 4.9.5 API documentation generated by ApiGen 2.8.0