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 = '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: $_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: 118: 119: 120:
121: public function getDateTimestamp() {
122: return $this->_settings['date_timestamp'];
123: }
124:
125: 126: 127: 128: 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: 149: 150: 151: 152: 153: 154: 155: 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:
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:
219:
220: $result .= date($char, $timestamp);
221: break;
222: }
223: } else {
224:
225:
226: $result .= $char;
227: }
228: }
229:
230: return $result;
231: }
232:
233: 234: 235: 236: 237: 238: 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: 263: 264: 265: 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: 285: 286: 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: 309: 310: 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: 320: 321: 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: }