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