コード例 #1
0
 /**
  * @param null|string $name
  */
 public function __construct($serviceLocator, $options = null)
 {
     parent::__construct('documentFile');
     $this->setServiceLocator($serviceLocator);
     $this->setAttribute('method', 'post');
     $filter = $this->getInputFilter();
     if ($options && isset($options['id']) && $options['id']) {
         $this->id = $options['id'];
     }
     $documentFile = new \Document\Model\DocumentFile();
     $documentFile->setDocumentId($this->id);
     $documentFile->setCreatedDateTime(DateBase::getCurrentDateTime());
     $targetFolder = Uri::getSavePath($documentFile);
     if (!file_exists($targetFolder)) {
         $oldmask = umask(0);
         mkdir($targetFolder, 0777, true);
         umask($oldmask);
     }
     $document = new Document();
     $task = new \Work\Model\Task();
     $fileUpload = new File('fileUpload');
     $this->add($fileUpload);
     $filter->add(array('name' => 'fileUpload', 'type' => '\\Zend\\InputFilter\\FileInput', 'required' => true, 'allowEmpty' => true, 'filters' => array(new \Zend\Filter\File\RenameUpload(array('target' => sprintf($targetFolder), 'use_upload_name' => true, 'overwrite' => true))), 'validators' => array(array('name' => 'NotEmpty', 'break_chain_on_failure' => true, 'options' => array('messages' => array('isEmpty' => 'Bạn chưa chọn file'))), array('name' => 'File\\Size', 'break_chain_on_failure' => true, 'options' => array('max' => Task::MAX_FILE_SIZE . 'MB', 'messages' => array(\Zend\Validator\File\Size::TOO_BIG => 'File upload phải < 100Mb'))), array('name' => 'File\\Extension', 'break_chain_on_failure' => true, 'options' => array('extension' => $task->getAllowExtension(), 'messages' => array(\Zend\Validator\File\Extension::FALSE_EXTENSION => 'File upload phải là file ảnh, excel, hoặc msword,pds,pdf'))))));
 }
コード例 #2
0
ファイル: MediaController.php プロジェクト: projectHN/mentor
 public function uploadTaskRequirementFileAction()
 {
     $taskId = $this->getRequest()->getPost('taskId');
     if (!$taskId) {
         return $this->getJsonModel()->setVariables(array('code' => 0, 'messages' => ['Dữ liệu không hợp lệ']));
     }
     $task = new \Work\Model\Task();
     $task->setId($taskId);
     $taskMapper = $this->getServiceLocator()->get('\\Work\\Model\\TaskMapper');
     if (!$taskMapper->get($task)) {
         return $this->getJsonModel()->setVariables(array('code' => 0, 'messages' => ['Dữ liệu không hợp lệ']));
     }
     $form = new \Home\Form\Media\TaskFile($this->getServiceLocator(), ['id' => $task->getId()]);
     $dataPopulate = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getQuery()->toArray(), $this->getRequest()->getFiles()->toArray());
     $dataPopulate['fileUpload']['name'] = Format::removeSigns($dataPopulate['fileUpload']['name']);
     $form->setData($dataPopulate);
     if ($form->isValid()) {
         $formData = $form->getData();
         $file = new \Work\Model\TaskFile();
         $file->setFileName($formData['fileUpload']['name']);
         $file->setFileSize($formData['fileUpload']['size']);
         $file->setTaskId($task->getId());
         $file->setCreatedById($this->user()->getIdentity());
         $file->setCreatedDateTime(DateBase::getCurrentDateTime());
         $fileMapper = $this->getServiceLocator()->get('\\Work\\Model\\TaskFileMapper');
         $fileMapper->save($file);
         return $this->getJsonModel()->setVariables(array('code' => 1, 'messages' => $formData));
     } else {
         return $this->getJsonModel()->setVariables(array('code' => 0, 'messages' => $form->getErrorMessagesList()));
     }
 }