Esempio n. 1
0
 public function action_register()
 {
     $this->request->response = new View_User_Register();
     $user = new Model_Vendo_User();
     $address = new Model_Vendo_Address();
     if ($_POST) {
         $user_post = arr::get($_POST, 'user', array());
         $address_post = arr::get($_POST, 'address', array());
         $validate = Validate::factory(array('password' => arr::get($user_post, 'password'), 'repeat_password' => arr::get($user_post, 'repeat_password')))->rule('repeat_password', 'not_empty')->rule('password', 'matches', array('repeat_password'));
         $roles = arr::get($user_post, 'role_id', array());
         unset($_POST['user']['role_id']);
         unset($_POST['user']['repeat_password']);
         $user->set_fields($user_post);
         $address->set_fields($address_post);
         try {
             // See if the user entered any address information
             $entered_address = FALSE;
             foreach ($address_post as $address_info) {
                 if ($address_info) {
                     $entered_address = TRUE;
                     break;
                 }
             }
             if ($entered_address) {
                 $address->save();
                 $user->address_id = $address->id;
             } else {
                 $user->address_id = NULL;
             }
             $user->save($validate);
             foreach ($roles as $role) {
                 $user->vendo_roles = $role;
             }
             Request::instance()->redirect('home');
         } catch (AutoModeler_Exception $e) {
             $this->request->response->errors = (string) $e;
             // If we've saved an address, get rid of it because it's junk
             // This can happen if the address is valid on the page, but the
             // user is not
             if ($address->id) {
                 $address->delete();
             }
         }
     }
     $this->request->response->user = $user;
     $this->request->response->address = $address;
 }