opensupports/server/controllers/staff/add.php

130 lines
3.8 KiB
PHP
Raw Normal View History

2016-12-06 03:17:18 +01:00
<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
2017-04-18 06:39:55 +02:00
/**
2017-04-21 08:09:24 +02:00
* @api {post} /staff/add Add a new staff member.
2017-04-18 06:39:55 +02:00
*
* @apiName Add
*
* @apiGroup staff
*
* @apiDescription This path add a new staff member.
*
* @apiPermission Staff level 3
*
* @apiParam {String} name The name of the new staff member.
* @apiParam {String} email The email of the new staff member.
* @apiParam {String} password The password of the new staff member.
2017-04-21 08:09:24 +02:00
* @apiParam {Number} level The level of the new staff member.
* @apiParam {String} profilePic The profile pic of the new staff member.
* @apiParam {String} departments The departments that will have assigned the new staff member.
*
* @apiUse NO_PERMISSION
* @apiUse INVALID_NAME
* @apiUse INVALID_EMAIL
* @apiUse INVALID_PASSWORD
* @apiUse INVALID_LEVEL
* @apiUse ALREADY_A_STAFF
2017-04-18 06:39:55 +02:00
*
* @apiSuccess {Object} data
*
*/
2016-12-06 03:17:18 +01:00
class AddStaffController extends Controller {
const PATH = '/add';
const METHOD = 'POST';
2016-12-06 03:17:18 +01:00
private $name;
private $email;
private $password;
private $profilePic;
private $level;
private $departments;
public function validations() {
return [
'permission' => 'staff_3',
'requestData' => [
'name' => [
'validation' => DataValidator::length(2, 55),
2016-12-06 03:17:18 +01:00
'error' => ERRORS::INVALID_NAME
],
'email' => [
'validation' => DataValidator::email(),
'error' => ERRORS::INVALID_EMAIL
],
'password' => [
'validation' => DataValidator::length(5, 200),
'error' => ERRORS::INVALID_PASSWORD
],
'level' => [
'validation' => DataValidator::between(1, 3, true),
'error' => ERRORS::INVALID_LEVEL
]
]
];
}
public function handler() {
$this->storeRequestData();
2016-12-07 20:59:42 +01:00
$staff = new Staff();
2016-12-06 03:17:18 +01:00
2016-12-07 20:59:42 +01:00
$staffRow = Staff::getDataStore($this->email,'email');
2016-12-06 03:17:18 +01:00
2016-12-07 20:59:42 +01:00
if($staffRow->isNull()) {
2016-12-06 03:17:18 +01:00
$staff->setProperties([
'name'=> $this->name,
'email' => $this->email,
2016-12-08 07:21:37 +01:00
'password'=> Hashing::hashPassword($this->password),
2016-12-06 03:17:18 +01:00
'profilePic' => $this->profilePic,
'level' => $this->level,
'sharedDepartmentList' => $this->getDepartmentList()
2016-12-06 03:17:18 +01:00
]);
$this->addOwner();
2016-12-08 07:21:37 +01:00
Log::createLog('ADD_STAFF', $this->name);
2016-12-08 07:21:37 +01:00
Response::respondSuccess([
'id' => $staff->store()
]);
2016-12-06 03:17:18 +01:00
return;
}
Response::respondError(ERRORS::ALREADY_A_STAFF);
}
2016-12-07 20:59:42 +01:00
2016-12-06 03:17:18 +01:00
public function storeRequestData() {
$this->name = Controller::request('name');
$this->email = Controller::request('email');
$this->password = Controller::request('password');
$this->profilePic = Controller::request('profilePic');
$this->level = Controller::request('level');
$this->departments = Controller::request('departments');
}
2016-12-07 20:59:42 +01:00
public function getDepartmentList() {
$listDepartments = new DataStoreList();
$departmentIds = json_decode($this->departments);
foreach($departmentIds as $id) {
$department = Department::getDataStore($id);
$listDepartments->add($department);
}
return $listDepartments;
}
public function addOwner() {
$departmentIds = json_decode($this->departments);
foreach($departmentIds as $id) {
$departmentRow = Department::getDataStore($id);
$departmentRow->owners++;
$departmentRow->store();
}
}
2016-12-06 03:17:18 +01:00
}