public function isValid() { $isValid = parent::isValid(); if ($isValid) { $data = parent::getData(); $file = new \Idea\Model\File(); $file->setFileName($data['fileUpload']['name']); $file->setIdeaId($data['ideaId']); $date = new \DateTime(); $file->setFilePath($date->format('dmY')); $fileMapper = $this->getServiceLocator()->get('\\Idea\\Model\\FileMapper'); if ($fileMapper->isExisted($file)) { $this->get('fileUpload')->setMessages(['file đã tồn tại trên hệ thống, không thể ghi đè']); $isValid = false; } } return $isValid; }
private function uploadIdeaFile() { $form = new \Home\Form\Media\IdeaFile($this->getServiceLocator()); 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()) { $formData = $form->getData(); $file = new \Idea\Model\File(); $file->exchangeArray($form->getData()); $file->setCreatedById($this->user()->getIdentity()); $file->setCreatedDateTime(DateBase::getCurrentDateTime()); $fileMapper = $this->getServiceLocator()->get('\\Idea\\Model\\FileMapper'); $fileMapper->save($file); //=======rename file upload============ $targetFolder = \Home\Service\Uri::getSavePath($file); if (!file_exists($targetFolder)) { $oldmask = umask(0); mkdir($targetFolder, 0777, true); umask($oldmask); } rename($formData['fileUpload']['tmp_name'], $targetFolder . '/' . $file->getFileName()); return $this->getJsonModel()->setVariables(array('code' => 1, 'data' => $form->getData())); } else { $formData = $form->getData(); if (isset($formData['fileUpload']['tmp_name'])) { @unlink($formData['fileUpload']['tmp_name']); } return $this->getJsonModel()->setVariables(array('code' => 0, 'messages' => $form->getErrorMessagesList())); } } else { return $this->getJsonModel()->setVariables(array('code' => 0, 'messages' => ['Dữ liệu không hợp lệ'])); } }