From 586fe9d44e6a9186916c3b50b1c57d4550639c6b Mon Sep 17 00:00:00 2001 From: Valentin Date: Mon, 9 Oct 2017 13:19:26 +0200 Subject: [PATCH] Refactor --- Controller/TicketController.php | 439 +++++++++++++------------------- config.json | 3 +- lang/fr_FR.json | 8 +- 3 files changed, 191 insertions(+), 259 deletions(-) diff --git a/Controller/TicketController.php b/Controller/TicketController.php index e225eec..f1c237a 100755 --- a/Controller/TicketController.php +++ b/Controller/TicketController.php @@ -1,279 +1,208 @@ isConnected){ - $this->set('title_for_layout',"Support"); - $this->layout = $this->Configuration->getKey('layout'); - $this->loadModel('Support.Ticket'); - - $tickets = $this->Ticket->find('all', array('conditions' => array('Ticket.author' => array($this->User->getKey('id'))))); - $this->set(compact('tickets')); - }else{ - throw new ForbiddenException(); + +class TicketController extends AppController +{ + + public function index() + { + if (!$this->isConnected) + throw new ForbiddenException(); + $this->set('title_for_layout', "Support"); + $this->loadModel('Support.Ticket'); + + $tickets = $this->Ticket->find('all', array('conditions' => array('Ticket.author' => array($this->User->getKey('id'))))); + $this->set(compact('tickets')); } - } - - public function getUser($tag, $id) - { - $this->loadModel('User'); - $user = $this->User->getFromUser($tag, $id); - return $user; - } - - function admin_index(){ - if($this->isConnected AND $this->User->isAdmin()){ - $this->set('title_for_layout', $this->Lang->get('SUPPORT__GESTION').' - '.$this->Lang->get('SUPPORT__SUPPORT')); + + public function getUser($tag, $id) + { + $this->loadModel('User'); + return $this->User->getFromUser($tag, $id); + } + + function admin_index() + { + if (!$this->Permissions->can('MANAGE_TICKETS')) + throw new ForbiddenException(); + $this->set('title_for_layout', $this->Lang->get('SUPPORT__GESTION') . ' - ' . $this->Lang->get('SUPPORT__SUPPORT')); $tickets = $this->Ticket->find('all', array('order' => array('Ticket.id DESC'))); $this->set(compact('tickets')); $this->layout = 'admin'; } - else - - throw new ForbiddenException(); - } - function admin_ticket($id){ - if($this->isConnected AND $this->User->isAdmin()){ + function admin_ticket($id) + { + if (!$this->Permissions->can('MANAGE_TICKETS')) + throw new ForbiddenException(); $this->loadModel('Support.Ticket'); $this->loadModel('Support.ReplyTicket'); - $ticket = $this->Ticket->find('first', array('conditions' => array("Ticket.id" => array($id)))); - if(!empty($ticket)){ - $answers = $this->ReplyTicket->find('all', array('conditions' => array("ReplyTicket.ticket_id" => array($id)))); - $this->set(compact('ticket')); - $this->set(compact('answers')); - $this->set('title_for_layout', $this->Lang->get('SUPPORT__TICKETNUMBER').''.$id); - }else{ - throw new ForbiddenException(); - } + $ticket = $this->Ticket->find('first', array('conditions' => array("Ticket.id" => array($id)))); + if (empty($ticket)) + throw new NotFoundException(); + $answers = $this->ReplyTicket->find('all', array('conditions' => array("ReplyTicket.ticket_id" => array($id)))); + + $this->set(compact('ticket', 'answers')); + $this->set('title_for_layout', $this->Lang->get('SUPPORT__TICKETNUMBER') . '' . $id); $this->layout = 'admin'; } - else - - throw new ForbiddenException(); - } - - function ticket($id) - { - if($this->isConnected) { + + function ticket($id) + { + if (!$this->isConnected) + throw new ForbiddenException(); $this->loadModel('Support.Ticket'); $this->loadModel('Support.ReplyTicket'); - $ticket = $this->Ticket->find('first', array('conditions' => array("Ticket.id" => array($id)))); - if(!empty($ticket)){ - if($ticket['Ticket']['author'] == $this->User->getKey('id')){ - $answers = $this->ReplyTicket->find('all', array('conditions' => array("ReplyTicket.ticket_id" => array($id)))); - $this->set(compact('ticket')); - $this->set(compact('answers')); - $this->set('title_for_layout', $this->Lang->get('SUPPORT__TICKETNUMBER').''.$id); - }else{ - throw new ForbiddenException(); - } - }else{ - throw new ForbiddenException(); - } - }else{ - throw new ForbiddenException(); + $ticket = $this->Ticket->find('first', ['conditions' => ['id' => $id, 'author' => $this->User->getKey('id')]]); + if (empty($ticket)) + throw new NotFoundException(); + + $answers = $this->ReplyTicket->find('all', array('conditions' => array("ReplyTicket.ticket_id" => array($id)))); + $this->set(compact('ticket', 'answers')); + $this->set('title_for_layout', $this->Lang->get('SUPPORT__TICKETNUMBER') . '' . $id); } - } - function create() { - if($this->isConnected) { - $this->set('title_for_layout', $this->Lang->get('SUPPORT__CREATETITLE')); - }else{ - throw new ForbiddenException(); + function create() + { + if (!$this->isConnected) + throw new ForbiddenException(); + $this->set('title_for_layout', $this->Lang->get('SUPPORT__CREATETITLE')); } - } - - function ajax_create() { - $this->autoRender = false; - $this->response->type('json'); - if($this->isConnected) { - if($this->request->is('Post')) { - if(!empty($this->request->data['subject']) && !empty($this->request->data['reponse_text'])) { - $contentTicket = $this->request->data['reponse_text']; - if(strlen($contentTicket) > 255){ - $timeActu = time(); - $this->loadModel('Support.Ticket'); - $this->loadModel('Notification'); - $this->Ticket->set(array( - 'author' => $this->User->getKey('id'), - 'subject' => $this->request->data['subject'], - 'created' => $timeActu, - 'reponse_text' => $contentTicket - )); - $this->Ticket->save(); - $this->Notification->setToAdmin($this->User->getKey('pseudo').' '.$this->Lang->get('SUPPORT__NOTIF_CREATE')); - $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('SUPPORT__SUCCESS_CREATE')))); - } else { - $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('SUPPORT__ERROR_PROBLEM_SHORT')))); - } - } else { - $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS')))); - } - } else { - $this->response->body(json_encode(array('statut' => false, 'msg' => "Internal Error"))); - } - }else{ - throw new ForbiddenException(); - } - } - - function admin_ajax_replya() { - $this->autoRender = false; - $this->response->type('json'); - if($this->isConnected AND $this->User->isAdmin()) { - if($this->request->is('Post')) { - $ticket = $this->Ticket->find('first', array('conditions' => array("Ticket.id" => array($this->request->data['idTicket'])))); - if(!empty($ticket)){ - if($ticket['Ticket']['id'] == $this->request->data['idTicket']){ - if(!empty($this->request->data['reponse_text'])) { - $contentAnswer = $this->request->data['reponse_text']; - if(strlen($contentAnswer) > 255){ - $timeActu = time(); - $this->loadModel('Support.Ticket'); - $this->loadModel('Support.ReplyTicket'); - $this->loadModel('Notification'); - $this->Ticket->read(null, $ticket['Ticket']['id']); - $this->Ticket->set(array( - 'state' => '1' - )); - $this->Ticket->save(); - $this->ReplyTicket->set(array( - 'ticket_id' => $this->request->data['idTicket'], - 'reply' => $contentAnswer, - 'created' => $timeActu, - 'author' => $this->User->getKey('id'), - 'type' => 1 - )); - $this->ReplyTicket->save(); - $this->Notification->setToUser($this->User->getKey('pseudo').' '.$this->Lang->get('SUPPORT__NOTIF_ANSWER').' '.$ticket['Ticket']['id'].' !', $ticket['Ticket']['author']); - $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('SUPPORT__SUCCESS_SEND_ANSWER')))); - } else { - $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('SUPPORT__ERROR_RESOLVE_SHORT')))); - } - } else { - $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS')))); - } - } else { + + function ajax_create() + { + if (!$this->isConnected) throw new ForbiddenException(); - } - } else { - throw new ForbiddenException(); - } - } else { - $this->response->body(json_encode(array('statut' => false, 'msg' => "Internal Error"))); - } - }else{ - throw new ForbiddenException(); + if (!$this->request->is('post')) + throw new BadRequestException(); + + if (empty($this->request->data['subject']) || empty($this->request->data['reponse_text'])) + return $this->sendJSON(['statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS')]); + $contentTicket = $this->request->data['reponse_text']; + if (strlen($contentTicket) < 255) + return $this->sendJSON(['statut' => false, 'msg' => $this->Lang->get('SUPPORT__ERROR_PROBLEM_SHORT')]); + + $this->loadModel('Support.Ticket'); + $this->loadModel('Notification'); + $this->Ticket->set(array( + 'author' => $this->User->getKey('id'), + 'subject' => $this->request->data['subject'], + 'reponse_text' => $contentTicket + )); + $this->Ticket->save(); + + $this->Notification->setToAdmin($this->User->getKey('pseudo') . ' ' . $this->Lang->get('SUPPORT__NOTIF_CREATE')); + $this->sendJSON(['statut' => true, 'msg' => $this->Lang->get('SUPPORT__SUCCESS_CREATE')]); } - } - - function ajax_reply() { - $this->autoRender = false; - $this->response->type('json'); - if($this->isConnected) { - if($this->request->is('Post')) { - $ticket = $this->Ticket->find('first', array('conditions' => array("Ticket.id" => array($this->request->data['idTicket'])))); - if(!empty($ticket)){ - if($ticket['Ticket']['author'] == $this->User->getKey('id')){ - if(!empty($this->request->data['reponse_text'])) { - $contentAnswer = $this->request->data['reponse_text']; - if(strlen($contentAnswer) > 255){ - $timeActu = time(); - $this->loadModel('Support.Ticket'); - $this->loadModel('Support.ReplyTicket'); - $this->loadModel('Notification'); - $this->Ticket->read(null, $ticket['Ticket']['id']); - $this->Ticket->set(array( - 'state' => '0' - )); - $this->Ticket->save(); - $this->ReplyTicket->set(array( - 'ticket_id' => $this->request->data['idTicket'], - 'reply' => $contentAnswer, - 'created' => $timeActu, - 'author' => $this->User->getKey('id'), - 'type' => 0 - )); - $this->ReplyTicket->save(); - $this->Notification->setToAdmin($this->User->getKey('pseudo').' '.$this->Lang->get('SUPPORT__NOTIF_ANSWER').' '.$ticket['Ticket']['id'].' !'); - $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('SUPPORT__SUCCESS_SEND_ANSWER')))); - } else { - $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('SUPPORT__ERROR_RESOLVE_SHORT')))); - } - } else { - $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS')))); - } - } else { + + function admin_ajax_replya() + { + if (!$this->Permissions->can('MANAGE_TICKETS')) throw new ForbiddenException(); - } - } else { - throw new ForbiddenException(); - } - } else { - $this->response->body(json_encode(array('statut' => false, 'msg' => "Internal Error"))); - } - }else{ - throw new ForbiddenException(); + if (!$this->request->is('post')) + throw new BadRequestException(); + $ticket = $this->Ticket->find('first', ['conditions' => ['id' => $this->request->data['idTicket']]]); + if (empty($ticket)) + throw new NotFoundException(); + + $contentAnswer = $this->request->data['reponse_text']; + if (strlen($contentAnswer) < 255) + $this->sendJSON(['statut' => false, 'msg' => $this->Lang->get('SUPPORT__ERROR_RESOLVE_SHORT')]); + + $this->loadModel('Support.Ticket'); + $this->loadModel('Support.ReplyTicket'); + $this->loadModel('Notification'); + + $this->Ticket->read(null, $ticket['Ticket']['id']); + $this->Ticket->set(['state' => '1']); + $this->Ticket->save(); + + $this->ReplyTicket->set([ + 'ticket_id' => $this->request->data['idTicket'], + 'reply' => $contentAnswer, + 'author' => $this->User->getKey('id'), + 'type' => 1 + ]); + $this->ReplyTicket->save(); + + $this->Notification->setToUser($this->User->getKey('pseudo') . ' ' . $this->Lang->get('SUPPORT__NOTIF_ANSWER') . ' ' . $ticket['Ticket']['id'] . ' !', $ticket['Ticket']['author']); + $this->sendJSON(['statut' => true, 'msg' => $this->Lang->get('SUPPORT__SUCCESS_SEND_ANSWER')]); } - } - - function ajax_clos() { - $this->autoRender = false; - $this->response->type('json'); - $this->loadModel('Support.Ticket'); - $this->loadModel('Notification'); - if($this->isConnected) { - if($this->request->is('Post')) { - $ticket = $this->Ticket->find('first', array('conditions' => array("Ticket.id" => array($this->request->data['idTicket'])))); - if(!empty($ticket)){ - if($ticket['Ticket']['author'] == $this->User->getKey('id')){ - $this->Ticket->read(null, $ticket['Ticket']['id']); - $this->Ticket->set(array( - 'state' => '2' - )); - $this->Ticket->save(); - $this->Notification->setToAdmin($this->User->getKey('pseudo').' '.$this->Lang->get('SUPPORT__NOTIF_CLOS').' '.$ticket['Ticket']['id'].' !'); - $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('SUPPORT__SUCCESS_CLOSE')))); - }else{ + + function ajax_reply() + { + if (!$this->isConnected) throw new ForbiddenException(); - } - }else{ - $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('SUPPORT__TICKET_NOT_EXIST')))); - } - }else{ - throw new ForbiddenException(); - } - }else{ - throw new ForbiddenException(); + if (!$this->request->is('post')) + throw new BadRequestException(); + + $ticket = $this->Ticket->find('first', ['conditions' => ['id' => $this->request->data['idTicket'], 'author' => $this->User->getKey('id')]]); + if (empty($ticket)) + throw new NotFoundException(); + + $contentAnswer = $this->request->data['reponse_text']; + if (strlen($contentAnswer) < 255) + $this->sendJSON(['statut' => false, 'msg' => $this->Lang->get('SUPPORT__ERROR_RESOLVE_SHORT')]); + + $this->loadModel('Support.Ticket'); + $this->loadModel('Support.ReplyTicket'); + $this->loadModel('Notification'); + + $this->Ticket->read(null, $ticket['Ticket']['id']); + $this->Ticket->set(['state' => 0]); + $this->Ticket->save(); + + $this->ReplyTicket->set([ + 'ticket_id' => $this->request->data['idTicket'], + 'reply' => $contentAnswer, + 'author' => $this->User->getKey('id'), + 'type' => 0 + ]); + $this->ReplyTicket->save(); + + $this->Notification->setToAdmin($this->User->getKey('pseudo') . ' ' . $this->Lang->get('SUPPORT__NOTIF_ANSWER') . ' ' . $ticket['Ticket']['id'] . ' !'); + $this->sendJSON(['statut' => true, 'msg' => $this->Lang->get('SUPPORT__SUCCESS_SEND_ANSWER')]); } - } - - function admin_ajax_closa() { - $this->autoRender = false; - $this->response->type('json'); - $this->loadModel('Support.Ticket'); - $this->loadModel('Notification'); - if($this->isConnected AND $this->User->isAdmin()) { - if($this->request->is('Post')) { - $ticket = $this->Ticket->find('first', array('conditions' => array("Ticket.id" => array($this->request->data['idTicket'])))); - if(!empty($ticket)){ - $this->Ticket->read(null, $ticket['Ticket']['id']); - $this->Ticket->set(array( - 'state' => '2' - )); - $this->Ticket->save(); - $this->Notification->setToUser($this->User->getKey('pseudo').' '.$this->Lang->get('SUPPORT__NOTIF_CLOS').' '.$ticket['Ticket']['id'].' !', $ticket['Ticket']['author']); - $this->Notification->setToAdmin($this->User->getKey('pseudo').' '.$this->Lang->get('SUPPORT__NOTIF_CLOS').' '.$ticket['Ticket']['id'].' !'); - $this->response->body(json_encode(array('statut' => true, 'msg' => $this->Lang->get('SUPPORT__SUCCESS_CLOSE')))); - }else{ - $this->response->body(json_encode(array('statut' => false, 'msg' => $this->Lang->get('SUPPORT__TICKET_NOT_EXIST')))); - } - }else{ - throw new ForbiddenException(); - } - }else{ - throw new ForbiddenException(); + + function ajax_clos() + { + if (!$this->isConnected) + throw new ForbiddenException(); + if (!$this->request->is('post')) + throw new BadRequestException(); + + $this->loadModel('Support.Ticket'); + $this->loadModel('Notification'); + $ticket = $this->Ticket->find('first', ['conditions' => ['id' => $this->request->data['idTicket'], 'author' => $this->User->getKey('id')]]); + if (empty($ticket)) + throw new NotFoundException(); + + $this->Ticket->read(null, $ticket['Ticket']['id']); + $this->Ticket->set(['state' => 2]); + $this->Ticket->save(); + + $this->Notification->setToAdmin($this->User->getKey('pseudo') . ' ' . $this->Lang->get('SUPPORT__NOTIF_CLOS') . ' ' . $ticket['Ticket']['id'] . ' !'); + $this->sendJSON(['statut' => true, 'msg' => $this->Lang->get('SUPPORT__SUCCESS_CLOSE')]); + } + + function admin_ajax_closa() + { + if (!$this->Permissions->can('MANAGE_TICKETS')) + throw new ForbiddenException(); + if (!$this->request->is('post')) + throw new BadRequestException(); + + $this->loadModel('Support.Ticket'); + $this->loadModel('Notification'); + $ticket = $this->Ticket->find('first', ['conditions' => ['id' => $this->request->data['idTicket']]]); + if (empty($ticket)) + throw new NotFoundException(); + + $this->Ticket->read(null, $ticket['Ticket']['id']); + $this->Ticket->set(['state' => 2]); + $this->Ticket->save(); + + $this->Notification->setToUser($this->User->getKey('pseudo') . ' ' . $this->Lang->get('SUPPORT__NOTIF_CLOS') . ' ' . $ticket['Ticket']['id'] . ' !', $ticket['Ticket']['author']); + $this->Notification->setToAdmin($this->User->getKey('pseudo') . ' ' . $this->Lang->get('SUPPORT__NOTIF_CLOS') . ' ' . $ticket['Ticket']['id'] . ' !'); + $this->sendJSON(['statut' => true, 'msg' => $this->Lang->get('SUPPORT__SUCCESS_CLOSE')]); } - } } diff --git a/config.json b/config.json index 4e59af0..067535c 100755 --- a/config.json +++ b/config.json @@ -8,6 +8,7 @@ "Support": { "index": 2, "icon": "ticket", + "permission": "MANAGE_TICKETS", "menu": { "Gérer les tickets": { "icon": "cog", @@ -22,7 +23,7 @@ }, "useEvents":false, "permissions" : { - "available" : [], + "available" : ["MANAGE_TICKETS"], "default" : { "0" : [], "2" : [] diff --git a/lang/fr_FR.json b/lang/fr_FR.json index 172cc6a..491853d 100755 --- a/lang/fr_FR.json +++ b/lang/fr_FR.json @@ -27,11 +27,13 @@ "SUPPORT__SUCCESS_CREATE": "Ticket créé avec succès !", "SUPPORT__SUCCESS_SEND_ANSWER": "Votre réponse a été envoyé avec succès !", "SUPPORT__SUCCESS_CLOSE": "Le ticket a été fermé avec succès !", - "SUPPORT__ERROR_PROBLEM_SHORT": "La description de votre problème est trop court ! (400 caractères minimum)", - "SUPPORT__ERROR_RESOLVE_SHORT": "La description de votre résolution est trop court ! (400 caractères minimum)", + "SUPPORT__ERROR_PROBLEM_SHORT": "La description de votre problème est trop court ! (255 caractères minimum)", + "SUPPORT__ERROR_RESOLVE_SHORT": "La description de votre résolution est trop court ! (255 caractères minimum)", "SUPPORT__GESTION": "Gestion des Tickets", "SUPPORT__TICKET_NOT_EXIST": "Le ticket semble être inexistant !", "SUPPORT__NOTIF_CREATE": "a ouvert un ticket !", "SUPPORT__NOTIF_ANSWER": "vous a répondu au ticket N°", - "SUPPORT__NOTIF_CLOS": "a clôturé le ticket N°" + "SUPPORT__NOTIF_CLOS": "a clôturé le ticket N°", + + "PERMISSIONS__MANAGE_TICKETS": "Gérer les tickets du support" } \ No newline at end of file