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

Last change on this file since deff0bc 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
RevLine 
[c6b84df]1<?php
2
3namespace App\Http\Controllers\Dashboard;
4
5use App\Helpers\Alert;
[4521f25]6use App\Http\Requests\Dashboard\FileRequest;
[c6b84df]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;
[e78295c]21use function Sodium\increment;
[c6b84df]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') {
[0a1fb54]38 $folders = $foldersInDeptSort->orderBy('created_at', 'desc')->paginate(9);
[c6b84df]39 }
40 else if ($request->query('sort') == 'name') {
[0a1fb54]41 $folders = $foldersInDeptSort->orderBy('name', 'asc')->paginate(9);
[c6b84df]42 }
[4b7e2d3]43 else if ($request->query('sort') == 'no_of_files') {
[0a1fb54]44 $folders = $foldersInDeptSort->orderBy('no_of_files', 'desc')->paginate(9);
[4b7e2d3]45 }
[c6b84df]46 else if($request->query('sort') == 'count'){
47 $total = $foldersInDeptSort->folder->files->count();
[0a1fb54]48 $folders = $foldersInDeptSort->orderBy($total, 'asc')->paginate(9);
[c6b84df]49 }
50 else {
[0a1fb54]51 $folders = Folder::where('department_id', $request->query('id'))->paginate(9);
[c6b84df]52 }
53 } else {
54 if ($request->query('sort') == 'newest') {
[0a1fb54]55 $folders = Folder::orderBy('created_at', 'desc')->paginate(9);
[c6b84df]56 }
57 else if ($request->query('sort') == 'name') {
[0a1fb54]58 $folders = Folder::orderBy('name', 'asc')->paginate(9);
[c6b84df]59 }
[4b7e2d3]60 else if ($request->query('sort') == 'no_of_files') {
[0a1fb54]61 $folders = Folder::orderBy('no_of_files', 'desc')->paginate(9);
[4b7e2d3]62 }
[c6b84df]63 else if ($request->query('sort') == 'recent') {
[0a1fb54]64 $folders = Folder::orderBy('created_at', 'desc')->paginate(9);
[c6b84df]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 {
[0a1fb54]75 $folders = Folder::paginate(9);
[c6b84df]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,
[4b7e2d3]103 "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes))
[c6b84df]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();
[e78295c]118
119 $existingFolder = Folder::where(['department_id' => $request->department, 'name' => $request->name, 'arch_id' => $request->arch_id])->count();
120
[28bab7b]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();
[e78295c]123
124 if($existingFolder > 0) {
125 $folder->version = $existingFolder + 1;
126 }
127
[28bab7b]128 //dd($existingFolderName, $existingFolderArchId);
129
130 if(($existingFolderName->isNotEmpty() && $existingFolderArchId->isEmpty()) || ($existingFolderName->isEmpty() && $existingFolderArchId->isNotEmpty())) {
[e78295c]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
[c6b84df]136 $department = Department::find($request->department);
137
[e78295c]138 $user = auth()->user();
139
[c6b84df]140 $folder->user()->associate($user);
141 $folder->department()->associate($department);
[4b7e2d3]142 $folder->department()->increment('no_of_folders');
[c6b84df]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
[8fdb18e]150 if (!Storage::disk('local')->has($location)) {
151 Storage::disk('local')->makeDirectory($location);
[c6b84df]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);
[4b7e2d3]168 $newFile->folder()->increment('no_of_files');
[c6b84df]169 $newFile->save();
170 }
171 }
172
173 Alert::flash("New folder created successfully");
174
[94f05dc]175 return redirect()->route("dashboard.folders.index");
[c6b84df]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
[4521f25]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
[e78295c]231 public function destroy($id)
[c6b84df]232 {
[e78295c]233 $folder = Folder::find($id);
[c6b84df]234
[ec7b69d]235 $existingFolders = Folder::where(['department_id' => $folder->department->id, 'name' => $folder->name, 'arch_id' => $folder->arch_id]);
[c6b84df]236
[ec7b69d]237 if($existingFolders->count() > 1 && $folder->version == 1 || $folder->version < $existingFolders->pluck('version')->max()) {
238 Alert::flash($folder->name . " has newer versions", "error");
[e78295c]239 return redirect()->back();
[c6b84df]240 }
241
[e78295c]242 $files = File::where('folder_id', $id)->get();
[4b7e2d3]243
[e78295c]244 if($files->count() > 0) {
245 Alert::flash($folder->name . " contains files", "error");
246 return redirect()->back();
[c6b84df]247 }
248
[190db9f]249 if (auth()->user()->hasPermission("delete_data")) {
[c6b84df]250
251 foreach ($files as $file) {
252 $file->delete();
253 }
[e78295c]254
[c6b84df]255 $folder->delete();
256 $location = $folder->department->location . DIRECTORY_SEPARATOR . $folder->name;
[8fdb18e]257 Storage::disk('local')->deleteDirectory($location);
[4b7e2d3]258 $folder->department()->decrement('no_of_folders');
[c6b84df]259 Alert::flash($folder->name . " deleted successfully");
[4b7e2d3]260 return redirect()->back();
[c6b84df]261 }
[e78295c]262 Alert::flash($folder->name . " cannot be deleted", "error");
[4b7e2d3]263 return redirect()->back();
[c6b84df]264 }
265
266 public function downloadfolder(Request $request, $id)
267 {
[df6e9ec]268 $folder = Folder::find($id);
269 $FileSystem = new Filesystem();
[c6b84df]270
[507ade0]271 if($folder->no_of_files > 0) {
[8fdb18e]272 $zip_file = Storage::disk('local')->path('Folder.zip');
[507ade0]273 $zip = new \ZipArchive();
274 $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
[c6b84df]275
[8fdb18e]276 $path = Storage::disk('local')->path($folder->department->location . DIRECTORY_SEPARATOR . $folder->name);
[c6b84df]277
[507ade0]278 $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
[c6b84df]279
[507ade0]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 }
[c6b84df]287 }
[1f7c934]288
[507ade0]289 $zip->close();
290 $headers = array('Content-Type' => 'application/octet-stream',);
[1f7c934]291 $zip_new_name = Carbon::now()->format('d.m.Y - ') . $folder->name . '.zip';
[507ade0]292 return response()->download($zip_file, $zip_new_name, $headers);
[c6b84df]293 }
[507ade0]294 else {
[233e950]295 Alert::flash("This folder has no files", "warning");
296 return redirect()->back();
[c6b84df]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 {
[4b7e2d3]319 $files = File::where('folder_id', $id)->paginate(12);
[c6b84df]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,
[e78295c]330 "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes)),
[c6b84df]331 ]);
332 }
333}
334
Note: See TracBrowser for help on using the repository browser.