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

develop
Last change on this file since aab55f4 was aab55f4, checked in by Berat Kjufliju <kufliju@…>, 3 years ago

bug fixes

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