1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
18:
19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class cContentTypeDate extends cContentTypeAbstract {
29:
30: 31: 32: 33: 34: 35:
36: private $_dateFormatsPhp;
37:
38: 39: 40: 41: 42: 43: 44: 45: 46: 47:
48: public function __construct($rawSettings, $id, array $contentTypes) {
49:
50:
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:
60: parent::__construct($rawSettings, $id, $contentTypes);
61:
62:
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:
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:
99: $additionalFormats = getEffectiveSettingsByType('cms_date');
100: foreach ($additionalFormats as $format) {
101: $formatArray = json_decode($format, true);
102:
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:
113:
114:
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:
117:
118:
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 {
122: $_POST['date_format'] = '{"dateFormat":"","timeFormat":""}';
123: }
124:
125: $this->_storeSettings();
126: }
127:
128:
129:
130:
131:
132: }
133:
134: 135: 136: 137: 138:
139: public function getDateTimestamp() {
140: return $this->_settings['date_timestamp'];
141: }
142:
143: 144: 145: 146: 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: 167: 168: 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: 189: 190: 191: 192: 193: 194: 195: 196: 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:
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:
260:
261: $result .= date($char, $timestamp);
262: break;
263: }
264: } else {
265:
266:
267: $result .= $char;
268: }
269: }
270:
271:
272:
273:
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:
280:
281:
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: 292: 293: 294: 295: 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: 320: 321: 322: 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: 342: 343: 344: 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: 367: 368: 369: 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: 379: 380: 381: 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: