Changeset ea7b12a for app/Http/Controllers/Dashboard
- Timestamp:
- 10/19/21 17:46:21 (3 years ago)
- Branches:
- develop, master
- Children:
- 6b95845
- Parents:
- b9c4a92
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
app/Http/Controllers/Dashboard/DocumentsController.php
rb9c4a92 rea7b12a 5 5 use App\Helpers\Alert; 6 6 use App\Http\Requests\Dashboard\DocumentRequest; 7 use App\Http\Requests\Dashboard\FileRequest; 8 use App\Http\Requests\Dashboard\PasswordSettingsRequest; 7 9 use App\Models\Department; 8 10 use App\Models\Document; 9 11 use App\Models\File; 10 12 use App\Services\UploadService; 13 use Carbon\Carbon; 11 14 use Illuminate\Http\Request; 15 use Illuminate\Http\Response; 12 16 use Illuminate\Support\Facades\Storage; 13 17 use App\Http\Controllers\Controller; … … 115 119 $document->description = $request->description; 116 120 117 $location = DIRECTORY_SEPARATOR . 'Departments' . DIRECTORY_SEPARATOR . $document->department->name . DIRECTORY_SEPARATOR . $request->name;121 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $request->name; 118 122 119 123 if (!Storage::disk('local')->has($location)) { … … 121 125 } 122 126 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 }130 131 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 } 132 139 133 140 Alert::flash("New document created successfully"); … … 140 147 return view("dashboard.documents.edit")->with([ 141 148 "document" => Document::findOrFail($id), 142 "departments" => Department::all() 149 "departments" => Department::all(), 150 "files" => File::where('document_id', $id)->get() 143 151 ]); 144 152 } … … 147 155 { 148 156 $document = Document::findOrFail($id); 157 $files = File::where('document_id', $id)->get(); 149 158 150 159 $department = Department::find($request->department); 151 160 152 161 $document->department()->associate($department); 162 163 $oldLocation = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name; 153 164 154 165 $document->name = $request->name; … … 156 167 $document->description = $request->description; 157 168 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; 169 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $request->name; 160 170 161 171 if ($document->isDirty('name')) { 162 172 if (!Storage::disk('local')->has($location)) { 163 173 Storage::disk('local')->move($oldLocation, $location); 164 } 165 } 166 167 $hasFileError = false; 174 foreach($files as $file){ 175 $file->location = "test"; 176 } 177 } 178 } 179 168 180 if ($request->has('file_item')) { 169 181 foreach ($request->file_item as $file) { 170 182 $fileName = $file->getClientOriginalName(); 171 if(Storage::disk('local')->has($location . DIRECTORY_SEPARATOR . $fileName)) { 172 $hasFileError = true; 173 break; 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(); 174 200 } 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]); 201 } 186 202 } 187 203 188 204 $document->save(); 189 205 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 190 213 Alert::flash("Document edited successfully"); 191 214 192 return redirect()-> route("dashboard.documents.index");215 return redirect()->back(); 193 216 } 194 217 … … 230 253 { 231 254 $document = Document::find($id); 255 $files = File::where('document_id', $id)->get(); 232 256 if (auth()->user()->hasPermission("delete_all_documents")) { 257 // @dd($files); 258 foreach ($files as $file) { 259 $file->delete(); 260 } 233 261 $document->delete(); 262 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name; 263 Storage::disk('local')->deleteDirectory($location); 234 264 Alert::flash($document->name . " deleted successfully"); 235 265 } 236 266 return redirect()->route("dashboard.documents.index"); 237 267 } 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 } 238 309 }
Note:
See TracChangeset
for help on using the changeset viewer.