Beispiel #1
0
 public function action_edit()
 {
     $request = $this->request->current();
     $id = (int) Request::current()->param('id');
     $helper_orm = ORM_Helper::factory('admin');
     $orm = $helper_orm->orm();
     if (!$this->acl->is_allowed($this->user, $orm, 'edit')) {
         throw new HTTP_Exception_404();
     } else {
         $this->left_menu_user_add();
     }
     if ((bool) $id) {
         $orm->and_where('id', '=', $id)->find();
         if (!$orm->loaded() or in_array($orm->username, $this->_exclude_admins)) {
             throw new HTTP_Exception_404();
         }
         $this->title = __('Edit admin');
     } else {
         $this->title = __('Add admin');
     }
     if (empty($this->back_url)) {
         $this->back_url = Route::url('admin', array('controller' => 'admins'));
     }
     if ($this->is_cancel) {
         $request->redirect($this->back_url);
     }
     $errors = array();
     $submit = Request::$current->post('submit');
     if ($submit) {
         try {
             if ((bool) $id) {
                 $orm->updater_id = $this->user->id;
                 $orm->updated = date('Y-m-d H:i:s');
             } else {
                 $orm->creator_id = $this->user->id;
             }
             $values = $request->post();
             if (!empty($values['password'])) {
                 $ex_validation = Model_Admin::get_password_validation($values);
             } else {
                 $ex_validation = NULL;
                 unset($values['password']);
             }
             $helper_orm->save($values, $ex_validation);
         } catch (ORM_Validation_Exception $e) {
             $errors = $this->errors_extract($e);
         }
     }
     // If add action then $submit = NULL
     if (!empty($errors) or $submit != 'save_and_exit') {
         $sites = ORM::factory('site')->find_all()->as_array('id', 'name');
         $this->template->set_filename('admins/edit')->set('errors', $errors)->set('helper_orm', $helper_orm)->set('roles', $this->acl_roles())->set('sites', $sites);
     } else {
         $request->redirect($this->back_url);
     }
 }