<?php
namespace Modules\Documents\DocumentsList; class Controller extends \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing { protected $listOrderingColumns = ['id', 'docDate']; protected function initialize() { parent::initialize(); $this->actionsCfg['delete'] = ['checkOption' => 'Modules/Documents/DocumentsList-deleteFile']; $this->actionsCfg['downloadFile'] = ['checkOption' => false]; } protected function optionDefineParams(\App\Mvc\View\Utils\RowOptions\Option\Option $option, $selection) { switch ($option->getId()) { case 'downloadFile': foreach ($selection as $selectedItem) { $docEnt = \Entity\Document::select($selectedItem); if (!$docEnt->docFile or !\Engine\File::check($docEnt->docFileNameWithPath)) { $option->setDisabled(); } } break; case 'delete': foreach ($selection as $selectedItem) { if (!$option->determineOperationAvailability(\Entity\Document::deleteFile($selectedItem))) { break; } } break; default: return false; } return true; } protected function insert_doInsert($formData, \App\MessagesCollector $msgs) {} protected function insert_getDefaultFormData($R) { return $R; } protected function update_doUpdate($formData, \App\MessagesCollector $msgs) {} protected function update_getDefaultFormData($idEntity) { return []; } protected function delete_doDelete(array $recordsIds, \App\MessagesCollector $msgs, array $R) { foreach($recordsIds as $record) { try { \DB::startTransaction(); $docEnt = \Entity\Document::deleteFile($record); $docEnt->save(); \DB::commitTransaction(); $msgs->addSuccess($this->lang['delete']['msgs']['saveOk']); $this->getListingSelection()->delete([$record]); } catch (\App\Entity\Exception $e) { \DB::rollbackTransaction(); $msgs->appendMessages($e->getMessagesCollector()); return false; } } return true; } public function actionDownloadFile($R) { $docEnt = \Entity\Document::select($R['id']); if ($docEnt->docFileNameWithPath and \Engine\File::check($docEnt->docFileNameWithPath)) { $filename = $docEnt->docFileClean; $fileContent = \Engine\File::getContents($docEnt->docFileNameWithPath); $response = new \Engine\Mvc\Response\FileDownload($fileContent); $response->setFilename($filename); return $response; } else { throw new \Exception("Selected file is not found. Probably deleted from disk."); } } protected function optionGetEntityName($id) { return \Entity\Document::select($id)->docNumber; } }