[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/calendar/src/ -> lmbCalendarWidget.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html
   8   */
   9  
  10  /**
  11   *  This class implements a simple PHP wrapper for the calendar.  It
  12   *  allows you to easily include all the calendar files and setup the
  13   *  calendar by instantiating and calling a PHP object.
  14   * @package calendar
  15   * @version $Id$
  16   */
  17  class lmbCalendarWidget
  18  {
  19    protected $newline = "\n";
  20    protected $calendar_lib_path;
  21  
  22    protected $calendar_file;
  23    protected $calendar_lang_file;
  24    protected $calendar_setup_file;
  25    protected $calendar_theme_file;
  26    protected $calendar_options;
  27  
  28    function __construct($lang              = 'en',
  29                         $stripped          = true,
  30                         $theme             = 'calendar-win2k-1',
  31                         $calendar_lib_path = '/shared/calendar/js/')
  32    {
  33      if($stripped)
  34      {
  35        $this->calendar_file = 'calendar_stripped.js';
  36        $this->calendar_setup_file = 'calendar-setup_stripped.js';
  37      }
  38      else
  39      {
  40        $this->calendar_file = 'calendar.js';
  41        $this->calendar_setup_file = 'calendar-setup.js';
  42      }
  43      $this->calendar_lang_file = 'lang/calendar-' . $lang . '.js';
  44      $this->calendar_theme_file = $theme.'.css';
  45      $this->calendar_lib_path = preg_replace('/\/+$/', '/', $calendar_lib_path);
  46      $this->calendar_options = array('ifFormat' => '%Y-%m-%d',
  47                                      'daFormat' => '%Y-%m-%d');
  48    }
  49  
  50    function setOption($name, $value)
  51    {
  52      $this->calendar_options[$name] = $value;
  53    } 
  54  
  55    function loadFiles()
  56    {
  57      static $rendered = false;
  58  
  59      $code  = '';
  60  
  61      if(!$rendered)
  62      {
  63        $code  = '<link rel="stylesheet" type="text/css" media="all" href="' .
  64                  $this->calendar_lib_path . $this->calendar_theme_file .
  65                   '" />' . $this->newline;
  66        $code .=  '<script type="text/javascript" src="' .
  67                  $this->calendar_lib_path . $this->calendar_file .
  68                  '"></script>' . $this->newline;
  69        $code .= '<script type="text/javascript" src="' .
  70                  $this->calendar_lib_path . $this->calendar_lang_file .
  71                  '"></script>' . $this->newline;
  72        $code .= '<script type="text/javascript" src="' .
  73                 $this->calendar_lib_path . $this->calendar_setup_file .
  74                 '"></script>';
  75      }
  76  
  77      $rendered = true;
  78  
  79      return $code;
  80    }
  81  
  82    function makeButton($field_id, $cal_options = array(), $field_attributes = array())
  83    {
  84      $id = $this->_genId();
  85      
  86      if(isset($field_attributes['src']) && $field_attributes['src'])
  87        $src = $field_attributes['src'];
  88      else
  89        $src = $this->calendar_lib_path . 'img.gif';
  90      
  91      $out = '<a href="#" id="'. $this->_triggerId($id) . '">' .
  92          '<img align="middle" border="0" src="' . $src . '" alt="" hspace="3"/></a>';
  93  
  94      $options = array_merge($cal_options,
  95                             array('inputField' => $field_id,
  96                                   'button'     => $this->_triggerId($id)));
  97      return $out . $this->_makeCalendar($options);
  98    }
  99  
 100    function _makeCalendar($other_options = array())
 101    {
 102      $js_options = $this->_makeJsHash(array_merge($this->calendar_options, $other_options));
 103      $code  = '<script type="text/javascript">Calendar.setup({' .
 104               $js_options .
 105               '});</script>';
 106      return $code;
 107    }
 108  
 109    function _fieldId($id) { return 'f-calendar-field-' . $id; }
 110    function _triggerId($id) { return 'f-calendar-trigger-' . $id; }
 111    function _genId() { static $id = 0; return ++$id; }
 112  
 113    function _makeJsHash($array)
 114    {
 115      $jstr = '';
 116      reset($array);
 117      while(list($key, $val) = each($array))
 118      {
 119        if(is_bool($val))
 120          $val = $val ? 'true' : 'false';
 121        else if(!is_numeric($val))
 122          $val = '"'.$val.'"';
 123        if($jstr) $jstr .= ',';
 124        $jstr .= '"' . $key . '":' . $val;
 125      }
 126      return $jstr;
 127    }
 128  
 129    function _makeHtmlAttr($array)
 130    {
 131      $attrstr = '';
 132      reset($array);
 133      while(list($key, $val) = each($array))
 134      {
 135        $attrstr .= $key . '="' . $val . '" ';
 136      }
 137      return $attrstr;
 138    }
 139  }
 140  
 141  ?>


Generated: Tue Oct 7 05:02:03 2008 Cross-referenced by PHPXref 0.7