示例#1
0
 public function editAction()
 {
     $id = (int) $this->params()->fromRoute('id', 0);
     if (!$id) {
         return $this->redirect()->toRoute('admin/states', array('action' => 'add'));
     }
     $state = $this->getStateTable()->getState($id);
     $state->setAdapter($this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter'));
     $form = new StateForm($this->getServiceLocator()->get('Admin\\Model\\CountriesTable'));
     $form->bind($state);
     $form->get('submit')->setAttribute('value', 'Edit');
     $request = $this->getRequest();
     if ($request->isPost()) {
         // Adding already exist validation on runtime excluding the current record
         $state->getInputFilter()->get('state_code')->getValidatorChain()->attach(new \Zend\Validator\Db\NoRecordExists(array('table' => 'state', 'field' => 'state_code', 'adapter' => $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter'), 'exclude' => array('field' => 'id', 'value' => $id))));
         $state->getInputFilter()->get('state_name')->getValidatorChain()->attach(new \Zend\Validator\Db\NoRecordExists(array('table' => 'state', 'field' => 'state_name', 'adapter' => $this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter'), 'exclude' => array('field' => 'id', 'value' => $id))));
         $form->setInputFilter($state->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $this->getStateTable()->saveState($form->getData());
             $this->flashMessenger()->addSuccessMessage('State updated successfully..!!');
             // Redirect to list of pages
             return $this->redirect()->toRoute('admin/states');
         } else {
             $this->errors = $form->getMessages();
         }
     }
     return array('id' => $id, 'form' => $form, 'errors' => $this->errors);
 }
 public function stateeditAction()
 {
     $this->_checkIfUserIsLoggedIn();
     $id = (int) $this->params()->fromRoute('id', 0);
     if (!$id) {
         return $this->redirect()->toRoute('admin/default', array('controller' => 'geography', 'action' => 'stateadd'));
     }
     $state = $this->getEntityManager()->find('Admin\\Entity\\State', $id);
     // print_r($city);die;
     if (!$state) {
         return $this->redirect()->toRoute('admin/default', array('controller' => 'geography', 'action' => 'state'));
     }
     $em = $this->getEntityManager();
     $form = new StateForm($em);
     $tempstate = $state->statePhoto;
     $form->bind($state);
     $form->get('save')->setAttribute('value', 'Edit');
     $form->getInputFilter()->get('statePhoto')->setRequired(false);
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setInputFilter($state->getInputFilter());
         //print_r($request->getPost());die;
         $data = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
         $form->setData($data);
         if ($form->isValid()) {
             $filname = $data['statePhoto']['name'];
             if ($filname == "") {
                 $filname = $tempstate;
             }
             $extension = pathinfo($filname, PATHINFO_EXTENSION);
             $stname = str_replace(" ", "-", $data['stname']) . $id . "." . $extension;
             $state->setstatePhoto($stname);
             $adapter = new \Zend\File\Transfer\Adapter\Http();
             $adapter->setDestination('public/images/state');
             $adapter->addFilter('Rename', $stname, $filname);
             $adapter->receive($filname);
             $this->getEntityManager()->flush();
             return $this->redirect()->toRoute('admin/default', array('controller' => 'geography', 'action' => 'state'));
         }
     }
     return array('id' => $id, 'form' => $form);
 }