[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class AphrontFormDateControl extends AphrontFormControl { 4 5 private $initialTime; 6 private $zone; 7 8 private $valueDay; 9 private $valueMonth; 10 private $valueYear; 11 private $valueTime; 12 private $allowNull; 13 14 public function setAllowNull($allow_null) { 15 $this->allowNull = $allow_null; 16 return $this; 17 } 18 19 const TIME_START_OF_DAY = 'start-of-day'; 20 const TIME_END_OF_DAY = 'end-of-day'; 21 const TIME_START_OF_BUSINESS = 'start-of-business'; 22 const TIME_END_OF_BUSINESS = 'end-of-business'; 23 24 public function setInitialTime($time) { 25 $this->initialTime = $time; 26 return $this; 27 } 28 29 public function readValueFromRequest(AphrontRequest $request) { 30 $day = $request->getInt($this->getDayInputName()); 31 $month = $request->getInt($this->getMonthInputName()); 32 $year = $request->getInt($this->getYearInputName()); 33 $time = $request->getStr($this->getTimeInputName()); 34 $enabled = $request->getBool($this->getCheckboxInputName()); 35 36 if ($this->allowNull && !$enabled) { 37 $this->setError(null); 38 $this->setValue(null); 39 return; 40 } 41 42 $err = $this->getError(); 43 44 if ($day || $month || $year || $time) { 45 $this->valueDay = $day; 46 $this->valueMonth = $month; 47 $this->valueYear = $year; 48 $this->valueTime = $time; 49 50 // Assume invalid. 51 $err = 'Invalid'; 52 53 $zone = $this->getTimezone(); 54 55 try { 56 $date = new DateTime("{$year}-{$month}-{$day} {$time}", $zone); 57 $value = $date->format('U'); 58 } catch (Exception $ex) { 59 $value = null; 60 } 61 62 if ($value) { 63 $this->setValue($value); 64 $err = null; 65 } else { 66 $this->setValue(null); 67 } 68 } else { 69 $value = $this->getInitialValue(); 70 if ($value) { 71 $this->setValue($value); 72 } else { 73 $this->setValue(null); 74 } 75 } 76 77 $this->setError($err); 78 79 return $this->getValue(); 80 } 81 82 protected function getCustomControlClass() { 83 return 'aphront-form-control-date'; 84 } 85 86 public function setValue($epoch) { 87 $result = parent::setValue($epoch); 88 89 if ($epoch === null) { 90 return $result; 91 } 92 93 $readable = $this->formatTime($epoch, 'Y!m!d!g:i A'); 94 $readable = explode('!', $readable, 4); 95 96 $this->valueYear = $readable[0]; 97 $this->valueMonth = $readable[1]; 98 $this->valueDay = $readable[2]; 99 $this->valueTime = $readable[3]; 100 101 return $result; 102 } 103 104 private function getMinYear() { 105 $cur_year = $this->formatTime( 106 time(), 107 'Y'); 108 $val_year = $this->getYearInputValue(); 109 110 return min($cur_year, $val_year) - 3; 111 } 112 113 private function getMaxYear() { 114 $cur_year = $this->formatTime( 115 time(), 116 'Y'); 117 $val_year = $this->getYearInputValue(); 118 119 return max($cur_year, $val_year) + 3; 120 } 121 122 private function getDayInputValue() { 123 return $this->valueDay; 124 } 125 126 private function getMonthInputValue() { 127 return $this->valueMonth; 128 } 129 130 private function getYearInputValue() { 131 return $this->valueYear; 132 } 133 134 private function getTimeInputValue() { 135 return $this->valueTime; 136 } 137 138 private function formatTime($epoch, $fmt) { 139 return phabricator_format_local_time( 140 $epoch, 141 $this->user, 142 $fmt); 143 } 144 145 private function getDayInputName() { 146 return $this->getName().'_d'; 147 } 148 149 private function getMonthInputName() { 150 return $this->getName().'_m'; 151 } 152 153 private function getYearInputName() { 154 return $this->getName().'_y'; 155 } 156 157 private function getTimeInputName() { 158 return $this->getName().'_t'; 159 } 160 161 private function getCheckboxInputName() { 162 return $this->getName().'_e'; 163 } 164 165 protected function renderInput() { 166 167 $disabled = null; 168 if ($this->getValue() === null) { 169 $this->setValue($this->getInitialValue()); 170 if ($this->allowNull) { 171 $disabled = 'disabled'; 172 } 173 } 174 175 $min_year = $this->getMinYear(); 176 $max_year = $this->getMaxYear(); 177 178 $days = range(1, 31); 179 $days = array_fuse($days); 180 181 $months = array( 182 1 => pht('Jan'), 183 2 => pht('Feb'), 184 3 => pht('Mar'), 185 4 => pht('Apr'), 186 5 => pht('May'), 187 6 => pht('Jun'), 188 7 => pht('Jul'), 189 8 => pht('Aug'), 190 9 => pht('Sep'), 191 10 => pht('Oct'), 192 11 => pht('Nov'), 193 12 => pht('Dec'), 194 ); 195 196 $checkbox = null; 197 if ($this->allowNull) { 198 $checkbox = javelin_tag( 199 'input', 200 array( 201 'type' => 'checkbox', 202 'name' => $this->getCheckboxInputName(), 203 'sigil' => 'calendar-enable', 204 'class' => 'aphront-form-date-enabled-input', 205 'value' => 1, 206 'checked' => ($disabled === null ? 'checked' : null), 207 )); 208 } 209 210 $years = range($this->getMinYear(), $this->getMaxYear()); 211 $years = array_fuse($years); 212 213 $days_sel = AphrontFormSelectControl::renderSelectTag( 214 $this->getDayInputValue(), 215 $days, 216 array( 217 'name' => $this->getDayInputName(), 218 'sigil' => 'day-input', 219 'disabled' => $disabled, 220 )); 221 222 $months_sel = AphrontFormSelectControl::renderSelectTag( 223 $this->getMonthInputValue(), 224 $months, 225 array( 226 'name' => $this->getMonthInputName(), 227 'sigil' => 'month-input', 228 'disabled' => $disabled, 229 )); 230 231 $years_sel = AphrontFormSelectControl::renderSelectTag( 232 $this->getYearInputValue(), 233 $years, 234 array( 235 'name' => $this->getYearInputName(), 236 'sigil' => 'year-input', 237 'disabled' => $disabled, 238 )); 239 240 $cicon = id(new PHUIIconView()) 241 ->setIconFont('fa-calendar'); 242 243 $cal_icon = javelin_tag( 244 'a', 245 array( 246 'href' => '#', 247 'class' => 'calendar-button', 248 'sigil' => 'calendar-button', 249 ), 250 $cicon); 251 252 $time_sel = javelin_tag( 253 'input', 254 array( 255 'name' => $this->getTimeInputName(), 256 'sigil' => 'time-input', 257 'value' => $this->getTimeInputValue(), 258 'type' => 'text', 259 'class' => 'aphront-form-date-time-input', 260 'disabled' => $disabled, 261 ), 262 ''); 263 264 Javelin::initBehavior('fancy-datepicker', array()); 265 266 return javelin_tag( 267 'div', 268 array( 269 'class' => 'aphront-form-date-container', 270 'sigil' => 'phabricator-date-control', 271 'meta' => array( 272 'disabled' => (bool)$disabled, 273 ), 274 ), 275 array( 276 $checkbox, 277 $days_sel, 278 $months_sel, 279 $years_sel, 280 $cal_icon, 281 $time_sel, 282 )); 283 } 284 285 private function getTimezone() { 286 if ($this->zone) { 287 return $this->zone; 288 } 289 290 $user = $this->getUser(); 291 if (!$this->getUser()) { 292 throw new Exception('Call setUser() before getTimezone()!'); 293 } 294 295 $user_zone = $user->getTimezoneIdentifier(); 296 $this->zone = new DateTimeZone($user_zone); 297 return $this->zone; 298 } 299 300 private function getInitialValue() { 301 $zone = $this->getTimezone(); 302 303 // TODO: We could eventually allow these to be customized per install or 304 // per user or both, but let's wait and see. 305 switch ($this->initialTime) { 306 case self::TIME_START_OF_DAY: 307 default: 308 $time = '12:00 AM'; 309 break; 310 case self::TIME_START_OF_BUSINESS: 311 $time = '9:00 AM'; 312 break; 313 case self::TIME_END_OF_BUSINESS: 314 $time = '5:00 PM'; 315 break; 316 case self::TIME_END_OF_DAY: 317 $time = '11:59 PM'; 318 break; 319 } 320 321 $today = $this->formatTime(time(), 'Y-m-d'); 322 try { 323 $date = new DateTime("{$today} {$time}", $zone); 324 $value = $date->format('U'); 325 } catch (Exception $ex) { 326 $value = null; 327 } 328 329 return $value; 330 } 331 332 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |