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: 49: 50: 51: 52:
53: public function __construct($rawSettings, $id, array $contentTypes) {
54:
55:
56: $this->_type = 'CMS_DATE';
57: $this->_prefix = 'date';
58: $this->_settingsType = self::SETTINGS_TYPE_XML;
59: $this->_formFields = array(
60: 'date_timestamp',
61: 'date_format'
62: );
63:
64:
65: parent::__construct($rawSettings, $id, $contentTypes);
66:
67:
68: $locale = cRegistry::getBackendLanguage();
69: if (empty($locale)
70: || false === setlocale(LC_TIME, $locale)) {
71: $oApiLang = new cApiLanguage(cRegistry::getLanguageId());
72: $locale = $oApiLang->getProperty('dateformat', 'locale');
73: if (empty($locale)) {
74: $language = $oApiLang->getProperty('language', 'code');
75: $country = $oApiLang->getProperty('country', 'code');
76:
77: $locale = $language . '_' . cString::toUpperCase($country);
78: }
79: if (false === empty($locale)) {
80: setlocale(LC_TIME, $locale);
81: }
82: }
83:
84:
85: $this->_dateFormatsPhp = array(
86: conHtmlentities('{"dateFormat":"","timeFormat":""}') => '',
87: conHtmlentities('{"dateFormat":"d.m.Y","timeFormat":""}') => $this->_formatDate('d.m.Y'),
88: conHtmlentities('{"dateFormat":"D, d.m.Y","timeFormat":""}') => $this->_formatDate('D, d.m.Y'),
89: conHtmlentities('{"dateFormat":"d. F Y","timeFormat":""}') => $this->_formatDate('d. F Y'),
90: conHtmlentities('{"dateFormat":"Y-m-d","timeFormat":""}') => $this->_formatDate('Y-m-d'),
91: conHtmlentities('{"dateFormat":"d/F/Y","timeFormat":""}') => $this->_formatDate('d/F/Y'),
92: conHtmlentities('{"dateFormat":"d/m/y","timeFormat":""}') => $this->_formatDate('d/m/y'),
93: conHtmlentities('{"dateFormat":"F y","timeFormat":""}') => $this->_formatDate('F y'),
94: conHtmlentities('{"dateFormat":"F-y","timeFormat":""}') => $this->_formatDate('F-y'),
95: conHtmlentities('{"dateFormat":"d.m.Y","timeFormat":"H:i"}') => $this->_formatDate('d.m.Y H:i'),
96: conHtmlentities('{"dateFormat":"m.d.Y","timeFormat":"H:i:s"}') => $this->_formatDate('m.d.Y H:i:s'),
97: conHtmlentities('{"dateFormat":"","timeFormat":"H:i"}') => $this->_formatDate('H:i'),
98: conHtmlentities('{"dateFormat":"","timeFormat":"H:i:s"}') => $this->_formatDate('H:i:s'),
99: conHtmlentities('{"dateFormat":"","timeFormat":"h:i A"}') => $this->_formatDate('h:i A'),
100: conHtmlentities('{"dateFormat":"","timeFormat":"h:i:s A"}') => $this->_formatDate('h:i:s A')
101: );
102:
103:
104: $additionalFormats = getEffectiveSettingsByType('cms_date');
105: foreach ($additionalFormats as $format) {
106: $formatArray = json_decode($format, true);
107:
108: if (empty($formatArray) || count($formatArray) != 2 || !array_key_exists('dateFormat', $formatArray) || !array_key_exists('timeFormat', $formatArray)) {
109: cWarning('An invalid date-time-format has been entered in the client settings.');
110: continue;
111: }
112: $key = conHtmlSpecialChars($format);
113: $value = implode(' ', $formatArray);
114: $this->_dateFormatsPhp[$key] = $this->_formatDate($value);
115: }
116:
117:
118:
119:
120: if (isset($_POST[$this->_prefix . '_action']) && $_POST[$this->_prefix . '_action'] === 'store' && isset($_POST[$this->_prefix . '_id']) && (int) $_POST[$this->_prefix . '_id'] == $this->_id) {
121:
122:
123:
124: if (!empty($_POST['date_format']) && base64_encode(base64_decode($_POST['date_format'])) === $_POST['date_format']) {
125: $_POST['date_format'] = stripslashes(base64_decode($_POST['date_format']));
126: } else {
127: $_POST['date_format'] = '{"dateFormat":"","timeFormat":""}';
128: }
129:
130: $this->_storeSettings();
131: }
132:
133:
134:
135:
136:
137: }
138:
139: 140: 141: 142: 143:
144: public function getDateTimestamp() {
145: return $this->_settings['date_timestamp'];
146: }
147:
148: 149: 150: 151: 152:
153: public function getDateFormat() {
154: $format = $this->_settings['date_format'];
155:
156: if (empty($format)) {
157: $format = '';
158: } else {
159: $decoded_array = json_decode($format, true);
160: if (is_array($decoded_array)) {
161: $format = implode(' ', $decoded_array);
162: } else {
163: $format = '';
164: }
165: }
166:
167: return $format;
168: }
169:
170: 171: 172: 173: 174:
175: public function getTimeFormat() {
176: $format = $this->_settings['date_format'];
177:
178: if (empty($format)) {
179: $format = '';
180: } else {
181: $decoded_array = json_decode($format, true);
182: if (is_array($decoded_array)) {
183: return $decoded_array['timeFormat'];
184: } else {
185: return '';
186: }
187: }
188:
189: return $format;
190: }
191:
192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202:
203: private function _formatDate($format, $timestamp = NULL) {
204: $result = '';
205: if ($timestamp === NULL) {
206: $timestamp = time();
207: }
208: $replacements = array(
209: 'd',
210: 'D',
211: 'j',
212: 'l',
213: 'N',
214: 'S',
215: 'w',
216: 'z',
217: 'W',
218: 'F',
219: 'm',
220: 'M',
221: 'n',
222: 't',
223: 'L',
224: 'o',
225: 'Y',
226: 'y',
227: 'a',
228: 'A',
229: 'B',
230: 'g',
231: 'G',
232: 'h',
233: 'H',
234: 'i',
235: 's',
236: 'u',
237: 'e',
238: 'I',
239: 'O',
240: 'P',
241: 'T',
242: 'Z',
243: 'c',
244: 'r',
245: 'U'
246: );
247: foreach (str_split($format) as $char) {
248: if (in_array($char, $replacements)) {
249:
250: switch ($char) {
251: case 'D':
252: $result .= strftime('%a', $timestamp);
253: break;
254: case 'l':
255: $result .= strftime('%A', $timestamp);
256: break;
257: case 'F':
258: $result .= strftime('%B', $timestamp);
259: break;
260: case 'M':
261: $result .= strftime('%b', $timestamp);
262: break;
263: default:
264:
265:
266: $result .= date($char, $timestamp);
267: break;
268: }
269: } else {
270:
271:
272: $result .= $char;
273: }
274: }
275:
276:
277:
278:
279: if (extension_loaded('iconv') && extension_loaded('mbstring')) {
280: $result = mb_convert_encoding($result, cRegistry::getEncoding(), iconv_get_encoding('output_encoding'));
281: $result = conHtmlentities($result);
282: }
283:
284: return $result;
285: }
286:
287: 288: 289: 290: 291: 292: 293:
294: public function generateViewCode() {
295: if (empty($this->_settings['date_timestamp'])) {
296: return '';
297: }
298:
299: $timestamp = $this->_settings['date_timestamp'];
300:
301: if (empty($this->_settings['date_format'])) {
302: $format = '';
303: } else {
304: $format = $this->_settings['date_format'];
305: $decoded_array = json_decode($format, true);
306: if (is_array($decoded_array)) {
307: $format = implode(' ', $decoded_array);
308: } else {
309: $format = '';
310: }
311: }
312:
313: return $this->_formatDate($format, $timestamp);
314: }
315:
316: 317: 318: 319: 320: 321: 322:
323: public function generateEditCode() {
324: $belang = cRegistry::getBackendLanguage();
325: $format = 'Y-m-d h:i:sA';
326: if ($belang == 'de_DE') {
327: $format = 'd.m.Y H:i:s';
328: }
329: $value = date($format, $this->_settings['date_timestamp']);
330: $code = new cHTMLTextbox('date_timestamp_' . $this->_id, $value, '', '', 'date_timestamp_' . $this->_id, true, '', '', 'date_timestamp');
331: $code .= $this->_generateFormatSelect();
332: $code .= $this->_generateStoreButton();
333: $code .= $this->_generateJavaScript();
334: $code = new cHTMLDiv($code, 'cms_date', 'cms_' . $this->_prefix . '_' . $this->_id . '_settings');
335:
336: return $this->_encodeForOutput($code);
337: }
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', cString::getPartOfString(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' => '0px 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: