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

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

bug fixes

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