public function addFileUploadRenameFilter($contractId) { $file = new \Crm\Model\Contract\File(); $file->setContractId($contractId); $targetFolder = \Home\Service\Uri::getSavePath($file); if (!file_exists($targetFolder)) { $oldmask = umask(0); mkdir($targetFolder, 0777, true); umask($oldmask); } $this->getInputFilter()->get('fileUpload')->getFilterChain()->attach(new \Zend\Filter\File\RenameUpload(array('target' => $targetFolder, 'use_upload_name' => true, 'overwrite' => true))); }
private function uploadContractFile() { $form = new \Home\Form\Media\ContentFile($this->getServiceLocator()); if ($this->getRequest()->getPost('contractId')) { $form->addFileUploadRenameFilter($this->getRequest()->getPost('contractId')); } elseif ($this->getRequest()->getQuery('contractId')) { $form->addFileUploadRenameFilter($this->getRequest()->getQuery('contractId')); } else { $form->addFileUploadRenameFilter('temp'); } $jsonModel = new JsonModel(); if ($this->getRequest()->isPost()) { $dataPopulate = array_merge_recursive($this->getRequest()->getPost()->toArray(), $this->getRequest()->getQuery()->toArray(), $this->getRequest()->getFiles()->toArray()); $form->setData($dataPopulate); if ($form->isValid()) { $contractFile = new \Crm\Model\Contract\File(); $contractFile->exchangeArray($form->getData()); $contractFileMapper = $this->getServiceLocator()->get('\\Crm\\Model\\Contract\\FileMapper'); if (!$contractFileMapper->isExisted($contractFile)) { $contractFile->setCreatedById($this->user()->getIdentity()); $contractFile->setCreatedDateTime(DateBase::getCurrentDateTime()); $contractFileMapper->save($contractFile); } $jsonModel->setVariables(['code' => 1, 'data' => ['id' => $contractFile->getId()]]); } else { $jsonModel->setVariables(['code' => 0, 'messages' => $form->getMessagesForUpload()]); } } else { $jsonModel->setVariables(['code' => 0, 'messages' => ['phải là request post']]); } return $jsonModel; }