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

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