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

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

added version

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