Changeset 99 for trunk

Show
Ignore:
Timestamp:
12/15/05 12:27:34 (6 years ago)
Author:
john
Message:

Added Form Helpers to action_view

Location:
trunk/trax/vendor/trax
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/trax/vendor/trax/action_controller.php

    r89 r99  
    188188                    $GLOBALS['current_controller_path'] = "$this->added_path/$this->controller"; 
    189189                    $GLOBALS['current_controller_name'] = $this->controller; 
     190                    $GLOBALS['current_controller_object'] = $this->controller_object; 
    190191                } 
    191192 
  • trunk/trax/vendor/trax/action_view.php

    r82 r99  
    2727# Include all the sub helper classes 
    2828include_once(TRAX_LIB_ROOT . "/action_view/helpers/url_helper.php"); 
    29 #include_once(TRAX_LIB_ROOT . "/action_view/helpers/form_helper.php"); 
     29include_once(TRAX_LIB_ROOT . "/action_view/helpers/form_helper.php"); 
    3030#include_once(TRAX_LIB_ROOT . "/action_view/helpers/text_helper.php"); 
    3131#include_once(TRAX_LIB_ROOT . "/action_view/helpers/active_record_helper.php"); 
  • trunk/trax/vendor/trax/action_view/helpers.php

    r96 r99  
    2929        $this->controller_name = $GLOBALS['current_controller_name']; 
    3030        $this->controller_path = $GLOBALS['current_controller_path']; 
     31        $this->controller_object = $GLOBALS['current_controller_object']; 
    3132    } 
    3233 
     
    3536            $html = array(); 
    3637            foreach($options as $key => $value) { 
    37                 $html[] = "$key = \"".htmlspecialchars($value, ENT_QUOTES)."\""; 
     38                $html[] = "$key=\"".htmlspecialchars($value, ENT_QUOTES)."\""; 
    3839            } 
    3940            sort($html); 
  • trunk/trax/vendor/trax/action_view/helpers/form_helper.php

    r96 r99  
    2626class FormHelper extends Helpers { 
    2727 
    28  
     28    function __construct($object_name, $field_name) { 
     29        parent::__construct(); 
     30        $this->object_name = $object_name; 
     31        $this->field_name = $field_name; 
     32        $this->default_field_options = $GLOBALS['DEFAULT_FIELD_OPTIONS'] ? $GLOBALS['DEFAULT_FIELD_OPTIONS'] : array("size" => 30); 
     33        $this->default_radio_options = $GLOBALS['DEFAULT_RADIO_OPTIONS'] ? $GLOBALS['DEFAULT_RADIO_OPTIONS'] : array(); 
     34        $this->default_text_area_options = $GLOBALS['DEFAULT_TEXT_AREA_OPTIONS'] ? $GLOBALS['DEFAULT_TEXT_AREA_OPTIONS'] : array("cols" => 40, "rows" => 20); 
     35        $this->default_date_options = $GLOBALS['DEFAULT_DATE_OPTIONS'] ? $GLOBALS['DEFAULT_DATE_OPTIONS'] : array(":discard_type" => true); 
     36    } 
     37 
     38    function value() { 
     39        $object = $this->object(); 
     40        if(is_object($object) && $this->field_name) { 
     41            return $object->send($this->field_name); 
     42        } 
     43    } 
     44 
     45    function object() { 
     46        $object_name = $this->object_name; 
     47        return $this->controller_object->$object_name; 
     48    } 
     49 
     50    function tag_name() { 
     51        return "{$this->object_name}[{$this->field_name}]"; 
     52    } 
     53 
     54    function tag_name_with_index($index) { 
     55        return "{$this->object_name}[{$index}][{$this->field_name}]"; 
     56    } 
     57 
     58    function tag_id() { 
     59        return "{$this->object_name}_{$this->field_name}"; 
     60    } 
     61 
     62    function tag_id_with_index($index) { 
     63        return "{$this->object_name}_{$index}_{$this->field_name}"; 
     64    } 
     65 
     66    function add_default_name_and_id($options) { 
     67        if(array_key_exists("index", $options)) { 
     68            $options["name"] = $options["name"] ? $options["name"] : $this->tag_name_with_index($options["index"]); 
     69            $options["id"] = $options["id"] ? $options["id"] : $this->tag_id_with_index($options["index"]); 
     70            unset($options["index"]); 
     71        } elseif($this->auto_index) { 
     72            $options["name"] = $options["name"] ? $options["name"] : $this->tag_name_with_index($this->auto_index); 
     73            $options["id"] = $options["id"] ? $options["id"] : $this->tag_id_with_index($this->auto_index); 
     74        } else { 
     75            $options["name"] = $options["name"] ? $options["name"] : $this->tag_name(); 
     76            $options["id"] = $options["id"] ? $options["id"] : $this->tag_id(); 
     77        } 
     78        return $options; 
     79    } 
     80 
     81    function to_input_field_tag($field_type, $options = array()) { 
     82        $default_size = $options["maxlength"] ? $options["maxlength"] : $this->default_field_options['size']; 
     83        $options["size"] = $options["size"] ? $options["size"] : $default_size; 
     84        $options = array_merge($this->default_field_options, $options); 
     85        if($field_type == "hidden") { 
     86            unset($options["size"]); 
     87        } 
     88        $options["type"] = $field_type; 
     89        if($field_type != "file") { 
     90            $options["value"] = $options["value"] ? $options["value"] : $this->value(); 
     91        } 
     92        $options = $this->add_default_name_and_id($options); 
     93        return $this->tag("input", $options); 
     94    } 
     95 
     96    function to_radio_button_tag($tag_value, $options = array()) { 
     97        $options = array_merge($this->default_radio_options, $options); 
     98        $options["type"] = "radio"; 
     99        $options["value"] = $tag_value; 
     100        if($this->value() == $tag_value) { 
     101            $options["checked"] = "checked"; 
     102        } 
     103        $pretty_tag_value = preg_replace('/\s/', "_", preg_replace('/\W/', "", strtolower($tag_value))); 
     104        $options["id"] = $this->auto_index ? 
     105            "{$this->object_name}_{$this->auto_index}_{$this->field_name}_{$pretty_tag_value}" : 
     106            "{$this->object_name}_{$this->field_name}_{$pretty_tag_value}"; 
     107        $options = $this->add_default_name_and_id($options); 
     108        return $this->tag("input", $options); 
     109    } 
     110 
     111    function to_text_area_tag($options = array()) { 
     112        $options = array_merge($this->default_text_area_options, $options); 
     113        $options = $this->add_default_name_and_id($options); 
     114        return $this->content_tag("textarea", htmlspecialchars($this->value()), $options); 
     115    } 
     116 
     117    function to_check_box_tag($options = array(), $checked_value = "1", $unchecked_value = "0") { 
     118        $options["type"] = "checkbox"; 
     119        $options["value"] = $checked_value; 
     120        switch(gettype($this->value())) { 
     121            case 'boolean': 
     122                $checked = $this->value(); 
     123                break; 
     124            case 'NULL': 
     125                $checked = false; 
     126                break; 
     127            case 'integer': 
     128                $checked = ($this->value() != 0); 
     129                break; 
     130            case 'string': 
     131                $checked = ($this->value() == $checked_value); 
     132                break; 
     133            default: 
     134                $checked = ($this->value() != 0); 
     135        } 
     136 
     137        if ($checked || $options["checked"] == "checked") { 
     138            $options["checked"] = "checked"; 
     139        } else { 
     140            unset($options["checked"]); 
     141        } 
     142 
     143        $options = $this->add_default_name_and_id($options); 
     144        return $this->tag("input", $options) . $this->tag("input", array("name" => $options["name"], "type" => "hidden", "value" => $unchecked_value)); 
     145    } 
     146 
     147    function to_boolean_select_tag($options = array()) { 
     148        $options = $this->add_default_name_and_id($options); 
     149        $tag_text = "<select"; 
     150        $tag_text .= $this->tag_options($options); 
     151        $tag_text .= ">\n"; 
     152        $tag_text .= "<option value=\"0\""; 
     153        if(!$this->value() == false) { 
     154            $tag_text .= " selected"; 
     155        } 
     156        $tag_text .= ">False</option>\n"; 
     157        $tag_text .= "<option value=\"1\""; 
     158        if($this->value()) { 
     159            $tag_text .= " selected"; 
     160        } 
     161        $tag_text .= ">True</option>\n"; 
     162        $tag_text .= "</select>\n"; 
     163        return $tag_text; 
     164    } 
     165 
     166} 
     167 
     168 
     169################################################################################################ 
     170## Avialble functions for use in views 
     171################################################################################################ 
     172# Example: text_field("post", "title"); 
     173# Result: <input type="text" id="post_title" name="post[title]" value="$post->title" /> 
     174function text_field($object, $field, $options = array()) { 
     175    $form = new FormHelper($object, $field); 
     176    return $form->to_input_field_tag("text", $options); 
     177} 
     178 
     179# Works just like text_field, but returns a input tag of the "password" type instead. 
     180# Example: password_field("user", "password"); 
     181# Result: <input type="password" id="user_password" name="user[password]" value="$user->password" /> 
     182function password_field($object, $field, $options = array()) { 
     183    $form = new FormHelper($object, $field); 
     184    return $form->to_input_field_tag("password", $options); 
     185} 
     186 
     187# Works just like text_field, but returns a input tag of the "hidden" type instead. 
     188# Example: hidden_field("post", "title"); 
     189# Result: <input type="hidden" id="post_title" name="post[title]" value="$post->title" /> 
     190function hidden_field($object, $field, $options = array()) { 
     191    $form = new FormHelper($object, $field); 
     192    return $form->to_input_field_tag("hidden", $options); 
     193} 
     194 
     195# Works just like text_field, but returns a input tag of the "file" type instead, which won't have any default value. 
     196function file_field($object, $field, $options = array()) { 
     197    $form = new FormHelper($object, $field); 
     198    return $form->to_input_field_tag("file", $options); 
     199} 
     200 
     201# Example: text_area("post", "body", array("cols" => 20, "rows" => 40)); 
     202# Result: <textarea cols="20" rows="40" id="post_body" name="post[body]">$post->body</textarea> 
     203function text_area($object, $field, $options = array()) { 
     204    $form = new FormHelper($object, $field); 
     205    return $form->to_text_area_tag($options); 
     206} 
     207 
     208# Returns a checkbox tag tailored for accessing a specified attribute (identified by $field) on an object 
     209# assigned to the template (identified by $object). It's intended that $field returns an integer and if that 
     210# integer is above zero, then the checkbox is checked. Additional $options on the input tag can be passed as an 
     211# array with $options. The $checked_value defaults to 1 while the default $unchecked_value 
     212# is set to 0 which is convenient for boolean values. Usually unchecked checkboxes don't post anything. 
     213# We work around this problem by adding a hidden value with the same name as the checkbox. 
     214# 
     215# Example: Imagine that $post->validated is 1: 
     216#   check_box("post", "validated"); 
     217# Result: 
     218#   <input type="checkbox" id="post_validate" name="post[validated] value="1" checked="checked" /> 
     219#   <input name="post[validated]" type="hidden" value="0" /> 
     220# 
     221# Example: Imagine that $puppy->gooddog is no: 
     222#   check_box("puppy", "gooddog", array(), "yes", "no"); 
     223# Result: 
     224#     <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog] value="yes" /> 
     225#     <input name="puppy[gooddog]" type="hidden" value="no" /> 
     226function check_box($object, $field, $options = array(), $checked_value = "1", $unchecked_value = "0") { 
     227    $form = new FormHelper($object, $field); 
     228    return $form->to_check_box_tag($options, $checked_value, $unchecked_value); 
     229} 
     230 
     231# Returns a radio button tag for accessing a specified attribute (identified by $field) on an object 
     232# assigned to the template (identified by $object). If the current value of $field is $tag_value the 
     233# radio button will be checked. Additional $options on the input tag can be passed as a 
     234# hash with $options. 
     235# Example: Imagine that $post->category is "trax": 
     236#   radio_button("post", "category", "trax"); 
     237#   radio_button("post", "category", "java"); 
     238# Result: 
     239#     <input type="radio" id="post_category" name="post[category] value="trax" checked="checked" /> 
     240#     <input type="radio" id="post_category" name="post[category] value="java" /> 
     241# 
     242function radio_button($object, $field, $tag_value, $options = array()) { 
     243    $form = new FormHelper($object, $field); 
     244    return $form->to_radio_button_tag($tag_value, $options); 
    29245} 
    30246 
  • trunk/trax/vendor/trax/action_view/helpers/url_helper.php

    r96 r99  
    6565    # Returns the URL for the set of +options+ provided. 
    6666    function url_for($options = array()) { 
     67        $url_base = null; 
    6768        $url = array(); 
    6869        if(is_string($options)) { 
     
    7071        } else { 
    7172            if($_SERVER['SERVER_PORT'] == 443) { 
    72                 $url[] = "https://".$_SERVER['HTTP_HOST']; 
     73                $url_base = "https://".$_SERVER['HTTP_HOST']; 
    7374            } else { 
    74                 $url[] = "http://".$_SERVER['HTTP_HOST']; 
     75                $url_base = "http://".$_SERVER['HTTP_HOST']; 
    7576            } 
    7677            if(array_key_exists(":controller", $options)) { 
     
    9697            } 
    9798        } 
    98         return implode("/", $url); 
     99        return $url_base . implode("/", $url); 
    99100    } 
    100101