[194a359] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | namespace App\Http\Controllers\Dashboard;
|
---|
| 4 |
|
---|
| 5 | use App\Helpers\Alert;
|
---|
| 6 | use App\Http\Requests\Dashboard\NewDepartmentRequest;
|
---|
| 7 | use App\Http\Requests\Dashboard\UpdateDepartmentRequest;
|
---|
| 8 | use App\Models\Department;
|
---|
| 9 | use App\Models\User;
|
---|
| 10 | use Illuminate\Http\Request;
|
---|
| 11 | use App\Http\Controllers\Controller;
|
---|
| 12 | use Illuminate\Support\Facades\Auth;
|
---|
[24a616f] | 13 | use Illuminate\Support\Facades\Storage;
|
---|
[194a359] | 14 |
|
---|
| 15 | class DepartmentsController extends Controller
|
---|
| 16 | {
|
---|
| 17 | public function index()
|
---|
| 18 | {
|
---|
| 19 | return view("dashboard.departments.index")->with([
|
---|
[24a616f] | 20 | "departments" => Department::all(),
|
---|
[194a359] | 21 | ]);
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | public function create()
|
---|
| 25 | {
|
---|
| 26 | return view("dashboard.departments.create");
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | public function editShow($id)
|
---|
| 30 | {
|
---|
| 31 | return view("dashboard.departments.edit")->with([
|
---|
| 32 | "department" => Department::findOrFail($id)
|
---|
| 33 | ]);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[120759b] | 36 | public function store(NewDepartmentRequest $request)
|
---|
| 37 | {
|
---|
| 38 | $department = new Department();
|
---|
| 39 |
|
---|
| 40 | $department->name = $request->name;
|
---|
| 41 | $department->code = $request->code;
|
---|
| 42 |
|
---|
| 43 | if(!Storage::disk('local')->has('Departments/' . $request->code)){
|
---|
| 44 | Storage::disk('local')->makeDirectory('Departments/' . $request->code);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | $department->user_id = auth()->id();
|
---|
| 48 | $department->location = '/Departments/' . $request->code;
|
---|
| 49 |
|
---|
| 50 | $department->save();
|
---|
| 51 |
|
---|
| 52 | Alert::flash("New Department added successfully");
|
---|
| 53 |
|
---|
| 54 | return redirect()->route("dashboard.departments.index");
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[194a359] | 57 | public function edit(UpdateDepartmentRequest $request, $id)
|
---|
| 58 | {
|
---|
| 59 | $department = Department::findOrFail($id);
|
---|
| 60 |
|
---|
[24a616f] | 61 | $oldDepartmentCode = $department->code;
|
---|
| 62 |
|
---|
[194a359] | 63 | $department->name = $request->name;
|
---|
| 64 | $department->code = $request->code;
|
---|
| 65 |
|
---|
[24a616f] | 66 | $path = '/Departments/' . $request->code;
|
---|
| 67 | $department->location = $path;
|
---|
| 68 |
|
---|
| 69 | $files = Storage::allFiles($oldDepartmentCode);
|
---|
| 70 |
|
---|
| 71 | if($department->isDirty('code'))
|
---|
| 72 | {
|
---|
| 73 | if(!Storage::disk('local')->has('Departments/' . $request->code)){
|
---|
| 74 | Storage::disk('local')->move('Departments/' . $oldDepartmentCode, 'Departments/' . $department->code);
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[194a359] | 78 | $department->save();
|
---|
| 79 |
|
---|
| 80 | Alert::flash("Department edited successfully");
|
---|
| 81 |
|
---|
| 82 | return redirect()->route("dashboard.departments.index");
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | public function destroy(Request $request, $id)
|
---|
| 86 | {
|
---|
| 87 | $department = Department::find($id);
|
---|
| 88 |
|
---|
| 89 | $department->delete();
|
---|
| 90 |
|
---|
| 91 | Alert::flash($department->name . " deleted successfully");
|
---|
| 92 |
|
---|
| 93 | return redirect()->route("dashboard.departments.index");
|
---|
| 94 | }
|
---|
| 95 | }
|
---|