src/class.jscalendar.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2006 - 2008 Stefan Galinski (stefan.galinski@gmail.com)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *
00017 *  This script is distributed in the hope that it will be useful,
00018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *  GNU General Public License for more details.
00021 *
00022 *  This copyright notice MUST APPEAR in all copies of the script!
00023 ***************************************************************/
00024 
00030 class JSCalendar
00031 {
00033         var $config = array();
00034 
00036         var $jsSent = false;
00037 
00039         var $lang = null;
00040 
00049         function JSCalendar() {
00050                 // add some paths
00051                 $this->config['backPath'] = $GLOBALS['BACK_PATH'] . (TYPO3_MODE == 'BE' ? '../' : '');
00052                 $this->config['relPath'] = $this->config['backPath'] . t3lib_extMgm::siteRelPath('date2cal');
00053                 $this->config['absPath'] = t3lib_extMgm::extPath('date2cal');
00054 
00055                 // set variable with the language object
00056                 $this->lang = TYPO3_MODE == 'FE' ? $GLOBALS['TSFE'] : $GLOBALS['LANG'];
00057 
00058                 // add some configuration
00059                 $this->setNLP(true);
00060                 $this->setCSS();
00061                 $this->setLanguage();
00062                 $this->setDateFormat();
00063 
00064                 // set help page
00065                 $this->setConfigOption('helpPage',  $this->config['relPath'] . 'res/helpPage.html');
00066         }
00067 
00074         function &getInstance() {
00075                 static $instance;
00076                 if (!isset($instance))
00077                         $instance = new JSCalendar();
00078                 return $instance;
00079         }
00080 
00094         function render($value, $name = '', $calImg = '', $helpImg = '') {
00095                 // generates the input field id/name if it not exists
00096                 if (!isset($this->config['inputField']))
00097                         $this->setInputField('date');
00098 
00099                 // render input field
00100                 $name = $name == '' ? $this->config['inputField'] : $name;
00101                 $content = '<input type="checkbox" name="' . $name . '_cb" class="jscalendar_cb" ' .
00102                         'id ="' . $this->config['inputField'] . '_cb" ' .
00103                         'onclick="date2cal_setDatetime(\'' . $this->config['inputField'] . '_hr\', ' .
00104                         strftime($this->config['calConfig']['ifFormat']) . ');" /> ';
00105                 $size = $this->config['calConfig']['showsTime'] ? 16 : 10;
00106                 $content .= '<input type="text" size="' . $size . '" maxlength="' . $size . '" ' .
00107                         'name="' . $name . '" id="' .  $this->config['inputField'] . '_hr" class="jscalendar" ' .
00108                         'onchange="date2cal_activeDateField(\'' . $this->config['inputField'] . '_cb\', \'' .
00109                         $this->config['inputField'] . '_hr\');" value="' . $value . '" />';
00110 
00111                 // render images
00112                 $content .= $this->renderImages($calImg, $helpImg);
00113 
00114                 return $content;
00115         }
00116 
00125         function renderImages($calImg = '', $helpImg = '') {
00126                 // check images
00127                 $calImg = $calImg == '' ? $this->config['relPath'] . 'res/calendar.png' : $calImg;
00128                 $helpImg = $helpImg == '' ? $this->config['relPath'] . 'res/helpIcon.gif' : $helpImg;
00129 
00130                 // vertical alignment
00131                 $valign = TYPO3_MODE == 'FE' ? 'vertical-align: middle;' : '';
00132 
00133                 // alt/title language labels for the images
00134                 $calImgTitle = $this->lang->sL('LLL:EXT:date2cal/locallang.xml:calendar_wizard');
00135                 $helpImgTitle = $this->lang->sL('LLL:EXT:date2cal/locallang.xml:help');
00136 
00137                 // calendar trigger image
00138                 $content .= ' <img class="date2cal_img_cal absMiddle" src="' . $calImg . '" ' .
00139                         'id="' . $this->config['inputField'] . '_trigger" style="cursor: pointer; ' . $valign . '" ' .
00140                         'title="' . $calImgTitle . '" alt="' . $calImgTitle . '" />' . "\n";
00141 
00142                 // natural language parse help image
00143                 if ($this->config['natLangParser']) {
00144                         $content .= '<img class="date2cal_img_help absMiddle" src="' . $helpImg . '" ' .
00145                                 'id="' . $this->config['inputField'] . '_help" style="cursor: pointer; ' . $valign . '" ' .
00146                                 'title="' . $helpImgTitle . '" alt="' . $helpImgTitle . '" />' . "\n";
00147                 }
00148 
00149                 // calendar javascript configuration
00150                 $content .= $this->getConfigJS();
00151 
00152                 return $content;
00153         }
00154 
00166         function setConfigOption($option, $value, $nonString=false) {
00167                 $this->config['calConfig'][$option] = !$nonString ? '\'' . $value . '\'' : $value;
00168         }
00169 
00178         function setInputField($field) {
00179                 $this->config['inputField'] = $field;
00180                 $this->setConfigOption('inputField', $field . '_hr');
00181                 $this->setConfigOption('button', $field . '_trigger');
00182         }
00183 
00189         function getInputField() {
00190                 return $this->config['inputField'];
00191         }
00192 
00200         function setLanguage($lang='') {
00201                 // language detection
00202                 if ($lang == '') {
00203                         if (TYPO3_MODE == 'FE')
00204                                 $lang = $GLOBALS['TSFE']->config['config']['language'];
00205                         else
00206                                 $lang = $GLOBALS['LANG']->lang;
00207                 }
00208 
00209                 // check availability of selected languages
00210                 $this->config['lang'] = $this->languageCheck($lang);
00211         }
00212 
00219         function setCSS($calendarCSS = 'aqua') {
00220                 $this->config['calendarCSS'] = $calendarCSS;
00221                 if (!is_file($this->config['absPath'] . 'js/jscalendar/skins/' . $calendarCSS . '/theme.css'))
00222                         $this->config['calendarCSS'] = 'aqua';
00223         }
00224 
00232         function setNLP($mode) {
00233                 $this->config['natLangParser'] = true;
00234                 if (!$mode || t3lib_div::int_from_ver(TYPO3_version) < 4001000)
00235                         $this->config['natLangParser'] = false;
00236         }
00237 
00246         function setDateFormat($time=false) {
00247                 $jsDate = $GLOBALS['TYPO3_CONF_VARS']['SYS']['USdateFormat'] ?
00248                         '%m-%d-%Y' : '%d-%m-%Y';
00249                 $jsDate = ($time ? '%H:%M ' : '') . $jsDate;
00250 
00251                 $value = $time ? 'true' : 'false';
00252                 $this->setConfigOption('showsTime', $value, true);
00253                 $this->setConfigOption('time24', $value, true);
00254                 $this->setConfigOption('ifFormat', $jsDate);
00255         }
00256 
00262         function getConfigJS()
00263         {
00264                 // generates the calendar configuration string
00265                 $tmp = array();
00266                 foreach($this->config['calConfig'] as $label => $value)
00267                         $tmp[] = $label . ': ' . $value;
00268                 $config = implode(",\n", $tmp);
00269 
00270                 // generates the javascript code for a single instance
00271                 if ($this->config['natLangParser']) {
00272                         $js = '
00273                                 <script type="text/javascript">
00274                                         new DatetimeToolbocks ({
00275                                                 format: ' . $this->config['calConfig']['ifFormat'] . ',
00276                                                 inputName: \'' . $this->config['inputField'] . '\',
00277                                                 elementId: \'' . $this->config['inputField'] . '\',
00278                                                 calendarOptions: {
00279                                                         ' . $config . '
00280                                                 }
00281                                         });
00282                                 </script>';
00283                 } else {
00284                         $js = '
00285                                 <script type="text/javascript">
00286                                         Calendar.setup ({
00287                                                 ' . $config . '
00288                                         });
00289                                 </script>';
00290                 }
00291 
00292                 return $js;
00293         }
00294 
00301         function getMainJS()
00302         {
00303                 // can only be called once
00304                 if ($this->jsSent)
00305                         return '';
00306                 $this->jsSent = true;
00307 
00308                 // jscalendar inclusion (javascript, languages and css)
00309                 $relPath = $this->config['relPath'] . 'js/';
00310                 $js = '<!-- inclusion of JSCalendar -->
00311                         <script type="text/javascript" src="' . $relPath . 'jscalendar/calendar.js"></script>
00312                         <script type="text/javascript" src="' . $relPath .
00313                                 'jscalendar/lang/calendar-en.js"></script>' . "\n";
00314                 if ($this->config['lang'] != 'en')
00315                         $js .= '<script type="text/javascript" src="' . $relPath . 'jscalendar/lang/calendar-' .
00316                                 $this->config['lang'] . '.js"></script>' . "\n";
00317                 $js .= '<script type="text/javascript" src="' . $relPath .
00318                                 'jscalendar/calendar-setup.js"></script>
00319                         <link rel="stylesheet" type="text/css" href="' . $relPath . 'jscalendar/skins/' .
00320                                 $this->config['calendarCSS'] . '/theme.css" />
00321                         <script type="text/javascript" src="' . $relPath . 'date2cal.js"></script>' . "\n";
00322 
00323                 // natural language parser scripts
00324                 if ($this->config['natLangParser']) {
00325                         $js .= '<!-- inclusion of datetime_toolbocks.js -->
00326                                 <script type="text/javascript" src="' . $this->config['backPath'] .
00327                                         'typo3/contrib/prototype/prototype.js"></script>
00328                                 <script type="text/javascript" src="' . $relPath .
00329                                         'naturalLanguageParser.js"></script>' . "\n";
00330                 }
00331 
00332                 return $js;
00333         }
00334 
00345         function languageCheck($lang)
00346         {
00347                 // convert language into an iso code
00348                 if (array_key_exists($lang, $this->lang->csConvObj->isoArray))
00349                         $lang = $this->lang->csConvObj->isoArray[$lang];
00350 
00351                 // check availability of utf8 encoding
00352                 $absPath = $this->config['absPath'] . 'js/';
00353                 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] == 'utf-8' &&
00354                         is_file($absPath . 'jscalendar/lang/calendar-' . $lang . '-utf8.js'))
00355                         return $lang . '-utf8';
00356 
00357                 // check availability of iso encoding
00358                 if (!is_file($absPath . 'jscalendar/lang/calendar-' . $lang . '.js'))
00359                         return 'en';
00360 
00361                 return $lang;
00362         }
00363 }
00364 ?>

Generated on Thu Mar 20 21:55:20 2008 for date2cal by  doxygen 1.5.3