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

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

added fileTypes controller, notifications, excel export, edited views

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