Changeset 1464

Show
Ignore:
Timestamp:
12/08/07 02:01:13 (8 months ago)
Author:
Shadowhand
Message:

Updated Forge:

  • Dateselect handles AM/PM properly now
  • Dateselect can now be overloaded to change format (almost)
  • updated /forge_demo
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/modules/forge/controllers/forge_demo.php

    r1461 r1464  
    2323                $form->input('username')->label(TRUE)->rules('required|length[5,32]'); 
    2424                $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)
    2626                $form->checkbox('remember')->label('Remember Me'); 
    2727                $form->checklist('foods')->label('Favorite Foods')->options($foods)->rules('required'); 
    2828                $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); 
    3030                $form->submit('Save'); 
    31  
    32                 $form->confirm->matches($form->password); 
    3331 
    3432                if ($form->validate()) 
     
    4038        } 
    4139 
     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 
    4265} // End 
  • trunk/modules/forge/libraries/Form_Dateselect.php

    r1463 r1464  
    1111        protected $protect = array('type'); 
    1212 
     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 
    1326        public function __construct($name) 
    1427        { 
     
    2033        } 
    2134 
     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 
    2247        public function html_element() 
    2348        { 
    2449                // Import base data 
    25                 $base_data = $this->data; 
    26                 $name = $base_data['name']; 
     50                $data = $this->data; 
    2751 
    2852                // 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)); 
    3054 
    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; 
    3792        } 
    3893 
    3994        protected function time_array($timestamp) 
    4095        { 
    41                 $time = array_combine( 
     96                $time = array_combine 
     97                ( 
    4298                        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                ); 
    44101 
    45102                // 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']); 
    47104 
    48105                return $time; 
     
    61118                $this->data['value'] = mktime 
    62119                ( 
    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']), 
    65121                        $time['minute'], 
    66122                        0, 
    67123                        $time['month'], 
    68124                        $time['day'], 
    69                         $time['year']); 
     125                        $time['year'] 
     126                ); 
    70127        } 
    71128 
  • trunk/modules/forge/libraries/Form_Input.php

    r1450 r1464  
    104104                if ($val === NULL) 
    105105                { 
    106                         return form::label($this->name, $this->label); 
     106                        if ($name = $this->name) 
     107                        { 
     108                                return form::label($name, $this->label); 
     109                        } 
    107110                } 
    108111                else 
  • trunk/system/helpers/date.php

    r1437 r1464  
    202202 
    203203        /** 
     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        /** 
    204231         * Method: days 
    205232         *  Number of days in month.