1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers\Dashboard;
|
---|
4 |
|
---|
5 | use App\Helpers\Alert;
|
---|
6 | use App\Http\Requests\Dashboard\NewUserRequest;
|
---|
7 | use App\Models\Role;
|
---|
8 | use App\Models\User;
|
---|
9 | use Illuminate\Http\Request;
|
---|
10 | use App\Notifications\WelcomeUser;
|
---|
11 | use App\Http\Controllers\Controller;
|
---|
12 | use Illuminate\Support\Facades\DB;
|
---|
13 | use Illuminate\Support\Facades\Storage;
|
---|
14 | use Propaganistas\LaravelPhone\PhoneNumber;
|
---|
15 |
|
---|
16 | class UsersController extends Controller
|
---|
17 | {
|
---|
18 | public function index()
|
---|
19 | {
|
---|
20 | return view("dashboard.users.index")->with([
|
---|
21 | "users" => User::all()
|
---|
22 | ]);
|
---|
23 | }
|
---|
24 |
|
---|
25 | public function create()
|
---|
26 | {
|
---|
27 | return view("dashboard.users.create")->with([
|
---|
28 | "roles" => Role::all(),
|
---|
29 | "countries" => country()->all()
|
---|
30 | ]);
|
---|
31 | }
|
---|
32 |
|
---|
33 | public function store(NewUserRequest $request)
|
---|
34 | {
|
---|
35 | DB::transaction(function () use ($request) {
|
---|
36 |
|
---|
37 | $user = new User();
|
---|
38 |
|
---|
39 | $user->name = $request->name;
|
---|
40 | $user->surname = $request->surname;
|
---|
41 | $user->email = $request->email;
|
---|
42 | $user->country_code = $request->mobile_number_country;
|
---|
43 | $user->mobile_number = PhoneNumber::make($request->mobile_number, $request->mobile_number_country)->formatInternational();
|
---|
44 | $user->username = $request->username;
|
---|
45 | $user->password = $user->generateTemporaryPassword();
|
---|
46 | $user->security_code = $user->generateSecurityCode();
|
---|
47 | $user->verify_token = $user->generateVerifyToken();
|
---|
48 |
|
---|
49 | $user->role_id = $request->userRole;
|
---|
50 |
|
---|
51 | $user->save();
|
---|
52 |
|
---|
53 | $user->userProfile()->create([
|
---|
54 | "profile_link" => $user->generateProfileLink($request->name, $request->surname),
|
---|
55 | "technoblog_email" => $user->generateTechnoblogEmail($request->name, $request->surname)
|
---|
56 | ]);
|
---|
57 |
|
---|
58 | $user->notify(new WelcomeUser($user));
|
---|
59 |
|
---|
60 | Alert::flash("New user added successfully");
|
---|
61 |
|
---|
62 | });
|
---|
63 |
|
---|
64 | return redirect()->route("dashboard.users.create");
|
---|
65 | }
|
---|
66 |
|
---|
67 | public function block(Request $request, $id)
|
---|
68 | {
|
---|
69 | $user = User::find($id);
|
---|
70 | $user->is_active = false;
|
---|
71 | $user->save();
|
---|
72 | Alert::flash($user->name . " blocked successfully");
|
---|
73 | return redirect()->route("dashboard.users.index");
|
---|
74 | }
|
---|
75 |
|
---|
76 | public function unblock(Request $request, $id)
|
---|
77 | {
|
---|
78 | $user = User::find($id);
|
---|
79 | $user->is_active = true;
|
---|
80 | $user->save();
|
---|
81 | Alert::flash($user->name . " unblocked successfully");
|
---|
82 | return redirect()->route("dashboard.users.index");
|
---|
83 | }
|
---|
84 |
|
---|
85 | public function destroy(Request $request, $id)
|
---|
86 | {
|
---|
87 | $user = User::find($id);
|
---|
88 |
|
---|
89 | if (Storage::disk("uploads")->exists("/users/" . $user->userProfile->profile_photo_link)) {
|
---|
90 | Storage::disk("uploads")->delete("/users/" . $user->userProfile->profile_photo_link);
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (Storage::disk("uploads")->exists("/users/" . $user->userProfile->cover_photo_link)) {
|
---|
94 | Storage::disk("uploads")->delete("/users/" . $user->userProfile->cover_photo_link);
|
---|
95 | }
|
---|
96 |
|
---|
97 | $user->userProfile->delete();
|
---|
98 | $user->delete();
|
---|
99 |
|
---|
100 | Alert::flash($user->name . " deleted successfully");
|
---|
101 |
|
---|
102 | return redirect()->route("dashboard.users.index");
|
---|
103 | }
|
---|
104 | }
|
---|