Beispiel #1
0
 /**
  * Change user info action
  * @param null $userId
  */
 public function actionEdit($userId = null)
 {
     $userId = null === $userId ? Rays::user()->id : $userId;
     $user = User::get($userId);
     RAssert::not_null($user);
     if (Rays::user()->roleId != Role::ADMINISTRATOR_ID && Rays::user()->id != $userId) {
         $this->flash("error", "You don't have the right to change the user information!");
         $this->redirectAction('user', 'view', $userId);
     }
     $data = array('user' => $user);
     if (Rays::isPost()) {
         $config = array(array('field' => 'username', 'label' => 'User name', 'rules' => 'trim|required|min_length[5]|max_length[20]'));
         // if set password, then go changing password
         if (isset($_POST['password']) && $_POST['password'] != '') {
             array_push($config, array('field' => 'password', 'label' => 'New Password', 'rules' => 'trim|required|min_length[6]|max_length[20]'));
             array_push($config, array('field' => 'password-confirm', 'label' => 'New Password Confirm', 'rules' => 'trim|required|min_length[6]|max_length[20]|equals[password]'));
         }
         $validation = new RValidation($config);
         if ($validation->run()) {
             if (isset($_POST['password']) && $_POST['password'] != '') {
                 // set new password
                 $user->password = md5($_POST['password']);
             }
             $user->name = $_POST['username'];
             foreach (User::$mapping as $objCol => $dbCol) {
                 if (in_array($objCol, ["password", "email", "id", "roleId", "credit", "private"])) {
                     continue;
                 }
                 if (isset($_POST[$objCol])) {
                     $user->{$objCol} = $_POST[$objCol];
                 }
             }
             $user->save();
             $this->flash("message", "Update information successfully.");
             // if picture selected
             if (isset($_FILES['user_picture']) && $_FILES['user_picture']['name'] != '') {
                 $pictureName = "pic_u_" . $user->id . RUpload::get_extension($_FILES['user_picture']['name']);
                 $upload = new RUpload(["file_name" => $pictureName, "upload_path" => Rays::app()->getBaseDir() . "/../" . User::PICTURE_DIR]);
                 $upload->upload('user_picture');
                 if ($upload->error != '') {
                     $this->flash("error", $upload->error);
                 } else {
                     $user->picture = "files/images/users/" . $upload->file_name;
                     $user->save();
                     RImage::updateStyle($user->picture, User::getPicOptions());
                 }
             }
             if (Rays::user()->id == $user->id) {
                 $this->redirectAction("user", "profile");
             } else {
                 $this->redirectAction("user", "view", [$user->id, "profile"]);
             }
         } else {
             $errors = $validation->getErrors();
             $data['validation_errors'] = $errors;
             $data['editForm'] = $_POST;
         }
     }
     $this->layout = 'user';
     $this->setHeaderTitle("Edit profile - " . $user->name);
     $this->render('edit', $data, false);
 }
Beispiel #2
0
 public function uploadPicture($fileTag)
 {
     $uploadPath = Rays::app()->getBaseDir() . '/../' . self::PICTURE_PATH;
     $picName = 'group_' . $this->id . RUpload::get_extension($_FILES[$fileTag]['name']);
     $upload = new RUpload(array('file_name' => $picName, 'upload_path' => $uploadPath));
     $upload->upload($fileTag);
     if ($upload->error != '') {
         return $upload->error;
     } else {
         $this->picture = "files/images/groups/" . $upload->file_name;
         $this->save();
         RImage::updateStyle($this->picture, static::getPicOptions());
         return true;
     }
 }