Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob
  • Smarty
    • Cacher
    • Compiler
    • Config
    • Debug
    • PluginsBlock
    • PluginsFilter
    • PluginsFunction
    • PluginsInternal
    • PluginsModifier
    • PluginsModifierCompiler
    • PluginsShared
    • Security
    • Template
    • TemplateResources
  • Swift
    • ByteStream
    • CharacterStream
    • Encoder
    • Events
    • KeyCache
    • Mailer
    • Mime
    • Plugins
    • Transport

Classes

  • Swift_FailoverTransport
  • Swift_LoadBalancedTransport
  • Swift_MailTransport
  • Swift_Plugins_Loggers_ArrayLogger
  • Swift_Plugins_Loggers_EchoLogger
  • Swift_SendmailTransport
  • Swift_SmtpTransport
  • Swift_Transport_AbstractSmtpTransport
  • Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  • Swift_Transport_Esmtp_Auth_LoginAuthenticator
  • Swift_Transport_Esmtp_Auth_PlainAuthenticator
  • Swift_Transport_Esmtp_AuthHandler
  • Swift_Transport_EsmtpTransport
  • Swift_Transport_FailoverTransport
  • Swift_Transport_LoadBalancedTransport
  • Swift_Transport_MailTransport
  • Swift_Transport_SendmailTransport
  • Swift_Transport_SimpleMailInvoker
  • Swift_Transport_StreamBuffer

