Exemplo n.º 1
0
 /**
  * 
  * @param string $field
  * @param array $attributes
  * @return string
  */
 public function field($field, array $attributes = NULL)
 {
     $field_data = Arr::get($this->form_columns(), $field);
     if ($field_data === NULL) {
         $field_data = array('type' => Arr::path($this->_table_columns, $field . '.data_type'));
     }
     $field_name = $field;
     if (Arr::get($field_data, 'free') !== TRUE) {
         $value = $this->get($field);
     } else {
         $value = NULL;
     }
     if (isset($attributes['prefix'])) {
         $field_name = $attributes['prefix'] . "[{$field_name}]";
         unset($attributes['prefix']);
     }
     $attributes['id'] = $this->object_name() . '_' . $field;
     if (isset($attributes['choices'])) {
         $field_data['choices'] = $attributes['choices'];
         unset($attributes['choices']);
     }
     if (isset($attributes['multiply'])) {
         $field_data['multiply'] = TRUE;
         $field_name .= '[]';
         unset($attributes['multiply']);
     }
     if (!empty($field_data['choices'])) {
         $choices = Callback::invoke($field_data['choices']);
     }
     $input = NULL;
     if (isset($attributes['class']) and !is_array($attributes['class'])) {
         $attributes['class'] = array($attributes['class']);
     }
     if (is_callable($field_data['type'])) {
         $input = call_user_func($field_data['type'], $this, $field, $attributes);
     } else {
         if (is_array($field_data['type'])) {
             $input = Callback::invoke_function($field_data['type'], array($this, $field, $attributes));
         } else {
             switch ($field_data['type']) {
                 case 'textarea':
                 case 'text':
                     $attributes['rows'] = 3;
                     $input = Form::textarea($field_name, $value, $attributes);
                     break;
                 case 'select':
                     $input = Form::select($field_name, $choices, $value, $attributes);
                     break;
                 case 'checkbox':
                     $default = Arr::get($field_data, 'value', 1);
                     $input = Form::label(NULL, Form::checkbox($field_name, $default, $default == $value, $attributes) . ' ' . $this->label($field, NULL, TRUE));
                     break;
                 case 'password':
                     $input = Form::password($field_name, NULL, $attributes);
                     break;
                 default:
                     $input = Form::input($field_name, $value, $attributes);
                     break;
             }
         }
     }
     return $input;
 }