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