[24a616f] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | namespace App\Http\Controllers\Dashboard;
|
---|
| 4 |
|
---|
| 5 | use App\Helpers\Alert;
|
---|
| 6 | use App\Http\Requests\Dashboard\DocumentRequest;
|
---|
| 7 | use App\Models\Department;
|
---|
| 8 | use App\Models\Document;
|
---|
| 9 | use App\Models\File;
|
---|
| 10 | use App\Services\UploadService;
|
---|
| 11 | use Illuminate\Http\Request;
|
---|
| 12 | use Illuminate\Support\Facades\Storage;
|
---|
| 13 | use App\Http\Controllers\Controller;
|
---|
| 14 |
|
---|
| 15 | class DocumentsController extends Controller
|
---|
| 16 | {
|
---|
[d795fa6] | 17 | public function index(Request $request)
|
---|
[24a616f] | 18 | {
|
---|
[e6c1f87] | 19 | $queries = explode(" ", $request->search);
|
---|
| 20 | $result = collect();
|
---|
| 21 |
|
---|
| 22 | foreach ($queries as $query) {
|
---|
| 23 | $result->push(Document::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | $result = $result->flatten();
|
---|
| 27 |
|
---|
| 28 |
|
---|
[bd9e8e3] | 29 | if ($request->query('id')) {
|
---|
[e6c1f87] | 30 |
|
---|
| 31 | $documentsInDeptSort = Document::with('department')->when($request->has('id'), function($query) use ($request) {
|
---|
| 32 | $query->where('department_id', $request->query('id'));
|
---|
| 33 | });
|
---|
| 34 |
|
---|
| 35 | if($request->query('sort') == 'newest') {
|
---|
| 36 | $documents = $documentsInDeptSort->orderBy('created_at', 'desc')->paginate(20);
|
---|
| 37 | }
|
---|
| 38 | else if($request->query('sort') == 'name') {
|
---|
| 39 | $documents = $documentsInDeptSort->orderBy('name', 'asc')->paginate(20);
|
---|
| 40 | }
|
---|
| 41 | else{
|
---|
| 42 | $documents = Document::where('department_id', $request->query('id'))->paginate(20);
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | else {
|
---|
| 46 | if($request->query('sort') == 'newest') {
|
---|
| 47 | $documents = Document::orderBy('created_at', 'desc')->paginate(20);
|
---|
| 48 | }
|
---|
| 49 | else if($request->query('sort') == 'name') {
|
---|
| 50 | $documents = Document::orderBy('name', 'asc')->paginate(20);
|
---|
| 51 | }
|
---|
| 52 | else if($request->query('sort') == 'important'){
|
---|
| 53 | $documents = Document::where('is_important', true)->paginate(20);
|
---|
| 54 | }
|
---|
| 55 | else if($request->query('sort') == 'recent') {
|
---|
| 56 | $documents = Document::orderBy('created_at', 'desc')->paginate(20);
|
---|
| 57 | }
|
---|
| 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 | }
|
---|
| 67 | else
|
---|
| 68 | {
|
---|
| 69 | $documents = Document::paginate(20);
|
---|
| 70 | }
|
---|
[d795fa6] | 71 | }
|
---|
| 72 |
|
---|
| 73 | $departments = Department::all();
|
---|
[24a616f] | 74 |
|
---|
[e6c1f87] | 75 | $diskTotal = disk_total_space('/');
|
---|
| 76 | $diskTotalSize = $diskTotal / 1073741824;
|
---|
| 77 |
|
---|
| 78 | $diskFree = disk_free_space('/');
|
---|
| 79 | $used = $diskTotal - $diskFree;
|
---|
| 80 |
|
---|
| 81 | $diskUsedSize = $used / 1073741824;
|
---|
| 82 | $diskUse1 = round(100 - (($diskUsedSize / $diskTotalSize) * 100));
|
---|
| 83 | $diskUse = round(100 - ($diskUse1)) . '%';
|
---|
| 84 |
|
---|
[24a616f] | 85 | return view("dashboard.documents.index")->with([
|
---|
| 86 | "documents" => $documents,
|
---|
| 87 | "currentUser" => auth()->user(),
|
---|
[d795fa6] | 88 | "departments" => $departments,
|
---|
[24a616f] | 89 | "docsCount" => Department::withCount('document')->get(),
|
---|
[e6c1f87] | 90 | "totalDocs" => Document::all()->count(),
|
---|
| 91 | "countImportant" => Document::where('is_important', true)->get()->count(),
|
---|
| 92 | "diskTotal" => $diskTotal,
|
---|
| 93 | "diskTotalSize" => $diskTotalSize,
|
---|
| 94 | "diskUse" => $diskUse,
|
---|
| 95 | "diskUsedSize" => $diskUsedSize
|
---|
| 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 |
|
---|
[e6c1f87] | 108 | public function store(DocumentRequest $request, UploadService $uploadService)
|
---|
| 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 | if (!Storage::disk('local')->has($document->department()->pluck('location')->join("") . '/' . $request->arch_id)) {
|
---|
| 122 | Storage::disk('local')->makeDirectory($document->department()->pluck('location')->join("") . '/' . $request->arch_id);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | $documentFile = $uploadService->upload(File::class, [
|
---|
| 126 | "file_item" => $request->file_item,
|
---|
| 127 | ], "link", true);
|
---|
| 128 |
|
---|
| 129 | $document->save();
|
---|
| 130 |
|
---|
| 131 | foreach ($documentFile as $df) {
|
---|
| 132 | $file = File::find($df);
|
---|
| 133 | $file->document()->associate($document);
|
---|
| 134 | $file->save();
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | Alert::flash("New document created successfully");
|
---|
| 138 |
|
---|
| 139 | return redirect()->route("dashboard.documents.index");
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[24a616f] | 142 | public function editShow($id)
|
---|
| 143 | {
|
---|
| 144 | // if (!auth()->user()->hasPermission("edit_all_documents")) {
|
---|
| 145 | // return redirect()->route("dashboard.documents.index");
|
---|
| 146 | // }
|
---|
| 147 |
|
---|
| 148 | return view("dashboard.documents.edit")->with([
|
---|
| 149 | "document" => Document::findOrFail($id),
|
---|
| 150 | "departments" => Department::all()
|
---|
| 151 | ]);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | public function edit(DocumentRequest $request, $id)
|
---|
| 155 | {
|
---|
| 156 | $document = Document::findOrFail($id);
|
---|
| 157 |
|
---|
| 158 | $department = Department::find($request->department);
|
---|
| 159 |
|
---|
| 160 | $document->department()->associate($department);
|
---|
| 161 |
|
---|
| 162 | $document->name = $request->name;
|
---|
| 163 | $document->arch_id = $request->arch_id;
|
---|
| 164 | $document->description = $request->description;
|
---|
| 165 |
|
---|
| 166 | $document->save();
|
---|
| 167 |
|
---|
| 168 | Alert::flash("Document edited successfully");
|
---|
| 169 |
|
---|
| 170 | return redirect()->route("dashboard.documents.index");
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[e6c1f87] | 173 | public function toggleImportant($id)
|
---|
[24a616f] | 174 | {
|
---|
[e6c1f87] | 175 | $document = Document::find($id);
|
---|
| 176 | $document->is_important = !$document->is_important;
|
---|
| 177 | $document->save();
|
---|
[24a616f] | 178 |
|
---|
[e6c1f87] | 179 | if($document->is_important==true)
|
---|
| 180 | Alert::flash("Document marked as important successfully");
|
---|
| 181 | else
|
---|
| 182 | Alert::flash("Document marked as not important successfully");
|
---|
[24a616f] | 183 |
|
---|
[e6c1f87] | 184 | return redirect()->back();
|
---|
| 185 | }
|
---|
[24a616f] | 186 |
|
---|
[e6c1f87] | 187 | public function search(Request $request)
|
---|
| 188 | {
|
---|
| 189 | $queries = explode(" ", $request->q);
|
---|
| 190 | $result = collect();
|
---|
[24a616f] | 191 |
|
---|
[e6c1f87] | 192 | foreach ($queries as $query) {
|
---|
| 193 | $result->push(Document::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
|
---|
| 194 | }
|
---|
[24a616f] | 195 |
|
---|
[e6c1f87] | 196 | $result = $result->flatten();
|
---|
[24a616f] | 197 |
|
---|
[e6c1f87] | 198 | $departments = Department::all();
|
---|
[24a616f] | 199 |
|
---|
[e6c1f87] | 200 | return view("dashboard.documents.search")
|
---|
| 201 | ->with("searchQuery", $request->q)
|
---|
| 202 | ->with("results", $result)
|
---|
| 203 | ->with("departments", $departments)
|
---|
| 204 | ->with("countImportant", Document::where('is_important', true)->get()->count());
|
---|
| 205 | }
|
---|
[24a616f] | 206 |
|
---|
[e6c1f87] | 207 | public function destroy($id)
|
---|
| 208 | {
|
---|
| 209 | $document = Document::find($id);
|
---|
| 210 | if (auth()->user()->hasPermission("delete_all_posts")) {
|
---|
| 211 | $document->delete();
|
---|
| 212 | Alert::flash($document->name . " deleted successfully");
|
---|
| 213 | }
|
---|
[24a616f] | 214 | return redirect()->route("dashboard.documents.index");
|
---|
| 215 | }
|
---|
| 216 | }
|
---|