Esempio n. 1
0
 /**
  * Save user
  *
  * @param Illuminate\Http\Request $request
  *
  * @return response
  *
  * @throws \Exception
  */
 public function save(Request $request)
 {
     if ($request->isMethod('POST')) {
         $edit = $request->has('id');
         $user = $edit ? $this->_getUserById($request->get('id')) : $this->user;
         $rules = $this->user->rules();
         $messages = $this->user->messages();
         if ($edit && str_equal($user->username, $request->get('username'))) {
             $rules = remove_rules($rules, ['username.unique:users,username']);
         }
         if ($edit && str_equal($user->email, $request->get('email'))) {
             $rules = remove_rules($rules, ['email.unique:users,email']);
         }
         if ($edit) {
             $rules = remove_rules($rules, ['password.required']);
         }
         $validator = Validator::make($request->all(), $rules, $messages);
         if ($validator->fails()) {
             return back()->withInput()->withErrors($validator, 'all');
         }
         try {
             $except = [];
             if ($request->password === '') {
                 $except = ['password'];
             }
             $user = $this->bind($user, $request->all(), $except);
             if (!$edit) {
                 $user->created_at = new \DateTime();
             }
             $user->updated_at = new \DateTime();
             //Upload avatar
             if ($request->hasFile('avatar')) {
                 $avatarPath = config('back.avatar_path');
                 $file = $request->file('avatar');
                 $filename = new FileName($avatarPath, $file->getClientOriginalExtension());
                 $filename->avatar()->generate();
                 $filename->setPrefix(_const('AVATAR_PREFIX'));
                 $filename->avatar()->group($this->_getAvatarGroup(), false);
                 $upload = new Upload($file);
                 $upload->setDirectory($avatarPath)->setName($filename->getName())->move();
                 $image = new Image($avatarPath . $upload->getName());
                 $image->setDirectory($avatarPath)->resizeGroup($filename->getGroup());
                 delete_file($avatarPath . $upload->getName());
                 $resizes = $image->getResizes();
                 $user->avatar = $resizes['small'];
             }
             $user->save();
         } catch (Exception $ex) {
             throw new \Exception(_t('backend_common_opp') . $ex->getMessage());
         }
         return redirect(route('backend_users'))->with('success', _t('backend_common_saved'));
     }
 }
Esempio n. 2
0
 public function ajaxChangeCover(Request $request)
 {
     if ($request->isMethod('POST')) {
         $rules = $this->_getCoverRules();
         $messages = $this->_getCoverMessages();
         $validator = Validator::make($request->all(), $rules, $messages);
         if ($validator->fails()) {
             return file_pong(['status' => _const('AJAX_ERROR'), 'messages' => $validator->errors()->first()], 403);
         }
         /**
          * 1. Get file, path and info
          * 2. Generate file name
          * 3. Upload
          * 4. Resize
          * 5. Delete old cover images and upload image
          * 6. Save new info
          */
         try {
             // 1
             $store = store();
             $coverPath = config('front.cover_path');
             $file = $request->file('__file');
             // 2
             $filename = new FileName($coverPath, $file->getClientOriginalExtension());
             $filename->cover()->generate();
             $filename->setPrefix(_const('COVER_PREFIX'));
             $filename->cover()->group($this->_getCoverGroup(), true);
             // 3
             $upload = new Upload($file);
             $upload->setDirectory($coverPath)->setName($filename->getName())->move();
             // 4
             $image = new Image($coverPath . $upload->getName());
             $image->setDirectory($coverPath)->resizeGroup($filename->getGroup());
             // 5
             delete_file([$coverPath . $upload->getName(), $coverPath . $store->cover_original, $coverPath . $store->cover_big, $coverPath . $store->cover_medium, $coverPath . $store->cover_small]);
             // 5
             $resizes = $image->getResizes();
             $store->cover_original = $resizes['original'];
             $store->cover_big = $resizes['big'];
             $store->cover_medium = $resizes['medium'];
             $store->cover_small = $resizes['small'];
             $store->update();
         } catch (Exception $ex) {
             $validator->errors()->add('__file', _t('opp'));
             return file_pong(['status' => _const('AJAX_OK'), 'messages' => $validator->errors()->first()], 500);
         }
         return file_pong(['status' => _const('AJAX_OK'), 'messages' => _t('saved_info'), 'data' => ['big' => asset($coverPath . $resizes['big']), 'medium' => asset($coverPath . $resizes['medium']), 'small' => asset($coverPath . $resizes['small'])]]);
     }
 }