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

74 lines
2.2 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/edit-department Edit department
* @apiVersion 4.7.0
2017-04-18 02:09:16 +02:00
*
* @apiName Edit department
*
* @apiGroup System
2017-04-18 02:09:16 +02:00
*
* @apiDescription This path edits a department's name.
2017-04-18 02:09:16 +02:00
*
* @apiPermission staff3
2017-04-18 02:09:16 +02:00
*
2017-04-21 08:09:24 +02:00
* @apiParam {String} name The new name of the department.
* @apiParam {Number} departmentId The Id of the department.
2018-11-16 00:51:44 +01:00
* @apiParam {Boolean} private Indicates if the department is shown to users;
2017-04-18 02:09:16 +02:00
*
2017-04-21 08:09:24 +02:00
* @apiUse NO_PERMISSION
* @apiUse INVALID_NAME
* @apiUse INVALID_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 EditDepartmentController extends Controller {
const PATH = '/edit-department';
const METHOD = 'POST';
public function validations() {
return [
'permission' => 'staff_3',
'requestData' => [
'departmentId' => [
'validation' => DataValidator::dataStoreId('department'),
'error' => ERRORS::INVALID_DEPARTMENT
],
'name' => [
'validation' => DataValidator::notBlank()->length(1, 200),
'error' => ERRORS::INVALID_NAME
],
]
];
}
public function handler() {
2018-11-29 22:37:04 +01:00
$newName = Controller::request('name');
$departmentId = Controller::request('departmentId');
2018-11-16 00:51:44 +01:00
$private = Controller::request('private');
$departmentInstance = Department::getDataStore($departmentId);
if($private && $departmentId == Setting::getSetting('default-department-id')->getValue()){
throw new RequestException(ERRORS::DEFAULT_DEPARTMENT_CAN_NOT_BE_PRIVATE);
}
2018-11-29 22:37:04 +01:00
if($private && Ticket::count(' author_id IS NOT NULL AND department_id = ? ', [$departmentId])) {
throw new RequestException(ERRORS::DEPARTMENT_PRIVATE_TICKETS);
}
if($newName) $departmentInstance->name = $newName;
2018-11-16 00:51:44 +01:00
$departmentInstance->private = $private ? 1 : 0;
$departmentInstance->store();
Log::createLog('EDIT_DEPARTMENT', $departmentInstance->name);
2018-11-16 00:51:44 +01:00
Response::respondSuccess();
}
2018-11-16 00:51:44 +01:00
}