source: app/Http/Controllers/Dashboard/FoldersController.php@ 3536fe9

Last change on this file since 3536fe9 was 190db9f, checked in by beratkjufliju <kufliju@…>, 3 years ago

edited user permissions

  • Property mode set to 100644
File size: 12.0 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') {
38 $folders = $foldersInDeptSort->orderBy('created_at', 'desc')->paginate(12);
39 }
40 else if ($request->query('sort') == 'name') {
41 $folders = $foldersInDeptSort->orderBy('name', 'asc')->paginate(12);
42 }
[4b7e2d3]43 else if ($request->query('sort') == 'no_of_files') {
44 $folders = $foldersInDeptSort->orderBy('no_of_files', 'desc')->paginate(12);
45 }
[c6b84df]46 else if($request->query('sort') == 'count'){
47 $total = $foldersInDeptSort->folder->files->count();
48 $folders = $foldersInDeptSort->orderBy($total, 'asc')->paginate(12);
49 }
50 else {
51 $folders = Folder::where('department_id', $request->query('id'))->paginate(12);
52 }
53 } else {
54 if ($request->query('sort') == 'newest') {
55 $folders = Folder::orderBy('created_at', 'desc')->paginate(12);
56 }
57 else if ($request->query('sort') == 'name') {
58 $folders = Folder::orderBy('name', 'asc')->paginate(12);
59 }
[4b7e2d3]60 else if ($request->query('sort') == 'no_of_files') {
61 $folders = Folder::orderBy('no_of_files', 'desc')->paginate(12);
62 }
[c6b84df]63 else if ($request->query('sort') == 'recent') {
64 $folders = Folder::orderBy('created_at', 'desc')->paginate(12);
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(12);
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
121 $existingFolderName = Folder::where(['department_id' => $request->department, 'name' => $request->name])->count();
122 $existingFolderArchId = Folder::where(['department_id' => $request->department, 'arch_id' => $request->arch_id])->count();
123
124 if($existingFolder > 0) {
125 $folder->version = $existingFolder + 1;
126 }
127
128 if(($existingFolderName > 0 && $existingFolderArchId <= 0) || ($existingFolderName <= 0 && $existingFolderArchId > 0)) {
129 Alert::flash("Can't create another version since folder name or archive ID is different", "error");
130
131 return redirect()->route("dashboard.folders.index");
132 }
133
[c6b84df]134 $department = Department::find($request->department);
135
[e78295c]136 $user = auth()->user();
137
[c6b84df]138 $folder->user()->associate($user);
139 $folder->department()->associate($department);
[4b7e2d3]140 $folder->department()->increment('no_of_folders');
[c6b84df]141
142 $folder->arch_id = $request->arch_id;
143 $folder->name = $request->name;
144 $folder->note = $request->note;
145
146 $location = $folder->department->location . DIRECTORY_SEPARATOR . $request->name;
147
[8fdb18e]148 if (!Storage::disk('local')->has($location)) {
149 Storage::disk('local')->makeDirectory($location);
[c6b84df]150 }
151
152 $users = User::all();
153
154 $folder->location = $location;
155
156 $folder->save();
157
158 if ($request->has('file_item')) {
159 foreach ($request->file_item as $file) {
160 $fileName = $folder->name . '-' . uniqid() . '.' . $file->getClientOriginalExtension();
161 $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName);
162 $newFile = new File();
163 $newFile->name = $fileName;
164 $newFile->location = $location . DIRECTORY_SEPARATOR . $fileName;
165 $newFile->folder()->associate($folder);
[4b7e2d3]166 $newFile->folder()->increment('no_of_files');
[c6b84df]167 $newFile->save();
168 }
169 }
170
171 Alert::flash("New folder created successfully");
172
[94f05dc]173 return redirect()->route("dashboard.folders.index");
[c6b84df]174 }
175
176 public function editShow($id)
177 {
178 return view("dashboard.folders.edit")->with([
179 "folder" => Folder::findOrFail($id),
180 "departments" => Department::all(),
181 "files" => File::where('folder_id', $id)->get(),
182 "excelExt" => array("xls", "xlsx", "xls", "csv"),
183 "textExt" => array("txt", "doc", "docx"),
184 "imageExt" => array("png", "jpg", "jpeg"),
185 ]);
186 }
187
[4521f25]188 public function uploadFiles(FileRequest $request, $id)
189 {
190 $file = new File();
191
192 $folder = Folder::findOrFail($id);
193
194 $location = $folder->location;
195
196 $users = User::all();
197
198 if ($request->has('file_item')) {
199 foreach ($request->file_item as $file) {
200 $fileName = $file->getClientOriginalName();
201
202 if(File::where(['folder_id' => $folder->id, 'name' => $fileName])->count() > 0) {
203 Alert::flash("The uploaded file already exists", "error");
204
205 return redirect()->back();
206 }
207
208 $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName);
209 $newFile = new File();
210 $newFile->name = $fileName;
211 $newFile->location = $location . DIRECTORY_SEPARATOR . $fileName;
212 //$newFile->folder()->associate($folder);
213 $newFile->folder()->associate($folder);
214 $newFile->folder()->increment('no_of_files');
215 $newFile->save();
216 }
217
218 Alert::flash("New files added successfully");
219
220 return redirect()->back();
221 }
222 else {
223 Alert::flash("No files were uploaded", "error");
224
225 return redirect()->back();
226 }
227 }
228
[e78295c]229 public function destroy($id)
[c6b84df]230 {
[e78295c]231 $folder = Folder::find($id);
[c6b84df]232
[e78295c]233 $existingFolders = Folder::where(['department_id' => $folder->department->id, 'name' => $folder->name, 'arch_id' => $folder->arch_id])->count();
[c6b84df]234
[e78295c]235 if($existingFolders > 1 && $folder->version == 1) {
236 Alert::flash($folder->name . " has versions", "error");
237 return redirect()->back();
[c6b84df]238 }
239
[e78295c]240 $files = File::where('folder_id', $id)->get();
[4b7e2d3]241
[e78295c]242 if($files->count() > 0) {
243 Alert::flash($folder->name . " contains files", "error");
244 return redirect()->back();
[c6b84df]245 }
246
[190db9f]247 if (auth()->user()->hasPermission("delete_data")) {
[c6b84df]248
249 foreach ($files as $file) {
250 $file->delete();
251 }
[e78295c]252
[c6b84df]253 $folder->delete();
254 $location = $folder->department->location . DIRECTORY_SEPARATOR . $folder->name;
[8fdb18e]255 Storage::disk('local')->deleteDirectory($location);
[4b7e2d3]256 $folder->department()->decrement('no_of_folders');
[c6b84df]257 Alert::flash($folder->name . " deleted successfully");
[4b7e2d3]258 return redirect()->back();
[c6b84df]259 }
[e78295c]260 Alert::flash($folder->name . " cannot be deleted", "error");
[4b7e2d3]261 return redirect()->back();
[c6b84df]262 }
263
264 public function downloadfolder(Request $request, $id)
265 {
[df6e9ec]266 $folder = Folder::find($id);
267 $FileSystem = new Filesystem();
[c6b84df]268
[507ade0]269 if($folder->no_of_files > 0) {
[8fdb18e]270 $zip_file = Storage::disk('local')->path('Folder.zip');
[507ade0]271 $zip = new \ZipArchive();
272 $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
[c6b84df]273
[8fdb18e]274 $path = Storage::disk('local')->path($folder->department->location . DIRECTORY_SEPARATOR . $folder->name);
[c6b84df]275
[507ade0]276 $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
[c6b84df]277
[507ade0]278 foreach ($files as $file) {
279 if (!$file->isDir()) {
280 $filePath = $file->getRealPath();
281 // extracting filename with substr/strlen
282 $relativePath = substr($filePath, strlen($path) + 1);
283 $zip->addFile($filePath, $relativePath);
284 }
[c6b84df]285 }
[507ade0]286 $zip->close();
287 $headers = array('Content-Type' => 'application/octet-stream',);
288 $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . $folder->name . '.zip';
289 return response()->download($zip_file, $zip_new_name, $headers);
[c6b84df]290 }
[507ade0]291 else {
[233e950]292 Alert::flash("This folder has no files", "warning");
293 return redirect()->back();
[c6b84df]294 }
295 }
296
297 public function files(Request $request, $id)
298 {
299 $folder = Folder::findOrFail($id);
300 $files = File::where('folder_id', $id);
301 $deptId = $folder->department_id;
302 $folders = Folder::where('department_id', $deptId)->get();
303
304 $queries = explode(" ", $request->search);
305
306 if ($request->query('search')) {
307 $result = collect();
308
309 foreach ($queries as $query) {
310 $result->push($files->where("name", "like", "%{$query}%")->get());
311 }
312 $result = $result->flatten();
313 $files = $result;
314 }
315 else {
[4b7e2d3]316 $files = File::where('folder_id', $id)->paginate(12);
[c6b84df]317 }
318
319 return view("dashboard.folders.files")->with([
320 "folder" => $folder,
321 "departments" => Department::all(),
322 "files" => $files,
323 "excelExt" => array("xls", "xlsx", "xls", "csv"),
324 "textExt" => array("txt", "doc", "docx"),
325 "imageExt" => array("png", "jpg", "jpeg"),
326 "folders" => $folders,
[e78295c]327 "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes)),
[c6b84df]328 ]);
329 }
330}
331
Note: See TracBrowser for help on using the repository browser.