Changeset 1438

Show
Ignore:
Timestamp:
12/06/07 23:17:17 (7 months ago)
Author:
Shadowhand
Message:

Updated Forge with some new syntax. Good ideas, PugFish?!

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/modules/forge/libraries/Forge.php

    r1435 r1438  
    2323        public function __get($key) 
    2424        { 
    25                 if ($key == 'validated'
     25                if (isset($this->inputs[$key])
    2626                { 
    27                         foreach($this->inputs as $input) 
    28                         { 
    29                                 if ( ! $input->is_valid) 
    30                                         return FALSE; 
    31                         } 
    32                         return TRUE; 
    33                 } 
    34                 elseif (isset($this->inputs[$key])) 
    35                 { 
    36                         return $this->inputs[$key]->value; 
     27                        return $this->inputs[$key]; 
    3728                } 
    3829        } 
    3930 
    40         public function add($input
     31        public function __call($method, $args
    4132        { 
     33                // Class name 
     34                $input = 'Form_'.ucfirst($method); 
     35 
     36                // Create the input 
     37                $input = new $input(empty($args) ? NULL : current($args)); 
     38 
    4239                if ( ! ($input instanceof Form_Input)) 
    4340                        throw new Kohana_Exception('forge.invalid_input'); 
     
    4542                if ($name = $input->name) 
    4643                { 
     44                        // Assign by name 
    4745                        $this->inputs[$name] = $input; 
    4846                } 
     
    5250                } 
    5351 
    54                 return $this
     52                return $input
    5553        } 
    5654 
    57         public function data() 
     55        public function validate() 
     56        { 
     57                $status = TRUE; 
     58                foreach($this->inputs as $input) 
     59                { 
     60                        if ($input->validate() == FALSE) 
     61                        { 
     62                                $status = FALSE; 
     63                        } 
     64                } 
     65 
     66                return $status; 
     67        } 
     68 
     69        public function as_array() 
    5870        { 
    5971                if (empty($_POST)) 
  • trunk/modules/forge/libraries/Form_Checklist.php

    r1435 r1438  
    1212        protected $protect = array('name', 'type'); 
    1313 
    14         // Name of the list 
    15         protected $list_name = ''; 
    16  
    17         // Associative array of options: value => checked 
    18         protected $list_options = array(); 
    19  
    20         // List input data 
    21         protected $list_data = array(); 
    22  
    23         public function __construct($name, $data) 
     14        public function __construct($name) 
    2415        { 
    25                 $this->list_name = $this->data['name'] = $name; 
    26  
    27                 $this->list_options = arr::remove('options', $data); 
    28  
    29                 foreach($data as $key => $val) 
    30                 { 
    31                         $this->$key = $val; 
    32                 } 
     16                $this->data['name'] = $name; 
    3317        } 
    3418 
     
    3721                if ($key == 'value') 
    3822                { 
    39                         return isset($_POST[$this->list_name]) ? $_POST[$this->list_name] : array(); 
     23                        // Return the currently checked values 
     24                        return array_keys($this->data['options'], TRUE); 
    4025                } 
    4126 
     
    4530        public function html() 
    4631        { 
    47                 // Load the submitted value 
    48                 $this->load_value(); 
    49  
    5032                // Import base data 
    5133                $base_data = $this->data; 
     34 
     35                // Make it an array 
     36                $base_data['name'] .= '[]'; 
    5237 
    5338                // Newline 
     
    5540 
    5641                $checklist = '<ul class="'.arr::remove('class', $base_data).'">'.$nl; 
    57                 foreach($this->list_options as $val => $checked) 
     42                foreach(arr::remove('options', $base_data) as $val => $checked) 
    5843                { 
    5944                        // New set of input data 
     
    6146 
    6247                        // Set the name, value, and checked status 
    63                         $data['name']    = $this->list_name.'[]'; 
    6448                        $data['value']   = $val; 
    6549                        $data['checked'] = $checked; 
     
    6953                $checklist .= '</ul>'; 
    7054 
    71                 return $checklist
     55                return $checklist.$this->error_message()
    7256        } 
    7357 
     
    7761                        return; 
    7862 
    79                 foreach($this->list_options as $val => $checked) 
     63                foreach($this->data['options'] as $val => $checked) 
    8064                { 
    81                         if (empty($_POST[$this->list_name])) 
     65                        if (empty($_POST[$this->data['name']])) 
    8266                        { 
    83                                 $this->list_options[$val] = FALSE; 
     67                                $this->data['options'][$val] = FALSE; 
    8468                        } 
    8569                        else 
    8670                        { 
    87                                 $this->list_options[$val] = in_array($val, $_POST[$this->list_name]); 
     71                                $this->data['options'][$val] = in_array($val, $_POST[$this->data['name']]); 
    8872                        } 
    8973                } 
  • trunk/modules/forge/libraries/Form_Dropdown.php

    r1436 r1438  
    66        ( 
    77                'name'  => '', 
    8                 'type'  => 'dropdown', 
    98                'class' => 'dropdown', 
    109        ); 
    1110 
    12         protected $protect = array('name', 'type'); 
    13  
    14         // Name of the list 
    15         protected $list_name = ''; 
    16  
    17         // Associative array of options: value => checked 
    18         protected $list_options = array(); 
    19  
    20         // List input data 
    21         protected $list_data = array(); 
    22  
    23         public function __construct($name, $data) 
    24         { 
    25                 $this->list_name = $this->data['name'] = $name; 
    26  
    27                 $this->list_options = arr::remove('options', $data); 
    28  
    29                 foreach($data as $key => $val) 
    30                 { 
    31                         $this->$key = $val; 
    32                 } 
    33         } 
     11        protected $protect = array('type'); 
    3412 
    3513        public function __get($key) 
     
    3715                if ($key == 'value') 
    3816                { 
    39                         return isset($_POST[$this->list_name]) ? $_POST[$this->list_name] : array()
     17                        return $this->default
    4018                } 
    4119 
     
    4523        public function html() 
    4624        { 
    47                 // Load the submitted value 
    48                 $this->load_value()
     25                // Import base data 
     26                $base_data = $this->data
    4927 
    50                 // Import base data and options 
    51                 $base_data = $this->data
    52                 $options = $this->list_options
     28                // Get the options and default selection 
     29                $options = arr::remove('options', $base_data)
     30                $default = arr::remove('default', $base_data)
    5331 
    54                 return form::dropdown(arr::remove('name', $base_data), $options, arr::remove('default', $base_data)); 
     32                // Add an empty option to the beginning of the options 
     33                arr::unshift_assoc($options, '', '--'); 
     34 
     35                return form::dropdown($base_data, $options, $default).$this->error_message(); 
    5536        } 
    5637 
     
    6041                        return; 
    6142 
    62                 foreach($this->list_options as $val => $checked
     43                if ($default = self::$input->post($this->name)
    6344                { 
    64                         if (empty($_POST[$this->list_name])) 
    65                         { 
    66                                 $this->list_options[$val] = FALSE; 
    67                         } 
    68                         else 
    69                         { 
    70                                 $this->list_options[$val] = in_array($val, $_POST[$this->list_name]); 
    71                         } 
     45                        $this->data['default'] = $default; 
    7246                } 
    7347        } 
     48 
    7449} // End Form Dropdown 
  • trunk/modules/forge/libraries/Form_Input.php

    r1435 r1438  
    33class Form_Input_Core { 
    44 
    5         // Input data 
     5        // Input instance 
     6        protected static $input; 
     7 
     8        // Element data 
    69        protected $data = array 
    710        ( 
     
    1417 
    1518        // Validtion rules 
    16         protected $rules = ''
     19        protected $rules = array()
    1720 
    1821        // Validation check 
     
    2225        protected $errors = array(); 
    2326 
    24         public function __construct($name, $data = array()) 
    25         { 
    26                 if ( ! is_array($data)) 
    27                 { 
    28                         // Set the data to the value 
    29                         $data = array('value' => $data); 
    30                 } 
    31  
    32                 if ( ! empty($name)) 
    33                 { 
    34                         // Set the name 
    35                         $data['name'] = $name; 
    36                 } 
    37  
    38                 if ($rules = arr::remove('rules', $data)) 
    39                 { 
    40                         // Set the rules 
    41                         $this->rules = $rules; 
    42                 } 
    43  
    44                 foreach($data as $key => $val) 
    45                 { 
    46                         // Load the data using __set, for protection 
    47                         $this->$key = $val; 
    48                 } 
    49  
    50                 // Load the value 
    51                 $this->load_value(); 
    52         } 
    53  
    54         public function __set($key, $val) 
    55         { 
    56                 if ( ! in_array($key, $this->protect)) 
    57                 { 
    58                         $this->data[$key] = $val; 
    59                 } 
     27        public function __construct($name) 
     28        { 
     29                if (self::$input === NULL) 
     30                { 
     31                        self::$input = new Input; 
     32                } 
     33 
     34                $this->data['name'] = $name; 
     35        } 
     36 
     37        public function __call($method, $args) 
     38        { 
     39                if ($method == 'rules') 
     40                { 
     41                        $this->add_rules(explode('|', $args[0])); 
     42                } 
     43                elseif ($method == 'name') 
     44                { 
     45                        // Yup, do nothing. The name should stay static once it is set. 
     46                } 
     47                else 
     48                { 
     49                        $this->data[$method] = $args[0]; 
     50                } 
     51 
     52                return $this; 
    6053        } 
    6154 
    6255        public function __get($key) 
    6356        { 
    64                 if ($key === 'is_valid') 
    65                 { 
    66                         // Make sure validation runs 
    67                         is_null($this->is_valid) and $this->validate(); 
    68  
    69                         return $this->is_valid; 
    70                 } 
    71                 elseif (isset($this->data[$key])) 
     57                if (isset($this->data[$key])) 
    7258                { 
    7359                        return $this->data[$key]; 
     
    7561        } 
    7662 
    77         public function label(
    78         { 
    79                 if ($this->label != ''
     63        public function label($val = NULL
     64        { 
     65                if ($val === NULL
    8066                { 
    8167                        return form::label($this->name, $this->label); 
     68                } 
     69                else 
     70                { 
     71                        $this->label = ($val === TRUE) ? ucfirst($this->name) : $val; 
     72                        return $this; 
    8273                } 
    8374        } 
     
    9283 
    9384                // Remove the label 
    94                 unset($data['label']); 
     85                unset($data['label'], $data['options'], $data['default']); 
    9586 
    9687                return form::input($data).$this->error_message(); 
     88        } 
     89 
     90        protected function add_rules( array $rules) 
     91        { 
     92                foreach($rules as $rule) 
     93                { 
     94                        if ( ! in_array($rule, $this->rules)) 
     95                        { 
     96                                $this->rules[] = $rule; 
     97                        } 
     98                } 
    9799        } 
    98100 
     
    113115        protected function load_value() 
    114116        { 
    115                 if ($name = $this->name) 
    116                 { 
    117                         if (isset($_POST[$name])) 
    118                         { 
    119                                 $this->data['value'] = $_POST[$name]; 
    120                         } 
    121                 } 
    122         } 
    123  
    124         protected function validate() 
     117                if ($value = self::$input->post($this->name)) 
     118                { 
     119                        $this->data['value'] = $value; 
     120                } 
     121        } 
     122 
     123        public function validate() 
    125124        { 
    126125                // Validation has already run 
     
    141140                if ( ! empty($this->rules)) 
    142141                { 
    143                         foreach(explode('|', $this->rules) as $rule) 
     142                        foreach($this->rules as $rule) 
    144143                        { 
    145144                                if (($offset = strpos($rule, '[')) !== FALSE) 
     
    191190        protected function rule_required() 
    192191        { 
    193                 if (empty($this->data['value'])
     192                if ($this->value == NULL
    194193                { 
    195194                        $this->errors[] = 'This field is required.'; 
  • trunk/modules/forge/libraries/Form_Submit.php

    r1435 r1438  
    2424        } 
    2525 
    26         protected function validate() 
     26        public function validate() 
    2727        { 
    2828                // Submit buttons do not need to be validated