source: app/Http/Controllers/Dashboard/FoldersController.php

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

added confirmation modal on userSettings

  • Property mode set to 100644
File size: 12.2 KB
Line 
1<?php
2
3namespace App\Http\Controllers\Dashboard;
4
5use App\Helpers\Alert;
6use App\Http\Requests\Dashboard\FileRequest;
7use App\Http\Requests\Dashboard\FolderRequest;
8use App\Http\Requests\Dashboard\FileNameRequest;
9use App\Models\Department;
10use App\Models\FileType;
11use App\Models\Folder;
12use App\Models\File;
13use App\Models\User;
14use App\Notifications\NewFolderCreated;
15use Carbon\Carbon;
16use Illuminate\Filesystem\Filesystem;
17use Illuminate\Http\Request;
18use Illuminate\Support\Facades\Notification;
19use Illuminate\Support\Facades\Storage;
20use App\Http\Controllers\Controller;
21use function Sodium\increment;
22
23class FoldersController extends Controller
24{
25 public function index(Request $request)
26 {
27 $deptName = "";
28 $deptCode = "";
29
30 if ($request->query('id')) {
31 $deptName = Department::find($request->query('id'))->getOriginal('name');
32 $deptCode = Department::find($request->query('id'))->getOriginal('code');
33 $foldersInDeptSort = Folder::with('department')->when($request->has('id'), function ($query) use ($request) {
34 $query->where('department_id', $request->query('id'));
35 });
36
37 if ($request->query('sort') == 'newest') {
38 $folders = $foldersInDeptSort->orderBy('created_at', 'desc')->paginate(9);
39 }
40 else if ($request->query('sort') == 'name') {
41 $folders = $foldersInDeptSort->orderBy('name', 'asc')->paginate(9);
42 }
43 else if ($request->query('sort') == 'no_of_files') {
44 $folders = $foldersInDeptSort->orderBy('no_of_files', 'desc')->paginate(9);
45 }
46 else if($request->query('sort') == 'count'){
47 $total = $foldersInDeptSort->folder->files->count();
48 $folders = $foldersInDeptSort->orderBy($total, 'asc')->paginate(9);
49 }
50 else {
51 $folders = Folder::where('department_id', $request->query('id'))->paginate(9);
52 }
53 } else {
54 if ($request->query('sort') == 'newest') {
55 $folders = Folder::orderBy('created_at', 'desc')->paginate(9);
56 }
57 else if ($request->query('sort') == 'name') {
58 $folders = Folder::orderBy('name', 'asc')->paginate(9);
59 }
60 else if ($request->query('sort') == 'no_of_files') {
61 $folders = Folder::orderBy('no_of_files', 'desc')->paginate(9);
62 }
63 else if ($request->query('sort') == 'recent') {
64 $folders = Folder::orderBy('created_at', 'desc')->paginate(9);
65 } else if ($request->query('search')) {
66
67 $result = collect();
68 $queries = explode(" ", $request->search);
69 foreach ($queries as $query) {
70 $result->push(Folder::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
71 }
72 $result = $result->flatten();
73 $folders = $result;
74 } else {
75 $folders = Folder::paginate(9);
76 }
77 }
78
79 $departments = Department::all();
80
81 $diskTotal = disk_total_space('/');
82 $diskTotalSize = $diskTotal / 1073741824;
83
84 $diskFree = disk_free_space('/');
85 $used = $diskTotal - $diskFree;
86
87 $diskUsedSize = $used / 1073741824;
88 $diskUse1 = round(100 - (($diskUsedSize / $diskTotalSize) * 100));
89 $diskUse = round(100 - ($diskUse1)) . '%';
90
91 return view("dashboard.folders.index")->with([
92 "folders" => $folders,
93 "currentUser" => auth()->user(),
94 "departments" => $departments,
95 "docsCount" => Department::withCount('folder')->get(),
96 "totalDocs" => Folder::all()->count(),
97 "diskTotal" => $diskTotal,
98 "diskTotalSize" => $diskTotalSize,
99 "diskUse" => $diskUse,
100 "diskUsedSize" => $diskUsedSize,
101 "deptName" => $deptName,
102 "deptCode" => $deptCode,
103 "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes))
104 ]);
105
106 }
107
108 public function create()
109 {
110 return view("dashboard.folders.create")->with([
111 "departments" => Department::all()
112 ]);
113 }
114
115 public function store(FolderRequest $request)
116 {
117 $folder = new Folder();
118
119 $existingFolder = Folder::where(['department_id' => $request->department, 'name' => $request->name, 'arch_id' => $request->arch_id])->count();
120
121 $existingFolderName = Folder::where(['department_id' => $request->department, 'name' => $request->name])->get();
122 $existingFolderArchId = Folder::where(['department_id' => $request->department, 'arch_id' => $request->arch_id])->get();
123
124 if($existingFolder > 0) {
125 $folder->version = $existingFolder + 1;
126 }
127
128 //dd($existingFolderName, $existingFolderArchId);
129
130 if(($existingFolderName->isNotEmpty() && $existingFolderArchId->isEmpty()) || ($existingFolderName->isEmpty() && $existingFolderArchId->isNotEmpty())) {
131 Alert::flash("Can't create another version since folder name or archive ID is different", "error");
132
133 return redirect()->route("dashboard.folders.index");
134 }
135
136 $department = Department::find($request->department);
137
138 $user = auth()->user();
139
140 $folder->user()->associate($user);
141 $folder->department()->associate($department);
142 $folder->department()->increment('no_of_folders');
143
144 $folder->arch_id = $request->arch_id;
145 $folder->name = $request->name;
146 $folder->note = $request->note;
147
148 $location = $folder->department->location . DIRECTORY_SEPARATOR . $request->name;
149
150 if (!Storage::disk('local')->has($location)) {
151 Storage::disk('local')->makeDirectory($location);
152 }
153
154 $users = User::all();
155
156 $folder->location = $location;
157
158 $folder->save();
159
160 if ($request->has('file_item')) {
161 foreach ($request->file_item as $file) {
162 $fileName = $folder->name . '-' . uniqid() . '.' . $file->getClientOriginalExtension();
163 $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName);
164 $newFile = new File();
165 $newFile->name = $fileName;
166 $newFile->location = $location . DIRECTORY_SEPARATOR . $fileName;
167 $newFile->folder()->associate($folder);
168 $newFile->folder()->increment('no_of_files');
169 $newFile->save();
170 }
171 }
172
173 Alert::flash("New folder created successfully");
174
175 return redirect()->route("dashboard.folders.index");
176 }
177
178 public function editShow($id)
179 {
180 return view("dashboard.folders.edit")->with([
181 "folder" => Folder::findOrFail($id),
182 "departments" => Department::all(),
183 "files" => File::where('folder_id', $id)->get(),
184 "excelExt" => array("xls", "xlsx", "xls", "csv"),
185 "textExt" => array("txt", "doc", "docx"),
186 "imageExt" => array("png", "jpg", "jpeg"),
187 ]);
188 }
189
190 public function uploadFiles(FileRequest $request, $id)
191 {
192 $file = new File();
193
194 $folder = Folder::findOrFail($id);
195
196 $location = $folder->location;
197
198 $users = User::all();
199
200 if ($request->has('file_item')) {
201 foreach ($request->file_item as $file) {
202 $fileName = $file->getClientOriginalName();
203
204 if(File::where(['folder_id' => $folder->id, 'name' => $fileName])->count() > 0) {
205 Alert::flash("The uploaded file already exists", "error");
206
207 return redirect()->back();
208 }
209
210 $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName);
211 $newFile = new File();
212 $newFile->name = $fileName;
213 $newFile->location = $location . DIRECTORY_SEPARATOR . $fileName;
214 //$newFile->folder()->associate($folder);
215 $newFile->folder()->associate($folder);
216 $newFile->folder()->increment('no_of_files');
217 $newFile->save();
218 }
219
220 Alert::flash("New files added successfully");
221
222 return redirect()->back();
223 }
224 else {
225 Alert::flash("No files were uploaded", "error");
226
227 return redirect()->back();
228 }
229 }
230
231 public function destroy($id)
232 {
233 $folder = Folder::find($id);
234
235 $existingFolders = Folder::where(['department_id' => $folder->department->id, 'name' => $folder->name, 'arch_id' => $folder->arch_id]);
236
237 if($existingFolders->count() > 1 && $folder->version == 1 || $folder->version < $existingFolders->pluck('version')->max()) {
238 Alert::flash($folder->name . " has newer versions", "error");
239 return redirect()->back();
240 }
241
242 $files = File::where('folder_id', $id)->get();
243
244 if($files->count() > 0) {
245 Alert::flash($folder->name . " contains files", "error");
246 return redirect()->back();
247 }
248
249 if (auth()->user()->hasPermission("delete_data")) {
250
251 foreach ($files as $file) {
252 $file->delete();
253 }
254
255 $folder->delete();
256 $location = $folder->department->location . DIRECTORY_SEPARATOR . $folder->name;
257 Storage::disk('local')->deleteDirectory($location);
258 $folder->department()->decrement('no_of_folders');
259 Alert::flash($folder->name . " deleted successfully");
260 return redirect()->back();
261 }
262 Alert::flash($folder->name . " cannot be deleted", "error");
263 return redirect()->back();
264 }
265
266 public function downloadfolder(Request $request, $id)
267 {
268 $folder = Folder::find($id);
269 $FileSystem = new Filesystem();
270
271 if($folder->no_of_files > 0) {
272 $zip_file = Storage::disk('local')->path('Folder.zip');
273 $zip = new \ZipArchive();
274 $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
275
276 $path = Storage::disk('local')->path($folder->department->location . DIRECTORY_SEPARATOR . $folder->name);
277
278 $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
279
280 foreach ($files as $file) {
281 if (!$file->isDir()) {
282 $filePath = $file->getRealPath();
283 // extracting filename with substr/strlen
284 $relativePath = substr($filePath, strlen($path) + 1);
285 $zip->addFile($filePath, $relativePath);
286 }
287 }
288
289 $zip->close();
290 $headers = array('Content-Type' => 'application/octet-stream',);
291 $zip_new_name = Carbon::now()->format('d.m.Y - ') . $folder->name . '.zip';
292 return response()->download($zip_file, $zip_new_name, $headers);
293 }
294 else {
295 Alert::flash("This folder has no files", "warning");
296 return redirect()->back();
297 }
298 }
299
300 public function files(Request $request, $id)
301 {
302 $folder = Folder::findOrFail($id);
303 $files = File::where('folder_id', $id);
304 $deptId = $folder->department_id;
305 $folders = Folder::where('department_id', $deptId)->get();
306
307 $queries = explode(" ", $request->search);
308
309 if ($request->query('search')) {
310 $result = collect();
311
312 foreach ($queries as $query) {
313 $result->push($files->where("name", "like", "%{$query}%")->get());
314 }
315 $result = $result->flatten();
316 $files = $result;
317 }
318 else {
319 $files = File::where('folder_id', $id)->paginate(12);
320 }
321
322 return view("dashboard.folders.files")->with([
323 "folder" => $folder,
324 "departments" => Department::all(),
325 "files" => $files,
326 "excelExt" => array("xls", "xlsx", "xls", "csv"),
327 "textExt" => array("txt", "doc", "docx"),
328 "imageExt" => array("png", "jpg", "jpeg"),
329 "folders" => $folders,
330 "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes)),
331 ]);
332 }
333}
334
Note: See TracBrowser for help on using the repository browser.