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