- Timestamp:
- 10/18/21 23:00:23 (3 years ago)
- Branches:
- develop, master
- Children:
- ea7b12a
- Parents:
- e6c1f87
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
app/Http/Controllers/Dashboard/DocumentsController.php
re6c1f87 rb9c4a92 26 26 $result = $result->flatten(); 27 27 28 $deptName = ""; 29 $deptCode = ""; 28 30 29 31 if ($request->query('id')) { 30 31 $documentsInDeptSort = Document::with('department')->when($request->has('id'), function($query) use ($request) { 32 $deptName = Department::find($request->query('id'))->getOriginal('name'); 33 $deptCode = Department::find($request->query('id'))->getOriginal('code'); 34 $documentsInDeptSort = Document::with('department')->when($request->has('id'), function ($query) use ($request) { 32 35 $query->where('department_id', $request->query('id')); 33 36 }); 34 37 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')){ 38 if ($request->query('sort') == 'newest') { 39 $documents = $documentsInDeptSort->orderBy('created_at', 'desc')->paginate(16); 40 } else if ($request->query('sort') == 'name') { 41 $documents = $documentsInDeptSort->orderBy('name', 'asc')->paginate(16); 42 } else { 43 $documents = Document::where('department_id', $request->query('id'))->paginate(16); 44 } 45 } else { 46 if ($request->query('sort') == 'newest') { 47 $documents = Document::orderBy('created_at', 'desc')->paginate(16); 48 } else if ($request->query('sort') == 'name') { 49 $documents = Document::orderBy('name', 'asc')->paginate(16); 50 } else if ($request->query('sort') == 'important') { 51 $documents = Document::where('is_important', true)->paginate(16); 52 } else if ($request->query('sort') == 'recent') { 53 $documents = Document::orderBy('created_at', 'desc')->paginate(16); 54 } else if ($request->query('search')) { 59 55 $result = collect(); 60 56 … … 64 60 $result = $result->flatten(); 65 61 $documents = $result; 66 } 67 else 68 { 62 } else { 69 63 $documents = Document::paginate(20); 70 64 } … … 76 70 $diskTotalSize = $diskTotal / 1073741824; 77 71 78 $diskFree 72 $diskFree = disk_free_space('/'); 79 73 $used = $diskTotal - $diskFree; 80 74 … … 93 87 "diskTotalSize" => $diskTotalSize, 94 88 "diskUse" => $diskUse, 95 "diskUsedSize" => $diskUsedSize 89 "diskUsedSize" => $diskUsedSize, 90 "deptName" => $deptName, 91 "deptCode" => $deptCode, 96 92 97 93 ]); … … 106 102 } 107 103 108 public function store(DocumentRequest $request , UploadService $uploadService)104 public function store(DocumentRequest $request) 109 105 { 110 106 $document = new Document(); … … 119 115 $document->description = $request->description; 120 116 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); 117 $location = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $request->name; 118 119 if (!Storage::disk('local')->has($location)) { 120 Storage::disk('local')->makeDirectory($location); 121 } 122 123 foreach ($request->file_item as $file) { 124 $file->storeAs($location . DIRECTORY_SEPARATOR, $file->getClientOriginalName()); 125 $newFile = new File(); 126 $newFile->link = $location . DIRECTORY_SEPARATOR . $file->getClientOriginalName(); 127 $newFile->document()->associate($document); 128 $newFile->save(); 129 } 128 130 129 131 $document->save(); 130 132 131 foreach ($documentFile as $df) {132 $file = File::find($df);133 $file->document()->associate($document);134 $file->save();135 }136 137 133 Alert::flash("New document created successfully"); 138 134 … … 142 138 public function editShow($id) 143 139 { 144 // if (!auth()->user()->hasPermission("edit_all_documents")) {145 // return redirect()->route("dashboard.documents.index");146 // }147 148 140 return view("dashboard.documents.edit")->with([ 149 141 "document" => Document::findOrFail($id), … … 164 156 $document->description = $request->description; 165 157 158 $oldLocation = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $document->name; 159 $location = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $request->name; 160 161 if ($document->isDirty('name')) { 162 if (!Storage::disk('local')->has($location)) { 163 Storage::disk('local')->move($oldLocation, $location); 164 } 165 } 166 167 $hasFileError = false; 168 if ($request->has('file_item')) { 169 foreach ($request->file_item as $file) { 170 $fileName = $file->getClientOriginalName(); 171 if(Storage::disk('local')->has($location . DIRECTORY_SEPARATOR . $fileName)) { 172 $hasFileError = true; 173 break; 174 } 175 $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName); 176 $newFile = new File(); 177 $newFile->link = $location . DIRECTORY_SEPARATOR . $fileName; 178 $newFile->document()->associate($document); 179 $newFile->save(); 180 } 181 } 182 183 if($hasFileError) { 184 Alert::flash('Document with the same name exists', 'error'); 185 return redirect()->route("dashboard.documents.edit", ['id' => $document->id]); 186 } 187 166 188 $document->save(); 167 189 … … 177 199 $document->save(); 178 200 179 if ($document->is_important==true)180 Alert::flash("Document marked as important successfully");201 if ($document->is_important == true) 202 Alert::flash("Document marked as important successfully"); 181 203 else 182 204 Alert::flash("Document marked as not important successfully"); … … 208 230 { 209 231 $document = Document::find($id); 210 if (auth()->user()->hasPermission("delete_all_ posts")) {232 if (auth()->user()->hasPermission("delete_all_documents")) { 211 233 $document->delete(); 212 234 Alert::flash($document->name . " deleted successfully");
Note:
See TracChangeset
for help on using the changeset viewer.