source: app/Http/Controllers/Dashboard/DepartmentsController.php@ b9c4a92

develop
Last change on this file since b9c4a92 was b9c4a92, checked in by Berat Kjufliju <kufliju@…>, 3 years ago

edited file upload, seeding and departments controller

  • Property mode set to 100644
File size: 2.9 KB
Line 
1<?php
2
3namespace App\Http\Controllers\Dashboard;
4
5use App\Helpers\Alert;
6use App\Http\Requests\Dashboard\NewDepartmentRequest;
7use App\Http\Requests\Dashboard\UpdateDepartmentRequest;
8use App\Models\Department;
9use App\Models\Document;
10use App\Models\User;
11use Carbon\Carbon;
12use Illuminate\Http\Request;
13use App\Http\Controllers\Controller;
14use Illuminate\Support\Facades\Auth;
15use Illuminate\Support\Facades\Storage;
16use function Illuminate\Events\queueable;
17
18class 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}
Note: See TracBrowser for help on using the repository browser.