diff --git a/server/controllers/article/add-topic.php b/server/controllers/article/add-topic.php index 8c0fc90a..3fef448a 100755 --- a/server/controllers/article/add-topic.php +++ b/server/controllers/article/add-topic.php @@ -4,7 +4,7 @@ DataValidator::with('CustomValidations', true); /** * @api {post} /article/add-topic Add topic - * @apiVersion 4.6.0 + * @apiVersion 4.6.1 * * @apiName Add topic * diff --git a/server/controllers/ticket.php b/server/controllers/ticket.php index bc046c51..c451e84a 100755 --- a/server/controllers/ticket.php +++ b/server/controllers/ticket.php @@ -25,5 +25,6 @@ $ticketControllers->addController(new GetTagsController); $ticketControllers->addController(new AddTagController); $ticketControllers->addController(new RemoveTagController); $ticketControllers->addController(new SearchController); +$ticketControllers->addController(new GetAuthorsController); $ticketControllers->finalize(); diff --git a/server/controllers/ticket/get-authors.php b/server/controllers/ticket/get-authors.php new file mode 100644 index 00000000..6e1c77a0 --- /dev/null +++ b/server/controllers/ticket/get-authors.php @@ -0,0 +1,104 @@ + 'staff_1', + 'requestData' => [ + 'query' => [ + 'validation' => DataValidator::oneOf(DataValidator::notBlank(),DataValidator::nullType()), + 'error' => ERRORS::INVALID_QUERY + ], + 'blackList' => [ + 'validation' => DataValidator::oneOf(DataValidator::notBlank(),DataValidator::nullType(),DataValidator::arrayType()), + 'error' => ERRORS::INVALID_BLACK_LIST + ] + ] + ]; + } + + public function handler() { + $query = Controller::request('query'); + + $idAuthorsQuery = "SELECT id,name,level FROM staff " . $this->GenerateAuthorsIdQuery($query) . " LIMIT 10"; + $authorsIdList = RedBean::getAll($idAuthorsQuery, [':query' => "%" .$query . "%",':query2' => $query . "%"] ); + $authorsList = []; + + foreach($authorsIdList as $item) { + if($item['level'] >=1 && $item['level'] <= 3){ + $author = Staff::getDataStore($item['id']*1); + } else { + $author = User::getDataStore($item['id']*1); + } + array_push($authorsList, $author->toArray()); + } + Response::respondSuccess([ + 'authors' => $authorsList + ]); + } + public function generateAuthorsIdQuery($query) { + if ($query){ + return "WHERE name LIKE :query " . $this->generateStaffBlackListQuery() . " UNION SELECT id,name,signup_date FROM user WHERE name LIKE :query " . $this->generateUserBlackListQuery() . " ORDER BY CASE WHEN (name LIKE :query2) THEN 1 ELSE 2 END ASC "; + } else { + return "WHERE 1=1 ". $this->generateStaffBlackListQuery() . " UNION SELECT id,name,signup_date FROM user WHERE 1=1". $this->generateUserBlackListQuery() ." ORDER BY id"; + } + } + + public function generateStaffBlackListQuery(){ + $StaffBlackList = $this->getBlackListFiltered(); + return $this->generateBlackListQuery($StaffBlackList); + } + + public function generateUserBlackListQuery(){ + $UserBlackList = $this->getBlackListFiltered(0); + return $this->generateBlackListQuery($UserBlackList); + } + + public function generateBlackListQuery($idList){ + $text = ""; + foreach ($idList as $id) { + $text .= " AND id != " . $id; + } + return $text; + } + + public function getBlackListFiltered($staff = 1){ + $blackList = json_decode(Controller::request('blackList')); + $idList = []; + if($blackList){ + foreach ($blackList as $item) { + if($staff == $item->staff) array_push($idList, $item->id); + } + } + return $idList; + } +} diff --git a/server/controllers/ticket/search.php b/server/controllers/ticket/search.php index 82a60a78..4729eefa 100644 --- a/server/controllers/ticket/search.php +++ b/server/controllers/ticket/search.php @@ -134,10 +134,10 @@ class SearchController extends Controller { $query = $this->getSQLQuery($inputs); $queryWithOrder = $this->getSQLQueryWithOrder($inputs); - $totalCount = RedBean::getAll("SELECT COUNT(*) FROM (SELECT COUNT(*) " . $query . " ) AS T2", [':query' => "%" . $inputs['query'] . "%"])[0]['COUNT(*)']; - $ticketIdList = RedBean::getAll($queryWithOrder, [':query' => "%" . $inputs['query'] . "%"]); + //throw new Exception($queryWithOrder); + $totalCount = RedBean::getAll("SELECT COUNT(*) FROM (SELECT COUNT(*) " . $query . " ) AS T2", [':query' => "%" . $inputs['query'] . "%", ':query2' => $inputs['query'] . "%" ])[0]['COUNT(*)']; + $ticketIdList = RedBean::getAll($queryWithOrder, [':query' => "%" . $inputs['query'] . "%", ':query2' => $inputs['query'] . "%"]); $ticketList = []; - foreach ($ticketIdList as $item) { $ticket = Ticket::getDataStore($item['id']); array_push($ticketList, $ticket->toArray()); @@ -410,9 +410,9 @@ class SearchController extends Controller { $ticketEventTableExists = RedBean::exec("select table_name from information_schema.tables where table_name = 'ticketevent';"); if($querysearch !== null){ - $ticketeventOrder = ( $ticketEventTableExists ? " CASE WHEN (ticketevent.type = 'COMMENT' and ticketevent.content LIKE :query) THEN ticketevent.content END desc," : ""); - $order .= "CASE WHEN (ticket.ticket_number LIKE :query) THEN ticket.ticket_number END desc,CASE WHEN (ticket.title LIKE :query) THEN ticket.title END desc, CASE WHEN ( ticket.content LIKE :query) THEN ticket.content END desc," . $ticketeventOrder ; - } + $ticketeventOrder = ( $ticketEventTableExists ? " WHEN (ticketevent.content LIKE :query) THEN 5 " : ""); + $order .= "CASE WHEN (ticket.ticket_number LIKE :query) THEN 1 WHEN (ticket.title LIKE :query2) THEN 2 WHEN (ticket.title LIKE :query) THEN 3 WHEN ( ticket.content LIKE :query) THEN 4 " . $ticketeventOrder ."END asc, "; + } } } diff --git a/server/data/ERRORS.php b/server/data/ERRORS.php index 7577e562..a1c35504 100755 --- a/server/data/ERRORS.php +++ b/server/data/ERRORS.php @@ -95,6 +95,10 @@ * @apiDefine INVALID_QUERY * @apiError {String} INVALID_QUERY The query is invalid. */ +/** + * @apiDefine INVALID_BLACK_LIST + * @apiError {String} INVALID_BLACK_LIST The black list is invalid. + */ /** * @apiDefine INVALID_TAG_FILTER * @apiError {String} INVALID_TAG_FILTER The tag filter is invalid. @@ -330,6 +334,7 @@ class ERRORS { const INVALID_PRIORITY = 'INVALID_PRIORITY'; const INVALID_PAGE = 'INVALID_PAGE'; const INVALID_QUERY = 'INVALID_QUERY'; + const INVALID_BLACK_LIST = 'INVALID_BLACK_LIST'; const INVALID_TAG_FILTER = 'INVALID_TAG_FILTER'; const INVALID_CLOSED_FILTER = 'INVALID_CLOSED_FILTER'; const INVALID_UNREAD_STAFF_FILTER = 'INVALID_UNREAD_STAFF_FILTER'; diff --git a/server/tests/controllers/ticket/searchTest.php b/server/tests/controllers/ticket/searchTest.php index b5fc92d8..33c96fd2 100644 --- a/server/tests/controllers/ticket/searchTest.php +++ b/server/tests/controllers/ticket/searchTest.php @@ -197,17 +197,7 @@ class SearchControllerTest extends TestCase { $this->searchController->getSQLQuery([ 'departments' => null, 'staffId' => 1, - 'allowedDepartments' => [ - [ - 'id' => 2 - ], - [ - 'id' => 1 - ], - [ - 'id' => 3 - ] - ] + 'allowedDepartments' => [2,1,3] ]), 'FROM (ticket LEFT JOIN tag_ticket ON tag_ticket.ticket_id = ticket.id LEFT JOIN ticketevent ON ticketevent.ticket_id = ticket.id) WHERE ( ticket.department_id = 2 or ticket.department_id = 1 or ticket.department_id = 3) GROUP BY ticket.id' ); @@ -216,17 +206,7 @@ class SearchControllerTest extends TestCase { $this->searchController->getSQLQuery([ 'departments' => [1], 'staffId' => 1, - 'allowedDepartments' => [ - [ - 'id' => 2 - ], - [ - 'id' => 1 - ], - [ - 'id' => 3 - ] - ] + 'allowedDepartments' => [2,1,3] ]), 'FROM (ticket LEFT JOIN tag_ticket ON tag_ticket.ticket_id = ticket.id LEFT JOIN ticketevent ON ticketevent.ticket_id = ticket.id) WHERE ( ticket.department_id = 1 ) GROUP BY ticket.id' ); @@ -235,14 +215,7 @@ class SearchControllerTest extends TestCase { $this->searchController->getSQLQuery([ 'departments' => [1,2,3,4], 'staffId' => 1, - 'allowedDepartments' => [ - [ - 'id' => 2 - ], - [ - 'id' => 1 - ] - ] + 'allowedDepartments' => [2,1] ]), 'FROM (ticket LEFT JOIN tag_ticket ON tag_ticket.ticket_id = ticket.id LEFT JOIN ticketevent ON ticketevent.ticket_id = ticket.id) WHERE ( ticket.department_id = 1 or ticket.department_id = 2 or (ticket.author_staff_id = 1 and ( ticket.department_id = 3 or ticket.department_id = 4)) ) GROUP BY ticket.id' ); @@ -251,14 +224,7 @@ class SearchControllerTest extends TestCase { $this->searchController->getSQLQuery([ 'departments' => [2], 'staffId' => 1, - 'allowedDepartments' => [ - [ - 'id' => 5 - ], - [ - 'id' => 6 - ] - ] + 'allowedDepartments' => [5,6] ]), 'FROM (ticket LEFT JOIN tag_ticket ON tag_ticket.ticket_id = ticket.id LEFT JOIN ticketevent ON ticketevent.ticket_id = ticket.id) WHERE (ticket.author_staff_id = 1 and ( ticket.department_id = 2)) GROUP BY ticket.id' ); @@ -317,7 +283,7 @@ class SearchControllerTest extends TestCase { 'page' => 1, 'query' => 'stark' ]), - "SELECT ticket.id FROM (ticket LEFT JOIN tag_ticket ON tag_ticket.ticket_id = ticket.id LEFT JOIN ticketevent ON ticketevent.ticket_id = ticket.id) WHERE (ticket.title LIKE :query or ticket.content LIKE :query or ticket.ticket_number LIKE :query or (ticketevent.type = 'COMMENT' and ticketevent.content LIKE :query) ) GROUP BY ticket.id ORDER BY CASE WHEN (ticket.ticket_number LIKE :query) THEN ticket.ticket_number END desc,CASE WHEN (ticket.title LIKE :query) THEN ticket.title END desc, CASE WHEN ( ticket.content LIKE :query) THEN ticket.content END desc, CASE WHEN (ticketevent.type = 'COMMENT' and ticketevent.content LIKE :query) THEN ticketevent.content END desc,ticket.closed asc, ticket.owner_id asc, ticket.unread_staff asc, ticket.priority desc, ticket.date desc LIMIT 10 OFFSET 0" + "SELECT ticket.id FROM (ticket LEFT JOIN tag_ticket ON tag_ticket.ticket_id = ticket.id LEFT JOIN ticketevent ON ticketevent.ticket_id = ticket.id) WHERE (ticket.title LIKE :query or ticket.content LIKE :query or ticket.ticket_number LIKE :query or (ticketevent.type = 'COMMENT' and ticketevent.content LIKE :query) ) GROUP BY ticket.id ORDER BY CASE WHEN (ticket.ticket_number LIKE :query) THEN 1 WHEN (ticket.title LIKE :query2) THEN 2 WHEN (ticket.title LIKE :query) THEN 3 WHEN ( ticket.content LIKE :query) THEN 4 WHEN (ticketevent.content LIKE :query) THEN 5 END asc, ticket.closed asc, ticket.owner_id asc, ticket.unread_staff asc, ticket.priority desc, ticket.date desc LIMIT 10 OFFSET 0" ); $this->assertEquals(