IHMInputOutputParamsPlotClass.php 9.48 KB
<?php

define ("PLOT_RESULT_FILE_KEY","plot");

/**
 * @class IHMInputOutputParamsPlotClass
 * @brief Implementation of IHMInputOutputParamsAbstractClass to treat plot request
 * @details
*/
class IHMInputOutputParamsPlotClass extends IHMInputOutputParamsAbstractClass
{
	private $isInteractiveRequest = false;
	
	/*
	 * @brief method to unmarshall a plot request
	*/
	protected function unmarshallRequest($input)
	{
		//save request
		$this->saveIHMRequest($input);
		
		//Request
		$requestIndex = 0;
		$this->isInteractiveRequest = ($input->{'file-output'} == 'INTERACTIVE');
		$postProcessCmd = "";
		foreach ($input->tabs as $tab)
		{
			$requestNode = $this->paramsData->addRequestNode();
			$outputsNode = $requestNode->getOutputsNode();
			
			//unmarshall time definition
			$this->unmarshallTimeDefinition($input, $requestIndex);
			
			$plotOutputNode = $outputsNode->addNewOutput(RequestOutputTypeEnum::PLOT);
			
			$plotOutputNode->setWriteContextFile($this->isInteractiveRequest ? "true" : "false");
			
			$compression = "";
			if (!$this->isInteractiveRequest)
			{
				switch ($input->{'file-output'})
				{
					case 'TGZ' :
						$plotOutputNode->addPostProcessing(RequestOutputPostProcessingEnumClass::TAR);
						$plotOutputNode->addPostProcessing(RequestOutputPostProcessingEnumClass::GZIP);
						$compression = ".tar.gz";
						break;
					case 'ZIP' :
						$plotOutputNode->addPostProcessing(RequestOutputPostProcessingEnumClass::ZIP);
						$compression = ".zip";
						break;
					default:
						throw new Exception('Compression not implemented.');
				}
			}
			
			if ($input->{'one-file-per-interval'})
				$plotOutputNode->setStructure(RequestOutputPlotStructureEnum::ONE_FILE_PER_INTERVAL);
			else
				$plotOutputNode->setStructure(RequestOutputPlotStructureEnum::ONE_FILE);
			
			//prefix
			$filePrefix = "plot";
			if ($input->{'file-prefix'} && ($input->{'file-prefix'} != ""))
				$filePrefix = $input->{'file-prefix'};
			$filePrefix .= $requestIndex;
			
			$plotOutputNode->setFilePrefix($filePrefix);
			
			//page
			$pageNode = $plotOutputNode->getPage();
			
			$fileFormat = RequestOutputPlotPageFormatEnum::PNG;
			$extension = ".png";
			switch ($input->{'file-format'})
			{
				case 'PNG' :
					$fileFormat = RequestOutputPlotPageFormatEnum::PNG;
					$extension = ".png";
					break;
				case 'PDF' :
					$fileFormat = RequestOutputPlotPageFormatEnum::PDF;
					$extension = ".pdf";
					break;
				case 'PS' :
					$fileFormat = RequestOutputPlotPageFormatEnum::PS;
					$extension = ".ps";
					break;
				case 'SVG' :
					$fileFormat = RequestOutputPlotPageFormatEnum::SVG;
					$extension = ".svg";
					break;
				default:
					throw new Exception('File format not implemented.');
			}
			
			$pageNode->setFormat($fileFormat);
			
			$this->unmarshallTitle($tab, 'page-title', $pageNode->getTitle());
			
			$isPortrait = false;
			switch ($tab->{'page-orientation'})
			{
				case 'landscape' :
					$pageNode->setOrientation(RequestOutputPlotPageOrientationEnum::LANDSCAPE);
					break;
				case 'portrait' :
					$pageNode->setOrientation(RequestOutputPlotPageOrientationEnum::PORTRAIT);
					$isPortrait = true;
					break;
			}
			
			switch ($tab->{'page-dimension'})
			{
				case 'ISO A4' :
					$pageNode->setDimension(RequestOutputPlotPageDimensionEnum::ISO_A4);
					break;
				case 'US letter' :
					$pageNode->setDimension(RequestOutputPlotPageDimensionEnum::US_LETTER);
					break;
				default:
					throw new Exception('Page dimension not implemented.');
			}
			
			switch ($tab->{'page-mode'})
			{
				case 'color' :
					$pageNode->setMode(RequestOutputPlotPageModeEnum::COLOR);
					break;
				case 'grayscale' :
					$pageNode->setMode(RequestOutputPlotPageModeEnum::GRAYSCALE);
					break;
				default:
					throw new Exception('Page mode not implemented.');
			}
			
			if ($tab->{'page-margins-activated'})
			{
				$pageNode->getMargins()->setHorizontal($tab->{'page-margin-x'});
				$pageNode->getMargins()->setVertical($tab->{'page-margin-y'});
			}
			
			if ($tab->{'page-font-activated'})
				$this->unmarshallFont($tab, 'page', $pageNode->getFont());
			
			$pageNode->getLayout()->setType(RequestOutputPlotLayoutTypeEnum::VERTICAL);
			
			if ($this->isInteractiveRequest)
			{
				$resultFile = $filePrefix."_*".$extension;
				$waitingResultFile = $filePrefix.$extension;
			}
			else
			{
				$resultFile = $filePrefix."_*".$compression;
				$waitingResultFile = $filePrefix.$compression;
			}
			
			$this->paramsData->addWaitingResult(PLOT_RESULT_FILE_KEY."_".$requestIndex, $waitingResultFile);
			
			//Remove old result files
			foreach (glob($this->paramsData->getWorkingPath().$resultFile) as $oldFile) {
				unlink($oldFile);
			}
			
			//Post process command to apply to the result file
			if ($postProcessCmd != "")
				$postProcessCmd .= " | ";
			$postProcessCmd .= "mv ".$resultFile." ".$waitingResultFile;
			
			if ($this->isInteractiveRequest && $isPortrait)
				$postProcessCmd .= " | convert ".$waitingResultFile." -rotate -90 ".$waitingResultFile;
			
			++$requestIndex;
		}
		
		$this->paramsData->setBatchEnable(!(($fileFormat == RequestOutputPlotPageFormatEnum::PNG) && $this->isInteractiveRequest));
		$this->paramsData->setPostCmd($postProcessCmd);
		
		//determine extension and add post processing if needed
		return $this->paramsData;
	}
	
