PageRenderTime 81ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 23ms app.codeStats 1ms

/atk4-addons/mvc/Form/Field/MonthSelector.php

https://github.com/mahimarathore/mahi
PHP | 166 lines | 131 code | 27 blank | 8 comment | 19 complexity | 9aad3468880d8796a08aa8dac9d52065 MD5 | raw file
  1<?php
  2/**
  3 * Created on 31.12.2007 by *Camper* (camper@adevel.com)
  4 */
  5class Form_Field_MonthSelector extends Form_Field{
  6
  7	protected $required = false;
  8
  9	protected $year_from;
 10	protected $year_to;
 11	protected $years = array();
 12	protected $months = array('1'=>'Jan', '2'=>'Feb', '3'=>'Mar', '4'=>'Apr',
 13			'5'=>'May', '6'=>'Jun', '7'=>'Jul', '8'=>'Aug',
 14			'9'=>'Sep', '10'=>'Oct', '11'=>'Nov', '12'=>'Dec');
 15
 16	protected $c_year;
 17	protected $c_month;
 18	protected $date_order=array('m','y');
 19
 20	function init(){
 21		parent::init();
 22
 23		$cur_year = date('Y');
 24		$this->setYearRange($cur_year-1, $cur_year+5);
 25		$this->c_year = $cur_year;
 26		$this->c_month= date('m');
 27	}
 28	function clearFieldValue(){
 29		$this->set(null);
 30	}
 31	function set($value){
 32		// value can be passed as YYYY-MM or as full date (YYYY-MM-DD)
 33		if(strtotime($value)===false)$value.='-01';
 34		$tm = strtotime($value);
 35		$yr = date('Y', $tm);
 36		if($yr > $this->year_to)$yr = $this->year_to;
 37		elseif($yr < $this->year_from)$yr = $this->year_from;
 38
 39		$this->c_year = $yr;
 40		$this->c_month= date('m', $tm);
 41
 42		return parent::set($value);
 43	}
 44
 45	function setRequired($is_required){
 46		$this->required = $is_required === true;
 47	}
 48
 49	function setYearRange($from=null, $to=null){
 50		if(!is_numeric($from))
 51			$from = null;
 52		if(!is_numeric($to))
 53			$to = null;
 54
 55		$cur_year = date('Y');
 56		if(($from === null) && ($to === null))
 57			return array($cur_year => $cur_year);
 58
 59		if(($from === null) && ($to !== null)){
 60			$from = ($to < $cur_year)?$to:$cur_year;
 61		}
 62		elseif(($from !== null) && ($to === null)){
 63			$to = ($from > $cur_year)?$from:$cur_year;
 64		}
 65		elseif($from > $to ){
 66			$temp = $to;
 67			$to = $from;
 68			$from=$temp;
 69		}
 70
 71		$res = array();
 72		for($i=$from; $i<=$to; $i++)
 73			$res[$i] = $i;
 74
 75		// correct the c_year value upon range change
 76		if($this->c_year > $to)
 77			$this->c_year = $to;
 78		if($this->c_year < $from)
 79			$this->c_year = $from;
 80
 81		$this->year_from = $from;
 82		$this->year_to = $to;
 83		$this->years = $res;
 84		return $this;
 85	}
 86
 87	function loadPOST(){
 88		if(empty($_POST))
 89			return;
 90
 91		if(isset($_POST[$this->name.'_year']))
 92			$this->c_year = $_POST[$this->name.'_year'];
 93		if(isset($_POST[$this->name.'_month']))
 94			$this->c_month = $_POST[$this->name.'_month'];
 95
 96		$this->set($this->c_year.'-'.sprintf("%02s",$this->c_month).'-01');
 97	}
 98
 99	function validate(){
100		if(false === strtotime($this->value.'-01'))
101			$this->owner->errors[$this->short_name]="Invalid date specified!";
102
103		return parent::validate();
104	}
105
106	function setOrder($order){
107		//pass an array with 'd','m','y' as members to set an order
108		$this->date_order=$order;
109		return $this;
110	}
111
112	function getInput($attr=array()){
113		$output=$this->getTag('span', array('style'=>'white-space: nowrap;'));
114		$onChange=($this->onchange)?$this->onchange->getString():'';
115
116		$xtraattrs = array();
117
118		// month control
119		$m=$this->getTag('select',array_merge(array(
120						'id'=>$this->name.'_month',
121						'name'=>$this->name.'_month',
122						'onchange'=>$onChange
123						), $attr, $this->attr, $xtraattrs)
124				);
125		foreach($this->months as $value=>$descr){
126			$m.=
127				$this->getTag('option',array(
128						'value'=>$value,
129						'selected'=>$value == $this->c_month
130					))
131				.htmlspecialchars($descr)
132				.$this->getTag('/option');
133		}
134		$m.=$this->getTag('/select').'&nbsp;';
135
136		// year control
137		$y=$this->getTag('select',array_merge(array(
138						'id'=>$this->name.'_year',
139						'name'=>$this->name.'_year',
140						'onchange'=>$onChange
141						), $attr, $this->attr, $xtraattrs)
142				);
143		foreach($this->years as $value=>$descr){
144			$y.=
145				$this->getTag('option',array(
146						'value'=>$value,
147						'selected'=>$value == $this->c_year
148					))
149				.htmlspecialchars($descr)
150				.$this->getTag('/option');
151		}
152		$y.=$this->getTag('/select');
153
154		$o1=$this->date_order[0];$o2=$this->date_order[1];
155		$output.=$$o1.$$o2;
156		$output.=$this->getTag('/span');
157		$output.='<!-- '.(is_null($this->value)?'null':$this->value).' -->';
158
159		return $output;
160	}
161
162	function get(){
163		if(parent::get()=='0000-00')return null;
164		return parent::get();
165	}
166}