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

Language PHP Lines 167
MD5 Hash 9aad3468880d8796a08aa8dac9d52065
Repository https://github.com/mahimarathore/mahi.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
 * Created on 31.12.2007 by *Camper* (camper@adevel.com)
 */
class Form_Field_MonthSelector extends Form_Field{

	protected $required = false;

	protected $year_from;
	protected $year_to;
	protected $years = array();
	protected $months = array('1'=>'Jan', '2'=>'Feb', '3'=>'Mar', '4'=>'Apr',
			'5'=>'May', '6'=>'Jun', '7'=>'Jul', '8'=>'Aug',
			'9'=>'Sep', '10'=>'Oct', '11'=>'Nov', '12'=>'Dec');

	protected $c_year;
	protected $c_month;
	protected $date_order=array('m','y');

	function init(){
		parent::init();

		$cur_year = date('Y');
		$this->setYearRange($cur_year-1, $cur_year+5);
		$this->c_year = $cur_year;
		$this->c_month= date('m');
	}
	function clearFieldValue(){
		$this->set(null);
	}
	function set($value){
		// value can be passed as YYYY-MM or as full date (YYYY-MM-DD)
		if(strtotime($value)===false)$value.='-01';
		$tm = strtotime($value);
		$yr = date('Y', $tm);
		if($yr > $this->year_to)$yr = $this->year_to;
		elseif($yr < $this->year_from)$yr = $this->year_from;

		$this->c_year = $yr;
		$this->c_month= date('m', $tm);

		return parent::set($value);
	}

	function setRequired($is_required){
		$this->required = $is_required === true;
	}

	function setYearRange($from=null, $to=null){
		if(!is_numeric($from))
			$from = null;
		if(!is_numeric($to))
			$to = null;

		$cur_year = date('Y');
		if(($from === null) && ($to === null))
			return array($cur_year => $cur_year);

		if(($from === null) && ($to !== null)){
			$from = ($to < $cur_year)?$to:$cur_year;
		}
		elseif(($from !== null) && ($to === null)){
			$to = ($from > $cur_year)?$from:$cur_year;
		}
		elseif($from > $to ){
			$temp = $to;
			$to = $from;
			$from=$temp;
		}

		$res = array();
		for($i=$from; $i<=$to; $i++)
			$res[$i] = $i;

		// correct the c_year value upon range change
		if($this->c_year > $to)
			$this->c_year = $to;
		if($this->c_year < $from)
			$this->c_year = $from;

		$this->year_from = $from;
		$this->year_to = $to;
		$this->years = $res;
		return $this;
	}

	function loadPOST(){
		if(empty($_POST))
			return;

		if(isset($_POST[$this->name.'_year']))
			$this->c_year = $_POST[$this->name.'_year'];
		if(isset($_POST[$this->name.'_month']))
			$this->c_month = $_POST[$this->name.'_month'];

		$this->set($this->c_year.'-'.sprintf("%02s",$this->c_month).'-01');
	}

	function validate(){
		if(false === strtotime($this->value.'-01'))
			$this->owner->errors[$this->short_name]="Invalid date specified!";

		return parent::validate();
	}

	function setOrder($order){
		//pass an array with 'd','m','y' as members to set an order
		$this->date_order=$order;
		return $this;
	}

	function getInput($attr=array()){
		$output=$this->getTag('span', array('style'=>'white-space: nowrap;'));
		$onChange=($this->onchange)?$this->onchange->getString():'';

		$xtraattrs = array();

		// month control
		$m=$this->getTag('select',array_merge(array(
						'id'=>$this->name.'_month',
						'name'=>$this->name.'_month',
						'onchange'=>$onChange
						), $attr, $this->attr, $xtraattrs)
				);
		foreach($this->months as $value=>$descr){
			$m.=
				$this->getTag('option',array(
						'value'=>$value,
						'selected'=>$value == $this->c_month
					))
				.htmlspecialchars($descr)
				.$this->getTag('/option');
		}
		$m.=$this->getTag('/select').'&nbsp;';

		// year control
		$y=$this->getTag('select',array_merge(array(
						'id'=>$this->name.'_year',
						'name'=>$this->name.'_year',
						'onchange'=>$onChange
						), $attr, $this->attr, $xtraattrs)
				);
		foreach($this->years as $value=>$descr){
			$y.=
				$this->getTag('option',array(
						'value'=>$value,
						'selected'=>$value == $this->c_year
					))
				.htmlspecialchars($descr)
				.$this->getTag('/option');
		}
		$y.=$this->getTag('/select');

		$o1=$this->date_order[0];$o2=$this->date_order[1];
		$output.=$$o1.$$o2;
		$output.=$this->getTag('/span');
		$output.='<!-- '.(is_null($this->value)?'null':$this->value).' -->';

		return $output;
	}

	function get(){
		if(parent::get()=='0000-00')return null;
		return parent::get();
	}
}
Back to Top