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