<?php
namespace Modules\Documents\BulkDownload; class Controller extends \App\Mvc\Controller\Form\PopupAbstract { protected function getDefaultFormData($R) { $formData = []; $formData['ids'] = $R['ids']; $formData['pivot'] = $R['pivot']; return $formData; } protected function processFormData(Array $R, \App\MessagesCollector $msgs) { } protected function actionIndex($R) { if ($R[$this->formSubmitButtonName]) { try { if (!$R['ids']) { throw new \App\MessagesException(\App\MessagesCollector::create()->addError($this->lang['index']['msgs']['pivot']['noSelection'])); } if (!$R['docTypes']) { throw new \App\MessagesException(\App\MessagesCollector::create()->addError($this->lang['index']['msgs']['pivot']['noDocTypesSelected'])); } $modelOutput = $this->createArchive($R); $fileName = sprintf("documents_%s.zip", \Engine\DateTime::create()->format('Ymd')); $response = new \Engine\Mvc\Response\BigFileDownload($modelOutput); $response->body->setOption('deleteAfterSend', true); $response->body->setOption('debug', true); $response->body->setOption('ignoreUserAbort', false); $response->setFilename($fileName); return $response; } catch (\App\MessagesException $e) { $msgs = \App\MessagesCollector::create(); $msgs->appendMessages($e->getMessagesCollector()); $view = new View_Index($this); $view->addInput('formData', $R); $view->getMessagesCollector()->appendMessages($msgs); $view->addInput('formSubmitButtonName', $this->formSubmitButtonName); return new \Engine\Mvc\Response\Html($view->render()); } } else { return parent::actionIndex($R); } } private function createArchive(Array $R) { $documentIdList = []; foreach ($R['ids'] as $idPivot) { $documentPivotObject = new \Modules\Documents\Generate\DocumentPivot($R['pivot'], $idPivot); $documentsList = $documentPivotObject->getDocumentsList(); $docs = []; foreach ($documentsList as $idDocument => $documentDetails) { if (in_array($documentDetails['docType'], $R['docTypes']) && !$documentDetails['preview']) { $docs[$documentDetails['docType']] = $idDocument; } } foreach ($docs as $docId) { $documentIdList[] = $docId; } } if (!$documentIdList) { throw new \App\MessagesException(\App\MessagesCollector::create()->addError(sprintf($this->lang['index']['msgs']['pivot']['selected']['noDocuments'], $idPivot))); } $model = new Model($documentIdList); $zipPath = $model->createDocumentsArchive(); return $zipPath; } }