/** * Save role * * @param Illuminate\Http\Request $request * * @return response * * @throws \Exception */ public function save(Request $request) { if ($request->isMethod('POST')) { $edit = $request->has('id'); $role = $edit ? $this->_getRoleById($request->get('id')) : $this->role; $rules = $this->role->rules(); $messages = $this->role->messages(); if ($edit && str_equal($role->role, $request->get('role'))) { $rules = remove_rules($rules, ['role.unique:roles,role']); } $validator = Validator::make($request->all(), $rules, $messages); if ($validator->fails()) { return back()->withInput()->withErrors($validator, 'all'); } try { $role = $this->bind($role, $request->all()); if (!$edit) { $role->created_at = new \DateTime(); } $role->updated_at = new \DateTime(); $role->save(); } catch (Exception $ex) { throw new \Exception(_t('backend_common_opp') . $ex->getMessage()); } return redirect(route('backend_roles'))->with('success', _t('backend_common_saved')); } }
/** * Save post category * * @param Illuminate\Http\Request $request * * @return response * @throws \Exception */ public function save(Request $request) { if ($request->isMethod('POST')) { $edit = $request->has('id'); $postCategory = $edit ? $this->_getPostCategoryById($request->get('id')) : $this->postCategory; $rules = $this->postCategory->rules(); $messages = $this->postCategory->messages(); if ($edit && str_equal($postCategory->slug, $request->get('slug'))) { $rules = remove_rules($rules, ['slug.unique:post_categories,slug']); } $validator = Validator::make($request->all(), $rules, $messages); if ($validator->fails()) { return back()->withInput()->withErrors($validator, 'all'); } try { $postCategory = $this->bind($postCategory, $request->all()); $postCategory->save(); } catch (Exception $ex) { throw new \Exception(_t('backend_common_opp') . $ex->getMessage()); } return redirect(route('backend_post_categories'))->with('success', _t('backend_common_saved')); } }
/** * 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')); } }
public function ajaxSaveStoreInfo(Request $request) { //Only accept AJAX request if ($request->ajax() && $request->isMethod('POST')) { $user = user(); if (($us = $user->store) === null) { $store = $this->store; } else { $store = store(); } $rules = $store->getRules(); if ($us === null) { $rules = remove_rules($rules, 'slug.required'); } if (str_equal($store->slug, $request->get('slug'))) { $rules = remove_rules($rules, 'slug.unique:stores,slug'); } $messages = $store->getMessages(); $validator = Validator::make($request->all(), $rules, $messages); if ($validator->fails()) { return pong(0, $validator->messages(), 403); } try { $store->user_id = $user->id; $store->name = $request->get('name'); $store->category_id = $request->get('category_id'); $store->street = $request->get('street'); $store->city_id = $request->get('city_id'); $store->district_id = $request->get('district_id'); $store->ward_id = $request->get('ward_id'); $store->phone_number = $request->get('phone_number'); if ($user->store === null) { $slug = str_slug($store->name); if ($store->findStoreBySlug($slug)) { $store->slug = $slug . '-' . $user->id; } else { $store->slug = $slug; } } else { $store->slug = str_replace('_', '-', $request->get('slug')); } if ($store->save()) { $productPath = config('front.product_path') . $store->id; $oldmask = umask(0); if (!file_exists($productPath)) { mkdir($productPath, 0777); umask($oldmask); } } } catch (Exception $ex) { $validator->errors()->add('name', _t('opp')); return pong(0, $validator->messages(), 500); } return pong(1, _t('saved_info')); } }
/** * Change basic info * * @param Illuminate\Http\Request $request * * @return JSON */ public function ajaxSaveBasicInfo(Request $request) { //Only accept ajax request. if ($request->ajax()) { $rules = remove_rules($this->_user->getRules(), 'password.min:6'); $messages = $this->_user->getMessages(); $user = user(); $dbUname = $user->user_name; $dbEmail = $user->email; $dbPass = $user->password; $uname = $request->get('user_name'); $email = $request->get('email'); $fname = $request->get('first_name'); $lname = $request->get('last_name'); $password = $request->get('password'); $checkPass = false; if (str_equal($dbUname, $uname)) { $rules = remove_rules($rules, 'user_name.unique:users,user_name'); } else { $checkPass = true; } if (str_equal($dbEmail, $email)) { $rules = remove_rules($rules, 'email.unique:users,email'); } else { $checkPass = true; } if (!$checkPass) { $rules = remove_rules($rules, 'password'); } $validator = Validator::make($request->all(), $rules, $messages); if ($checkPass && !Hash::check($password, $dbPass)) { $validator->after(function ($validator) { $validator->errors()->add('password', _t('pass_incorrect')); }); } if ($validator->fails()) { return pong(0, $validator->messages(), 403); } try { $user->user_name = $uname; $user->email = $email; $user->first_name = $fname; $user->last_name = $lname; $user->save(); } catch (Exception $ex) { return pong(0, _t('opp'), 500); } return pong(1, _t('saved_info')); } }