Show
Ignore:
Timestamp:
02/13/06 12:15:03 (6 years ago)
Author:
john
Message:

fixed a bunch of helpers

Location:
trunk/trax/vendor/trax/action_view/helpers
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/trax/vendor/trax/action_view/helpers/date_helper.php

    r128 r131  
    2424 
    2525class DateHelper extends Helpers { 
    26  
     26    public $selected_years = array(); 
    2727    function __construct($object_name = null, $attribute_name = null) { 
    2828        parent::__construct(); 
     
    3232 
    3333    private function value() { 
    34         if(!$value = $_REQUEST[$this->object_name][$this->attribute_name]) { 
     34        if(!$value = $this->check_request_for_value()) { 
    3535            $object = $this->object(); 
    3636            if(is_object($object) && $this->attribute_name) { 
     
    4040        return $value; 
    4141    } 
     42     
     43    private function check_request_for_value() { 
     44        if(!$value = $_REQUEST[$this->object_name][$this->attribute_name]) { 
     45            # check if this a date / datetime 
     46            if($year_value = $_REQUEST[$this->object_name][$this->attribute_name."(1i)"]) { 
     47                $this->request_years[$this->attribute_name] = $year_value;     
     48            } 
     49            if($month_value = $_REQUEST[$this->object_name][$this->attribute_name."(2i)"]) { 
     50                $this->request_months[$this->attribute_name] = $month_value;     
     51            } 
     52            if($day_value = $_REQUEST[$this->object_name][$this->attribute_name."(3i)"]) { 
     53                $this->request_days[$this->attribute_name] = $day_value;     
     54            } 
     55            if($minute_value = $_REQUEST[$this->object_name][$this->attribute_name."(4i)"]) { 
     56                $this->request_minutes[$this->attribute_name] = $minute_value;     
     57            }    
     58            if($hour_value = $_REQUEST[$this->object_name][$this->attribute_name."(5i)"]) { 
     59                $this->request_hours[$this->attribute_name] = $hour_value;     
     60            }               
     61            if($second_value = $_REQUEST[$this->object_name][$this->attribute_name."(6i)"]) { 
     62                $this->request_seconds[$this->attribute_name] = $second_value;     
     63            }                                                                    
     64        }    
     65        return $value;      
     66    } 
    4267 
    4368    private function object($object_name = null) { 
     
    4772 
    4873    private function select_html($type, $options, $prefix = null, $include_blank = false, $discard_type = false) { 
    49         $select_html  = "<select name=\"$prefix"; 
    50         if(!$discard_type) $select_html .= "[$type]"; 
     74        $select_html  = "<select name=\"$prefix";        
     75        if(!$discard_type) { 
     76            if($prefix) $select_html .= "[";  
     77            $select_html .= $type; 
     78            if($prefix) $select_html .= "]";  
     79        } 
    5180        $select_html .= "\">\n"; 
    5281        if($include_blank) $select_html .= "<option value=\"\"></option>\n"; 
     
    5887    private function leading_zero_on_single_digits($number) { 
    5988        return $number > 9 ? $number : "0$number"; 
     89    } 
     90     
     91    function expiration_date_select($options = array()) { 
     92        return $this->to_expiration_date_select_tag($options);       
    6093    } 
    6194         
     
    68101    #   datetime_select("post", "written_on") 
    69102    #   datetime_select("post", "written_on", array("start_year" => 1995)) 
    70     function date_select($options = array()) {      
     103    function date_select($options = array()) {    
    71104        return $this->to_date_select_tag($options); 
    72     }            
     105    }   
     106     
     107    function select_expiration_date($date = null, $options = array()) { 
     108        $options['month_before_year'] = true;       
     109        $options['use_month_numbers'] = true;    
     110        $options['start_year'] = date("Y"); 
     111        $options['end_year'] = date("Y") + 7; 
     112        $options['field_seperator'] = " / ";         
     113        $options['field_name'] = $options['year_name'] ? $options['year_name'] : "expiration_year";  
     114        $date = ($_REQUEST[$options['field_name']]) ? date("Y-m-d", strtotime($_REQUEST[$options['field_name']]."-01-01")) : date("Y-m-d"); 
     115        $year_select = $this->select_year($date, $options); 
     116        $options['field_name'] = $options['month_name'] ? $options['month_name'] : "expiration_month"; 
     117        $date = ($_REQUEST[$options['field_name']]) ? date("Y-m-d", strtotime("2006-".$_REQUEST[$options['field_name']]."-01")) : date("Y-m-d"); 
     118        $month_select = $this->select_month($date, $options); 
     119        if($options['month_before_year']) { 
     120            $select_html =  $month_select . $options['field_seperator'] .  $year_select;      
     121        } else { 
     122            $select_html =  $year_select . $options['field_seperator'] .  $month_select; 
     123        } 
     124        return $select_html; 
     125    }                
    73126 
    74127    # Returns a set of html select-tags (one for year, month, and day) pre-selected with the +date+. 
     
    103156    function select_second($datetime, $options = array()) { 
    104157        $second_options = ""; 
    105  
    106         for($second = 0; $second <= 59; $second++) { 
     158         
     159        if($this->request_seconds[$this->attribute_name]) { 
     160            $datetime_sec = $this->request_seconds[$this->attribute_name];     
     161        } elseif(strlen($datetime) == 2 && is_numeric($datetime)) { 
     162            $datetime_sec = $datetime; 
     163        } else {                   
     164            $datetime = $datetime ? $datetime : date("Y-m-d H:i:s");  
    107165            $datetime_sec = date("s",strtotime($datetime)); 
     166        } 
     167         
     168        for($second = 0; $second <= 59; $second++) {           
    108169            $second_options .= ($datetime && ($datetime_sec == $second)) ? 
    109170            "<option value=\"".$this->leading_zero_on_single_digits($second)."\"  selected=\"selected\">".$this->leading_zero_on_single_digits($second)."</option>\n" : 
     
    120181    function select_minute($datetime, $options = array()) { 
    121182        $minute_options = ""; 
    122  
    123         for($minute = 0; $minute <= 59; $minute++) { 
     183         
     184        if($this->request_minutes[$this->attribute_name]) { 
     185            $datetime_min = $this->request_minutes[$this->attribute_name];     
     186        } elseif(strlen($datetime) == 2 && is_numeric($datetime)) { 
     187            $datetime_min = $datetime; 
     188        } else {                   
     189            $datetime = $datetime ? $datetime : date("Y-m-d H:i:s");  
    124190            $datetime_min = date("i",strtotime($datetime)); 
     191        } 
     192         
     193        for($minute = 0; $minute <= 59; $minute++) {         
    125194            $minute_options .= ($datetime && ($datetime_min == $minute)) ? 
    126195            "<option value=\"".$this->leading_zero_on_single_digits($minute)."\"  selected=\"selected\">".$this->leading_zero_on_single_digits($minute)."</option>\n" : 
     
    136205    function select_hour($datetime, $options = array()) { 
    137206        $hour_options = ""; 
     207         
     208        if($this->request_hours[$this->attribute_name]) { 
     209            $datetime_hour = $this->request_hours[$this->attribute_name];     
     210        } elseif(strlen($datetime) == 2 && is_numeric($datetime)) { 
     211            $datetime_hour = $datetime; 
     212        } else {                   
     213            $datetime = $datetime ? $datetime : date("Y-m-d H:i:s");  
     214            $datetime_hour = date("H",strtotime($datetime));  
     215        } 
    138216 
    139217        for($hour = 0; $hour <= 23; $hour++) { 
    140             $datetime_hour = date("H",strtotime($datetime)); 
     218             
    141219            $hour_options .= ($datetime && ($datetime_hour == $hour)) ? 
    142220            "<option value=\"".$this->leading_zero_on_single_digits($hour)."\"  selected=\"selected\">".$this->leading_zero_on_single_digits($hour)."</option>\n" : 
     
    152230    function select_day($datetime, $options = array()) { 
    153231        $day_options = ""; 
    154  
    155         for($day = 1; $day <= 31; $day++) { 
    156             $datetime_day = date("d",strtotime($datetime)); 
     232         
     233        if($this->request_days[$this->attribute_name]) { 
     234            $datetime_day = $this->request_days[$this->attribute_name];     
     235        } elseif(strlen($datetime) == 2 && is_numeric($datetime)) { 
     236            $datetime_day = $datetime; 
     237        } else {                   
     238            $datetime = $datetime ? $datetime : date("Y-m-d H:i:s");  
     239            $datetime_day = date("d",strtotime($datetime));   
     240        } 
     241         
     242        for($day = 1; $day <= 31; $day++) {         
    157243            $day_options .= ($datetime && ($datetime_day == $day)) ? 
    158244            "<option value=\"".$this->leading_zero_on_single_digits($day)."\"  selected=\"selected\">".$this->leading_zero_on_single_digits($day)."</option>\n" : 
     
    176262    function select_month($date, $options = array()) { 
    177263        $month_options = ""; 
    178         $date_month = date("m",strtotime($date)); 
     264         
     265        if($this->request_months[$this->attribute_name]) { 
     266            $date_month = $this->request_months[$this->attribute_name];     
     267        } elseif(strlen($date) != 2 && !is_numeric($date)) { 
     268            $date = $date ? $date : date("Y-m-d");  
     269            $date_month = date("m",strtotime($date));     
     270        } 
     271    
    179272        for($month_number = 1; $month_number <= 12; $month_number++) { 
    180273            if($options['use_month_numbers']) { 
     
    205298    function select_year($date, $options = array()) { 
    206299        $year_options = ""; 
    207         $y = $date ? date("Y",strtotime($date)) : date("Y"); 
     300 
     301        if($this->request_years[$this->attribute_name]) { 
     302            $date_year = $this->request_years[$this->attribute_name];     
     303        } elseif(strlen($date) == 4 && is_numeric($date)) { 
     304            $date_year = $date;      
     305        } else { 
     306            $date_year = $date ? date("Y",strtotime($date)) : date("Y"); 
     307        }  
    208308 
    209309        $start_year = ($options['start_year']) ? $options['start_year'] : $y - 5; 
     
    211311 
    212312        for($year = $start_year; $year <= $end_year; $year++) { 
    213             $date_year = date("Y",strtotime($date)); 
    214313            $year_options .= ($date && ($date_year == $year)) ? 
    215314            "<option value=\"$year\" selected=\"selected\">$year</option>\n" : 
     
    225324        $options  = array_merge($defaults, $options); 
    226325        $options_with_prefix = array(); 
    227         for($i=1; $i < 4 ; $i++) { 
     326        for($i=1 ; $i <= 3 ; $i++) { 
    228327            $options_with_prefix[$i] = array_merge($options, array('prefix' => "{$this->object_name}[{$this->attribute_name}({$i}i)]")); 
    229328        }         
     
    237336        } 
    238337 
    239         $date_select = ''; 
     338        $date_select = array(); 
    240339        if($options['month_before_year']) { 
    241340            $options['order'] = array('month', 'year', 'day'); 
     
    253352        foreach($options['order'] as $param) { 
    254353            if(!$discard[$param]) { 
    255                 $date_select .= call_user_func(array($this, "select_$param"),  $date, $options_with_prefix[$position[$param]]); 
    256             } 
     354                $date_select[] = call_user_func(array($this, "select_$param"),  $date, $options_with_prefix[$position[$param]]); 
     355            } 
     356        } 
     357         
     358        if(count($date_select)) { 
     359            $seperator = $options['field_seperator'] ? $options['field_seperator'] : " "; 
     360            $date_select = implode($seperator, $date_select);             
    257361        } 
    258362 
     
    264368        $options = array_merge($defaults, $options); 
    265369        $options_with_prefix = array(); 
    266         for($i=1; $i < 6 ; $i++) { 
     370        for($i=1 ; $i < 6 ; $i++) { 
    267371            $options_with_prefix[$i] = array_merge($options, array('prefix' => "{$this->object_name}[{$this->attribute_name}({$i}i)]")); 
    268372        } 
     
    288392        return $datetime_select; 
    289393    } 
     394     
     395    function to_expiration_date_select_tag($options = array()) { 
     396        $options['discard_day'] = true;  
     397        $options['month_before_year'] = true;       
     398        $options['use_month_numbers'] = true;    
     399        $options['start_year'] = date("Y"); 
     400        $options['end_year'] = date("Y") + 7; 
     401        $options['field_seperator'] = " / "; 
     402        return $this->to_date_select_tag($options);                
     403    }   
    290404 
    291405} 
     
    308422} 
    309423 
     424# select_expiration_date($datetime = null, $options = array()) 
     425function select_expiration_date() { 
     426    $date_helper = new DateHelper(); 
     427    $args = func_get_args(); 
     428    return call_user_func_array(array($date_helper, 'select_expiration_date'), $args);         
     429} 
     430 
    310431function datetime_select($object, $attribute, $options = array()) { 
    311432    $date_helper = new DateHelper($object, $attribute); 
    312     $args = func_get_args(); 
    313     return call_user_func_array(array($date_helper, 'datetime_select'), $options);     
     433    return $date_helper->datetime_select($options);     
    314434} 
    315435 
    316436function date_select($object, $attribute, $options = array()) { 
    317437    $date_helper = new DateHelper($object, $attribute); 
    318     $args = func_get_args(); 
    319     return call_user_func_array(array($date_helper, 'date_select'), $options);     
     438    return $date_helper->date_select($options);     
     439} 
     440 
     441function expiration_date_select($object, $attribute, $options = array()) { 
     442    $date_helper = new DateHelper($object, $attribute); 
     443    return $date_helper->expiration_date_select($options);         
     444} 
     445 
     446function select_year() { 
     447    $date_helper = new DateHelper(); 
     448    $args = func_get_args(); 
     449    return call_user_func_array(array($date_helper, 'select_year'), $args);     
     450} 
     451 
     452function select_month() { 
     453    $date_helper = new DateHelper(); 
     454    $args = func_get_args(); 
     455    return call_user_func_array(array($date_helper, 'select_month'), $args);     
     456} 
     457 
     458function select_day() { 
     459    $date_helper = new DateHelper(); 
     460    $args = func_get_args(); 
     461    return call_user_func_array(array($date_helper, 'select_day'), $args);     
    320462} 
    321463 
  • trunk/trax/vendor/trax/action_view/helpers/form_helper.php

    r130 r131  
    3939            $object = $this->object(); 
    4040            if(is_object($object) && $this->attribute_name) { 
    41                 $attribute = $this->attribute_name; 
    42                 if(!$value = $object->$attribute) { 
    43                     $value = $object->send($attribute); 
    44                 } 
     41                $value = $object->send($this->attribute_name); 
    4542            } 
    4643        } 
     
    115112 
    116113    function to_text_area_tag($options = array()) { 
    117         if (isset($options["size"])) { 
    118             $options["cols"] = reset(explode('x', $options["size"])); 
    119             $options["rows"] = end(explode('x', $options["size"])); 
    120             unset($options["size"]); 
    121         }           
    122114        $options = array_merge($this->default_text_area_options, $options); 
    123         $options = $this->add_default_name_and_id($options);       
     115        $options = $this->add_default_name_and_id($options); 
    124116        return $this->error_wrapping($this->content_tag("textarea", htmlspecialchars($this->value()), $options),$this->object()->errors[$this->attribute_name]); 
    125117    } 
  • trunk/trax/vendor/trax/action_view/helpers/form_tag_helper.php

    r128 r131  
    2626 
    2727 
    28     private function convert_options($options) { 
     28    private function convert_options($options = array()) { 
    2929        $arr = array('disabled', 'readonly', 'multiple'); 
    3030        foreach($arr as $a) { 
    31             $this->boolean_attribute($options, $a); 
     31            $this->boolean_attribute(&$options, $a); 
    3232        } 
    3333        return $options; 
    3434    } 
    3535 
    36     private function boolean_attribute($options, $attribute) { 
    37         if(isset($options[$attribute])) { 
     36    private function boolean_attribute(&$options, $attribute) { 
     37        if($options[$attribute]) { 
    3838            $options[$attribute] = $attribute; 
    3939        } else { 
     
    8080 
    8181    function text_area_tag($name, $content = null, $options = array()) { 
    82         if (isset($options["size"])) { 
     82        if ($options["size"]) { 
    8383            $options["cols"] = reset(explode('x', $options["size"])); 
    8484            $options["rows"] = end(explode('x', $options["size"]));