source: app/Http/Controllers/Dashboard/DocumentsController.php@ ea7b12a

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

added files crud in table

  • 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){
175 $file->location = "test";
176 }
[b9c4a92]177 }
178 }
179
180 if ($request->has('file_item')) {
181 foreach ($request->file_item as $file) {
182 $fileName = $file->getClientOriginalName();
[ea7b12a]183 if (Storage::disk('local')->has($location . DIRECTORY_SEPARATOR . $fileName)) {
184 // $hasFileError = true;
185 // break;
186 $NewFileName = time() . $fileName;
187 $file->storeAs($location . DIRECTORY_SEPARATOR, $NewFileName);
188 $newFile = new File();
189 $newFile->name = rand() . $fileName;
190 $newFile->location = $location . DIRECTORY_SEPARATOR . $NewFileName;
191 $newFile->document()->associate($document);
192 $newFile->save();
193 } else {
194 $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName);
195 $newFile = new File();
196 $newFile->name = $fileName;
197 $newFile->location = $location . DIRECTORY_SEPARATOR . $fileName;
198 $newFile->document()->associate($document);
199 $newFile->save();
[b9c4a92]200 }
201 }
202 }
203
[24a616f]204 $document->save();
205
[ea7b12a]206// if($hasFileError) {
207// Alert::flash('Document with the same name exists', 'error');
208// return redirect()->route("dashboard.documents.edit", ['id' => $document->id]);
209// }
210
211
212
[24a616f]213 Alert::flash("Document edited successfully");
214
[ea7b12a]215 return redirect()->back();
[24a616f]216 }
217
[e6c1f87]218 public function toggleImportant($id)
[24a616f]219 {
[e6c1f87]220 $document = Document::find($id);
221 $document->is_important = !$document->is_important;
222 $document->save();
[24a616f]223
[b9c4a92]224 if ($document->is_important == true)
225 Alert::flash("Document marked as important successfully");
[e6c1f87]226 else
227 Alert::flash("Document marked as not important successfully");
[24a616f]228
[e6c1f87]229 return redirect()->back();
230 }
[24a616f]231
[e6c1f87]232 public function search(Request $request)
233 {
234 $queries = explode(" ", $request->q);
235 $result = collect();
[24a616f]236
[e6c1f87]237 foreach ($queries as $query) {
238 $result->push(Document::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
239 }
[24a616f]240
[e6c1f87]241 $result = $result->flatten();
[24a616f]242
[e6c1f87]243 $departments = Department::all();
[24a616f]244
[e6c1f87]245 return view("dashboard.documents.search")
246 ->with("searchQuery", $request->q)
247 ->with("results", $result)
248 ->with("departments", $departments)
249 ->with("countImportant", Document::where('is_important', true)->get()->count());
250 }
[24a616f]251
[e6c1f87]252 public function destroy($id)
253 {
254 $document = Document::find($id);
[ea7b12a]255 $files = File::where('document_id', $id)->get();
[b9c4a92]256 if (auth()->user()->hasPermission("delete_all_documents")) {
[ea7b12a]257 // @dd($files);
258 foreach ($files as $file) {
259 $file->delete();
260 }
[e6c1f87]261 $document->delete();
[ea7b12a]262 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name;
263 Storage::disk('local')->deleteDirectory($location);
[e6c1f87]264 Alert::flash($document->name . " deleted successfully");
265 }
[24a616f]266 return redirect()->route("dashboard.documents.index");
267 }
[ea7b12a]268
269 public function deleteFile($id)
270 {
271 $file = File::find($id);
272 $file->delete();
273 Storage::disk('local')->delete($file->location);
274
275 Alert::flash($file->name . " deleted successfully");
276
277 return redirect()->back();
278 }
279
280 public function downloadFile($id)
281 {
282 $file = File::find($id);
283 return Storage::download($file->location);
284 }
285
286 public function renameFile(FileRequest $request, $id)
287 {
288 $file = File::find($id);
289 $fileExtension = explode('.', $file->name)[1];
290
291 $file->name = $request->name . '.' . $fileExtension;
292 $newLocation = 'Departments' . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[1] . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[2] . DIRECTORY_SEPARATOR . $file->name;
293
294 if(Storage::disk('local')->has($newLocation)) {
295 Alert::flash("A file with the same name already exists", "error");
296 return redirect()->back();
297 }
298 else {
299 Storage::disk('local')->copy($file->location, $newLocation);
300 Storage::disk('local')->delete($file->location);
301
302 $file->location = $newLocation;
303 $file->save();
304
305 Alert::flash($file->name . " updated successfully");
306 return redirect()->back();
307 }
308 }
[24a616f]309}
Note: See TracBrowser for help on using the repository browser.