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