source: app/Http/Controllers/Dashboard/DepartmentsController.php@ 6b95845

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

edited DocumentsController

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