source: app/Http/Controllers/Dashboard/DocumentsController.php@ 6b95845

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

edited DocumentsController

  • Property mode set to 100644
File size: 11.1 KB
RevLine 
[24a616f]1<?php
2
3namespace App\Http\Controllers\Dashboard;
4
5use App\Helpers\Alert;
6use App\Http\Requests\Dashboard\DocumentRequest;
[ea7b12a]7use App\Http\Requests\Dashboard\FileRequest;
8use App\Http\Requests\Dashboard\PasswordSettingsRequest;
[24a616f]9use App\Models\Department;
10use App\Models\Document;
11use App\Models\File;
12use App\Services\UploadService;
[ea7b12a]13use Carbon\Carbon;
[24a616f]14use Illuminate\Http\Request;
[ea7b12a]15use Illuminate\Http\Response;
[24a616f]16use Illuminate\Support\Facades\Storage;
17use App\Http\Controllers\Controller;
18
19class DocumentsController extends Controller
20{
[d795fa6]21 public function index(Request $request)
[24a616f]22 {
[e6c1f87]23 $queries = explode(" ", $request->search);
24 $result = collect();
25
26 foreach ($queries as $query) {
27 $result->push(Document::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
28 }
29
30 $result = $result->flatten();
31
[b9c4a92]32 $deptName = "";
33 $deptCode = "";
[e6c1f87]34
[bd9e8e3]35 if ($request->query('id')) {
[b9c4a92]36 $deptName = Department::find($request->query('id'))->getOriginal('name');
37 $deptCode = Department::find($request->query('id'))->getOriginal('code');
38 $documentsInDeptSort = Document::with('department')->when($request->has('id'), function ($query) use ($request) {
[e6c1f87]39 $query->where('department_id', $request->query('id'));
40 });
41
[b9c4a92]42 if ($request->query('sort') == 'newest') {
43 $documents = $documentsInDeptSort->orderBy('created_at', 'desc')->paginate(16);
44 } else if ($request->query('sort') == 'name') {
45 $documents = $documentsInDeptSort->orderBy('name', 'asc')->paginate(16);
46 } else {
47 $documents = Document::where('department_id', $request->query('id'))->paginate(16);
[e6c1f87]48 }
[b9c4a92]49 } else {
50 if ($request->query('sort') == 'newest') {
51 $documents = Document::orderBy('created_at', 'desc')->paginate(16);
52 } else if ($request->query('sort') == 'name') {
53 $documents = Document::orderBy('name', 'asc')->paginate(16);
54 } else if ($request->query('sort') == 'important') {
55 $documents = Document::where('is_important', true)->paginate(16);
56 } else if ($request->query('sort') == 'recent') {
57 $documents = Document::orderBy('created_at', 'desc')->paginate(16);
58 } else if ($request->query('search')) {
[e6c1f87]59 $result = collect();
60
61 foreach ($queries as $query) {
62 $result->push(Document::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
63 }
64 $result = $result->flatten();
65 $documents = $result;
[b9c4a92]66 } else {
[e6c1f87]67 $documents = Document::paginate(20);
68 }
[d795fa6]69 }
70
71 $departments = Department::all();
[24a616f]72
[e6c1f87]73 $diskTotal = disk_total_space('/');
74 $diskTotalSize = $diskTotal / 1073741824;
75
[b9c4a92]76 $diskFree = disk_free_space('/');
[e6c1f87]77 $used = $diskTotal - $diskFree;
78
79 $diskUsedSize = $used / 1073741824;
80 $diskUse1 = round(100 - (($diskUsedSize / $diskTotalSize) * 100));
81 $diskUse = round(100 - ($diskUse1)) . '%';
82
[24a616f]83 return view("dashboard.documents.index")->with([
84 "documents" => $documents,
85 "currentUser" => auth()->user(),
[d795fa6]86 "departments" => $departments,
[24a616f]87 "docsCount" => Department::withCount('document')->get(),
[e6c1f87]88 "totalDocs" => Document::all()->count(),
89 "countImportant" => Document::where('is_important', true)->get()->count(),
90 "diskTotal" => $diskTotal,
91 "diskTotalSize" => $diskTotalSize,
92 "diskUse" => $diskUse,
[b9c4a92]93 "diskUsedSize" => $diskUsedSize,
94 "deptName" => $deptName,
95 "deptCode" => $deptCode,
[e6c1f87]96
[24a616f]97 ]);
[d795fa6]98
[24a616f]99 }
100
101 public function create()
102 {
103 return view("dashboard.documents.create")->with([
104 "departments" => Department::all()
105 ]);
106 }
107
[b9c4a92]108 public function store(DocumentRequest $request)
[e6c1f87]109 {
110 $document = new Document();
111 $user = auth()->user();
112 $department = Department::find($request->department);
113
114 $document->user()->associate($user);
115 $document->department()->associate($department);
116
117 $document->arch_id = $request->arch_id;
118 $document->name = $request->name;
119 $document->description = $request->description;
120
[ea7b12a]121 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $request->name;
[b9c4a92]122
123 if (!Storage::disk('local')->has($location)) {
124 Storage::disk('local')->makeDirectory($location);
[e6c1f87]125 }
126
127 $document->save();
128
[ea7b12a]129 if ($request->has('file_item')) {
130 foreach ($request->file_item as $file) {
131 $file->storeAs($location . DIRECTORY_SEPARATOR, $file->getClientOriginalName());
132 $newFile = new File();
133 $newFile->name = $file->getClientOriginalName();
134 $newFile->location = $location . DIRECTORY_SEPARATOR . $file->getClientOriginalName();
135 $newFile->document()->associate($document);
136 $newFile->save();
137 }
138 }
139
[e6c1f87]140 Alert::flash("New document created successfully");
141
142 return redirect()->route("dashboard.documents.index");
143 }
144
[24a616f]145 public function editShow($id)
146 {
147 return view("dashboard.documents.edit")->with([
148 "document" => Document::findOrFail($id),
[ea7b12a]149 "departments" => Department::all(),
150 "files" => File::where('document_id', $id)->get()
[24a616f]151 ]);
152 }
153
154 public function edit(DocumentRequest $request, $id)
155 {
156 $document = Document::findOrFail($id);
[ea7b12a]157 $files = File::where('document_id', $id)->get();
[24a616f]158
159 $department = Department::find($request->department);
160
161 $document->department()->associate($department);
162
[ea7b12a]163 $oldLocation = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name;
164
[24a616f]165 $document->name = $request->name;
166 $document->arch_id = $request->arch_id;
167 $document->description = $request->description;
168
[ea7b12a]169 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $request->name;
[b9c4a92]170
171 if ($document->isDirty('name')) {
172 if (!Storage::disk('local')->has($location)) {
173 Storage::disk('local')->move($oldLocation, $location);
[ea7b12a]174 foreach($files as $file){
[6b95845]175 $file->location = $location . DIRECTORY_SEPARATOR . $file->name;
176 $file->save();
[ea7b12a]177 }
[b9c4a92]178 }
179 }
180
181 if ($request->has('file_item')) {
182 foreach ($request->file_item as $file) {
183 $fileName = $file->getClientOriginalName();
[ea7b12a]184 if (Storage::disk('local')->has($location . DIRECTORY_SEPARATOR . $fileName)) {
185 // $hasFileError = true;
186 // break;
187 $NewFileName = time() . $fileName;
188 $file->storeAs($location . DIRECTORY_SEPARATOR, $NewFileName);
189 $newFile = new File();
190 $newFile->name = rand() . $fileName;
191 $newFile->location = $location . DIRECTORY_SEPARATOR . $NewFileName;
192 $newFile->document()->associate($document);
193 $newFile->save();
194 } else {
195 $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName);
196 $newFile = new File();
197 $newFile->name = $fileName;
198 $newFile->location = $location . DIRECTORY_SEPARATOR . $fileName;
199 $newFile->document()->associate($document);
200 $newFile->save();
[b9c4a92]201 }
202 }
203 }
204
[24a616f]205 $document->save();
206
[ea7b12a]207// if($hasFileError) {
208// Alert::flash('Document with the same name exists', 'error');
209// return redirect()->route("dashboard.documents.edit", ['id' => $document->id]);
210// }
211
212
213
[24a616f]214 Alert::flash("Document edited successfully");
215
[ea7b12a]216 return redirect()->back();
[24a616f]217 }
218
[e6c1f87]219 public function toggleImportant($id)
[24a616f]220 {
[e6c1f87]221 $document = Document::find($id);
222 $document->is_important = !$document->is_important;
223 $document->save();
[24a616f]224
[b9c4a92]225 if ($document->is_important == true)
226 Alert::flash("Document marked as important successfully");
[e6c1f87]227 else
228 Alert::flash("Document marked as not important successfully");
[24a616f]229
[e6c1f87]230 return redirect()->back();
231 }
[24a616f]232
[e6c1f87]233 public function search(Request $request)
234 {
235 $queries = explode(" ", $request->q);
236 $result = collect();
[24a616f]237
[e6c1f87]238 foreach ($queries as $query) {
239 $result->push(Document::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
240 }
[24a616f]241
[e6c1f87]242 $result = $result->flatten();
[24a616f]243
[e6c1f87]244 $departments = Department::all();
[24a616f]245
[e6c1f87]246 return view("dashboard.documents.search")
247 ->with("searchQuery", $request->q)
248 ->with("results", $result)
249 ->with("departments", $departments)
250 ->with("countImportant", Document::where('is_important', true)->get()->count());
251 }
[24a616f]252
[e6c1f87]253 public function destroy($id)
254 {
255 $document = Document::find($id);
[ea7b12a]256 $files = File::where('document_id', $id)->get();
[b9c4a92]257 if (auth()->user()->hasPermission("delete_all_documents")) {
[ea7b12a]258 // @dd($files);
259 foreach ($files as $file) {
260 $file->delete();
261 }
[e6c1f87]262 $document->delete();
[ea7b12a]263 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name;
264 Storage::disk('local')->deleteDirectory($location);
[e6c1f87]265 Alert::flash($document->name . " deleted successfully");
266 }
[24a616f]267 return redirect()->route("dashboard.documents.index");
268 }
[ea7b12a]269
270 public function deleteFile($id)
271 {
272 $file = File::find($id);
273 $file->delete();
274 Storage::disk('local')->delete($file->location);
275
276 Alert::flash($file->name . " deleted successfully");
277
278 return redirect()->back();
279 }
280
281 public function downloadFile($id)
282 {
283 $file = File::find($id);
284 return Storage::download($file->location);
285 }
286
287 public function renameFile(FileRequest $request, $id)
288 {
289 $file = File::find($id);
290 $fileExtension = explode('.', $file->name)[1];
291
292 $file->name = $request->name . '.' . $fileExtension;
293 $newLocation = 'Departments' . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[1] . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[2] . DIRECTORY_SEPARATOR . $file->name;
294
295 if(Storage::disk('local')->has($newLocation)) {
296 Alert::flash("A file with the same name already exists", "error");
297 return redirect()->back();
298 }
299 else {
300 Storage::disk('local')->copy($file->location, $newLocation);
301 Storage::disk('local')->delete($file->location);
302
303 $file->location = $newLocation;
304 $file->save();
305
306 Alert::flash($file->name . " updated successfully");
307 return redirect()->back();
308 }
309 }
[24a616f]310}
Note: See TracBrowser for help on using the repository browser.