public function countryaddAction() { $this->_checkIfUserIsLoggedIn(); $form = new CountryForm(); $request = $this->getRequest(); if ($request->isPost()) { $country = new Country(); $form->setInputFilter($country->getInputFilter()); $data = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray()); $form->setData($data); if ($form->isValid()) { $country->exchangeArray($form->getData()); $this->getEntityManager()->persist($country); $this->getEntityManager()->flush(); $recentid = $country->countryId; $filname = $data['contryphoto']['name']; $extension = pathinfo($filname, PATHINFO_EXTENSION); $cntryname = str_replace(" ", "-", $data['cntryName']) . $recentid . "." . $extension; $country->setcountryPhoto($cntryname); $adapter = new \Zend\File\Transfer\Adapter\Http(); $adapter->setDestination('public/images/country'); $adapter->addFilter('Rename', $cntryname, $filname); if ($adapter->receive($filname)) { $this->getEntityManager()->persist($country); $this->getEntityManager()->flush(); return $this->redirect()->toRoute('admin/default', array('controller' => 'geography', 'action' => 'country')); } } } return new ViewModel(array('form' => $form, 'scc' => $this->flashMessenger()->getCurrentSuccessMessages(), 'err' => $this->flashMessenger()->getCurrentErrorMessages())); }
public function editAction() { $id = (int) $this->params()->fromRoute('id', 0); if (!$id) { return $this->redirect()->toRoute('admin/countries', array('action' => 'add')); } $country = $this->getCountryTable()->getCountry($id); $country->setAdapter($this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter')); $form = new CountryForm(); $form->bind($country); $form->get('submit')->setAttribute('value', 'Edit'); $request = $this->getRequest(); if ($request->isPost()) { // Adding already exist validation on runtime excluding the current record $country->getInputFilter()->get('country_code')->getValidatorChain()->attach(new \Zend\Validator\Db\NoRecordExists(array('table' => 'country', 'field' => 'country_code', 'adapter' => $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter'), 'exclude' => array('field' => 'id', 'value' => $id)))); $country->getInputFilter()->get('country_name')->getValidatorChain()->attach(new \Zend\Validator\Db\NoRecordExists(array('table' => 'country', 'field' => 'country_name', 'adapter' => $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter'), 'exclude' => array('field' => 'id', 'value' => $id)))); $form->setInputFilter($country->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $this->getCountryTable()->saveCountry($form->getData()); $this->flashMessenger()->addSuccessMessage('Country updated successfully..!!'); // Redirect to list of pages return $this->redirect()->toRoute('admin/countries'); } else { $this->errors = $form->getMessages(); } } return array('id' => $id, 'form' => $form, 'errors' => $this->errors); }