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