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
Line 
1<?php
2
3namespace App\Http\Controllers\Dashboard;
4
5use App\Helpers\Alert;
6use App\Http\Requests\Dashboard\DocumentRequest;
7use App\Http\Requests\Dashboard\FileRequest;
8use App\Http\Requests\Dashboard\PasswordSettingsRequest;
9use App\Models\Department;
10use App\Models\Document;
11use App\Models\File;
12use App\Services\UploadService;
13use Carbon\Carbon;
14use Illuminate\Http\Request;
15use Illuminate\Http\Response;
16use Illuminate\Support\Facades\Storage;
17use App\Http\Controllers\Controller;
18
19class DocumentsController extends Controller
20{
21 public function index(Request $request)
22 {
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
32 $deptName = "";
33 $deptCode = "";
34
35 if ($request->query('id')) {
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) {
39 $query->where('department_id', $request->query('id'));
40 });
41
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);
48 }
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')) {
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;
66 } else {
67 $documents = Document::paginate(20);
68 }
69 }
70
71 $departments = Department::all();
72
73 $diskTotal = disk_total_space('/');
74 $diskTotalSize = $diskTotal / 1073741824;
75
76 $diskFree = disk_free_space('/');
77 $used = $diskTotal - $diskFree;
78
79 $diskUsedSize = $used / 1073741824;
80 $diskUse1 = round(100 - (($diskUsedSize / $diskTotalSize) * 100));
81 $diskUse = round(100 - ($diskUse1)) . '%';
82
83 return view("dashboard.documents.index")->with([
84 "documents" => $documents,
85 "currentUser" => auth()->user(),
86 "departments" => $departments,
87 "docsCount" => Department::withCount('document')->get(),
88 "totalDocs" => Document::all()->count(),
89 "countImportant" => Document::where('is_important', true)->get()->count(),
90 "diskTotal" => $diskTotal,
91 "diskTotalSize" => $diskTotalSize,
92 "diskUse" => $diskUse,
93 "diskUsedSize" => $diskUsedSize,
94 "deptName" => $deptName,
95 "deptCode" => $deptCode,
96
97 ]);
98
99 }
100
101 public function create()
102 {
103 return view("dashboard.documents.create")->with([
104 "departments" => Department::all()
105 ]);
106 }
107
108 public function store(DocumentRequest $request)
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
121 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $request->name;
122
123 if (!Storage::disk('local')->has($location)) {
124 Storage::disk('local')->makeDirectory($location);
125 }
126
127 $document->save();
128
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
140 Alert::flash("New document created successfully");
141
142 return redirect()->route("dashboard.documents.index");
143 }
144
145 public function editShow($id)
146 {
147 return view("dashboard.documents.edit")->with([
148 "document" => Document::findOrFail($id),
149 "departments" => Department::all(),
150 "files" => File::where('document_id', $id)->get()
151 ]);
152 }
153
154 public function edit(DocumentRequest $request, $id)
155 {
156 $document = Document::findOrFail($id);
157 $files = File::where('document_id', $id)->get();
158
159 $department = Department::find($request->department);
160
161 $document->department()->associate($department);
162
163 $oldLocation = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name;
164
165 $document->name = $request->name;
166 $document->arch_id = $request->arch_id;
167 $document->description = $request->description;
168
169 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $request->name;
170
171 if ($document->isDirty('name')) {
172 if (!Storage::disk('local')->has($location)) {
173 Storage::disk('local')->move($oldLocation, $location);
174 foreach($files as $file){
175 $file->location = $location . DIRECTORY_SEPARATOR . $file->name;
176 $file->save();
177 }
178 }
179 }
180
181 if ($request->has('file_item')) {
182 foreach ($request->file_item as $file) {
183 $fileName = $file->getClientOriginalName();
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();
201 }
202 }
203 }
204
205 $document->save();
206
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
214 Alert::flash("Document edited successfully");
215
216 return redirect()->back();
217 }
218
219 public function toggleImportant($id)
220 {
221 $document = Document::find($id);
222 $document->is_important = !$document->is_important;
223 $document->save();
224
225 if ($document->is_important == true)
226 Alert::flash("Document marked as important successfully");
227 else
228 Alert::flash("Document marked as not important successfully");
229
230 return redirect()->back();
231 }
232
233 public function search(Request $request)
234 {
235 $queries = explode(" ", $request->q);
236 $result = collect();
237
238 foreach ($queries as $query) {
239 $result->push(Document::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
240 }
241
242 $result = $result->flatten();
243
244 $departments = Department::all();
245
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 }
252
253 public function destroy($id)
254 {
255 $document = Document::find($id);
256 $files = File::where('document_id', $id)->get();
257 if (auth()->user()->hasPermission("delete_all_documents")) {
258 // @dd($files);
259 foreach ($files as $file) {
260 $file->delete();
261 }
262 $document->delete();
263 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name;
264 Storage::disk('local')->deleteDirectory($location);
265 Alert::flash($document->name . " deleted successfully");
266 }
267 return redirect()->route("dashboard.documents.index");
268 }
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 }
310}
Note: See TracBrowser for help on using the repository browser.