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
  • 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 = '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:      * Returns the displayed timestamp
118:      *
119:      * @return string
120:      */
121:     public function getDateTimestamp() {
122:         return $this->_settings['date_timestamp'];
123:     }
124: 
125:     /**
126:      * Returns the PHP style format string
127:      *
128:      * @return string
129:      */
130:     public function getDateFormat() {
131:         $format = $this->_settings['date_format'];
132: 
133:         if (empty($format)) {
134:             $format = '';
135:         } else {
136:             $decoded_array = json_decode($format, true);
137:             if (is_array($decoded_array)) {
138:                 $format = implode(' ', $decoded_array);
139:             } else {
140:                 $format = '';
141:             }
142:         }
143: 
144:         return $format;
145:     }
146: 
147:     /**
148:      * Formats the given timestamp according to the given format.
149:      * Localises the
150:      * output.
151:      *
152:      * @param string $format the format string in the PHP date format
153:      * @param int $timestamp the timestamp representing the date which should be
154:      *        formatted
155:      * @return string the formatted, localised date
156:      */
157:     private function _formatDate($format, $timestamp = NULL) {
158:         $result = '';
159:         if ($timestamp === NULL) {
160:             $timestamp = time();
161:         }
162:         $replacements = array(
163:             'd',
164:             'D',
165:             'j',
166:             'l',
167:             'N',
168:             'S',
169:             'w',
170:             'z',
171:             'W',
172:             'F',
173:             'm',
174:             'M',
175:             'n',
176:             't',
177:             'L',
178:             'o',
179:             'Y',
180:             'y',
181:             'a',
182:             'A',
183:             'B',
184:             'g',
185:             'G',
186:             'h',
187:             'H',
188:             'i',
189:             's',
190:             'u',
191:             'e',
192:             'I',
193:             'O',
194:             'P',
195:             'T',
196:             'Z',
197:             'c',
198:             'r',
199:             'U'
200:         );
201:         foreach (str_split($format) as $char) {
202:             if (in_array($char, $replacements)) {
203:                 // replace the format chars with localised values
204:                 switch ($char) {
205:                     case 'D':
206:                         $result .= strftime('%a', $timestamp);
207:                         break;
208:                     case 'l':
209:                         $result .= strftime('%A', $timestamp);
210:                         break;
211:                     case 'F':
212:                         $result .= strftime('%B', $timestamp);
213:                         break;
214:                     case 'M':
215:                         $result .= strftime('%b', $timestamp);
216:                         break;
217:                     default:
218:                         // use the default date() format if no localisation is
219:                         // needed
220:                         $result .= date($char, $timestamp);
221:                         break;
222:                 }
223:             } else {
224:                 // if this is not a format char, just add it to the result
225:                 // string
226:                 $result .= $char;
227:             }
228:         }
229: 
230:         return $result;
231:     }
232: 
233:     /**
234:      * Generates the code which should be shown if this content type is shown in
235:      * the frontend.
236:      *
237:      * @return string escaped HTML code which sould be shown if content type is
238:      *         shown in frontend
239:      */
240:     public function generateViewCode() {
241:         $format = $this->_settings['date_format'];
242: 
243:         if (empty($format)) {
244:             $format = '';
245:         } else {
246:             $decoded_array = json_decode($format, true);
247:             if (is_array($decoded_array)) {
248:                 $format = implode(' ', $decoded_array);
249:             } else {
250:                 $format = '';
251:             }
252:         }
253:         $timestamp = $this->_settings['date_timestamp'];
254:         if (empty($timestamp)) {
255:             return '';
256:         }
257: 
258:         return $this->_formatDate($format, $timestamp);
259:     }
260: 
261:     /**
262:      * Generates the code which should be shown if this content type is edited.
263:      *
264:      * @return string escaped HTML code which should be shown if content type is
265:      *         edited
266:      */
267:     public function generateEditCode() {
268:         $belang = cRegistry::getBackendLanguage();
269:         $format = 'Y-m-d h:i:sA';
270:         if ($belang == 'de_DE') {
271:             $format = 'd.m.Y H:i:s';
272:         }
273:         $value = date($format, $this->_settings['date_timestamp']);
274:         $code = new cHTMLTextbox('date_timestamp_' . $this->_id, $value, '', '', 'date_timestamp_' . $this->_id, true, '', '', 'date_timestamp');
275:         $code .= $this->_generateJavaScript();
276:         $code .= $this->_generateFormatSelect();
277:         $code .= $this->_generateStoreButton();
278:         $code = new cHTMLDiv($code, 'cms_date', 'cms_' . $this->_prefix . '_' . $this->_id . '_settings');
279: 
280:         return $this->_encodeForOutput($code);
281:     }
282: 
283:     /**
284:      * Generates the JavaScript needed for CMS_DATE.
285:      *
286:      * @return string HTML code which includes the needed JavaScript
287:      */
288:     private function _generateJavaScript() {
289:         $template = new cTemplate();
290:         $pathBackend = $this->_cfg['path']['contenido_fullhtml'];
291: 
292:         $template->set('s', 'PREFIX', $this->_prefix);
293:         $template->set('s', 'ID', $this->_id);
294:         $template->set('s', 'IDARTLANG', $this->_idArtLang);
295:         $template->set('s', 'LANG', substr(cRegistry::getBackendLanguage(), 0, 2));
296:         $template->set('s', 'PATH_TO_CALENDAR_PIC', $pathBackend . $this->_cfg['path']['images'] . 'calendar.gif');
297:         $setting = $this->_settings;
298:         if (array_key_exists('date_format', $setting)) {
299:             $setting['date_format'] = json_decode($setting['date_format'], true);
300:         }
301:         $template->set('s', 'SETTINGS', json_encode($setting));
302:         $template->set('s', 'BELANG', cRegistry::getBackendLanguage());
303: 
304:         return $template->generate($this->_cfg['path']['contenido'] . 'templates/standard/template.cms_date.html', true);
305:     }
306: 
307:     /**
308:      * Generates the save button.
309:      *
310:      * @return string HTML code for the save button
311:      */
312:     private function _generateStoreButton() {
313:         $saveButton = new cHTMLImage($this->_cfg['path']['contenido_fullhtml'] . $this->_cfg['path']['images'] . 'but_ok.gif', 'save_settings');
314: 
315:         return $saveButton->render();
316:     }
317: 
318:     /**
319:      * Generates a select box for defining the format of the date.
320:      *
321:      * @return string the HTML code of the format select box
322:      */
323:     private function _generateFormatSelect() {
324:         $formatSelect = new cHTMLSelectElement($this->_prefix . '_format_select_' . $this->_id, '', $this->_prefix . '_format_select_' . $this->_id);
325:         $formatSelect->appendStyleDefinitions(array(
326:             'border' => '1px solid #ccc',
327:             'margin' => '2px 5px 5px'
328:         ));
329:         $formatSelect->autoFill($this->_dateFormatsPhp);
330:         $phpDateFormat = conHtmlSpecialChars($this->_settings[$this->_prefix . '_format']);
331:         $formatSelect->setDefault($phpDateFormat);
332: 
333:         return $formatSelect->render();
334:     }
335: 
336: }
CMS CONTENIDO 4.9.3 API documentation generated by ApiGen 2.8.0