Merged in OS154-CSV-Import (pull request #127)

Os154 csv import
This commit is contained in:
Ivan Diaz 2017-01-16 19:00:07 -03:00
commit ebc2800698
8 changed files with 100 additions and 18 deletions

View File

@ -16,6 +16,7 @@ require_once 'system/add-api-key.php';
require_once 'system/delete-api-key.php';
require_once 'system/get-all-keys.php';
require_once 'system/delete-all-users.php';
require_once 'system/csv-import.php';
require_once 'system/backup-database.php';
require_once 'system/download.php';
@ -41,5 +42,6 @@ $systemControllerGroup->addController(new GetAllKeyController);
$systemControllerGroup->addController(new DeleteAllUsersController);
$systemControllerGroup->addController(new BackupDatabaseController);
$systemControllerGroup->addController(new DownloadController);
$systemControllerGroup->addController(new CSVImportController);
$systemControllerGroup->finalize();

View File

@ -0,0 +1,55 @@
<?php
class CSVImportController extends Controller {
const PATH = '/csv-import';
public function validations() {
return [
'permission' => 'staff_3',
'requestData' => []
];
}
public function handler() {
$fileUploader = $this->uploadFile();
if(!$fileUploader instanceof FileUploader) {
throw new Exception(ERRORS::INVALID_FILE);
}
$file = fopen($fileUploader->getFullFilePath(),'r');
$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);
unlink($fileUploader->getFullFilePath());
Response::respondSuccess($errors);
}
}

View File

@ -47,10 +47,12 @@ class CommentController extends Controller {
}
private function storeComment() {
$fileUploader = $this->uploadFile();
$comment = Ticketevent::getEvent(Ticketevent::COMMENT);
$comment->setProperties(array(
'content' => $this->content,
'file' => $this->uploadFile(),
'file' => ($fileUploader instanceof FileUploader) ? $fileUploader->getFileName() : null,
'date' => Date::getCurrentDate()
));

View File

@ -53,6 +53,8 @@ class CreateController extends Controller {
$department = Department::getDataStore($this->departmentId);
$author = Controller::getLoggedUser();
$fileUploader = $this->uploadFile();
$ticket = new Ticket();
$ticket->setProperties(array(
'title' => $this->title,
@ -60,7 +62,7 @@ class CreateController extends Controller {
'language' => $this->language,
'author' => $author,
'department' => $department,
'file' => $this->uploadFile(),
'file' => ($fileUploader instanceof FileUploader) ? $fileUploader->getFileName() : null,
'date' => Date::getCurrentDate(),
'unread' => false,
'unreadStaff' => true,

View File

@ -10,9 +10,14 @@ class SignUpController extends Controller {
private $userName;
private $userPassword;
private $verificationToken;
private $csvImported;
public function __construct($csvImported = false) {
$this->csvImported = $csvImported;
}
public function validations() {
return [
$validations = [
'permission' => 'any',
'requestData' => [
'name' => [
@ -26,13 +31,18 @@ class SignUpController extends Controller {
'password' => [
'validation' => DataValidator::length(5, 200),
'error' => ERRORS::INVALID_PASSWORD
],
'captcha' => [
'validation' => DataValidator::captcha(),
'error' => ERRORS::INVALID_CAPTCHA
]
]
];
if(!$this->csvImported) {
$validations['requestData']['captcha'] = [
'validation' => DataValidator::captcha(),
'error' => ERRORS::INVALID_CAPTCHA
];
}
return $validations;
}
public function handler() {
@ -42,19 +52,16 @@ class SignUpController extends Controller {
$existentUser = User::getUser($this->userEmail, 'email');
if (!$existentUser->isNull()) {
Response::respondError(ERRORS::USER_EXISTS);
return;
throw new Exception(ERRORS::USER_EXISTS);
}
$banRow = Ban::getDataStore($this->userEmail,'email');
if (!$banRow->isNull()) {
Response::respondError(ERRORS::ALREADY_BANNED);
return;
throw new Exception(ERRORS::ALREADY_BANNED);
}
if (!Setting::getSetting('registration')->value && $apiKey->isNull() ) {
Response::respondError(ERRORS::NO_PERMISSION);
return;
if (!Setting::getSetting('registration')->value && $apiKey->isNull() && !$this->csvImported) {
throw new Exception(ERRORS::NO_PERMISSION);
}
$userId = $this->createNewUserAndRetrieveId();

View File

@ -23,6 +23,8 @@ include_once 'libs/FileManager.php';
include_once 'libs/FileDownloader.php';
include_once 'libs/FileUploader.php';
Controller::init();
// LOAD DATA
spl_autoload_register(function ($class) {
$classPath = "data/{$class}.php";

View File

@ -3,6 +3,7 @@ require_once 'libs/Validator.php';
require_once 'models/Session.php';
abstract class Controller {
private static $dataRequester;
/**
* Instance-related stuff
@ -28,10 +29,20 @@ abstract class Controller {
$validator->validate($this->validations());
}
public static function request($key) {
$app = self::getAppInstance();
public static function init() {
self::$dataRequester = function ($key) {
$app = self::getAppInstance();
return $app->request()->post($key);
return $app->request()->post($key);
};
}
public static function setDataRequester($dataRequester) {
self::$dataRequester = $dataRequester;
}
public static function request($key) {
return call_user_func(self::$dataRequester, $key);
}
public static function getLoggedUser() {
@ -77,7 +88,7 @@ abstract class Controller {
$fileQuantity->value++;
$fileQuantity->store();
return $fileUploader->getFileName();
return $fileUploader;
} else {
throw new Exception(ERRORS::INVALID_FILE);
}

View File

@ -59,3 +59,4 @@ require './system/add-api-key.rb'
require './system/delete-api-key.rb'
require './system/get-all-keys.rb'
require './system/file-upload-download.rb'
require './system/csv-import.rb'