opensupports/server/controllers/staff/get-new-tickets.php

90 lines
2.5 KiB
PHP
Raw Normal View History

2016-10-15 10:03:56 +02:00
<?php
use RedBeanPHP\Facade as RedBean;
2016-10-15 10:03:56 +02:00
use Respect\Validation\Validator as DataValidator;
2017-04-18 06:39:55 +02:00
/**
* @api {post} /staff/get-new-tickets Get new tickets
* @apiVersion 4.7.0
2017-04-18 06:39:55 +02:00
*
* @apiName Get new tickets
*
* @apiGroup Staff
2017-04-18 06:39:55 +02:00
*
* @apiDescription This path retrieves the new tickets of the departments the staff has assigned.
2017-04-18 06:39:55 +02:00
*
* @apiPermission staff1
2017-04-18 06:39:55 +02:00
*
* @apiParam {Number} page The page number.
*
2017-04-21 08:09:24 +02:00
* @apiUse NO_PERMISSION
* @apiUse INVALID_PAGE
*
* @apiSuccess {Object} data Information about a tickets and quantity of pages.
* @apiSuccess {[Ticket](#api-Data_Structures-ObjectTicket)[]} data.tickets Array of new tickets of the current page.
* @apiSuccess {Number} data.page Number of current page.
* @apiSuccess {Number} data.pages Quantity of pages.
2017-04-18 06:39:55 +02:00
*
*/
class GetNewTicketsStaffController extends Controller {
2016-10-15 10:03:56 +02:00
const PATH = '/get-new-tickets';
const METHOD = 'POST';
2016-10-15 10:03:56 +02:00
public function validations() {
return[
'permission' => 'staff_1',
'requestData' => [
'page' => [
'validation' => DataValidator::numeric(),
'error' => ERRORS::INVALID_PAGE
]
]
2016-10-15 10:03:56 +02:00
];
}
public function handler() {
2018-11-29 04:31:00 +01:00
$page = Controller::request('page');
2019-07-26 12:49:43 +02:00
$departmentId = Controller::request('departmentId');
2018-11-29 04:31:00 +01:00
if (Ticket::isTableEmpty()) {
2018-11-29 04:31:00 +01:00
Response::respondSuccess([
'tickets' => [],
'page' => $page,
'pages' => 0
]);
return;
}
$user = Controller::getLoggedUser();
$query = ' (';
foreach ($user->sharedDepartmentList as $department) {
$query .= 'department_id=' . $department->id . ' OR ';
}
$ownerExists = RedBean::exec('SHOW COLUMNS FROM ticket LIKE \'owner_id\'');
if($ownerExists != 0) {
$query .= 'FALSE) AND closed = 0 AND owner_id IS NULL';
} else {
$query .= 'FALSE) AND closed = 0';
}
2019-07-26 12:49:43 +02:00
if($departmentId) {
$query .= ' AND department_id=' . $departmentId;
}
$countTotal = Ticket::count($query);
$query .= ' ORDER BY unread_staff DESC';
$query .= ' LIMIT 10 OFFSET ' . ($page-1)*10;
$ticketList = Ticket::find($query);
Response::respondSuccess([
'tickets' => $ticketList->toArray(true),
'page' => $page,
'pages' => ceil($countTotal / 10)
]);
2016-10-15 10:03:56 +02:00
}
}