Пример #1
0
 public function editAction()
 {
     $authService = $this->getServiceLocator()->get('auth-service');
     if (!$authService->hasIdentity()) {
         // not connected
         $this->redirect()->toRoute('apic-admin/default', ['controller' => 'login', 'action' => 'index']);
     } else {
         $countryTable = new CountryTable($this->getServiceLocator()->get('country-table-gateway'));
         try {
             $data = $countryTable->getCountryByISO3166($this->params()->fromRoute('id'));
             $country = new CountryModel();
             $country->exchangeArray($data);
             $countryForm = $this->getServiceLocator()->get('editCountry-form');
             //					var_dump("n");
             $countryForm->bind($country);
             return new ViewModel(['countryForm' => $countryForm]);
         } catch (\Exception $e) {
             return $this->redirect()->toRoute(null, ["controller" => "admin", "action" => "index"]);
         }
     }
 }
Пример #2
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();
 }