	protected function unmarshallTitle($inputData, $keyPrefix, $titleNode)
	{
		if ($inputData->{$keyPrefix.'-text'} != '')
		{
			$titleNode->setText($inputData->{$keyPrefix.'-text'});
		
			switch ($inputData->{$keyPrefix.'-position'})
			{
				case 'top' :
					$titleNode->setPosition(RequestOutputPlotTitlePosition::TOP);
					break;
				case 'bottom' :
					$titleNode->setPosition(RequestOutputPlotTitlePosition::BOTTOM);
					break;
			}
		
			switch ($inputData->{$keyPrefix.'-alignment'})
			{
				case 'center' :
					$titleNode->setAlign(RequestOutputPlotTitleAlign::CENTER);
					break;
				case 'left' :
					$titleNode->setAlign(RequestOutputPlotTitleAlign::LEFT);
					break;
				case 'right' :
					$titleNode->setAlign(RequestOutputPlotTitleAlign::RIGHT);
					break;
			}
			
			if ($inputData->{$keyPrefix.'-color'} != '')
				$titleNode->setColor($this->hexColor2KernelColor($inputData->{$keyPrefix.'-color'}));
		
			$this->unmarshallLabel($inputData, $keyPrefix, $titleNode);
		}
	}
	
	protected function unmarshallLabel($inputData, $keyPrefix, $labelNode)
	{
		if ($inputData->{$keyPrefix.'-font-activated'})
		{
			$labelNode->setFontName($inputData->{$keyPrefix.'-font-name'});
			$labelNode->setFontSize($inputData->{$keyPrefix.'-font-size'});
			$style = "";
			if ($inputData->{$keyPrefix.'-font-bold'})
			{
				if ($style != "")
					$style .= ",";
				$style .= "bold";
			}
			if ($inputData->{$keyPrefix.'-font-italic'})
			{
				if ($style != "")
					$style .= ",";
				$style .= "italic";
			}
			if ($style != "")
				$labelNode->setFontStyle($style);
		}
		
	}
	
	protected function unmarshallFont($inputData, $keyPrefix, $fontNode)
	{
		if ($inputData->{$keyPrefix.'-font-activated'})
		{
			$fontNode->setFontName($inputData->{$keyPrefix.'-font-name'});
			$fontNode->setSize($inputData->{$keyPrefix.'-font-size'});
			
			if ($inputData->{$keyPrefix.'-font-italic'})
				$fontNode->setStyle(RequestOutputPlotFontStyle::ITALIC);
			else
				$fontNode->setStyle(RequestOutputPlotFontStyle::UPRIGHT);
			
			if ($inputData->{$keyPrefix.'-font-bold'})
				$fontNode->setWeight(RequestOutputPlotFontWeight::BOLD);
			else
				$fontNode->setWeight(RequestOutputPlotFontWeight::MEDIUM);
		}
	}

	/*
	 * @brief method to marshall the result of a download request
	*/
	protected function marshallResult($data)
	{
		if (!$this->isInteractiveRequest)
		{
			//add to job
			$commonRes = $this->commonMarshallResult($data,PLOT_RESULT_FILE_KEY."_0");
		
			return $commonRes;
		}
		
		if (!$data->getSuccess())
			return array(
					'success' => false,
					'message' => $data->getLastErrorMessage());
		
		switch ($data->getStatus())
		{
			case ProcessStatusEnumClass::DONE :
				
				$result = array();
				foreach ($data->getWaitingResults() as $waitingResult)
				{
					$contextResult = str_replace(".png","_context.xml",$waitingResult);
					
					$result[] = array(
						"context" => IHMPlotContextFileClass::parse($this->getWorkingPath().$contextResult),
						"plot" => $waitingResult
					);
				}
				
				return array(
					'success'     => true,
					'id'          => $data->getId(),
					'folder'      => $this->getWorkingDirName(),
					'result'      => $result
				);
			default :
				return array(
				'success' => false,
				'message'   => 'Error to process the interractive request');
		}
	}
	
	private function hexColor2KernelColor($hex) {
		$hex = str_replace("#", "", $hex);

		$r = hexdec(substr($hex,0,2));
		$g = hexdec(substr($hex,2,2));
		$b = hexdec(substr($hex,4,2));

		return "[".$r.",".$g.",".$b."]";
	}
	
	private function saveIHMRequest($input)
	{
		$path = $this->getWorkingPath()."ihm.request";
		if (!is_dir($this->getWorkingPath()))
			mkdir($this->getWorkingPath(),0777);
		$file = fopen($path, 'w');
		fwrite($file, json_encode($input));
		fclose($file);
	}
	
	private function loadIHMRequest()
	{
		$path = $this->getWorkingPath()."ihm.request";
		if (!file_exists($path))
			return NULL;
		return json_decode(file_get_contents($path));
	}
}
?>