May 21, 2012

pear quickform jquery calendar

usiamo il calendario delle jquery come campo “datetime”

demo:

http://tecnoxtutti.com/xot/extPear/sample/example_jquery_datetime.php

download:

[download id="3"]

screen shot:

file jquery_datetime.php

<?php
/*
$pathz = array($_SERVER['DOCUMENT_ROOT'].'/PEAR/PEAR/');
set_include_path(implode(PATH_SEPARATOR,$pathz));
*/
// Load the main class
/*
require_once 'HTML/QuickForm.php';
require_once 'HTML/QuickForm/element.php';
require_once 'HTML/QuickForm/date.php';
*/
//require_once("HTML/QuickForm/text.php");
require_once ('HTML/QuickForm/date.php');

HTML_QuickForm::registerElementType('jquery_datetime', __FILE__, 'HTML_QuickForm_jquery_datetime');

class HTML_QuickForm_jquery_datetime extends HTML_QuickForm_date{
    var $_name = '';
    var $_options = array(
        'language'         => 'it',
        'format'           => 'd/m/Y H:i',
        'minYear'          => 2001,
        'maxYear'          => 2010,
        'addEmptyOption'   => false,
        'emptyOptionValue' => '',
        'emptyOptionText'  => ' ',
        'optionIncrement'  => array('i' => 1, 's' => 1)
    );

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
	 function setValue($v) {
	    //echo  '<h3>setValue(</h3><pre>';print_r($v);echo ')</pre>';
		if (is_null($v))		{
			return null;
		}
		if (!is_array($v)) {
			$dateTime =  strtotime($v);
			//echo '<br/>[1['.$dateTime.']]'.date('d/m/Y H:i',$dateTime);
			return parent::setValue($v);
			//return parent::setValue($dateTime_arr);
		}
		if (isset($v['dmy'])) {
		    //echo '<br/>[dmy]['.$v['dmy'].']]';
			if($v['dmy']=='') $v['dmy']=date('d/m/Y');
			list($v['d'], $v['m'], $v['Y']) = explode('/', $v['dmy']);
		}
		if(!isset($v['H'])) $v['H']=0;
		if(!isset($v['i'])) $v['i']=0;

		if (is_array($v['H'])) {
			$v['H'] = $v['H'][0];
			$v['i'] = $v['i'][0];
			$v['m'] = $v['m'][0];
			$v['d'] = $v['d'][0];
			$v['Y'] = $v['Y'][0];
		}
		if (isset($v['m']) && isset($v['d']) && isset($v['Y'])) {
		    //echo '<br/>mktime('.$v['H'].' , '.$v['i'].' , 0, $v['m'], $v['d'], $v['Y']);
			$dateTime = mktime($v['H'] *1, $v['i']*1, 0, $v['m']*1, $v['d']*1, $v['Y']*1);
		}else {
			$dateTime = mktime($v['H'] , $v['i'], 0);
		}
		//echo '[2['.$dateTime.']]'.date('d/m/Y H:i',$dateTime);
		return parent::setValue($dateTime);

    }
///////////////////////////////////////////////////////////////////////////////////////////////	

