Back to index

plone3  3.1.7
calendar.php
Go to the documentation of this file.
00001 <?php
00002 
00013 define('NEWLINE', "\n");
00014 
00015 class DHTML_Calendar {
00016     var $calendar_lib_path;
00017 
00018     var $calendar_file;
00019     var $calendar_lang_file;
00020     var $calendar_setup_file;
00021     var $calendar_theme_file;
00022     var $calendar_options;
00023 
00024     function DHTML_Calendar($calendar_lib_path = '/calendar/',
00025                             $lang              = 'en',
00026                             $theme             = 'calendar-win2k-1',
00027                             $stripped          = true) {
00028         if ($stripped) {
00029             $this->calendar_file = 'calendar_stripped.js';
00030             $this->calendar_setup_file = 'calendar-setup_stripped.js';
00031         } else {
00032             $this->calendar_file = 'calendar.js';
00033             $this->calendar_setup_file = 'calendar-setup.js';
00034         }
00035         $this->calendar_lang_file = 'lang/calendar-' . $lang . '.js';
00036         $this->calendar_theme_file = $theme.'.css';
00037         $this->calendar_lib_path = preg_replace('/\/+$/', '/', $calendar_lib_path);
00038         $this->calendar_options = array('ifFormat' => '%Y/%m/%d',
00039                                         'daFormat' => '%Y/%m/%d');
00040     }
00041 
00042     function set_option($name, $value) {
00043         $this->calendar_options[$name] = $value;
00044     }
00045 
00046     function load_files() {
00047         echo $this->get_load_files_code();
00048     }
00049 
00050     function get_load_files_code() {
00051         $code  = ( '<link rel="stylesheet" type="text/css" media="all" href="' .
00052                    $this->calendar_lib_path . $this->calendar_theme_file .
00053                    '" />' . NEWLINE );
00054         $code .= ( '<script type="text/javascript" src="' .
00055                    $this->calendar_lib_path . $this->calendar_file .
00056                    '"></script>' . NEWLINE );
00057         $code .= ( '<script type="text/javascript" src="' .
00058                    $this->calendar_lib_path . $this->calendar_lang_file .
00059                    '"></script>' . NEWLINE );
00060         $code .= ( '<script type="text/javascript" src="' .
00061                    $this->calendar_lib_path . $this->calendar_setup_file .
00062                    '"></script>' );
00063         return $code;
00064     }
00065 
00066     function _make_calendar($other_options = array()) {
00067         $js_options = $this->_make_js_hash(array_merge($this->calendar_options, $other_options));
00068         $code  = ( '<script type="text/javascript">Calendar.setup({' .
00069                    $js_options .
00070                    '});</script>' );
00071         return $code;
00072     }
00073 
00074     function make_input_field($cal_options = array(), $field_attributes = array()) {
00075         $id = $this->_gen_id();
00076         $attrstr = $this->_make_html_attr(array_merge($field_attributes,
00077                                                       array('id'   => $this->_field_id($id),
00078                                                             'type' => 'text')));
00079         echo '<input ' . $attrstr .'/>';
00080         echo '<a href="#" id="'. $this->_trigger_id($id) . '">' .
00081             '<img align="middle" border="0" src="' . $this->calendar_lib_path . 'img.gif" alt="" /></a>';
00082 
00083         $options = array_merge($cal_options,
00084                                array('inputField' => $this->_field_id($id),
00085                                      'button'     => $this->_trigger_id($id)));
00086         echo $this->_make_calendar($options);
00087     }
00088 
00090 
00091     function _field_id($id) { return 'f-calendar-field-' . $id; }
00092     function _trigger_id($id) { return 'f-calendar-trigger-' . $id; }
00093     function _gen_id() { static $id = 0; return ++$id; }
00094 
00095     function _make_js_hash($array) {
00096         $jstr = '';
00097         reset($array);
00098         while (list($key, $val) = each($array)) {
00099             if (is_bool($val))
00100                 $val = $val ? 'true' : 'false';
00101             else if (!is_numeric($val))
00102                 $val = '"'.$val.'"';
00103             if ($jstr) $jstr .= ',';
00104             $jstr .= '"' . $key . '":' . $val;
00105         }
00106         return $jstr;
00107     }
00108 
00109     function _make_html_attr($array) {
00110         $attrstr = '';
00111         reset($array);
00112         while (list($key, $val) = each($array)) {
00113             $attrstr .= $key . '="' . $val . '" ';
00114         }
00115         return $attrstr;
00116     }
00117 };
00118 
00119 ?>