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\Document;
|
---|
10 | use App\Models\User;
|
---|
11 | use Carbon\Carbon;
|
---|
12 | use Illuminate\Http\Request;
|
---|
13 | use App\Http\Controllers\Controller;
|
---|
14 | use Illuminate\Support\Facades\Auth;
|
---|
15 | use Illuminate\Support\Facades\Storage;
|
---|
16 | use function Illuminate\Events\queueable;
|
---|
17 |
|
---|
18 | class DepartmentsController extends Controller
|
---|
19 | {
|
---|
20 | public function index()
|
---|
21 | {
|
---|
22 | return view("dashboard.departments.index")->with([
|
---|
23 | "departments" => Department::all(),
|
---|
24 | ]);
|
---|
25 | }
|
---|
26 |
|
---|
27 | public function create()
|
---|
28 | {
|
---|
29 | return view("dashboard.departments.create");
|
---|
30 | }
|
---|
31 |
|
---|
32 | public function store(NewDepartmentRequest $request)
|
---|
33 | {
|
---|
34 | $department = new Department();
|
---|
35 |
|
---|
36 | $department->name = $request->name;
|
---|
37 | $department->code = $request->code;
|
---|
38 |
|
---|
39 | $location = 'Departments' . DIRECTORY_SEPARATOR . $request->code;
|
---|
40 |
|
---|
41 | if(!Storage::disk('local')->has($location)){
|
---|
42 | Storage::disk('local')->makeDirectory($location);
|
---|
43 |
|
---|
44 | }
|
---|
45 | $department->location = Storage::disk('local')->path('') . $location;
|
---|
46 | $department->user_id = auth()->id();
|
---|
47 |
|
---|
48 | $department->save();
|
---|
49 |
|
---|
50 | Alert::flash("New Department added successfully");
|
---|
51 |
|
---|
52 | return redirect()->route("dashboard.departments.index");
|
---|
53 | }
|
---|
54 |
|
---|
55 | public function editShow($id)
|
---|
56 | {
|
---|
57 | return view("dashboard.departments.edit")->with([
|
---|
58 | "department" => Department::findOrFail($id)
|
---|
59 | ]);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public function edit(UpdateDepartmentRequest $request, $id)
|
---|
63 | {
|
---|
64 | $department = Department::findOrFail($id);
|
---|
65 |
|
---|
66 | $oldLocation = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $department->code;
|
---|
67 |
|
---|
68 | $department->name = $request->name;
|
---|
69 | $department->code = $request->code;
|
---|
70 |
|
---|
71 | if($department->isDirty('code'))
|
---|
72 | {
|
---|
73 | $location = 'Departments' . DIRECTORY_SEPARATOR . $request->code;
|
---|
74 | if(!Storage::disk('local')->has($location)){
|
---|
75 | Storage::disk('local')->move($oldLocation, $location);
|
---|
76 | $department->location = Storage::disk('local')->path('') . $location;
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | $department->save();
|
---|
81 |
|
---|
82 | Alert::flash("Department edited successfully");
|
---|
83 |
|
---|
84 | return redirect()->route("dashboard.departments.index");
|
---|
85 | }
|
---|
86 |
|
---|
87 | public function destroy($id)
|
---|
88 | {
|
---|
89 | $department = Department::find($id);
|
---|
90 | //$department->delete();
|
---|
91 | $documents = $department->document()->count();
|
---|
92 |
|
---|
93 | if($documents > 0){
|
---|
94 | Alert::flash($department->name . " has " . $documents . " document/s associated", "error");
|
---|
95 | }
|
---|
96 | else {
|
---|
97 | $department->delete();
|
---|
98 |
|
---|
99 | Alert::flash($department->name . " deleted successfully");
|
---|
100 | }
|
---|
101 |
|
---|
102 | return redirect()->route("dashboard.departments.index");
|
---|
103 | }
|
---|
104 | }
|
---|