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