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 | use function Sodium\increment;
|
---|
21 |
|
---|
22 | class 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 | }
|
---|
42 | else if ($request->query('sort') == 'no_of_files') {
|
---|
43 | $folders = $foldersInDeptSort->orderBy('no_of_files', 'desc')->paginate(12);
|
---|
44 | }
|
---|
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 | }
|
---|
59 | else if ($request->query('sort') == 'no_of_files') {
|
---|
60 | $folders = Folder::orderBy('no_of_files', 'desc')->paginate(12);
|
---|
61 | }
|
---|
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,
|
---|
102 | "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes))
|
---|
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();
|
---|
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 |
|
---|
133 | $department = Department::find($request->department);
|
---|
134 |
|
---|
135 | $user = auth()->user();
|
---|
136 |
|
---|
137 | $folder->user()->associate($user);
|
---|
138 | $folder->department()->associate($department);
|
---|
139 | $folder->department()->increment('no_of_folders');
|
---|
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);
|
---|
166 | $newFile->folder()->increment('no_of_files');
|
---|
167 | $newFile->save();
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | Alert::flash("New folder created successfully");
|
---|
172 |
|
---|
173 | return redirect()->route("dashboard.folders.index");
|
---|
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 |
|
---|
188 | public function destroy($id)
|
---|
189 | {
|
---|
190 | $folder = Folder::find($id);
|
---|
191 |
|
---|
192 | $existingFolders = Folder::where(['department_id' => $folder->department->id, 'name' => $folder->name, 'arch_id' => $folder->arch_id])->count();
|
---|
193 |
|
---|
194 | if($existingFolders > 1 && $folder->version == 1) {
|
---|
195 | Alert::flash($folder->name . " has versions", "error");
|
---|
196 | return redirect()->back();
|
---|
197 | }
|
---|
198 |
|
---|
199 | $files = File::where('folder_id', $id)->get();
|
---|
200 |
|
---|
201 | if($files->count() > 0) {
|
---|
202 | Alert::flash($folder->name . " contains files", "error");
|
---|
203 | return redirect()->back();
|
---|
204 | }
|
---|
205 |
|
---|
206 | if (auth()->user()->hasPermission("delete_all_folders")) {
|
---|
207 |
|
---|
208 | foreach ($files as $file) {
|
---|
209 | $file->delete();
|
---|
210 | }
|
---|
211 |
|
---|
212 | $folder->delete();
|
---|
213 | $location = $folder->department->location . DIRECTORY_SEPARATOR . $folder->name;
|
---|
214 | Storage::disk('uploads')->deleteDirectory($location);
|
---|
215 | $folder->department()->decrement('no_of_folders');
|
---|
216 | Alert::flash($folder->name . " deleted successfully");
|
---|
217 | return redirect()->back();
|
---|
218 | }
|
---|
219 | Alert::flash($folder->name . " cannot be deleted", "error");
|
---|
220 | return redirect()->back();
|
---|
221 | }
|
---|
222 |
|
---|
223 | public function downloadfolder(Request $request, $id)
|
---|
224 | {
|
---|
225 | $folder = Folder::find($id);
|
---|
226 | $FileSystem = new Filesystem();
|
---|
227 |
|
---|
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);
|
---|
232 |
|
---|
233 | $path = Storage::disk('uploads')->path($folder->department->location . DIRECTORY_SEPARATOR . $folder->name);
|
---|
234 |
|
---|
235 | $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
|
---|
236 |
|
---|
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 | }
|
---|
244 | }
|
---|
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);
|
---|
249 | }
|
---|
250 | else {
|
---|
251 | Alert::flash("This folder has no files", "warning");
|
---|
252 | return redirect()->back();
|
---|
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 {
|
---|
275 | $files = File::where('folder_id', $id)->paginate(12);
|
---|
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,
|
---|
286 | "fileTypes" => '.' . implode(',.', explode(',', FileType::find('1')->mimes)),
|
---|
287 | ]);
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|