mirror of
https://github.com/opensupports/opensupports.git
synced 2025-07-04 12:34:51 +02:00
* Remove unexpected error in upload csv file * Fix bug when render csv file error list * csv files without eof line work OK * [DEV-239] Prettify code
76 lines
1.9 KiB
PHP
Executable File
76 lines
1.9 KiB
PHP
Executable File
<?php
|
|
|
|
/**
|
|
* @api {post} /system/csv-import CSV import
|
|
* @apiVersion 4.11.0
|
|
*
|
|
* @apiName CSV import
|
|
*
|
|
* @apiGroup System
|
|
*
|
|
* @apiDescription This path receives a csv file with a list of users to signup.
|
|
*
|
|
* @apiPermission staff3
|
|
*
|
|
* @apiParam {String} file A csv file with this content format: email, password, name.
|
|
*
|
|
* @apiUse NO_PERMISSION
|
|
* @apiUse INVALID_FILE
|
|
*
|
|
* @apiSuccess {String[]} data Array of errors found
|
|
*
|
|
*/
|
|
|
|
class CSVImportController extends Controller {
|
|
const PATH = '/csv-import';
|
|
const METHOD = 'POST';
|
|
|
|
public function validations() {
|
|
return [
|
|
'permission' => 'staff_3',
|
|
'requestData' => []
|
|
];
|
|
}
|
|
|
|
public function handler() {
|
|
$fileUploader = $this->uploadFile(true);
|
|
|
|
if(!$fileUploader instanceof FileUploader) {
|
|
throw new RequestException(ERRORS::INVALID_FILE);
|
|
}
|
|
|
|
$file = fopen($fileUploader->getFullFilePath(),'r');
|
|
$errors = [];
|
|
|
|
while(($user = fgetcsv($file)) != false) {
|
|
Controller::setDataRequester(function ($key) use ($user) {
|
|
switch ($key) {
|
|
case 'email':
|
|
return $user[0];
|
|
case 'password':
|
|
return $user[1];
|
|
case 'name':
|
|
return $user[2];
|
|
}
|
|
|
|
return null;
|
|
});
|
|
|
|
$signupController = new SignUpController(true);
|
|
|
|
try {
|
|
$signupController->validate();
|
|
$signupController->handler();
|
|
} catch (\Exception $exception) {
|
|
$errors[] = $exception->getMessage() . ' in email ' . $user[0];
|
|
}
|
|
}
|
|
|
|
fclose($file);
|
|
|
|
unlink($fileUploader->getFullFilePath());
|
|
|
|
Response::respondSuccess($errors);
|
|
}
|
|
}
|