pub/TWiki/JSCalendarContrib/calendar.php
changeset 0 414e01d06fd5
equal deleted inserted replaced
-1:000000000000 0:414e01d06fd5
       
     1 <?php
       
     2 
       
     3 /**
       
     4  *  File: calendar.php | (c) dynarch.com 2004
       
     5  *  Distributed as part of "The Coolest DHTML Calendar"
       
     6  *  under the same terms.
       
     7  *  -----------------------------------------------------------------
       
     8  *  This file implements a simple PHP wrapper for the calendar.  It
       
     9  *  allows you to easily include all the calendar files and setup the
       
    10  *  calendar by instantiating and calling a PHP object.
       
    11  */
       
    12 
       
    13 define('NEWLINE', "\n");
       
    14 
       
    15 class DHTML_Calendar {
       
    16     var $calendar_lib_path;
       
    17 
       
    18     var $calendar_file;
       
    19     var $calendar_lang_file;
       
    20     var $calendar_setup_file;
       
    21     var $calendar_theme_file;
       
    22     var $calendar_options;
       
    23 
       
    24     function DHTML_Calendar($calendar_lib_path = '/calendar/',
       
    25                             $lang              = 'en',
       
    26                             $theme             = 'calendar-win2k-1',
       
    27                             $stripped          = true) {
       
    28         if ($stripped) {
       
    29             $this->calendar_file = 'calendar_stripped.js';
       
    30             $this->calendar_setup_file = 'calendar-setup_stripped.js';
       
    31         } else {
       
    32             $this->calendar_file = 'calendar.js';
       
    33             $this->calendar_setup_file = 'calendar-setup.js';
       
    34         }
       
    35         $this->calendar_lang_file = 'lang/calendar-' . $lang . '.js';
       
    36         $this->calendar_theme_file = $theme.'.css';
       
    37         $this->calendar_lib_path = preg_replace('/\/+$/', '/', $calendar_lib_path);
       
    38         $this->calendar_options = array('ifFormat' => '%Y/%m/%d',
       
    39                                         'daFormat' => '%Y/%m/%d');
       
    40     }
       
    41 
       
    42     function set_option($name, $value) {
       
    43         $this->calendar_options[$name] = $value;
       
    44     }
       
    45 
       
    46     function load_files() {
       
    47         echo $this->get_load_files_code();
       
    48     }
       
    49 
       
    50     function get_load_files_code() {
       
    51         $code  = ( '<link rel="stylesheet" type="text/css" media="all" href="' .
       
    52                    $this->calendar_lib_path . $this->calendar_theme_file .
       
    53                    '" />' . NEWLINE );
       
    54         $code .= ( '<script type="text/javascript" src="' .
       
    55                    $this->calendar_lib_path . $this->calendar_file .
       
    56                    '"></script>' . NEWLINE );
       
    57         $code .= ( '<script type="text/javascript" src="' .
       
    58                    $this->calendar_lib_path . $this->calendar_lang_file .
       
    59                    '"></script>' . NEWLINE );
       
    60         $code .= ( '<script type="text/javascript" src="' .
       
    61                    $this->calendar_lib_path . $this->calendar_setup_file .
       
    62                    '"></script>' );
       
    63         return $code;
       
    64     }
       
    65 
       
    66     function _make_calendar($other_options = array()) {
       
    67         $js_options = $this->_make_js_hash(array_merge($this->calendar_options, $other_options));
       
    68         $code  = ( '<script type="text/javascript">Calendar.setup({' .
       
    69                    $js_options .
       
    70                    '});</script>' );
       
    71         return $code;
       
    72     }
       
    73 
       
    74     function make_input_field($cal_options = array(), $field_attributes = array()) {
       
    75         $id = $this->_gen_id();
       
    76         $attrstr = $this->_make_html_attr(array_merge($field_attributes,
       
    77                                                       array('id'   => $this->_field_id($id),
       
    78                                                             'type' => 'text')));
       
    79         echo '<input ' . $attrstr .'/>';
       
    80         echo '<a href="#" id="'. $this->_trigger_id($id) . '">' .
       
    81             '<img align="middle" border="0" src="' . $this->calendar_lib_path . 'img.gif" alt="" /></a>';
       
    82 
       
    83         $options = array_merge($cal_options,
       
    84                                array('inputField' => $this->_field_id($id),
       
    85                                      'button'     => $this->_trigger_id($id)));
       
    86         echo $this->_make_calendar($options);
       
    87     }
       
    88 
       
    89     /// PRIVATE SECTION
       
    90 
       
    91     function _field_id($id) { return 'f-calendar-field-' . $id; }
       
    92     function _trigger_id($id) { return 'f-calendar-trigger-' . $id; }
       
    93     function _gen_id() { static $id = 0; return ++$id; }
       
    94 
       
    95     function _make_js_hash($array) {
       
    96         $jstr = '';
       
    97         reset($array);
       
    98         while (list($key, $val) = each($array)) {
       
    99             if (is_bool($val))
       
   100                 $val = $val ? 'true' : 'false';
       
   101             else if (!is_numeric($val))
       
   102                 $val = '"'.$val.'"';
       
   103             if ($jstr) $jstr .= ',';
       
   104             $jstr .= '"' . $key . '":' . $val;
       
   105         }
       
   106         return $jstr;
       
   107     }
       
   108 
       
   109     function _make_html_attr($array) {
       
   110         $attrstr = '';
       
   111         reset($array);
       
   112         while (list($key, $val) = each($array)) {
       
   113             $attrstr .= $key . '="' . $val . '" ';
       
   114         }
       
   115         return $attrstr;
       
   116     }
       
   117 };
       
   118 
       
   119 ?>