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: class cContentTypeDate extends cContentTypeAbstract {
26:
27: 28: 29: 30: 31: 32:
33: private $_dateFormatsPhp;
34:
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: 122: 123: 124:
125: private function _formatDate($format, $timestamp = NULL) {
126: $result = '';
127: if ($timestamp === NULL) {
128: $timestamp = time();
129: }
130: $replacements = array(
131: 'd',
132: 'D',
133: 'j',
134: 'l',
135: 'N',
136: 'S',
137: 'w',
138: 'z',
139: 'W',
140: 'F',
141: 'm',
142: 'M',
143: 'n',
144: 't',
145: 'L',
146: 'o',
147: 'Y',
148: 'y',
149: 'a',
150: 'A',
151: 'B',
152: 'g',
153: 'G',
154: 'h',
155: 'H',
156: 'i',
157: 's',
158: 'u',
159: 'e',
160: 'I',
161: 'O',
162: 'P',
163: 'T',
164: 'Z',
165: 'c',
166: 'r',
167: 'U'
168: );
169: foreach (str_split($format) as $char) {
170: if (in_array($char, $replacements)) {
171:
172: switch ($char) {
173: case 'D':
174: $result .= strftime('%a', $timestamp);
175: break;
176: case 'l':
177: $result .= strftime('%A', $timestamp);
178: break;
179: case 'F':
180: $result .= strftime('%B', $timestamp);
181: break;
182: case 'M':
183: $result .= strftime('%b', $timestamp);
184: break;
185: default:
186:
187:
188: $result .= date($char, $timestamp);
189: break;
190: }
191: } else {
192:
193:
194: $result .= $char;
195: }
196: }
197:
198: return $result;
199: }
200:
201: 202: 203: 204: 205: 206: 207:
208: public function generateViewCode() {
209: $format = $this->_settings['date_format'];
210:
211: if (empty($format)) {
212: $format = '';
213: } else {
214: $decoded_array = json_decode($format, true);
215: if (is_array($decoded_array)) {
216: $format = implode(' ', $decoded_array);
217: } else {
218: $format = '';
219: }
220: }
221: $timestamp = $this->_settings['date_timestamp'];
222: if (empty($timestamp)) {
223: return '';
224: }
225:
226: return $this->_formatDate($format, $timestamp);
227: }
228:
229: 230: 231: 232: 233: 234:
235: public function generateEditCode() {
236: $belang = cRegistry::getBackendLanguage();
237: $format = 'Y-m-d h:i:sA';
238: if ($belang == 'de_DE') {
239: $format = 'd.m.Y H:i:s';
240: }
241: $value = date($format, $this->_settings['date_timestamp']);
242: $code = new cHTMLTextbox('date_timestamp_' . $this->_id, $value, '', '', 'date_timestamp_' . $this->_id, true, '', '', 'date_timestamp');
243: $code .= $this->_generateJavaScript();
244: $code .= $this->_generateFormatSelect();
245: $code .= $this->_generateStoreButton();
246: $code = new cHTMLDiv($code, 'cms_date', 'cms_' . $this->_prefix . '_' . $this->_id . '_settings');
247:
248: return $this->_encodeForOutput($code);
249: }
250:
251: 252: 253: 254: 255:
256: private function _generateJavaScript() {
257: $template = new cTemplate();
258: $pathBackend = $this->_cfg['path']['contenido_fullhtml'];
259:
260: $template->set('s', 'PREFIX', $this->_prefix);
261: $template->set('s', 'ID', $this->_id);
262: $template->set('s', 'IDARTLANG', $this->_idArtLang);
263: $template->set('s', 'PATH_BACKEND', $pathBackend);
264: $template->set('s', 'LANG', substr(cRegistry::getBackendLanguage(), 0, 2));
265: $template->set('s', 'PATH_TO_CALENDAR_PIC', $pathBackend . $this->_cfg['path']['images'] . 'calendar.gif');
266: $setting = $this->_settings;
267: if (array_key_exists('date_format', $setting)) {
268: $setting['date_format'] = json_decode($setting['date_format'], true);
269: }
270: $template->set('s', 'SETTINGS', json_encode($setting));
271: $template->set('s', 'BELANG', cRegistry::getBackendLanguage());
272:
273: return $template->generate($this->_cfg['path']['contenido'] . 'templates/standard/template.cms_date.html', true);
274: }
275:
276: 277: 278: 279: 280:
281: private function _generateStoreButton() {
282: $saveButton = new cHTMLImage($this->_cfg['path']['contenido_fullhtml'] . $this->_cfg['path']['images'] . 'but_ok.gif', 'save_settings');
283:
284: return $saveButton->render();
285: }
286:
287: 288: 289: 290: 291:
292: private function _generateFormatSelect() {
293: $formatSelect = new cHTMLSelectElement($this->_prefix . '_format_select_' . $this->_id, '', $this->_prefix . '_format_select_' . $this->_id);
294: $formatSelect->appendStyleDefinitions(array(
295: 'border' => '1px solid #ccc',
296: 'margin' => '2px 5px 5px'
297: ));
298: $formatSelect->autoFill($this->_dateFormatsPhp);
299: $phpDateFormat = conHtmlSpecialChars($this->_settings[$this->_prefix . '_format']);
300: $formatSelect->setDefault($phpDateFormat);
301:
302: return $formatSelect->render();
303: }
304:
305: }