Beispiel #1
0
 private function uploadAnnouncementFile()
 {
     $form = new \Company\Form\AnnouncementFile($this->getServiceLocator());
     $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()) {
             if ($this->getRequest()->getPost('announcementId') && $this->getRequest()->getPost('companyId')) {
                 $form->addFileUploadRenameFilter($this->getRequest()->getPost('announcementId'), $this->getRequest()->getPost('companyId'));
             } elseif ($this->getRequest()->getQuery('announcementId') && $this->getRequest()->getQuery('companyId')) {
                 $form->addFileUploadRenameFilter($this->getRequest()->getQuery('announcementId'), $this->getRequest()->getQuery('companyId'));
             } else {
                 $form->addFileUploadRenameFilter('temp');
             }
             $announcementFile = new \Company\Model\AnnouncementFile();
             $announcementFile->exchangeArray($form->getData());
             $announcementFileMapper = $this->getServiceLocator()->get('\\Company\\Model\\AnnouncementFileMapper');
             if (!$announcementFileMapper->isExisted($announcementFile)) {
                 $announcementFile->setCreatedById($this->user()->getIdentity());
                 $oldname = Uri::getSavePath($announcementFile);
                 $announcementFile->setFilePath(DateBase::toFormat(DateBase::getCurrentDateTime(), 'Ymd'));
                 $announcementFile->setCreatedDateTime(DateBase::getCurrentDateTime());
                 $newname = Uri::getSavePath($announcementFile);
                 if (!file_exists($newname)) {
                     $oldmask = umask(0);
                     mkdir($newname, 0777, true);
                     umask($oldmask);
                 }
                 rename($oldname . '/' . $announcementFile->getFileName(), $newname . '/' . $announcementFile->getFileName());
                 $announcementFileMapper->save($announcementFile);
             }
             $jsonModel->setVariables(['code' => 1, 'data' => ['id' => $announcementFile->getId()]]);
         } else {
             $jsonModel->setVariables(['code' => 0, 'messages' => $form->getMessagesForUpload()]);
         }
     } else {
         $jsonModel->setVariables(['code' => 0, 'messages' => ['phải là request post']]);
     }
     return $jsonModel;
 }
 /**
  * @author KienNN
  * @return \Zend\View\Model\ViewModel
  * chuyển các file của thông báo về đúng thư mục của nó
  */
 public function changeanoucementfilelocationAction()
 {
     $dbSql = $this->getServiceLocator()->get('dbSql');
     $dbAdapter = $this->getServiceLocator()->get('dbAdapter');
     $select = $dbSql->select(['af' => \Company\Model\AnnouncementFileMapper::TABLE_NAME]);
     $select->join(['a' => \Company\Model\AnnouncementMapper::TABLE_NAME], 'af.announcementId=a.id', ['companyId']);
     $select->where(['filePath IS NULL']);
     $select->order(['id asc']);
     $paginatorAdapter = new \Zend\Paginator\Adapter\DbSelect($select, $dbAdapter);
     $paginator = new \Zend\Paginator\Paginator($paginatorAdapter);
     $paginator->setItemCountPerPage(200);
     $page = $this->getRequest()->getQuery('page', 1);
     $totalFile = $this->getRequest()->getQuery('totalFile', 0);
     $paginator->setCurrentPageNumber($page);
     $fileMapper = $this->getServiceLocator()->get('\\Company\\Model\\AnnouncementFileMapper');
     foreach ($paginator as $row) {
         $row = (array) $row;
         $fileModel = new \Company\Model\AnnouncementFile();
         $fileModel->exchangeArray($row);
         $filePath = DateBase::toFormat($fileModel->getCreatedDateTime(), 'Ymd');
         $fileModel->setFilePath($filePath);
         $oldSavePath = MEDIA_PATH . '/company/announcement/' . $row['companyId'] . '/' . $fileModel->getAnnouncementId() . '/' . $fileModel->getFileName();
         $tempPath = MEDIA_PATH . '/announcement/temp/' . $fileModel->getAnnouncementId() . '/' . $fileModel->getFileName();
         if (file_exists($oldSavePath)) {
             $newSavePath = Uri::getSavePath($fileModel);
             if (!file_exists($newSavePath)) {
                 $oldmask = umask(0);
                 mkdir($newSavePath, 0777, true);
                 umask($oldmask);
             }
             @copy($oldSavePath, $newSavePath . '/' . $fileModel->getFileName());
             $fileMapper->save($fileModel);
             $totalFile++;
         } elseif (file_exists($tempPath)) {
             $newSavePath = Uri::getSavePath($fileModel);
             if (!file_exists($newSavePath)) {
                 $oldmask = umask(0);
                 mkdir($newSavePath, 0777, true);
                 umask($oldmask);
             }
             @copy($oldSavePath, $newSavePath . '/' . $fileModel->getFileName());
             $fileMapper->save($fileModel);
             $totalFile++;
         }
     }
     $this->getViewModel()->setTerminal(true);
     $this->getViewModel()->setVariable('page', $page);
     $this->getViewModel()->setVariable('totalPages', $paginator->count() + 1);
     if ($page <= $paginator->count()) {
         $this->getViewModel()->setVariable('redirectUri', Uri::build('/system/tool/changeanoucementfilelocation', ['page' => $page + 1, 'totalFile' => $totalFile]));
     }
     $this->getViewModel()->setVariable('totalFile', $totalFile);
     return $this->getViewModel();
 }