source: app/Http/Controllers/Dashboard/UsersController.php@ 5e56e8a

develop
Last change on this file since 5e56e8a was c6b84df, checked in by beratkjufliju <kufliju@…>, 3 years ago

added fileTypes controller, notifications, excel export, edited views

  • Property mode set to 100644
File size: 5.2 KB
RevLine 
[194a359]1<?php
2
3namespace App\Http\Controllers\Dashboard;
4
5use App\Helpers\Alert;
6use App\Http\Requests\Dashboard\NewUserRequest;
[24a616f]7use App\Http\Requests\Dashboard\UpdateUserData;
[194a359]8use App\Http\Requests\Dashboard\UpdateUserRequest;
[24a616f]9use App\Models\Avatar;
[d795fa6]10use Carbon\Carbon;
[24a616f]11use Illuminate\Support\Facades\File;
[194a359]12use App\Models\Role;
13use App\Models\User;
14use App\Notifications\VerifyNewEmail;
15use Illuminate\Http\Request;
16use App\Notifications\WelcomeUser;
17use App\Http\Controllers\Controller;
[24a616f]18use Illuminate\Support\Facades\Storage;
[194a359]19
20class UsersController extends Controller
21{
[24a616f]22
[194a359]23 public function index()
24 {
25 return view("dashboard.users.index")->with([
[c6b84df]26 "users" => User::all(),
27 "roles" => Role::all(),
[194a359]28 ]);
29 }
30
31 public function create()
32 {
33 return view("dashboard.users.create")->with([
34 "roles" => Role::all(),
35 ]);
36 }
37
38 public function store(NewUserRequest $request)
39 {
40 $user = new User();
41
42 $user->name = $request->name;
43 $user->surname = $request->surname;
44 $user->email = $request->email;
[d795fa6]45 $user->phone_number = $request->phone_number;
[194a359]46 $user->username = $request->username;
47 $user->password = $user->generateTemporaryPassword();
48 $user->security_code = $user->generateSecurityCode();
49 $user->verify_token = $user->generateVerifyToken();
50
51 $user->role_id = $request->userRole;
52
[24a616f]53 if ($request->hasFile("avatar")) {
54 $image = $request->file("avatar");
55 $avatarName = $user->id . '_avatar' . time() . '.' . request()->avatar->getClientOriginalExtension();
56
57 if (!empty($user->avatar)) {
58 Storage::disk('uploads')->delete("/users/" . $user->avatar);
59 }
60
61 Storage::disk('uploads')->put("/users/" . $avatarName, File::get($image));
62
63 $user->avatar = $avatarName;
64 }
[c6b84df]65
66 $user->created_by = auth()->user()->id;
[194a359]67 $user->save();
68
69 $user->notify(new WelcomeUser($user));
70
71 Alert::flash("New user added successfully");
72
73 return redirect()->route("dashboard.users.index");
74 }
75
76 public function editShow($id)
77 {
78 return view("dashboard.users.edit")->with([
79 "user" => User::findOrFail($id),
80 "roles" => Role::all(),
81 ]);
82 }
83
84 public function edit(UpdateUserRequest $request, $id)
85 {
86 $user = User::findOrFail($id);
87 $user->name = $request->name;
88 $user->surname = $request->surname;
89 $user->username = $request->username;
90 $user->email = $request->email;
[d795fa6]91 $user->phone_number = $request->phone_number;
[194a359]92 $user->role_id = $request->userRole;
[c6b84df]93 $user->updated_at = Carbon::now();
[24a616f]94
95 if ($request->hasFile("avatar")) {
96 $image = $request->file("avatar");
97 $avatarName = $user->id . '_avatar' . time() . '.' . request()->avatar->getClientOriginalExtension();
98
99 if (!empty($user->avatar)) {
100 Storage::disk('uploads')->delete("/users/" . $user->avatar);
101 }
102
103 Storage::disk('uploads')->put("/users/" . $avatarName, File::get($image));
104
105 $user->avatar = $avatarName;
106 }
107
[194a359]108 if($user->isDirty('email')) {
109 $user->notify(new VerifyNewEmail($user));
110 }
111 $user->save();
112
113 Alert::flash("User updated successfully");
114
115 return redirect()->route("dashboard.users.index");
116 }
117
118 public function block(Request $request, $id)
119 {
120 $user = User::find($id);
121 $user->is_active = false;
122 $user->save();
123 Alert::flash($user->name . " User blocked successfully");
124 return redirect()->route("dashboard.users.index");
125 }
126
127 public function unblock(Request $request, $id)
128 {
129 $user = User::find($id);
130 $user->is_active = true;
131 $user->save();
132 Alert::flash($user->name . " User unblocked successfully");
133 return redirect()->route("dashboard.users.index");
134 }
135
136 public function destroy(Request $request, $id)
137 {
138 $user = User::find($id);
139
140 $user->userProfile->delete();
141 $user->delete();
142
143 Alert::flash($user->name . " deleted successfully");
144
145 return redirect()->route("dashboard.users.index");
146 }
147
148 public function getUserRoles()
149 {
150 $roles = Role::get();
151 return $roles;
152 }
[24a616f]153
154 public function editUserData(UpdateUserData $request, $id)
155 {
156 $user = User::findOrFail($id);
157 $user->name = $request->name;
158 $user->surname = $request->surname;
[d795fa6]159 $user->phone_number = $request->phone_number;
[24a616f]160
161 if ($request->hasFile("avatar")) {
162 $image = $request->file("avatar");
163 $avatarName = $user->id . '_avatar' . time() . '.' . request()->avatar->getClientOriginalExtension();
164
165 if (!empty($user->avatar)) {
166 Storage::disk('uploads')->delete("/users/" . $user->avatar);
167 }
168
169 Storage::disk('uploads')->put("/users/" . $avatarName, File::get($image));
170
171 $user->avatar = $avatarName;
172 }
173
174 $user->save();
175
176 Alert::flash("User data updated successfully");
177
178 return redirect()->route("dashboard.settings.index");
179 }
[194a359]180}
Note: See TracBrowser for help on using the repository browser.