moe-lk /
bulk-upload
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Http\Controllers; |
||
| 4 | |||
| 5 | use App\Models\Upload; |
||
| 6 | use Aws\Ses\SesClient; |
||
| 7 | use Illuminate\Http\Request; |
||
| 8 | use Illuminate\Support\Facades\Auth; |
||
| 9 | use Illuminate\Support\Facades\DB; |
||
| 10 | use Illuminate\Support\Facades\Response; |
||
| 11 | use Illuminate\Support\Facades\Storage; |
||
| 12 | use Illuminate\Support\Facades\Validator; |
||
| 13 | use App\Models\Institution_class; |
||
| 14 | |||
| 15 | |||
| 16 | class FileController extends Controller |
||
| 17 | { |
||
| 18 | |||
| 19 | |||
| 20 | public function __construct() |
||
| 21 | { |
||
| 22 | $this->middleware('auth'); |
||
| 23 | $this->ses = new SesClient( |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 24 | [ |
||
| 25 | 'version' => '2010-12-01', |
||
| 26 | 'region' => 'us-east-2', |
||
| 27 | |||
| 28 | ] |
||
| 29 | ); |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param Request $request |
||
| 34 | * @return \Illuminate\Http\JsonResponse |
||
| 35 | */ |
||
| 36 | public function upload(Request $request){ |
||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | $validator = Validator::make( |
||
| 41 | [ |
||
| 42 | 'import_file' => $request->import_file, |
||
| 43 | 'extension' => strtolower($request->import_file->getClientOriginalExtension()), |
||
| 44 | 'class' => $request->class, |
||
| 45 | 'email' => auth()->user()->email |
||
| 46 | ], |
||
| 47 | [ |
||
| 48 | 'import_file' => 'required', |
||
| 49 | 'extension' => 'required|in:xlsx,xls,ods|max:2048', |
||
| 50 | 'class' => 'required', |
||
| 51 | 'email' => 'required' |
||
| 52 | |||
| 53 | ], |
||
| 54 | ['email.required' => 'You dont have email in your account, pleas contact your Zonal/Provincial Coordinator and update the email to get notification'] |
||
| 55 | ); |
||
| 56 | // try { |
||
| 57 | // $result = $this->ses->verifyEmailIdentity([ |
||
| 58 | // 'EmailAddress' => auth()->user()->email, |
||
| 59 | // ]); |
||
| 60 | // var_dump($result); |
||
| 61 | // } catch (AwsException $e) { |
||
| 62 | // // output error message if fails |
||
| 63 | // echo $e->getMessage(); |
||
| 64 | // echo "\n"; |
||
| 65 | // } |
||
| 66 | // if ($validator->fails()) { |
||
| 67 | // return back() |
||
| 68 | // ->withErrors($validator); |
||
| 69 | // } |
||
| 70 | |||
| 71 | |||
| 72 | $uploadFile = $validator->validated()['import_file']; |
||
| 73 | $class = Institution_class::find($validator->validated()['class']); |
||
| 74 | // dd(auth()->user()->principal[0]->institution_group[0]->institution); |
||
| 75 | $institution = auth()->user()->permissions->isEmpty() ? auth()->user()->principal[0]->institution_group[0]->institution->code : auth()->user()->permissions[0]->institution_staff->institution->code; |
||
| 76 | |||
| 77 | |||
| 78 | $fileName = time().'_'.$institution.'_'.str_replace(' ','_', clean($class->name)).'_'.auth()->user()->openemis_no.'_student_bulk_data.xlsx'; |
||
|
0 ignored issues
–
show
|
|||
| 79 | Storage::disk('local')->putFileAs( |
||
| 80 | 'sis-bulk-data-files/', |
||
| 81 | $uploadFile, |
||
| 82 | $fileName |
||
| 83 | ); |
||
| 84 | |||
| 85 | $upload = new Upload; |
||
| 86 | $upload->fileName =$fileName; |
||
|
0 ignored issues
–
show
|
|||
| 87 | $upload->model = 'Student'; |
||
| 88 | $upload->node = 'None'; |
||
| 89 | $upload->institution_class_id = $class->id; |
||
| 90 | $upload->user()->associate(auth()->user()); |
||
| 91 | $upload->save(); |
||
| 92 | |||
| 93 | |||
| 94 | return redirect('/')->withSuccess('The file is uploaded, we will process and let you know by your email'); |
||
|
0 ignored issues
–
show
|
|||
| 95 | } |
||
| 96 | |||
| 97 | public function updateQueueWithUnprocessedFiles($id, $action){ |
||
| 98 | if($action == 100){ |
||
| 99 | DB::table('uploads') |
||
| 100 | ->where('id', $id) |
||
| 101 | ->update(['is_processed' => 0]); |
||
| 102 | }elseif ($action == 200) { |
||
| 103 | DB::table('uploads') |
||
| 104 | ->where('id', $id) |
||
| 105 | ->update(['is_processed' => 4]); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 | public function downloadTemplate(){ |
||
| 111 | $filename = 'censusNo_className_sis_students_bulk_upload'; |
||
| 112 | $version = '2007_V2.0.2_20201211.xlsx'; |
||
| 113 | $file_path = storage_path() .'/app/public/'. $filename.'_'.$version;; |
||
| 114 | if (file_exists($file_path)) |
||
| 115 | { |
||
| 116 | return Response::download($file_path, Auth::user()->openemis_no.'_'.$filename.$version, [ |
||
|
0 ignored issues
–
show
|
|||
| 117 | 'Content-Length: '. filesize($file_path) |
||
| 118 | ]); |
||
| 119 | } |
||
| 120 | else |
||
| 121 | { |
||
| 122 | return response()->view('errors.404'); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * @param $filename |
||
| 129 | * @return Processed excel file with error |
||
|
0 ignored issues
–
show
The type
App\Http\Controllers\Processed was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 130 | */ |
||
| 131 | public function downloadErrorFile($filename){ |
||
| 132 | |||
| 133 | $file_path = storage_path().'/app/sis-bulk-data-files/processed/'. $filename; |
||
| 134 | if (file_exists($file_path)) |
||
| 135 | { |
||
| 136 | return Response::download($file_path, $filename, [ |
||
|
0 ignored issues
–
show
|
|||
| 137 | 'Content-Length: '. filesize($file_path) |
||
| 138 | ]); |
||
| 139 | } |
||
| 140 | else |
||
| 141 | { |
||
| 142 | abort(404, 'We did not found an error file.'); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | |||
| 147 | public function downloadFile($filename){ |
||
| 148 | $file_path = storage_path().'/app/sis-bulk-data-files/'. $filename; |
||
| 149 | if (file_exists($file_path)) |
||
| 150 | { |
||
| 151 | return Response::download($file_path, $filename, [ |
||
| 152 | 'Content-Length: '. filesize($file_path) |
||
| 153 | ]); |
||
| 154 | } |
||
| 155 | else |
||
| 156 | { |
||
| 157 | |||
| 158 | abort(404, 'We did not found an error file.'); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 |