Changeset 1473

Show
Ignore:
Timestamp:
12/09/07 01:55:10 (9 months ago)
Author:
Shadowhand
Message:

Optimize User_Edit

Files:

Legend:

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

    r1472 r1473  
    5050                        echo Kohana::debug('user edited!', $form->as_array()); 
    5151                } 
    52                 else 
    53                 { 
    54                         echo $form; 
    55                 } 
     52 
     53                echo $form; 
    5654        } 
    5755 
  • trunk/modules/forge/libraries/Form_Input.php

    r1464 r1473  
    111111                else 
    112112                { 
    113                         $this->label = ($val === TRUE) ? ucwords(inflector::humanize($this->name)) : $val; 
     113                        $this->data['label'] = ($val === TRUE) ? ucwords(inflector::humanize($this->name)) : $val; 
    114114                        return $this; 
    115115                } 
  • trunk/modules/forge/models/user_edit.php

    r1472 r1473  
    1717                $this->form = new Forge($action, $title); 
    1818 
    19                 foreach(self::$fields[$this->table] as $field => $meta) 
     19                $this->form->input('username')->label(TRUE)->rules('required|length[5,32]')->value($this->object->username); 
     20                $this->form->input('email')->label(TRUE)->rules('required|length[5,127]|valid_email')->value($this->object->email); 
     21                $this->form->password('password')->label(TRUE)->rules('length[5,64]'); 
     22                $this->form->password('confirm')->label(TRUE)->matches($this->form->password); 
     23 
     24                // Make sure that the username does not already exist 
     25                $this->form->username->callback(array($this, 'is_existing_user')); 
     26 
     27                if ($this->object->id == 0) 
    2028                { 
    21                         // User id and login data is not editable 
    22                         if (substr($field, -2) == 'id' or $field == 'logins') 
    23                                 continue; 
    24  
    25                         if ($field == 'password') 
    26                         { 
    27                                 // Add password and confirm password fields 
    28                                 $this->form->password($field)->label(TRUE)->rules('length[5,32]'); 
    29                                 $this->form->password('confirm')->label(TRUE)->matches($this->form->password); 
    30  
    31                                 if ($this->object->id == 0) 
    32                                 { 
    33                                         // Password fields are required for new users 
    34                                         $this->form->password->rules('+required'); 
    35                                 } 
    36                         } 
    37                         else 
    38                         { 
    39                                 // All fields are required by default 
    40                                 $rules = 'required'; 
    41  
    42                                 if (isset($meta['length'])) 
    43                                 { 
    44                                         $rules .= '|length['.(empty($meta['exact']) ? '1,' : '').$meta['length'].']'; 
    45                                 } 
    46  
    47                                 if ($field == 'email') 
    48                                 { 
    49                                         $rules .= '|valid_email'; 
    50                                 } 
    51  
    52                                 // Add an input for this field with the value of the field 
    53                                 $this->form->input($field)->label(TRUE)->rules($rules)->value($this->object->$field); 
    54                         } 
     29                        // Password fields are required for new users 
     30                        $this->form->password->rules('+required'); 
    5531                } 
    5632 
    57                 // Find all roles 
    58                 $roles = new Role_Model(); 
    59                 $roles = $roles->find(ALL); 
    60  
    61                 $options = array(); 
    62                 foreach($roles as $role) 
    63                
    64                       // Add each role to the options 
    65                       $options[$role->name] = isset($this->roles[$role->id]); 
    66                
    67  
    68                 // Create a checklist of roles 
    69                 $this->form->checklist('roles')->options($options)->label(TRUE); 
     33                // // Find all roles 
     34                // $roles = new Role_Model(); 
     35                // $roles = $roles->find(ALL); 
     36                //  
     37                // $options = array(); 
     38                // foreach($roles as $role) 
     39                //
     40                //    // Add each role to the options 
     41                //    $options[$role->name] = isset($this->roles[$role->id]); 
     42                //
     43                //  
     44                // // Create a checklist of roles 
     45                // $this->form->checklist('roles')->options($options)->label(TRUE); 
    7046 
    7147                // Add the save button 
    7248                $this->form->submit('Save'); 
     49        } 
     50 
     51        public function is_existing_user($input) 
     52        { 
     53                if ($this->object->username == $input->value) 
     54                        return TRUE; 
     55 
     56                if (self::$db->count_records($this->table, array('username' => $input->value)) > 0) 
     57                { 
     58                        $input->add_error(__FUNCTION__, 'The username <strong>'.$input->value.'</strong> is already in use.'); 
     59                        return FALSE; 
     60                } 
     61 
     62                return TRUE; 
    7363        } 
    7464 
     
    8777 
    8878                        // Remove the roles from data 
    89                         $roles = arr::remove('roles', $data); 
     79                        isset($data['roles']) and $roles = arr::remove('roles', $data); 
    9080 
    9181                        foreach($data as $field => $val) 
     
    9787                        if ($status = parent::save()) 
    9888                        { 
    99                                 if ($new_user) 
    100                                
    101                                       foreach($roles as $role) 
    102                                       { 
    103                                               // Add the user roles 
    104                                               $this->add_role($role); 
    105                                       } 
    106                                
    107                                 else 
    108                                
    109                                       foreach(array_diff($this->roles, $roles) as $role) 
    110                                       { 
    111                                               // Remove roles that were deactivated 
    112                                               $this->remove_role($role); 
    113                                       } 
    114  
    115                                       foreach(array_diff($roles, $this->roles) as $role) 
    116                                       { 
    117                                               // Add new roles 
    118                                               $this->add_role($role); 
    119                                       } 
    120                                
     89                                // if ($new_user) 
     90                                //
     91                                //    foreach($roles as $role) 
     92                                //    { 
     93                                //            // Add the user roles 
     94                                //            $this->add_role($role); 
     95                                //    } 
     96                                //
     97                                // else 
     98                                //
     99                                //    foreach(array_diff($this->roles, $roles) as $role) 
     100                                //    { 
     101                                //            // Remove roles that were deactivated 
     102                                //            $this->remove_role($role); 
     103                                //    } 
     104                                //  
     105                                //    foreach(array_diff($roles, $this->roles) as $role) 
     106                                //    { 
     107                                //            // Add new roles 
     108                                //            $this->add_role($role); 
     109                                //    } 
     110                                //
    121111                        } 
    122112 
     
    140130        } 
    141131 
    142 } 
     132} // End User Edit Model