    function toHtml()    {
        $html = '';
		$v = $this->getValue();
        if ($this->_flagFrozen) {
            setlocale(LC_TIME, 'ita');
			if (is_array($v['H'])) {
				$v['H'] = $v['H'][0];
				$v['i'] = $v['i'][0];
				$v['m'] = $v['m'][0];
				$v['d'] = $v['d'][0];
				$v['Y'] = $v['Y'][0];
			}
            $timestamp = mktime($v['H'], $v['i'], 0, $v['m'], $v['d'], $v['Y']);
            return utf8_encode(strftime("%A %d %B %Y - %H:%M",$timestamp));
        }
		//*/
        if (!defined('HTML_QUICKFORM_jquery_LOADED')) {
			$html.= '<link type="text/css" href="/css/ui-lightness/jquery-ui-1.8.1.custom.css" rel="stylesheet" />
			<script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
			<script type = "text/javascript" src = "/js/jquery-ui-1.8.1.custom.min.js" > </script> ';
            define('HTML_QUICKFORM_jquery_LOADED', true);
        }
		 if (!defined('HTML_QUICKFORM_jquery_datetime_LOADED')) {
	//$(document).ready(function() {
		//$('#rows_15121_data').datepicker({ dateFormat: 'dd/mm/yy' });
			$html.= "<script type=\"text/javascript\">
				$(function() {
				//$('.datepick').each(function(){
				//	$(this).datepicker({ dateFormat: 'dd/mm/yy' });
				//});
				$('.datepick').datepicker({ dateFormat: 'dd/mm/yy' });
			});
			</script>";
		define('HTML_QUICKFORM_jquery_datetime_LOADED', true);
        }
		//$html .= parent::toHtml();

if(isset($v['H'])) if (is_array($v['H'])) {
			$v['H'] = $v['H'][0];
			$v['i'] = $v['i'][0];
}
if(isset($v['m'])) if (is_array($v['m'])) {
			$v['m'] = $v['m'][0];
			$v['d'] = $v['d'][0];
			$v['Y'] = $v['Y'][0];
		}
if(isset($v['d']) && isset($v['m']) && isset($v['Y'])){
	$v['dmy'] = $v['d'].'/'.$v['m'].'/'.$v['Y'];
}
 $html.= '';

if(strpos($this->_options['format'],'d')!==false && strpos($this->_options['format'],'m')!==false && strpos($this->_options['format'],'Y')!==false){
	$html.= '<span><input id="'.$this->_name.'" name="'.$this->_name.'[dmy]" type="text"  value="'.$v['dmy'].'" size="12" class="datepick" />';
	$html.= '<label for="data_end[dmy]">GG/MM/AAAA</label></span> ';
}
if(strpos($this->_options['format'],'H')!==false && strpos($this->_options['format'],'i')!==false){
	$html.='
<span><input id="'.$this->_name.'[H]"
name="'.$this->_name.'[H]"
class="field text"
size="2"
type="text"
maxlength="2"
value="'.$v['H'].'"  /> :
<label for="'.$this->_name.'[H]">Ore</label></span>

<span><input id="'.$this->_name.'[i]"
name="'.$this->_name.'[i]"
class="field text "
size="2"
type="text"
maxlength="2"
value="'.$v['i'].'"  />
<label for="'.$this->_name.'[i]">Min</label></span>';
}
    return $html;
    }
////////////////////////////////////////
    function setName($name)    {
        $this->_name = $name;
    }
////////////////////////////////////////
    function getName()    {
        return $this->_name;
    }    

/////////////////////////////////////////////////////
//-----------------------------------
function onQuickFormEvent($event, $arg, &$caller){
    switch ($event) {
        case 'updateValue':
		    $dmy=$caller->_submitValues[$this->_name]['dmy'];
			list($d, $m, $Y) = explode('/',$dmy);
			$H = 0;
			$i = 0;
			if(isset($caller->_submitValues[$this->_name]['H'])) $H=$caller->_submitValues[$this->_name]['H'];
			if (isset($caller->_submitValues[$this->_name]['i'])) $i = $caller->_submitValues[$this->_name]['i'];
			$v = array('d' => $d,
						'm' => $m,
						'Y' => $Y,
						'H' => $H,
						'i' => $i,
						'dmy' => $dmy);
			$value = $this->_findValue($caller->_constantValues);
            if (null === $value) {
                $value = $this->_findValue($caller->_submitValues);
                if (null === $value) {
                    $value = $this->_findValue($caller->_defaultValues);
                }
            }
            if (null !== $value) {
			    $txt="";
				if($v!=""){
					$caller->_submitValues[$this->getName()]=$v;
					$this->_value=$v;
					$this->setValue($v);
				}else{
					$this->setValue($value);
				}
            }else if($v!=""){
				$caller->_submitValues[$this->getName()]=$v;
				//echo "cccccccc[".$v."]";
				$this->_value=$v;
				$this->setValue($v);
			}
        break;
        default:
            parent::onQuickFormEvent($event, $arg, $caller);
    }
	//*/
	//parent::onQuickFormEvent($event, $arg, $caller);
    return true;
} // end func onQuickFormEvent
//------------------------------------------------------------------------------------
}//end class

?>

file example_jquery_datetime.php

<html>
<head>
<?php
error_reporting(E_ALL);
$type="jquery_datetime";
?>
<title><?php echo $type; ?> examples</title>
</head>
<body>
<h1><?php echo $type; ?> examples</h1>
<?php
//http://www.netsols.de/pear/
$pearpath = $_SERVER['DOCUMENT_ROOT'].'/PEAR/PEAR/';
define('xotpath',$_SERVER['DOCUMENT_ROOT'].'/xot/');
$pearpath=str_replace('/htdocs//','',$pearpath); // correzione per hosting register
$pathz = array_unique(array($pearpath,get_include_path()));
set_include_path(implode(PATH_SEPARATOR,$pathz));
require_once 'HTML/QuickForm.php';
require_once xotpath.'/extPear/'.$type.'.php';
$form = new HTML_QuickForm;
$form->addElement('header', 'header1', $type.' examples');
$form->addElement($type, 'test', 'test');
$form->addElement('date', 'test1', 'test1',array('format'=>'dmY - Hi'));
$form->addElement('static', null, ' ');
$form->addElement('submit', '__submit__', 'Go!');
if ($form->validate()) {
    //$form->freeze();
	$form->process('show');
}
echo $form->toHtml();

function show($param) {
	echo '<pre>';
	print_r($param);
	echo '</pre>';
}

?>
</body>
</html>

Speak Your Mind

*