示例#1
0
 public function saveAction()
 {
     $returnJson = array();
     $returnJson['error'] = false;
     $userModel = new Admin_Model_User();
     $data = array('user_name' => trim($_POST['user_name']), 'user_login' => trim($_POST['user_login']), 'user_email' => trim($_POST['user_email']), 'user_password' => trim($_POST['user_password']));
     $validate = array('user_name' => array('required' => true), 'user_login' => array('required' => true, 'userExists'), 'user_password' => array('pwdTest'), 'user_email' => array('required' => true, 'lengthTest', 'email', 'userExists'));
     if ($_POST['save_type'] == 'update') {
         $data['user_id'] = $_POST['user_id'];
         $validate['user_login'][0] = 'userExistsUpdate';
         $validate['user_email'][2] = 'userExistsUpdate';
         $validate['user_password'][0] = 'pwdTestUpdate';
     }
     if ($userModel->isValidRow($data, $validate)) {
         if ($_POST['save_type'] == 'add') {
             $data['user_password'] = md5(md5($data['user_password'] . K_Registry::get('Configure.salt')));
             $user_id = $userModel->save($data);
             $returnJson['user']['type'] = 'add';
             $returnJson['user']['id'] = $user_id;
         } else {
             if ($_POST['save_type'] == 'update') {
                 if (mb_strlen($data['user_password']) > 0) {
                     $data['user_password'] = md5(md5($data['user_password'] . K_Registry::get('Configure.salt')));
                 } else {
                     unset($data['user_password']);
                 }
                 $user_id = $userModel->update($data, array('user_id' => (int) $_POST['user_id']));
                 $returnJson['user']['type'] = 'update';
                 $user_id = intval($_POST['user_id']);
                 $returnJson['user']['id'] = $_POST['user_id'];
             }
         }
         $userRolesModel = new Admin_Model_UserRole();
         $userRolesModel->remove(K_Db_Select::create()->where(array('usrol_user_id' => $user_id)));
         if (isset($_POST['roles']) && count($_POST['roles']) > 0) {
             foreach ($_POST['roles'] as $v) {
                 $rd['usrol_user_id'] = $user_id;
                 $rd['usrol_role_id'] = intval($v);
                 $rolesData[] = $rd;
             }
             $userRolesModel->saveAll($rolesData);
             $returnJson['user']['roles'] = $_POST['roles'];
         }
         $returnJson['user']['name'] = $data['user_name'];
         $returnJson['user']['login'] = $data['user_login'];
         $returnJson['user']['email'] = $data['user_email'];
         $returnJson['error'] = false;
         $returnJson['msg'] = "<strong>OK:</strong>Пользователь удачно сохранён";
     } else {
         $returnJson['error'] = true;
         $returnJson['msg'] = $userModel->getErrorsD($this->dictionary);
     }
     $this->putJSON($returnJson);
 }