source: app/Http/Controllers/Dashboard/DepartmentsController.php@ 1451c6f

Last change on this file since 1451c6f was 1451c6f, checked in by beratkjufliju <kufliju@…>, 3 years ago

bug fixes

  • Property mode set to 100644
File size: 6.8 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\File;
10use App\Models\Folder;
11use App\Models\User;
12use App\Notifications\NewDepartmentCreated;
13use Carbon\Carbon;
14use App\Http\Controllers\Controller;
15use Illuminate\Support\Facades\Notification;
16use Illuminate\Support\Facades\Storage;
17use Illuminate\Filesystem\Filesystem;
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 = $location;
47 $department->user_id = auth()->id();
48
49 $users = User::all();
50
51 $department->save();
52
53 Alert::flash("New Department added successfully");
54
55 return redirect()->route("dashboard.departments.index");
56 }
57
58 public function editShow($id)
59 {
60 return view("dashboard.departments.edit")->with([
61 "department" => Department::findOrFail($id)
62 ]);
63 }
64
65 public function edit(UpdateDepartmentRequest $request, $id)
66 {
67 $department = Department::findOrFail($id);
68
69 $folders = $department->folder;
70 $oldLocation = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $department->code;
71
72 $department->name = $request->name;
73 $department->code = $request->code;
74 $department->updated_at = Carbon::now();
75
76 if($department->isDirty('code'))
77 {
78 $location = 'Departments' . DIRECTORY_SEPARATOR . $request->code;
79
80 if(!Storage::disk('local')->has($location) && Folder::where('department_id', $department->id)->pluck('no_of_files')->first() > 0){
81 Storage::disk('local')->move($oldLocation, $location);
82 $department->location = $location;
83 }
84
85 foreach ($folders as $folder) {
86 $currArchId = explode('/', $folder->arch_id)[1];
87 $folder->arch_id = $department->code . '/' . $currArchId;
88 $folder->save();
89 foreach($folder->files as $file) {
90 $file->location = $location . DIRECTORY_SEPARATOR . $folder->name . DIRECTORY_SEPARATOR . $file->name;
91 $file->save();
92 }
93 }
94 }
95
96 $department->save();
97
98 Alert::flash("Department edited successfully");
99
100 return redirect()->route("dashboard.departments.index");
101 }
102
103 public function destroy($id)
104 {
105 $department = Department::find($id);
106 //$department->delete();
107 $folders = $department->folder()->count();
108
109 if($folders > 0){
110 Alert::flash($department->name . " has " . $folders . " document/s associated", "error");
111 }
112 else {
113 $department->delete();
114
115 Alert::flash($department->name . " deleted successfully");
116 }
117
118 return redirect()->route("dashboard.departments.index");
119 }
120
121 public function downloadAll()
122 {
123 $departments = Department::all();
124 $flag=false;
125
126 foreach ($departments as $department) {
127 if($department->no_of_folders > 0) {
128 $folders = Folder::where('department_id', $department->id)->get();
129 }
130 foreach ($folders as $folder){
131 if($folder->no_of_files > 0){
132 $flag=true;
133 break;
134 }
135 }
136 }
137
138 if($flag) {
139 $zip_file = Storage::disk('local')->path('Departments.zip');
140 $zip = new \ZipArchive();
141 $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
142
143 $path = Storage::disk('local')->path('Departments');
144
145 $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
146
147 foreach ($files as $file) {
148
149 // We're skipping all subfolders
150 if (!$file->isDir()) {
151 $filePath = $file->getRealPath();
152 // extracting filename with substr/strlen
153 $relativePath = substr($filePath, strlen($path) + 1);
154 $zip->addFile($filePath, $relativePath);
155 }
156 }
157 $zip->close();
158 $headers = array('Content-Type' => 'application/octet-stream');
159 $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . '- Departments.zip';
160 return response()->download($zip_file, $zip_new_name, $headers);
161 }
162
163 else {
164 Alert::flash("All departments are empty", "warning");
165 return redirect()->back();
166 }
167
168 }
169
170 public function downloadDepartment($id)
171 {
172 $department = Department::find($id);
173 $folders = Folder::where('department_id', $id)->get();
174 $flag = false;
175
176 foreach($folders as $folder){
177 if($folder->no_of_files > 0)
178 $flag=true;
179 break;
180 }
181
182 if($flag) {
183 $zip_file=Storage::disk('local')->path('Department.zip');
184 $zip = new \ZipArchive();
185 $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
186 $path = Storage::disk('local')->path($department->location);
187
188 $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
189 foreach ($files as $file) {
190
191 // We're skipping all subfolders
192 if (!$file->isDir()) {
193 $filePath = $file->getRealPath();
194 // extracting filename with substr/strlen
195 $relativePath = substr($filePath, strlen($path) + 1);
196 $zip->addFile($filePath, $relativePath);
197 }
198 }
199 $zip->close();
200 $headers = array('Content-Type' => 'application/octet-stream');
201 $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . '- Departments.zip';
202 return response()->download($zip_file, $zip_new_name, $headers);
203 }
204 else {
205 Alert::flash("This department has no files", "warning");
206 return redirect()->back();
207 }
208 }
209}
Note: See TracBrowser for help on using the repository browser.