Exemplo n.º 1
0
 public function addAction()
 {
     if (!$this->zfcUserAuthentication()->hasIdentity()) {
         return $this->redirect()->toRoute('zfcuser');
     }
     $form = new UploadForm();
     $optionSubject = $this->getSubjectTable()->getSubjectsForSelect();
     $form->get('subject')->setAttribute('options', $optionSubject);
     $optionCategory = $this->getCategoryTable()->getCategoriesForSelect();
     $form->get('category')->setAttribute('options', $optionCategory);
     $request = $this->getRequest();
     if ($request->isPost()) {
         $file = new File();
         $data = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getFiles()->toArray());
         $form->setData($data);
         $uploadPath = $this->getOptions()->getUploadFolderPath();
         // Validatoren
         $size = new Size(array('max' => $this->getOptions()->getMaxFileSizeInByte()));
         $extension = new Extension($this->getOptions()->getAllowedFileExtensions());
         // Filter für Zufallsnamen
         if ($this->options->getRandomizeFileName()) {
             $rename = new Rename(array('target' => $uploadPath . '/file', 'randomize' => true));
         } else {
             $rename = null;
         }
         //TODO Add to factory
         $adapter = new Http();
         $adapter->setValidators(array($size, $extension));
         $adapter->setFilters(array($rename));
         if (!$adapter->isValid()) {
             $dataError = $adapter->getMessages();
             array_merge($dataError, $adapter->getErrors());
             foreach ($dataError as $key => $row) {
                 echo $row;
             }
             header('HTTP/1.1 500 Internal Server Error');
             exit;
         } else {
             $adapter->setDestination($uploadPath);
             if ($adapter->receive()) {
                 $subjectID = $data['subject'];
                 $categoryID = $data['category'];
                 $dbdata = array();
                 $dbdata['fileName'] = $data['file']['name'];
                 $filename = $adapter->getFileName();
                 if (is_array($filename)) {
                     $dbdata['url'] = basename(current($filename));
                 } else {
                     $dbdata['url'] = basename($filename);
                 }
                 $file->exchangeArray($dbdata);
                 $this->getFileTable()->saveFile($file, $subjectID, $categoryID);
                 header('HTTP/1.1 200 OK');
                 exit;
             }
         }
     }
     return array('form' => $form);
 }
Exemplo n.º 2
0
 public function createFiles()
 {
     $this->getEvent()->attach('album.model.upload.create.post', array($this, 'connectFileToAlbum'));
     $this->getEvent()->attach('album.model.upload.create.post', array($this, 'setAlbumCover'));
     return parent::createFiles();
 }