2017-01-14 22:19:21 +01:00
|
|
|
<?php
|
|
|
|
|
2017-04-18 02:09:16 +02:00
|
|
|
/**
|
2017-05-12 06:58:40 +02:00
|
|
|
* @api {post} /system/csv-import CSV import
|
2018-11-29 17:35:14 +01:00
|
|
|
* @apiVersion 4.3.2
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
2017-04-21 08:09:24 +02:00
|
|
|
* @apiName CSV import
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiGroup System
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
2017-04-21 08:09:24 +02:00
|
|
|
* @apiDescription This path receives a csv file with a list of users to signup.
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiPermission staff3
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiParam {String} file A csv file with this content format: email, password, name.
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
2017-04-21 08:09:24 +02:00
|
|
|
* @apiUse NO_PERMISSION
|
2017-04-22 03:33:17 +02:00
|
|
|
* @apiUse INVALID_FILE
|
2017-04-21 08:09:24 +02:00
|
|
|
*
|
2017-04-22 03:33:17 +02:00
|
|
|
* @apiSuccess {String[]} data Array of errors found
|
2017-04-18 02:09:16 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-01-14 22:19:21 +01:00
|
|
|
class CSVImportController extends Controller {
|
|
|
|
const PATH = '/csv-import';
|
2017-02-08 19:09:15 +01:00
|
|
|
const METHOD = 'POST';
|
2017-01-14 22:19:21 +01:00
|
|
|
|
|
|
|
public function validations() {
|
|
|
|
return [
|
|
|
|
'permission' => 'staff_3',
|
|
|
|
'requestData' => []
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handler() {
|
2017-03-04 22:38:49 +01:00
|
|
|
$fileUploader = $this->uploadFile(true);
|
2017-01-15 21:33:33 +01:00
|
|
|
|
2017-01-16 22:04:26 +01:00
|
|
|
if(!$fileUploader instanceof FileUploader) {
|
2018-11-20 23:41:00 +01:00
|
|
|
throw new RequestException(ERRORS::INVALID_FILE);
|
2017-01-16 22:04:26 +01:00
|
|
|
}
|
|
|
|
|
2017-01-15 21:33:33 +01:00
|
|
|
$file = fopen($fileUploader->getFullFilePath(),'r');
|
2017-01-14 22:19:21 +01:00
|
|
|
$errors = [];
|
|
|
|
|
|
|
|
while(!feof($file)) {
|
|
|
|
$userList = fgetcsv($file);
|
|
|
|
|
|
|
|
Controller::setDataRequester(function ($key) use ($userList) {
|
|
|
|
switch ($key) {
|
|
|
|
case 'email':
|
|
|
|
return $userList[0];
|
|
|
|
case 'password':
|
|
|
|
return $userList[1];
|
|
|
|
case 'name':
|
|
|
|
return $userList[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
$signupController = new SignUpController(true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$signupController->validate();
|
|
|
|
$signupController->handler();
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
$errors[] = $exception->getMessage() . ' in email ' . $userList[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($file);
|
2017-01-15 21:33:33 +01:00
|
|
|
|
|
|
|
unlink($fileUploader->getFullFilePath());
|
|
|
|
|
2017-01-14 22:19:21 +01:00
|
|
|
Response::respondSuccess($errors);
|
|
|
|
}
|
|
|
|
}
|