Re: quickform jscalendar with "multiple dates feature"

From: Date: Fri, 27 Oct 2006 10:57:38 +0000
Subject: Re: quickform jscalendar with "multiple dates feature"
References: 1 2 3 4 5 6  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-25658@lists.php.net to get a copy of this message
----- Original Message ----- From: "Justin Patrin" <papercrane@gmail.com> To: "Marco Sottana" <marco.sottana@q-web.it> Cc: <pear-general@lists.php.net> Sent: Wednesday, October 25, 2006 8:07 PM Subject: Re: [PEAR] quickform jscalendar with "multiple dates feature"
On 10/25/06, Marco Sottana <marco.sottana@q-web.it> wrote:
i find jscalendar element for HTML_QuickForm http://www.netsols.de/pear/jscalendar/ i need to use multiple dates feature http://www.dynarch.com/demos/jscalendar/multiple-dates.html anyone can help me?
Please send your messages to the list only once. -- Justin Patrin
sorry for my mistake sending twice time my message .... perhaps now i am near solution i change jscalendar.php [code] <?php require_once 'HTML/QuickForm/element.php'; //HTML_QuickForm::registerElementType('jscalendar', 'HTML/QuickForm/jscalendar.php', 'HTML_QuickForm_jscalendar'); /** http://www.netsols.de/pear/jscalendar/jscalendar.phps * HTML class for a jscalendar field * * jscalendar is "The coolest DHTML/JavaScript Calendar" which can be obtained * from http://dynarch.com/projects/calendar * * @author Jan Wagner <wagner@netsols.de> * @license LGPL * @copyright Copyright &copy; 2006, Jan Wagner <wagner@netsols.de> */ class HTML_QuickForm_jscalendar extends HTML_QuickForm_element { /**
    * Name of the element
    *
    * @var string
    * @access protected
    */
var $_name = ''; /**
    * Options should contain:
    *
    * baseURL: where to find the jscalendar files (with trailing /)
    * styleCss: name of style css file
    * language: Language code
    * setup: A hash of options to pass to Calendar.setup
    *
    * @var array
    * @access protected
    */
var $_options; /**
    * Just the constructor
    *
    * @access public
    * @param string elementName
    * @param string elementLabel
    * @param array options Array of element option
    * @param array attributes HTML attributes
    * @return void
    */
   function HTML_QuickForm_jscalendar($elementName = null, $elementLabel = null, $options = array(), $attributes = null)
{
       $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
       $this->_type = 'jscalendar';
       if (is_array($options)) {
           $this->_options = $options;
       }
} /**
    * Function that renders the element into HTML
    *
    * @access public
    * @return string HTML output
    */
function toHtml() { $html="";
       if ($this->_flagFrozen) {
           return '';
       }
       if (!defined('HTML_QUICKFORM_JSCALENDAR_LOADED')) {
           $html = sprintf(
               '<style type="text/css">@import url(%s);</style>' .
               '<script type="text/javascript" src="%scalendar.js"></script>' .
               '<script type="text/javascript" src="%slang/calendar-%s.js"></script>' .
               '<script type="text/javascript" src="%scalendar-setup.js"></script>',
               $this->_options['baseURL'] . $this->_options['styleCss'],
               $this->_options['baseURL'],
               $this->_options['baseURL'], $this->_options['language'],
               $this->_options['baseURL']
           );
           define('HTML_QUICKFORM_JSCALENDAR_LOADED', true);
       }
       if (isset($this->_options['image'])) {
           if (!isset($this->_options['image']['id'])) {
               $name = $this->getName('name');
               $this->_options['image']['id'] = $name . '_trigger';
               $this->_options['setup']['button'] = $name . '_trigger';
           }
           $html .= '<a href="javascript:void(null);"><img ';
           foreach ($this->_options['image'] as $k => $v) {
               $html .= sprintf('%s="%s" ', $k, $v);
           }
           $html .= '/></a>';
       }
       $html .= '<script type="text/javascript">';
 $html .='// the default multiple dates selected, first time the calendar is instantiated
var MA = []; function closed(cal) { //alert(cal);
     // here we ll write the output; this is only for example.  You
     // will normally fill an input field or something with the dates.
     var el = document.getElementById("listadata");
     // reset initial content.
     el.innerHTML = "";
     // Reset the "MA", in case one triggers the calendar again.
     // CAREFUL!  You don t want to do "MA = [];".  We need to modify
     // the value of the current array, instead of creating a new one.
     // Calendar.setup is called only once! :-)  So be careful.
     MA.length = 0;
     // walk the calendar s multiple dates selection hash
     for (var i in cal.multiple) {
       var d = cal.multiple[i];
       // sometimes the date is not actually selected, that s why we need to check.
       if (d) {
         // OK, selected.  Fill an input field.  Or something.  Just for example,
         // we will display all selected dates in the element having the id "output".
         el.innerHTML += d.print("%d/%m/%Y") + ",";
         // and push it in the "MA", in case one triggers the calendar again.
         MA[MA.length] = d;
       }
     }
     cal.hide();
     return true;
};';
       $html .= 'Calendar.setup(';
       $html .= str_replace(" 'multiple' : true"," 'multiple' : MA, onClose : closed ",$this->_array2JS($this->_options['setup']));
       $html .= ');';
$html .= "Calendar.multiple[0]='27/10/2006';"; $html .= "Calendar.multiple[1]='18/10/2006';";
       $html .= '</script>';
       return $html;
} /**
    * Set the name of the element
    *
    * @access public
    * @param string name Name of the element
    * @return void
    */
