Ejemplo n.º 1
0
 public function editAction()
 {
     $form = new \Admin\Form\NewsForm();
     $id = (int) $this->params()->fromRoute('id', 0);
     if ($id > 0) {
         $model = $this->getObjectManager()->find($this->_modelName, $id);
         $form->setData($model->toArray());
         $form->setAttribute('action', '/admin/' . $this->_modelAlias . '/edit/' . $id);
         $form->get('submit')->setAttribute('value', 'Edit');
         $model->setUpdatedDate(new \DateTime());
         $viewVariables['action'] = 'Edit';
     } else {
         $model = new News();
         $form->setAttribute('action', '/admin/' . $this->_modelAlias . '/add');
         $form->get('submit')->setAttribute('value', 'Add');
         $model->setUpdatedDate(new \DateTime());
         $model->setCreatedDate(new \DateTime());
         $viewVariables['action'] = 'Add';
     }
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setData($request->getPost());
         $model->populate($request->getPost());
         $model->setBody(stripslashes($model->getBody()));
         if ($form->isValid()) {
             $this->getObjectManager()->persist($model);
             $this->getObjectManager()->flush();
             return $this->redirect()->toUrl('/admin/' . $this->_modelAlias);
         }
     }
     $viewVariables['form'] = $form;
     $viewVariables['model'] = $model;
     $this->_view->setTemplate('admin/' . $this->_modelAlias . '/edit');
     $this->_view->setVariables($viewVariables);
     return $this->_view;
 }