1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers\Dashboard;
|
---|
4 |
|
---|
5 | use App\Helpers\Alert;
|
---|
6 | use App\Http\Requests\Dashboard\FolderRequest;
|
---|
7 | use App\Http\Requests\Dashboard\FileNameRequest;
|
---|
8 | use App\Models\Department;
|
---|
9 | use App\Models\FileType;
|
---|
10 | use App\Models\Folder;
|
---|
11 | use App\Models\File;
|
---|
12 | use App\Models\User;
|
---|
13 | use App\Notifications\NewFolderCreated;
|
---|
14 | use Carbon\Carbon;
|
---|
15 | use Illuminate\Filesystem\Filesystem;
|
---|
16 | use Illuminate\Http\Request;
|
---|
17 | use Illuminate\Support\Facades\Notification;
|
---|
18 | use Illuminate\Support\Facades\Storage;
|
---|
19 | use App\Http\Controllers\Controller;
|
---|
20 |
|
---|
21 | class 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 |
|
---|
280 | $FileSystem = new Filesystem();
|
---|
281 | $zip_file = Storage::disk('uploads')->path('Folder.zip');
|
---|
282 | $zip = new \ZipArchive();
|
---|
283 | $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
|
---|
284 | $path = Storage::disk('uploads')->path($folder->department->location . DIRECTORY_SEPARATOR . $folder->name);
|
---|
285 |
|
---|
286 | try {
|
---|
287 | $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
|
---|
288 |
|
---|
289 | $filesInDoc = $FileSystem->allFiles($path);
|
---|
290 |
|
---|
291 | foreach ($files as $file) {
|
---|
292 | if (!$file->isDir()) {
|
---|
293 | $filePath = $file->getRealPath();
|
---|
294 | // extracting filename with substr/strlen
|
---|
295 | $relativePath = substr($filePath, strlen($path) + 1);
|
---|
296 | $zip->addFile($filePath, $relativePath);
|
---|
297 | }
|
---|
298 | }
|
---|
299 | $zip->close();
|
---|
300 | $headers = array('Content-Type' => 'application/octet-stream',);
|
---|
301 | $zip_new_name = Carbon::now()->format('d.m.Y - H:i') . $folder->name . '.zip';
|
---|
302 | return response()->download($zip_file, $zip_new_name, $headers);
|
---|
303 | }
|
---|
304 | catch (\Exception $e) {
|
---|
305 | Alert::flash("This folder has no files", "warning");
|
---|
306 | return redirect()->back();
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | public function files(Request $request, $id)
|
---|
311 | {
|
---|
312 | $folder = Folder::findOrFail($id);
|
---|
313 | $files = File::where('folder_id', $id);
|
---|
314 | $deptId = $folder->department_id;
|
---|
315 | $folders = Folder::where('department_id', $deptId)->get();
|
---|
316 |
|
---|
317 | $queries = explode(" ", $request->search);
|
---|
318 |
|
---|
319 | if ($request->query('search')) {
|
---|
320 | $result = collect();
|
---|
321 |
|
---|
322 | foreach ($queries as $query) {
|
---|
323 | $result->push($files->where("name", "like", "%{$query}%")->get());
|
---|
324 | }
|
---|
325 | $result = $result->flatten();
|
---|
326 | $files = $result;
|
---|
327 | }
|
---|
328 | else {
|
---|
329 | $files = File::where('folder_id', $id)->paginate(12);
|
---|
330 | }
|
---|
331 |
|
---|
332 | return view("dashboard.folders.files")->with([
|
---|
333 | "folder" => $folder,
|
---|
334 | "departments" => Department::all(),
|
---|
335 | "files" => $files,
|
---|
336 | "excelExt" => array("xls", "xlsx", "xls", "csv"),
|
---|
337 | "textExt" => array("txt", "doc", "docx"),
|
---|
338 | "imageExt" => array("png", "jpg", "jpeg"),
|
---|
339 | "folders" => $folders,
|
---|
340 | "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes))
|
---|
341 | ]);
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|