function setName($name) {
       $this->_name = $name;
} /**
    * Returns the name of the element
    *
    * @access public
    * @return string Name of the element
    */
function getName() {
       return $this->_name;
} /**
    * Helper function that translates a php var into a JavaScript value
    *
    * @access protected
    * @param mixed var Any type of variable
    * @param boolean quote Should a string be quoted
    * @return mixed JavaScript value of the variable
    */
function _var2JS($var, $quote = true) {
       $type = gettype($var);
       if ($type == "array") {
           $value = $this->_array2JS($var);
       } elseif ($type == "boolean") {
           $value = ($var) ? 'true' : 'false';
       } elseif ($type == "integer" || $type == "double") {
           $value = $var;
       } else {
           $value = ($quote) ? "'$var'" : $var;
       }
       return $value;
} /**
    * Return JavaSript representation of an array (hash and indexed)
    *
    * @access protected
    * @return string
    */
function _array2JS($value) {
       $hash = true;
       if (is_array($value)) {
           $key = key($value);
           if (is_numeric($key) && $key === 0) {
               $hash = false;
           }
           reset($value);
       }
       $js  = ($hash) ? '{ ' : '[ ';
       $i = 1;
       $j = count($value);
       foreach ($value as $k => $v) {
           $quote = true;
           if ($hash) {
               $js .= "'$k' : ";
               // workaround for function callbacks (sf.net patch #1441561)
               switch($k) {
                   case 'disableFunc':
                   case 'dateStatusFunc':
                   case 'flatCallback':
                   case 'onSelect':
                   case 'onClose':
                   case 'onUpdate':
                       $quote = false;
                       break;
               }
           }
           $js .= $this->_var2JS($v, $quote);
           if ($i++ < $j) {
               $js .= ', ';
           }
       }
       $js .= ($hash) ? ' }' : ' ]';
       return $js;
} } ?> [/code] and from my form builder i call it so: [code] function preGenerateForm() { HTML_QuickForm::registerElementType('jscalendar', '/srv/www/prgs/extPear/jscalendar.php', 'HTML_QuickForm_jscalendar'); $options1 = array( 'baseURL' => '/js/jscalendar/', 'styleCss' => 'calendar-win2k-1.css', 'language' => 'en', 'image' => array(
       'src' => '/js/jscalendar/calendar.gif',
       'border' => 0
), 'setup' => array(
       'inputField' => 'listadata',
       'ifFormat' => '%d/%m/%Y"',
       'showsTime' => false,
       'time24' => true,
       'weekNumbers' => false,
       'showOthers' => true,
'multiple' => true ) ); $attributes1 = array( 'readonly' => '1', 'id' => 'listadata' ); //* $box=array(); $box[]=& HTML_QuickForm::createElement('textarea', 'listadata', null, $attributes1); $box[]=& HTML_QuickForm::createElement('jscalendar', 'listadata_calendar', null, $options1); $group =& new HTML_QuickForm_group('group1', 'Date 1:', $box,null,false); $this->fb_preDefElements['listadata'] =& $group; } [/code] now i need to implement better setDefault .. because i can choose my set of data.. i can save my choosed data .. but when i show my calendar there isn't any data selected ..

« previous php.pear.general (#25658) next »