2017-01-14 22:19:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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) {
|
|
|
|
throw new Exception(ERRORS::INVALID_FILE);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|