1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class cContentTypeDate extends cContentTypeAbstract {
27:
28: 29: 30: 31: 32: 33:
34: private $_dateFormatsPhp;
35:
36: 37: 38: 39: 40: 41: 42: 43: 44:
45: public function __construct($rawSettings, $id, array $contentTypes) {
46:
47:
48:
49:
50: $this->_type = 'CMS_DATE';
51: $this->_prefix = 'date';
52: $this->_settingsType = self::SETTINGS_TYPE_XML;
53: $this->_formFields = array(
54: 'date_timestamp',
55: 'date_format'
56: );
57: parent::__construct($rawSettings, $id, $contentTypes);
58:
59:
60:
61: $locale = cRegistry::getBackendLanguage();
62: if (empty($locale)
63: || false === setlocale(LC_TIME, $locale)) {
64: $oApiLang = new cApiLanguage(cRegistry::getLanguageId());
65: $locale = $oApiLang->getProperty('dateformat', 'locale');
66: if (empty($locale)) {
67: $language = $oApiLang->getProperty('language', 'code');
68: $country = $oApiLang->getProperty('country', 'code');
69:
70: $locale = $language . '_' . strtoupper($country);
71: }
72: if (false === empty($locale)) {
73: setlocale(LC_TIME, $locale);
74: }
75: }
76:
77: $this->_dateFormatsPhp = array(
78: conHtmlentities('{"dateFormat":"","timeFormat":""}') => '',
79: conHtmlentities('{"dateFormat":"d.m.Y","timeFormat":""}') => $this->_formatDate('d.m.Y'),
80: conHtmlentities('{"dateFormat":"D, d.m.Y","timeFormat":""}') => $this->_formatDate('D, d.m.Y'),
81: conHtmlentities('{"dateFormat":"d. F Y","timeFormat":""}') => $this->_formatDate('d. F Y'),
82: conHtmlentities('{"dateFormat":"Y-m-d","timeFormat":""}') => $this->_formatDate('Y-m-d'),
83: conHtmlentities('{"dateFormat":"d/F/Y","timeFormat":""}') => $this->_formatDate('d/F/Y'),
84: conHtmlentities('{"dateFormat":"d/m/y","timeFormat":""}') => $this->_formatDate('d/m/y'),
85: conHtmlentities('{"dateFormat":"F y","timeFormat":""}') => $this->_formatDate('F y'),
86: conHtmlentities('{"dateFormat":"F-y","timeFormat":""}') => $this->_formatDate('F-y'),
87: conHtmlentities('{"dateFormat":"d.m.Y","timeFormat":"H:i"}') => $this->_formatDate('d.m.Y H:i'),
88: conHtmlentities('{"dateFormat":"m.d.Y","timeFormat":"H:i:s"}') => $this->_formatDate('m.d.Y H:i:s'),
89: conHtmlentities('{"dateFormat":"","timeFormat":"H:i"}') => $this->_formatDate('H:i'),
90: conHtmlentities('{"dateFormat":"","timeFormat":"H:i:s"}') => $this->_formatDate('H:i:s'),
91: conHtmlentities('{"dateFormat":"","timeFormat":"h:i A"}') => $this->_formatDate('h:i A'),
92: conHtmlentities('{"dateFormat":"","timeFormat":"h:i:s A"}') => $this->_formatDate('h:i:s A')
93: );
94:
95:
96: $additionalFormats = getEffectiveSettingsByType('cms_date');
97: foreach ($additionalFormats as $format) {
98: $formatArray = json_decode($format, true);
99:
100: if (empty($formatArray) || count($formatArray) != 2 || !array_key_exists('dateFormat', $formatArray) || !array_key_exists('timeFormat', $formatArray)) {
101: cWarning('An invalid date-time-format has been entered in the client settings.');
102: continue;
103: }
104: $key = conHtmlSpecialChars($format);
105: $value = implode(' ', $formatArray);
106: $this->_dateFormatsPhp[$key] = $this->_formatDate($value);
107: }
108:
109:
110:
111:
112: if (isset($_POST[$this->_prefix . '_action']) && $_POST[$this->_prefix . '_action'] === 'store' && isset($_POST[$this->_prefix . '_id']) && (int) $_POST[$this->_prefix . '_id'] == $this->_id) {
113:
114:
115:
116: if (!empty($_POST['date_format']) && base64_encode(base64_decode($_POST['date_format'])) === $_POST['date_format']) {
117: $_POST['date_format'] = stripslashes(base64_decode($_POST['date_format']));
118: } else {
119: $_POST['date_format'] = '{"dateFormat":"","timeFormat":""}';
120: }
121:
122: $this->_storeSettings();
123: }
124:
125:
126:
127:
128:
129: }
130:
131: 132: 133: 134: 135:
136: public function getDateTimestamp() {
137: return $this->_settings['date_timestamp'];
138: }
139:
140: 141: 142: 143: 144:
145: public function getDateFormat() {
146: $format = $this->_settings['date_format'];
147:
148: if (empty($format)) {
149: $format = '';
150: } else {
151: $decoded_array = json_decode($format, true);
152: if (is_array($decoded_array)) {
153: $format = implode(' ', $decoded_array);
154: } else {
155: $format = '';
156: }
157: }
158:
159: return $format;
160: }
161:
162: 163: 164: 165: 166:
167: public function getTimeFormat() {
168: $format = $this->_settings['date_format'];
169:
170: if (empty($format)) {
171: $format = '';
172: } else {
173: $decoded_array = json_decode($format, true);
174: if (is_array($decoded_array)) {
175: return $decoded_array['timeFormat'];
176: } else {
177: return '';
178: }
179: }
180:
181: return $format;
182: }
183:
184: 185: 186: 187: 188: 189: 190: 191: 192: 193:
194: private function _formatDate($format, $timestamp = NULL) {
195: $result = '';
196: if ($timestamp === NULL) {
197: $timestamp = time();
198: }
199: $replacements = array(
200: 'd',
201: 'D',
202: 'j',
203: 'l',
204: 'N',
205: 'S',
206: 'w',
207: 'z',
208: 'W',
209: 'F',
210: 'm',
211: 'M',
212: 'n',
213: 't',
214: 'L',
215: 'o',
216: 'Y',
217: 'y',
218: 'a',
219: 'A',
220: 'B',
221: 'g',
222: 'G',
223: 'h',
224: 'H',
225: 'i',
226: 's',
227: 'u',
228: 'e',
229: 'I',
230: 'O',
231: 'P',
232: 'T',
233: 'Z',
234: 'c',
235: 'r',
236: 'U'
237: );
238: foreach (str_split($format) as $char) {
239: if (in_array($char, $replacements)) {
240:
241: switch ($char) {
242: case 'D':
243: $result .= strftime('%a', $timestamp);
244: break;
245: case 'l':
246: $result .= strftime('%A', $timestamp);
247: break;
248: case 'F':
249: $result .= strftime('%B', $timestamp);
250: break;
251: case 'M':
252: $result .= strftime('%b', $timestamp);
253: break;
254: default:
255:
256:
257: $result .= date($char, $timestamp);
258: break;
259: }
260: } else {
261:
262:
263: $result .= $char;
264: }
265: }
266:
267:
268:
269:
270: if (extension_loaded('iconv') && extension_loaded('mbstring')) {
271: $result = mb_convert_encoding($result, cRegistry::getEncoding(), iconv_get_encoding('output_encoding'));
272: $result = conHtmlentities($result);
273: }
274:
275: return $result;
276: }
277:
278: 279: 280: 281: 282: 283: 284:
285: public function generateViewCode() {
286: $format = $this->_settings['date_format'];
287:
288: if (empty($format)) {
289: $format = '';
290: } else {
291: $decoded_array = json_decode($format, true);
292: if (is_array($decoded_array)) {
293: $format = implode(' ', $decoded_array);
294: } else {
295: $format = '';
296: }
297: }
298: $timestamp = $this->_settings['date_timestamp'];
299: if (empty($timestamp)) {
300: return '';
301: }
302:
303: return $this->_formatDate($format, $timestamp);
304: }
305:
306: 307: 308: 309: 310: 311:
312: public function generateEditCode() {
313: $belang = cRegistry::getBackendLanguage();
314: $format = 'Y-m-d h:i:sA';
315: if ($belang == 'de_DE') {
316: $format = 'd.m.Y H:i:s';
317: }
318: $value = date($format, $this->_settings['date_timestamp']);
319: $code = new cHTMLTextbox('date_timestamp_' . $this->_id, $value, '', '', 'date_timestamp_' . $this->_id, true, '', '', 'date_timestamp');
320: $code .= $this->_generateJavaScript();
321: $code .= $this->_generateFormatSelect();
322: $code .= $this->_generateStoreButton();
323: $code = new cHTMLDiv($code, 'cms_date', 'cms_' . $this->_prefix . '_' . $this->_id . '_settings');
324:
325: return $this->_encodeForOutput($code);
326: }
327:
328: 329: 330: 331: 332:
333: private function _generateJavaScript() {
334: $template = new cTemplate();
335: $pathBackend = $this->_cfg['path']['contenido_fullhtml'];
336:
337: $template->set('s', 'PREFIX', $this->_prefix);
338: $template->set('s', 'ID', $this->_id);
339: $template->set('s', 'IDARTLANG', $this->_idArtLang);
340: $template->set('s', 'LANG', substr(cRegistry::getBackendLanguage(), 0, 2));
341: $template->set('s', 'PATH_TO_CALENDAR_PIC', $pathBackend . $this->_cfg['path']['images'] . 'calendar.gif');
342: $setting = $this->_settings;
343: if (array_key_exists('date_format', $setting)) {
344: $setting['date_format'] = json_decode($setting['date_format'], true);
345: }
346: $template->set('s', 'SETTINGS', json_encode($setting));
347: $template->set('s', 'BELANG', cRegistry::getBackendLanguage());
348:
349: return $template->generate($this->_cfg['path']['contenido'] . 'templates/standard/template.cms_date.html', true);
350: }
351:
352: 353: 354: 355: 356:
357: private function _generateStoreButton() {
358: $saveButton = new cHTMLImage($this->_cfg['path']['contenido_fullhtml'] . $this->_cfg['path']['images'] . 'but_ok.gif', 'save_settings');
359:
360: return $saveButton->render();
361: }
362:
363: 364: 365: 366: 367:
368: private function _generateFormatSelect() {
369: $formatSelect = new cHTMLSelectElement($this->_prefix . '_format_select_' . $this->_id, '', $this->_prefix . '_format_select_' . $this->_id);
370: $formatSelect->appendStyleDefinitions(array(
371: 'border' => '1px solid #ccc',
372: 'margin' => '2px 5px 5px'
373: ));
374: $formatSelect->autoFill($this->_dateFormatsPhp);
375: $phpDateFormat = conHtmlSpecialChars($this->_settings[$this->_prefix . '_format']);
376: $formatSelect->setDefault($phpDateFormat);
377:
378: return $formatSelect->render();
379: }
380:
381: }