Пример #1
0
 public function iso3166Action()
 {
     if ($this->getRequest()->isGet()) {
         // get
         $countryTable = new CountryTable($this->getServiceLocator()->get('country-table-gateway'));
         try {
             $data = $countryTable->getCountryByISO3166($this->params()->fromRoute('iso3166'));
             $this->data = new Country();
             $this->data->exchangeArray($data);
         } catch (\Exception $e) {
             if ($e->getCode() === 0) {
                 // row not found
                 $this->code = Response::STATUS_CODE_404;
             } else {
                 if ($e->getCode() === 1) {
                     // wrong finder (wrong request)
                     $this->code = Response::STATUS_CODE_405;
                 } else {
                     // undefined error
                     $this->code = Response::STATUS_CODE_400;
                 }
             }
         }
     } else {
         if ($this->getRequest()->isPatch()) {
             // patch
             // check if PATCH message is a JSON object
             if ($this->getRequest()->getHeader('Content-Type')->getFieldValue() === "application/json") {
                 $countryTable = new CountryTable($this->getServiceLocator()->get('country-table-gateway'));
                 $newCountry = new Country();
                 $newCountry->exchangeArray(json_decode($this->getRequest()->getContent()));
                 try {
                     $countryTable->saveCountry($newCountry);
                 } catch (\Exception $e) {
                     $this->code = Response::STATUS_CODE_404;
                     // ID not found
                 }
             } else {
                 $this->code = Response::STATUS_CODE_406;
                 // Only accept JSON object
             }
         } else {
             if ($this->getRequest()->isDelete()) {
                 // delete
                 $countryTable = new CountryTable($this->getServiceLocator()->get('country-table-gateway'));
                 try {
                     $countryTable->deleteCountry($this->params()->fromRoute('iso3166'));
                 } catch (\Exception $e) {
                     $this->code = Response::STATUS_CODE_404;
                     // iso3166 not found
                 }
             } else {
                 if ($this->getRequest()->isPut()) {
                     // put -> forbidden
                     $this->code = Response::STATUS_CODE_403;
                 } else {
                     $this->code = Response::STATUS_CODE_405;
                     // 405
                 }
             }
         }
     }
     return $this->parseResponse();
 }
Пример #2
0
 public function deleteAction()
 {
     $authService = $this->getServiceLocator()->get('auth-service');
     if (!$authService->hasIdentity()) {
         // not connected
         $this->redirect()->toRoute('apic-admin/default', ['controller' => 'login', 'action' => 'index']);
     } else {
         $id = (int) $this->params()->fromRoute('id');
         $countryTable = new CountryTable($this->getServiceLocator()->get('country-table-gateway'));
         try {
             $countryTable->deleteCountry($id);
             $alert = ['content' => 'Suppression effectuée', 'type' => 'success'];
         } catch (\Exception $e) {
             $alert = ['content' => 'Suppression impossible', 'type' => 'danger'];
         }
         $this->redirect()->toRoute('apic-admin', ['alert' => $alert]);
     }
 }