コード例 #1
0
ファイル: SingleUpload.php プロジェクト: pnaq57/zf2demo
 public function addElements()
 {
     // File Input
     $file = new Element\File('file');
     $file->setLabel('File Input')->setAttributes(array('id' => 'file', 'class' => 'form-control'));
     $this->add($file);
     $this->add(array('type' => 'Zend\\Form\\Element\\Hidden', 'name' => 'id'));
     $this->add(array('type' => 'Zend\\Form\\Element\\Select', 'name' => 'file_type', 'attributes' => array('id' => 'file_type', 'options' => \Application\Form\OptionsConfig::getFormOption('file_upload_type'), 'class' => 'form-control', 'required' => true), 'options' => array('label' => 'Datei-Typ')));
 }
コード例 #2
0
 public function editAction()
 {
     $form = new \FileUpload\Form\SingleUpload('file-form');
     if ($this->getRequest()->isPost()) {
         $postData = $this->getRequest()->getPost()->toArray();
         if (!((int) $postData['id'] > 0)) {
             $this->flashMessenger()->addMessage('Die Aktion is ungültig!!');
             return $this->redirect()->toRoute('file-upload', array('action' => 'index'));
         }
         $id = (int) $postData['id'];
         $file = $this->fileUploadService->getDb()->find('FileUpload\\Entity\\File', $id);
         if (!$file) {
             $this->flashMessenger()->addMessage('Die Datei ist nicht gefunden!!');
             return $this->redirect()->toRoute('file-upload', array('action' => 'index'));
         }
         if (empty($postData['file_type']) || !in_array($postData['file_type'], \Application\Form\OptionsConfig::getFormOption('file_upload_type'))) {
             $this->flashMessenger()->addMessage('Die Dateityp ist nicht definiert!!');
             return $this->redirect()->toRoute('file-upload', array('action' => 'edit', 'id' => $id));
         }
         $file->setFileType($postData['file_type']);
         // Postback
         $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray());
         $form->setData($data);
         if ($form->isValid()) {
             $formData = $form->getData();
             $originalName = $formData['file']['name'];
             $newName = htmlentities(strip_tags(str_replace(array(' '), array('_'), $formData['file']['name'])));
             if ($newName != $file->getFileName() && !file_exists($this->fileUploadService->getUploadPath() . $newName)) {
                 unlink($this->fileUploadService->getUploadPath() . $file->getFileName());
                 if (rename($this->fileUploadService->getUploadTempPath() . $originalName, $this->fileUploadService->getUploadPath() . $newName)) {
                     $file->setFileName($newName);
                 }
             } else {
                 //
                 $this->flashMessenger()->addMessage('Die hochgeledene Datei ist bereits in System vorhaden unter anderem ID!!');
                 return $this->redirect()->toRoute('file-upload', array('action' => 'edit', 'id' => $id));
             }
         }
         $this->fileUploadService->getDb()->persist($file);
         $this->fileUploadService->getDb()->flush();
     } else {
         $id = (int) $this->params()->fromRoute('id', 0);
         if (!$id) {
             return $this->redirect()->toRoute('file-upload', array('action' => 'index'));
         }
         $file = $this->fileUploadService->getDb()->find('FileUpload\\Entity\\File', $id);
         $form->get('id')->setValue($file->getId());
         $form->get('file_type')->setValue($file->getFileType());
     }
     return array('form' => $form, 'file' => $file);
 }