예제 #1
0
 public static function getNameById($id)
 {
     return PersonalInfo::where('user_id', '=', $id)->select('names')->first()->names;
 }
 public function update(Request $request, $id = null)
 {
     if ($id == null) {
         $id = Auth::user()->id;
     }
     $rules = ['names' => 'required', 'password' => 'min:5', 'coordinates' => 'required', 'address' => 'required', 'mobile' => 'required', 'birth_date' => 'regex:/[0-9]{2}\\/[0-9]{2}\\/[0-9]{4}/', 'avatar' => 'image|mimes:jpg,jpeg,bmp,png,gif,tiff', 'cv' => 'mimes:doc,docx,ppt,pps,pptx,ppsx,xls,xlsx'];
     $this->validate($request, $rules);
     $avatar_file_name = null;
     $cv_file_name = null;
     if ($request->file('avatar')) {
         $avatar_file_name = Common::randString(6) . "." . $request->file('avatar')->getClientOriginalExtension();
         Storage::put('avatars/' . $avatar_file_name, file_get_contents($request->file('avatar')->getRealPath()));
         ImageResize::load(storage_path("app") . '/avatars/' . $avatar_file_name);
         ImageResize::resizeToWidth(300);
         ImageResize::save();
     }
     if ($request->file('cv')) {
         $cv_file_name = Common::randString(6) . "." . $request->file('cv')->getClientOriginalExtension();
         Storage::put('cv/' . $cv_file_name, file_get_contents($request->file('cv')->getRealPath()));
     }
     $user = User::find($id);
     if ($request->input('password') != null) {
         $user->password = Hash::make($request->input('password'));
     }
     $user->save();
     $info = PersonalInfo::where('user_id', $id)->first();
     $info->names = $request->input('names');
     $info->address = $request->input('address');
     $info->coordinates = $request->input('coordinates');
     $info->mobile = $request->input('mobile');
     $info->gender = $request->input('gender');
     $info->birth_date = $request->input('birth_date');
     $info->home_phone = $request->input('home_phone');
     $info->work_phone = $request->input('work_phone');
     $info->fax = $request->input('fax');
     $info->other = $request->input('other');
     if ($avatar_file_name != null) {
         $info->avatar = $avatar_file_name;
     }
     if ($cv_file_name != null) {
         $info->cv = $cv_file_name;
     }
     $info->save();
     if ($id != Auth::user()->id) {
         Notification::add($id, 'USER_UPDATE_BY_ADMIN', ['admin_id' => Auth::user()->id]);
     }
     if ($request->input('password') != null) {
         Mail::send('emails.editUserPassword', ['user' => $user, 'info' => $info, 'password' => $request->input('password'), 'company' => implode(', ', User::find($id)->company->pluck('name')->toArray())], function ($m) use($user) {
             $m->from('*****@*****.**', 'TIMELINE');
             $m->to($user->email, $user->names)->subject('Your account on TIMELINE platform was edited');
         });
         return redirect('/users/' . $id)->with(['message' => "User was updated successfully. The new password was sent to his/her email ({$user->email})."]);
     }
     if (Auth::user()->role == "worker" || $id == Auth::user()->id) {
         return redirect('/profile/')->with(['message' => "Data was updated successfully."]);
     }
     return redirect('/users/' . $id)->with(['message' => "User was updated successfully."]);
 }