Exemplo n.º 1
0
 public function editAction()
 {
     $form = new \Admin\Form\EventForm();
     $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 Event();
         $form->setAttribute('action', '/admin/' . $this->_modelAlias . '/add');
         $form->get('submit')->setAttribute('value', 'Add');
         $model->setUpdatedDate(new \DateTime());
         $model->setCreatedDate(new \DateTime());
         $viewVariables['action'] = 'Add';
     }
     $thumbDateObject = new \DateTime();
     $thumbDate = $thumbDateObject->format("Y-m-d");
     if ($model->getUpdatedDate() instanceof \DateTime) {
         $thumbDate = $model->getUpdatedDate()->format("Y-m-d");
     } elseif ($model->getCreatedDate() instanceof \DateTime) {
         $thumbDate = $model->getCreatedDate()->format("Y-m-d");
     }
     $eventThumbFolder = '/public/uploads/events/' . $thumbDate;
     $viewVariables['eventThumbFolder'] = $eventThumbFolder;
     $tempFile = null;
     $prg = $this->fileprg($form);
     if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
         return $prg;
     } elseif (is_array($prg)) {
         $model->populate($form->getData());
         $fileErrors = $form->get('thumb')->getMessages();
         if (empty($fileErrors)) {
             $tempFile = $form->get('thumb')->getValue();
             if (!is_null($tempFile)) {
                 $tempFileName = $eventThumbFolder . "/" . basename($tempFile['tmp_name']);
                 $model->setThumb($tempFileName);
             }
         }
         $model->setBody(stripslashes($model->getBody()));
         if ($form->isValid()) {
             $this->getObjectManager()->persist($model);
             $this->getObjectManager()->flush();
             return $this->redirect()->toUrl('/admin/' . $this->_modelAlias);
         } else {
             var_dump($form->getMessages());
         }
     }
     $viewVariables['form'] = $form;
     $viewVariables['model'] = $model;
     $this->_view->setTemplate('admin/' . $this->_modelAlias . '/edit');
     $this->_view->setVariables($viewVariables);
     return $this->_view;
 }