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
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(12);
39 }
40 else if ($request->query('sort') == 'name') {
41 $folders = $foldersInDeptSort->orderBy('name', 'asc')->paginate(12);
42 }
43 else if ($request->query('sort') == 'no_of_files') {
44 $folders = $foldersInDeptSort->orderBy('no_of_files', 'desc')->paginate(12);
45 }
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 }
60 else if ($request->query('sort') == 'no_of_files') {
61 $folders = Folder::orderBy('no_of_files', 'desc')->paginate(12);
62 }
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,
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])->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
134 $department = Department::find($request->department);
135
136 $user = auth()->user();
137
138 $folder->user()->associate($user);
139 $folder->department()->associate($department);
140 $folder->department()->increment('no_of_folders');
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
148 if (!Storage::disk('local')->has($location)) {
149 Storage::disk('local')->makeDirectory($location);
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);
167 $newFile->folder()->increment('no_of_files');
168 $newFile->save();
169 }
170 }
171
172 Alert::flash("New folder created successfully");
173
174 return redirect()->route("dashboard.folders.index");
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
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
230 public function destroy($id)
231 {
232 $folder = Folder::find($id);
233
234 $existingFolders = Folder::where(['department_id' => $folder->department->id, 'name' => $folder->name, 'arch_id' => $folder->arch_id])->count();
235
236 if($existingFolders > 1 && $folder->version == 1) {
237 Alert::flash($folder->name . " has versions", "error");
238 return redirect()->back();
239 }
240
241 $files = File::where('folder_id', $id)->get();
242
243 if($files->count() > 0) {
244 Alert::flash($folder->name . " contains files", "error");
245 return redirect()->back();
246 }
247
248 if (auth()->user()->hasPermission("delete_all_folders")) {
249
250 foreach ($files as $file) {
251 $file->delete();
252 }
253
254 $folder->delete();
255 $location = $folder->department->location . DIRECTORY_SEPARATOR . $folder->name;
256 Storage::disk('local')->deleteDirectory($location);
257 $folder->department()->decrement('no_of_folders');
258 Alert::flash($folder->name . " deleted successfully");
259 return redirect()->back();
260 }
261 Alert::flash($folder->name . " cannot be deleted", "error");
262 return redirect()->back();
263 }
264
265 public function downloadfolder(Request $request, $id)
266 {
267 $folder = Folder::find($id);
268 $FileSystem = new Filesystem();
269
270 if($folder->no_of_files > 0) {
271 $zip_file = Storage::disk('local')->path('Folder.zip');
272 $zip = new \ZipArchive();
273 $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
274
275 $path = Storage::disk('local')->path($folder->department->location . DIRECTORY_SEPARATOR . $folder->name);
276
277 $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
278
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 }
286 }
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);
291 }
292 else {
293 Alert::flash("This folder has no files", "warning");
294 return redirect()->back();
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 {
317 $files = File::where('folder_id', $id)->paginate(12);
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,
328 "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes)),
329 ]);
330 }
331}
332
Note: See TracBrowser for help on using the repository browser.