opensupports/server/controllers/system/delete-department.php

120 lines
3.8 KiB
PHP
Raw Normal View History

<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
2017-04-18 02:09:16 +02:00
/**
* @api {post} /system/delete-department Delete department
* @apiVersion 4.7.0
2017-04-18 02:09:16 +02:00
*
* @apiName Delete department
*
* @apiGroup System
2017-04-18 02:09:16 +02:00
*
* @apiDescription This path deletes a department.
2017-04-18 02:09:16 +02:00
*
* @apiPermission staff3
2017-04-18 02:09:16 +02:00
*
* @apiParam {Number} departmentId Id of the department to be deleted.
2017-04-21 08:09:24 +02:00
* @apiParam {Number} transferDepartmentId Id of the department where the tickets will be transfer to.
2017-04-18 02:09:16 +02:00
*
2017-04-21 08:09:24 +02:00
* @apiUse NO_PERMISSION
* @apiUse INVALID_DEPARTMENT
* @apiUse SAME_DEPARTMENT
2017-04-18 02:09:16 +02:00
*
2017-04-21 08:09:24 +02:00
* @apiSuccess {Object} data Empty object
2017-04-18 02:09:16 +02:00
*
*/
class DeleteDepartmentController extends Controller {
const PATH = '/delete-department';
const METHOD = 'POST';
private $departmentId;
private $transferDepartmentId;
public function validations() {
return [
'permission' => 'staff_3',
'requestData' => [
'departmentId' => [
'validation' => DataValidator::dataStoreId('department'),
'error' => ERRORS::INVALID_DEPARTMENT
],
'transferDepartmentId' => [
'validation' => DataValidator::dataStoreId('department'),
'error' => ERRORS::INVALID_DEPARTMENT
]
]
];
}
public function handler() {
$this->departmentId = Controller::request('departmentId');
$this->transferDepartmentId = Controller::request('transferDepartmentId');
$this->checkDepartmentIdDefault();
if ($this->departmentId === $this->transferDepartmentId) {
2018-11-20 23:41:00 +01:00
throw new RequestException(ERRORS::SAME_DEPARTMENT);
}
2018-11-29 22:37:04 +01:00
$departmentToTransfer = Department::getDataStore($this->transferDepartmentId);
$departmentInstance = Department::getDataStore($this->departmentId);
2018-11-29 22:37:04 +01:00
if($departmentToTransfer->private && Ticket::count(' author_id IS NOT NULL AND department_id = ? ', [$departmentInstance->id])) {
throw new RequestException(ERRORS::DEPARTMENT_PRIVATE_TICKETS);
}
$this->transferDepartmentTickets();
$departmentInstance->delete();
Log::createLog('DELETE_DEPARTMENT', $departmentInstance->name);
Response::respondSuccess();
}
2018-11-29 22:37:04 +01:00
public function transferDepartmentTickets() {
$tickets = Ticket::find('department_id = ?', [$this->departmentId]);
$newDepartment = Department::getDataStore($this->transferDepartmentId);
foreach($tickets as $ticket) {
$staffOwner = $ticket->owner;
if($staffOwner) {
$hasDepartment = false;
foreach($staffOwner->sharedDepartmentList as $department) {
if($department->id === $newDepartment->id) {
$hasDepartment = true;
}
}
if(!$hasDepartment) {
$staffOwner->sharedTicketList->remove($ticket);
$staffOwner->store();
$ticket->owner = null;
$ticket->unread = true;
$event = Ticketevent::getEvent(Ticketevent::UN_ASSIGN);
$event->setProperties(array(
'authorStaff' => $staffOwner,
'date' => Date::getCurrentDate()
));
$ticket->addEvent($event);
}
}
$ticket->department = $newDepartment;
$ticket->store();
}
}
public function checkDepartmentIdDefault() {
$defaultDepartment = Setting::getSetting('default-department-id');
if ($defaultDepartment && $this->departmentId == $defaultDepartment->value) throw new Exception(ERRORS::CAN_NOT_DELETE_DEFAULT_DEPARTMENT);
}
2018-11-29 22:37:04 +01:00
}