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: $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:
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:
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:
89: $additionalFormats = getEffectiveSettingsByType('cms_date');
90: foreach ($additionalFormats as $format) {
91: $formatArray = json_decode($format, true);
92:
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:
103:
104:
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:
107:
108: if (!empty($_POST['date_format'])) {
109: $_POST['date_format'] = stripslashes(base64_decode($_POST['date_format']));
110: } else {
111: $_POST['date_format'] = '{"dateFormat":"","timeFormat":""}';
112: }
113: $this->_storeSettings();
114: }
115:
116:
117: $_POST[$this->_prefix . '_action'] = '';
118: $_POST['date_format'] = '';
119: }
120:
121: 122: 123: 124: 125:
126: public function getDateTimestamp() {
127: return $this->_settings['date_timestamp'];
128: }
129:
130: 131: 132: 133: 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: 154: 155: 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: 176: 177: 178: 179: 180: 181: 182: 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:
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:
246:
247: $result .= date($char, $timestamp);
248: break;
249: }
250: } else {
251:
252:
253: $result .= $char;
254: }
255: }
256:
257: return $result;
258: }
259:
260: 261: 262: 263: 264: 265: 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: 290: 291: 292: 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: 312: 313: 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: 336: 337: 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: 347: 348: 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: }