Esempio n. 1
0
 public function indexAction()
 {
     /* Instanciation d'un tableGateway pour country */
     $sm = $this->getServiceLocator();
     $dbAdapter = $sm->get('Zend\\Db\\Adapter\\Adapter');
     $resultSetPrototype = new \Zend\Db\ResultSet\ResultSet();
     $resultSetPrototype->setArrayObjectPrototype(new Country());
     $tableGateWay = new TableGateway('country', $dbAdapter, null, $resultSetPrototype);
     $countryTable = new CountryTable($tableGateWay);
     $form = new Form\Register();
     $libelleValidation = "";
     if ($this->request->isPost()) {
         $dataPosted = $this->request->getPost();
         //var_dump($dataPosted);
         /* S'il a cliqué sur le bouton suppression, on supprime l'id, sinon c'est un create ou update */
         if (isset($dataPosted->suppression)) {
             $countryTable->deleteCountry($dataPosted->id);
         } else {
             $formReceivedData = new Form\Register();
             $formReceivedData->setData($dataPosted);
             if ($formReceivedData->isValid()) {
                 $libelleValidation = "Enregistrement effectué ";
                 $newCountry = new Country();
                 $newCountry->exchangeArray($dataPosted);
                 $countryTable->saveCountry($newCountry);
             } else {
                 $libelleValidation = "Les données inserées ne sont pas valides";
                 $form->setData($dataPosted);
             }
         }
     }
     //saveCountry
     $form2 = new Form\RegisterArrayLine();
     $allCountries = $countryTable->fetchAll();
     $viewModel = new ViewModel(["allCountries" => $allCountries, "form" => $form, "form2" => $form2, "libelleValidation" => $libelleValidation]);
     return $viewModel;
 }
Esempio n. 2
0
 public function singleRecordAction()
 {
     $retour = array();
     $iso = $this->params('iso3166');
     $request = new \Zend\Http\PhpEnvironment\Request();
     $methode = $request->getServer('REQUEST_METHOD');
     if (isset($iso)) {
         $retour["search"] = $iso;
         $sm = $this->getServiceLocator();
         $dbAdapter = $sm->get('Zend\\Db\\Adapter\\Adapter');
         $resultSetPrototype = new \Zend\Db\ResultSet\ResultSet();
         $resultSetPrototype->setArrayObjectPrototype(new Country());
         $tableGateWay = new TableGateway('country', $dbAdapter, null, $resultSetPrototype);
         $countryTable = new CountryTable($tableGateWay);
         switch ($methode) {
             case "GET":
                 $country = $countryTable->getCountryWithSearch($iso);
                 if (isset($country)) {
                     $retour[] = $country->toArray();
                 } else {
                     $retour["error"] = "Pas de resultat";
                 }
                 break;
             case "PATCH":
                 $country = $countryTable->getCountryWithSearch($iso);
                 if (isset($country)) {
                     $postContent = $request->getContent();
                     if (trim($postContent)) {
                         //$retour["content"] = json_decode($postContent);
                         $formReceivedData = new Form\Register();
                         $formReceivedData->setData(json_decode($postContent, true));
                         //$retour["insert"] = json_decode($postContent,true);
                         if ($formReceivedData->isValid()) {
                             $newCountry = new Country();
                             $newCountry->exchangeArray(json_decode($postContent, true));
                             $countryTable->saveCountry($newCountry);
                             $retour["success"] = "Donnees valides";
                         } else {
                             $retour["error"] = "Donnees recues non valides";
                         }
                     } else {
                         $retour["error"] = "Pas de donnees recues";
                     }
                 } else {
                     $retour["error"] = "Pays non trouve";
                 }
                 break;
             case "DELETE":
                 $country = $countryTable->getCountryWithSearch($iso);
                 if (isset($country)) {
                     $countryTable->deleteCountryWithSearch($iso);
                     $retour["success"] = "Suppression reussie";
                 } else {
                     $retour["error"] = "Pays non trouve";
                 }
                 break;
             default:
                 $retour["error"] = "405 : Forbidden method";
                 break;
         }
     } else {
         $retour["error"] = "Pas d'iso3166 specifie";
     }
     return new JsonModel($retour);
 }