Interfaces

  • Swift_Plugins_Logger
  • Swift_Plugins_Pop_Pop3Exception
  • Swift_Transport
  • Swift_Transport_Esmtp_Authenticator
  • Swift_Transport_EsmtpHandler
  • Swift_Transport_IoBuffer
  • Swift_Transport_MailInvoker
  • Swift_Transport_SmtpAgent
  • Swift_TransportException
  • Overview
  • Package
  • Function
  • Todo
  • Download
  1: <?php
  2: /**
  3:  * This file contains the cContentTypeDate class.
  4:  *
  5:  * @package Core
  6:  * @subpackage ContentType
  7:  * @version SVN Revision $Rev:$
  8:  * @author Bilal Arslan, Timo Trautmann, Simon Sprankel
  9:  * @copyright four for business AG <www.4fb.de>
 10:  * @license http://www.contenido.org/license/LIZENZ.txt
 11:  * @link http://www.4fb.de
 12:  * @link http://www.contenido.org
 13:  */
 14: 
 15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 16: 
 17: /**
 18:  * Content type CMS_DATE which allows the editor to select a date from a
 19:  * calendar and a date format.
 20:  * The selected date is then shown in the selected
 21:  * format.
 22:  *
 23:  * @package Core
 24:  * @subpackage ContentType
 25:  */
 26: class cContentTypeDate extends cContentTypeAbstract {
 27: 
 28:     /**
 29:      * The possible PHP date formats in which the selected date can be
 30:      * displayed.
 31:      *
 32:      * @var array
 33:      */
 34:     private $_dateFormatsPhp;
 35: 
 36:     /**
 37:      * Initialises class attributes and handles store events.
 38:      *
 39:      * @param string $rawSettings the raw settings in an XML structure or as
 40:      *        plaintext
 41:      * @param int $id ID of the content type, e.g. 3 if CMS_DATE[3] is used
 42:      * @param array $contentTypes array containing the values of all content
 43:      *        types
 44:      */
 45:     public function __construct($rawSettings, $id, array $contentTypes) {
 46: 
 47: 
 48:         // change attributes from the parent class and call the parent
 49:         // constructor
 50:         $this->_type = 'CMS_DATE';
 51:         $this->_prefix = 'date';
 52:         $this->_settingsType = self::SETTINGS_TYPE_XML;
 53:         $this->_formFields = array(
 54:             'date_timestamp',
 55:             'date_format'
 56:         );
 57:         parent::__construct($rawSettings, $id, $contentTypes);
 58: 
 59: 
 60:         // set the locale
 61:         $locale = cRegistry::getBackendLanguage();
 62:         if (empty($locale)
 63:         || false === setlocale(LC_TIME, $locale)) {
 64:             $oApiLang = new cApiLanguage(cRegistry::getLanguageId());
 65:             $locale = $oApiLang->getProperty('dateformat', 'locale');
 66:             if (empty($locale)) {
 67:                 $language = $oApiLang->getProperty('language', 'code');
 68:                 $country = $oApiLang->getProperty('country', 'code');
 69: 
 70:                 $locale = $language . '_' . strtoupper($country);
 71:             }
 72:             if (false === empty($locale)) {
 73:                 setlocale(LC_TIME, $locale);
 74:             }
 75:         }
 76:         // initialise the date formats
 77:         $this->_dateFormatsPhp = array(
 78:             conHtmlentities('{"dateFormat":"","timeFormat":""}') => '',
 79:             conHtmlentities('{"dateFormat":"d.m.Y","timeFormat":""}') => $this->_formatDate('d.m.Y'),
 80:             conHtmlentities('{"dateFormat":"D, d.m.Y","timeFormat":""}') => $this->_formatDate('D, d.m.Y'),
 81:             conHtmlentities('{"dateFormat":"d. F Y","timeFormat":""}') => $this->_formatDate('d. F Y'),
 82:             conHtmlentities('{"dateFormat":"Y-m-d","timeFormat":""}') => $this->_formatDate('Y-m-d'),
 83:             conHtmlentities('{"dateFormat":"d/F/Y","timeFormat":""}') => $this->_formatDate('d/F/Y'),
 84:             conHtmlentities('{"dateFormat":"d/m/y","timeFormat":""}') => $this->_formatDate('d/m/y'),
 85:             conHtmlentities('{"dateFormat":"F y","timeFormat":""}') => $this->_formatDate('F y'),
 86:             conHtmlentities('{"dateFormat":"F-y","timeFormat":""}') => $this->_formatDate('F-y'),
 87:             conHtmlentities('{"dateFormat":"d.m.Y","timeFormat":"H:i"}') => $this->_formatDate('d.m.Y H:i'),
 88:             conHtmlentities('{"dateFormat":"m.d.Y","timeFormat":"H:i:s"}') => $this->_formatDate('m.d.Y H:i:s'),
 89:             conHtmlentities('{"dateFormat":"","timeFormat":"H:i"}') => $this->_formatDate('H:i'),
 90:             conHtmlentities('{"dateFormat":"","timeFormat":"H:i:s"}') => $this->_formatDate('H:i:s'),
 91:             conHtmlentities('{"dateFormat":"","timeFormat":"h:i A"}') => $this->_formatDate('h:i A'),
 92:             conHtmlentities('{"dateFormat":"","timeFormat":"h:i:s A"}') => $this->_formatDate('h:i:s A')
 93:         );
 94: 
 95:         // add formats from client settings
 96:         $additionalFormats = getEffectiveSettingsByType('cms_date');
 97:         foreach ($additionalFormats as $format) {
 98:             $formatArray = json_decode($format, true);
 99:             // ignore invalid formats
100:             if (empty($formatArray) || count($formatArray) != 2 || !array_key_exists('dateFormat', $formatArray) || !array_key_exists('timeFormat', $formatArray)) {
101:                 cWarning('An invalid date-time-format has been entered in the client settings.');
102:                 continue;
103:             }
104:             $key = conHtmlSpecialChars($format);
105:             $value = implode(' ', $formatArray);
106:             $this->_dateFormatsPhp[$key] = $this->_formatDate($value);
107:         }
108: 
109:         // if form is submitted, store the current date settings
110:         // notice: also check the ID of the content type (there could be more
111:         // than one content type of the same type on the same page!)
112:         if (isset($_POST[$this->_prefix . '_action']) && $_POST[$this->_prefix . '_action'] === 'store' && isset($_POST[$this->_prefix . '_id']) && (int) $_POST[$this->_prefix . '_id'] == $this->_id) {
113:             // convert the given date string into a valid timestamp, so that a
114:             // timestamp is stored
115:             //CON-2049 additional check for base64 strings
116:             if (!empty($_POST['date_format']) && base64_encode(base64_decode($_POST['date_format'])) === $_POST['date_format']) {
117:                 $_POST['date_format'] = stripslashes(base64_decode($_POST['date_format']));
118:             } else { // if no date_format is given, set standard value
119:                 $_POST['date_format'] = '{"dateFormat":"","timeFormat":""}';
120:             }
121: 
122:             $this->_storeSettings();
123:         }
124: 
125:         // CON-2049
126:         // reset specific date variable
127:         // $_POST[$this->_prefix . '_action'] = '';
128:         // $_POST['date_format'] = '';
129:     }
130: 
131:     /**
132:      * Returns the displayed timestamp
133:      *
134:      * @return string
135:      */
136:     public function getDateTimestamp() {
137:         return $this->_settings['date_timestamp'];
138:     }
139: 
140:     /**
141:      * Returns the full PHP style format string
142:      *
143:      * @return string
144:      */
145:     public function getDateFormat() {
146:         $format = $this->_settings['date_format'];
147: 
148:         if (empty($format)) {
149:             $format = '';
150:         } else {
151:             $decoded_array = json_decode($format, true);
152:             if (is_array($decoded_array)) {
153:                 $format = implode(' ', $decoded_array);
154:             } else {
155:                 $format = '';
156:             }
157:         }
158: 
159:         return $format;
160:     }
161: 
162:     /**
163:      * Returns only the time portion of the PHP style format string
164:      *
165:      * @return string
166:      */
167:     public function getTimeFormat() {
168:         $format = $this->_settings['date_format'];
169: 
170:         if (empty($format)) {
171:             $format = '';
172:         } else {
173:             $decoded_array = json_decode($format, true);
174:             if (is_array($decoded_array)) {
175:                 return $decoded_array['timeFormat'];
176:             } else {
177:                 return '';
178:             }
179:         }
180: 
181:         return $format;
182:     }
183: 
184:     /**
185:      * Formats the given timestamp according to the given format.
186:      * Localises the
187:      * output.
188:      *
189:      * @param string $format the format string in the PHP date format
190:      * @param int $timestamp the timestamp representing the date which should be
191:      *        formatted
192:      * @return string the formatted, localised date
193:      */
194:     private function _formatDate($format, $timestamp = NULL) {
195:         $result = '';
196:         if ($timestamp === NULL) {
197:             $timestamp = time();
198:         }
199:         $replacements = array(
200:             'd',
201:             'D',
202:             'j',
203:             'l',
204:             'N',
205:             'S',
206:             'w',
207:             'z',
208:             'W',
209:             'F',
210:             'm',
211:             'M',
212:             'n',
213:             't',
214:             'L',
215:             'o',
216:             'Y',
217:             'y',
218:             'a',
219:             'A',
220:             'B',
221:             'g',
222:             'G',
223:             'h',
224:             'H',
225:             'i',
226:             's',
227:             'u',
228:             'e',
229:             'I',
230:             'O',
231:             'P',
232:             'T',
233:             'Z',
234:             'c',
235:             'r',
236:             'U'
237:         );
238:         foreach (str_split($format) as $char) {
239:             if (in_array($char, $replacements)) {
240:                 // replace the format chars with localised values
241:                 switch ($char) {
242:                     case 'D':
243:                         $result .= strftime('%a', $timestamp);
244:                         break;
245:                     case 'l':
246:                         $result .= strftime('%A', $timestamp);
247:                         break;
248:                     case 'F':
249:                         $result .= strftime('%B', $timestamp);
250:                         break;
251:                     case 'M':
252:                         $result .= strftime('%b', $timestamp);
253:                         break;
254:                     default:
255:                         // use the default date() format if no localisation is
256:                         // needed
257:                         $result .= date($char, $timestamp);
258:                         break;
259:                 }
260:             } else {
261:                 // if this is not a format char, just add it to the result
262:                 // string
263:                 $result .= $char;
264:             }
265:         }
266: 
267:         // strftime returns a string in an encoding that is specified by the locale
268:         // use iconv extension to get the content encoding of string
269:         // use mbstring extension to convert encoding to contenido's target encoding
270:         if (extension_loaded('iconv') && extension_loaded('mbstring')) {
271:             $result = mb_convert_encoding($result, cRegistry::getEncoding(), iconv_get_encoding('output_encoding'));
272:             $result = conHtmlentities($result);
273:         }
274: 
275:         return $result;
276:     }
277: 
278:     /**
279:      * Generates the code which should be shown if this content type is shown in
280:      * the frontend.
281:      *
282:      * @return string escaped HTML code which sould be shown if content type is
283:      *         shown in frontend
284:      */
285:     public function generateViewCode() {
286:         $format = $this->_settings['date_format'];
287: 
288:         if (empty($format)) {
289:             $format = '';
290:         } else {
291:             $decoded_array = json_decode($format, true);
292:             if (is_array($decoded_array)) {
293:                 $format = implode(' ', $decoded_array);
294:             } else {
295:                 $format = '';
296:             }
297:         }
298:         $timestamp = $this->_settings['date_timestamp'];
299:         if (empty($timestamp)) {
300:             return '';
301:         }
302: 
303:         return $this->_formatDate($format, $timestamp);
304:     }
305: 
306:     /**
307:      * Generates the code which should be shown if this content type is edited.
308:      *
309:      * @return string escaped HTML code which should be shown if content type is
310:      *         edited
311:      */
312:     public function generateEditCode() {
313:         $belang = cRegistry::getBackendLanguage();
314:         $format = 'Y-m-d h:i:sA';
315:         if ($belang == 'de_DE') {
316:             $format = 'd.m.Y H:i:s';
317:         }
318:         $value = date($format, $this->_settings['date_timestamp']);
319:         $code = new cHTMLTextbox('date_timestamp_' . $this->_id, $value, '', '', 'date_timestamp_' . $this->_id, true, '', '', 'date_timestamp');
320:         $code .= $this->_generateJavaScript();
321:         $code .= $this->_generateFormatSelect();
322:         $code .= $this->_generateStoreButton();
323:         $code = new cHTMLDiv($code, 'cms_date', 'cms_' . $this->_prefix . '_' . $this->_id . '_settings');
324: 
325:         return $this->_encodeForOutput($code);
326:     }
327: 
328:     /**
329:      * Generates the JavaScript needed for CMS_DATE.
330:      *
331:      * @return string HTML code which includes the needed JavaScript
332:      */
333:     private function _generateJavaScript() {
334:         $template = new cTemplate();
335:         $pathBackend = $this->_cfg['path']['contenido_fullhtml'];
336: 
337:         $template->set('s', 'PREFIX', $this->_prefix);
338:         $template->set('s', 'ID', $this->_id);
339:         $template->set('s', 'IDARTLANG', $this->_idArtLang);
340:         $template->set('s', 'LANG', substr(cRegistry::getBackendLanguage(), 0, 2));
341:         $template->set('s', 'PATH_TO_CALENDAR_PIC', $pathBackend . $this->_cfg['path']['images'] . 'calendar.gif');
342:         $setting = $this->_settings;
343:         if (array_key_exists('date_format', $setting)) {
344:             $setting['date_format'] = json_decode($setting['date_format'], true);
345:         }
346:         $template->set('s', 'SETTINGS', json_encode($setting));
347:         $template->set('s', 'BELANG', cRegistry::getBackendLanguage());
348: 
349:         return $template->generate($this->_cfg['path']['contenido'] . 'templates/standard/template.cms_date.html', true);
350:     }
351: 
352:     /**
353:      * Generates the save button.
354:      *
355:      * @return string HTML code for the save button
356:      */
357:     private function _generateStoreButton() {
358:         $saveButton = new cHTMLImage($this->_cfg['path']['contenido_fullhtml'] . $this->_cfg['path']['images'] . 'but_ok.gif', 'save_settings');
359: 
360:         return $saveButton->render();
361:     }
362: 
363:     /**
364:      * Generates a select box for defining the format of the date.
365:      *
366:      * @return string the HTML code of the format select box
367:      */
368:     private function _generateFormatSelect() {
369:         $formatSelect = new cHTMLSelectElement($this->_prefix . '_format_select_' . $this->_id, '', $this->_prefix . '_format_select_' . $this->_id);
370:         $formatSelect->appendStyleDefinitions(array(
371:             'border' => '1px solid #ccc',
372:             'margin' => '2px 5px 5px'
373:         ));
374:         $formatSelect->autoFill($this->_dateFormatsPhp);
375:         $phpDateFormat = conHtmlSpecialChars($this->_settings[$this->_prefix . '_format']);
376:         $formatSelect->setDefault($phpDateFormat);
377: 
378:         return $formatSelect->render();
379:     }
380: 
381: }
CMS CONTENIDO 4.9.7 API documentation generated by ApiGen