PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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