<?php
namespace Modules\Documents\DocumentsList;

class Controller extends \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing
{

	/**
	 * The list of columns which support ordering.
	 * @var array
	 */
	protected $listOrderingColumns = ['id', 'docDate'];

	/**
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Window\PaginatedListing\ListingAbstract::initialize()
	 */
	protected function initialize()
	{
		parent::initialize();

		$this->actionsCfg['delete'] =   		['checkOption' => 'Modules/Documents/DocumentsList-deleteFile'];
		$this->actionsCfg['downloadFile'] =		['checkOption' => false];
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\ListingAbstract::optionDefineParams()
	 */
	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;

			// DELETE
			case 'delete':
				foreach ($selection as $selectedItem) {
					if (!$option->determineOperationAvailability(\Entity\Document::deleteFile($selectedItem))) {
						break;
					}
				}
			break;

			default:
				return false;
		}
		return true;
 	}

 	/**
 	 *
 	 * {@inheritDoc}
 	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::insert_doInsert()
 	 */
 	protected function insert_doInsert($formData, \App\MessagesCollector $msgs)
 	{}

 	/**
 	 *
 	 * {@inheritDoc}
 	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::update_getDefaultFormData()
 	 */
 	protected function insert_getDefaultFormData($R)
 	{
 		return $R;
 	}

 	/**
 	 *
 	 * {@inheritDoc}
 	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::update_doUpdate()
 	 */
 	protected function update_doUpdate($formData, \App\MessagesCollector $msgs)
 	{}


 	/**
 	 *
 	 * {@inheritDoc}
 	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::update_getDefaultFormData()
 	 */
 	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']);

				// remove this record from the internal list
				$this->getListingSelection()->delete([$record]);

	 		} catch (\App\Entity\Exception $e) {
				\DB::rollbackTransaction();
				$msgs->appendMessages($e->getMessagesCollector());
				return false;
	 		}
 		}

 		return true;
 	}

 	/**
 	 * Download the imported document xml
 	 * @param array $R
 	 */
 	public function actionDownloadFile($R)
 	{
 		$docEnt = \Entity\Document::select($R['id']);

 		if ($docEnt->docFileNameWithPath and \Engine\File::check($docEnt->docFileNameWithPath)) {
 			// render the view
 			$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.");
 		}
 	}


	/**
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Window\PaginatedListing\ListingAbstract::optionGetEntityName()
	 */
	protected function optionGetEntityName($id)
	{
		return \Entity\Document::select($id)->docNumber;
	}
}