1 <?php
2 3 4 5 6 7
8
9 namespace Hawk;
10
11 12 13 14 15
16 class DatetimeInput extends TextInput{
17 18 19
20 public $max = null,
21
22 23 24
25 $min = null,
26
27 28 29
30 $format = null,
31
32 33 34
35 $dbformat = 'Y-m-d';
36
37 38 39 40 41
42 public function __construct($param){
43 parent::__construct($param);
44
45 if(empty($this->format)) {
46 $this->format = Lang::get('main.date-format');
47 }
48
49 }
50
51 52 53 54 55
56 public function display(){
57
58 if(is_numeric($this->value)) {
59 $this->timestamp = $this->value;
60 }
61 else{
62 $this->timestamp = strtotime($this->value);
63 }
64
65 $this->value = date($this->format, $this->timestamp);
66 $this->class .= " datetime";
67
68
69 $picker = array(
70 'format' => Lang::get('main.date-mask'),
71 'orientation' => 'right'
72 );
73 if($this->max) {
74 $picker['endDate'] = $this->max;
75 }
76 if($this->min) {
77 $picker['startDate'] = $this->min;
78 }
79
80 Controller::current()->addJavaScriptInline('$("#' . $this->id . '").datepicker(' . json_encode($picker) . ');');
81 return parent::display();
82 }
83
84
85 86 87 88 89 90 91
92 public function check(&$form = null){
93
94 if(! parent::check($form)) {
95 return false;
96 }
97
98 if($this->value!="") {
99
100 $tmp = date_parse_from_format($this->format, $this->value);
101 if(empty($tmp)) {
102 $form->error($this->errorAt, Lang::get('form.date-format'));
103 return false;
104 }
105
106 if(!checkdate($tmp['month'], $tmp['day'], $tmp['year'])) {
107 $form->error($this->errorAt, Lang::get('form.invalid-date'));
108 return false;
109 }
110
111 }
112 return true;
113 }
114
115
116 117 118 119 120
121 public function dbvalue(){
122 $date = \DateTime::createFromFormat($this->format, $this->value);
123 if($this->dataType == 'int') {
124 return $date->getTimestamp();
125 }
126 else{
127 return $date->format($this->dbformat);
128 }
129 }
130 }
131