Changeset 1464
- Timestamp:
- 12/08/07 02:01:13 (8 months ago)
- Files:
-
- trunk/modules/forge/controllers/forge_demo.php (modified) (2 diffs)
- trunk/modules/forge/libraries/Form_Dateselect.php (modified) (3 diffs)
- trunk/modules/forge/libraries/Form_Input.php (modified) (1 diff)
- trunk/system/helpers/date.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/modules/forge/controllers/forge_demo.php
r1461 r1464 23 23 $form->input('username')->label(TRUE)->rules('required|length[5,32]'); 24 24 $form->password('password')->label(TRUE)->rules('required|length[5,32]'); 25 $form->password('confirm')->label(TRUE) ;25 $form->password('confirm')->label(TRUE)->matches($form->password); 26 26 $form->checkbox('remember')->label('Remember Me'); 27 27 $form->checklist('foods')->label('Favorite Foods')->options($foods)->rules('required'); 28 28 $form->dropdown('state')->label('Home State')->options(locale_US::states())->rules('required'); 29 $form->dateselect('birthday')->label(TRUE)-> value(time());29 $form->dateselect('birthday')->label(TRUE)->minutes(5); 30 30 $form->submit('Save'); 31 32 $form->confirm->matches($form->password);33 31 34 32 if ($form->validate()) … … 40 38 } 41 39 40 public function bench() 41 { 42 Benchmark::start('using_array'); 43 $output = array(); 44 for ($i = 0; $i < 1000; $i++) 45 { 46 $output[] = ($i % 2 == 0) ? 'a' : 'b'; 47 } 48 $output = implode('', $output); 49 $array = Benchmark::get('using_array'); 50 51 unset($i, $output); 52 53 Benchmark::start('using_string'); 54 $output = ''; 55 for ($i = 0; $i < 1000; $i++) 56 { 57 $output .= ($i % 2 == 0) ? 'a' : 'b'; 58 } 59 60 $string = Benchmark::get('using_string'); 61 62 echo Kohana::debug('imploded array: ', $array, 'string append: ', $string); 63 } 64 42 65 } // End trunk/modules/forge/libraries/Form_Dateselect.php
r1463 r1464 11 11 protected $protect = array('type'); 12 12 13 // Precision for the parts, you can use @ to insert a literal @ symbol 14 protected $parts = array 15 ( 16 'month' => NULL, 17 'day' => 1, 18 'year' => NULL, 19 ' @ ', 20 'hour' => NULL, 21 ':', 22 'minute' => 5, 23 'am_pm' => NULL, 24 ); 25 13 26 public function __construct($name) 14 27 { … … 20 33 } 21 34 35 public function __call($method, $args) 36 { 37 if (isset($this->parts[substr($method, 0, -1)])) 38 { 39 // Set options for date generation 40 $this->parts[substr($method, 0, -1)] = $args[0]; 41 return $this; 42 } 43 44 return parent::__call($method, $args); 45 } 46 22 47 public function html_element() 23 48 { 24 49 // Import base data 25 $base_data = $this->data; 26 $name = $base_data['name']; 50 $data = $this->data; 27 51 28 52 // Get the options and default selection 29 $time = $this->time_array(arr::remove('value', $ base_data));53 $time = $this->time_array(arr::remove('value', $data)); 30 54 31 return form::dropdown($name.'[month]', date::months(), $time['month']).' '. 32 form::dropdown($name.'[day]', date::days(date('m')), $time['day']).' '. 33 form::dropdown($name.'[year]', date::years(), $time['year']).' @ '. 34 form::dropdown($name.'[hour]', date::hours(), $time['hour']).':'. 35 form::dropdown($name.'[minute]', date::minutes(), $time['minute']).' '. 36 form::dropdown($name.'[am_pm]', array('AM' => 'AM', 'PM' => 'PM'), $time['am_pm']); 55 // No labels or values 56 unset($data['label']); 57 58 $input = ''; 59 foreach($this->parts as $type => $option) 60 { 61 if (is_int($type)) 62 { 63 // Just add the separators 64 $input .= $option; 65 continue; 66 } 67 68 // Set this input name 69 $data['name'] = $this->data['name'].'['.$type.']'; 70 71 // Set the selected option 72 $selected = $time[$type]; 73 74 if ($type == 'am_pm') 75 { 76 // Options are static 77 $options = array('AM' => 'AM', 'PM' => 'PM'); 78 } 79 else 80 { 81 // Minute(s), Hour(s), etc 82 $type .= 's'; 83 84 // Use the date helper to generate the options 85 $options = ($option === NULL) ? date::$type() : date::$type($option); 86 } 87 88 $input .= form::dropdown($data, $options, $selected); 89 } 90 91 return $input; 37 92 } 38 93 39 94 protected function time_array($timestamp) 40 95 { 41 $time = array_combine( 96 $time = array_combine 97 ( 42 98 array('month', 'day', 'year', 'hour', 'minute', 'am_pm'), 43 explode('--', date('n--j--Y--g--i--A', $timestamp))); 99 explode('--', date('n--j--Y--g--i--A', $timestamp)) 100 ); 44 101 45 102 // Minutes should always be in 5 minute increments 46 $time['minute'] = num::round($time['minute'], 5);103 $time['minute'] = num::round($time['minute'], $this->parts['minute']); 47 104 48 105 return $time; … … 61 118 $this->data['value'] = mktime 62 119 ( 63 // If the time is PM, add 12 hours for 24 hour time 64 ($time['am_pm'] == 'PM') ? $time['hour'] + 12 : $time['hour'], 120 date::adjust($time['hour'], $time['am_pm']), 65 121 $time['minute'], 66 122 0, 67 123 $time['month'], 68 124 $time['day'], 69 $time['year']); 125 $time['year'] 126 ); 70 127 } 71 128 trunk/modules/forge/libraries/Form_Input.php
r1450 r1464 104 104 if ($val === NULL) 105 105 { 106 return form::label($this->name, $this->label); 106 if ($name = $this->name) 107 { 108 return form::label($name, $this->label); 109 } 107 110 } 108 111 else trunk/system/helpers/date.php
r1437 r1464 202 202 203 203 /** 204 * Adjusts a non-24-hour number into a 24-hour number. 205 * 206 * @param integer hour to adjust 207 * @param string AM or PM 208 * @return string 209 */ 210 public static function adjust($hour, $ampm) 211 { 212 $hour = (int) $hour; 213 $ampm = strtolower($ampm); 214 215 switch($ampm) 216 { 217 case 'am': 218 if ($hour == 12) 219 $hour = 0; 220 break; 221 case 'pm': 222 if ($hour < 12) 223 $hour += 12; 224 break; 225 } 226 227 return sprintf('%02s', $hour); 228 } 229 230 /** 204 231 * Method: days 205 232 * Number of days in month.
