Blame view

src/InputOutput/IHMImpl/Params/PlotImpl/IHMInputOutputParamsPlotClass.php 9.37 KB
22521f1c   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
<?php

define ("PLOT_RESULT_FILE_KEY","plot");

/**
 * @class IHMInputOutputParamsPlotClass
 * @brief Implementation of IHMInputOutputParamsAbstractClass to treat plot request
 * @details
*/
class IHMInputOutputParamsPlotClass extends IHMInputOutputParamsAbstractClass
{
98881737   Benjamin Renard   Add post processi...
12
13
	private $isInteractiveRequest = false;
	
22521f1c   Benjamin Renard   First commit
14
15
16
17
18
	/*
	 * @brief method to unmarshall a plot request
	*/
	protected function unmarshallRequest($input)
	{
8c57155b   Benjamin Renard   Integration for t...
19
20
21
22
23
		//save request
		$this->saveIHMRequest($input);
		
		//Request
		$requestIndex = 0;
98881737   Benjamin Renard   Add post processi...
24
25
		$this->isInteractiveRequest = ($input->{'file-output'} == 'INTERACTIVE');
		$postProcessCmd = "";
8c57155b   Benjamin Renard   Integration for t...
26
		foreach ($input->tabs as $tab)
22521f1c   Benjamin Renard   First commit
27
		{
8c57155b   Benjamin Renard   Integration for t...
28
29
			$requestNode = $this->paramsData->addRequestNode();
			$outputsNode = $requestNode->getOutputsNode();
22521f1c   Benjamin Renard   First commit
30
			
8c57155b   Benjamin Renard   Integration for t...
31
32
			//unmarshall time definition
			$this->unmarshallTimeDefinition($input, $requestIndex);
22521f1c   Benjamin Renard   First commit
33
			
8c57155b   Benjamin Renard   Integration for t...
34
35
			$plotOutputNode = $outputsNode->addNewOutput(RequestOutputTypeEnum::PLOT);
			
98881737   Benjamin Renard   Add post processi...
36
37
			$compression = "";
			if (!$this->isInteractiveRequest)
22521f1c   Benjamin Renard   First commit
38
			{
98881737   Benjamin Renard   Add post processi...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
				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.');
				}
8c57155b   Benjamin Renard   Integration for t...
53
54
55
56
57
58
59
60
			}
			
			if ($input->{'one-file-per-interval'})
				$plotOutputNode->setStructure(RequestOutputPlotStructureEnum::ONE_FILE_PER_INTERVAL);
			else
				$plotOutputNode->setStructure(RequestOutputPlotStructureEnum::ONE_FILE);
			
			//prefix
98881737   Benjamin Renard   Add post processi...
61
62
			$filePrefix = "plot";
			if ($input->{'file-prefix'} && ($input->{'file-prefix'} != ""))
8c57155b   Benjamin Renard   Integration for t...
63
64
65
66
67
68
69
70
71
				$filePrefix = $input->{'file-prefix'};
			$filePrefix .= $requestIndex;
			
			$plotOutputNode->setFilePrefix($filePrefix);
			
			//page
			$pageNode = $plotOutputNode->getPage();
			
			$fileFormat = RequestOutputPlotPageFormatEnum::PNG;
98881737   Benjamin Renard   Add post processi...
72
			$extension = ".png";
8c57155b   Benjamin Renard   Integration for t...
73
74
75
76
			switch ($input->{'file-format'})
			{
				case 'PNG' :
					$fileFormat = RequestOutputPlotPageFormatEnum::PNG;
98881737   Benjamin Renard   Add post processi...
77
					$extension = ".png";
22521f1c   Benjamin Renard   First commit
78
					break;
8c57155b   Benjamin Renard   Integration for t...
79
80
				case 'PDF' :
					$fileFormat = RequestOutputPlotPageFormatEnum::PDF;
98881737   Benjamin Renard   Add post processi...
81
					$extension = ".pdf";
22521f1c   Benjamin Renard   First commit
82
					break;
8c57155b   Benjamin Renard   Integration for t...
83
84
				case 'PS' :
					$fileFormat = RequestOutputPlotPageFormatEnum::PS;
98881737   Benjamin Renard   Add post processi...
85
					$extension = ".ps";
22521f1c   Benjamin Renard   First commit
86
					break;
8c57155b   Benjamin Renard   Integration for t...
87
88
				case 'SVG' :
					$fileFormat = RequestOutputPlotPageFormatEnum::SVG;
98881737   Benjamin Renard   Add post processi...
89
					$extension = ".svg";
8c57155b   Benjamin Renard   Integration for t...
90
91
92
					break;
				default:
					throw new Exception('File format not implemented.');
22521f1c   Benjamin Renard   First commit
93
			}
22521f1c   Benjamin Renard   First commit
94
			
8c57155b   Benjamin Renard   Integration for t...
95
96
97
98
			$pageNode->setFormat($fileFormat);
			
			$this->unmarshallTitle($tab, 'page-title', $pageNode->getTitle());
			
98881737   Benjamin Renard   Add post processi...
99
			$isPortrait = false;
8c57155b   Benjamin Renard   Integration for t...
100
			switch ($tab->{'page-orientation'})
22521f1c   Benjamin Renard   First commit
101
			{
8c57155b   Benjamin Renard   Integration for t...
102
103
				case 'landscape' :
					$pageNode->setOrientation(RequestOutputPlotPageOrientationEnum::LANDSCAPE);
22521f1c   Benjamin Renard   First commit
104
					break;
8c57155b   Benjamin Renard   Integration for t...
105
106
				case 'portrait' :
					$pageNode->setOrientation(RequestOutputPlotPageOrientationEnum::PORTRAIT);
98881737   Benjamin Renard   Add post processi...
107
					$isPortrait = true;
22521f1c   Benjamin Renard   First commit
108
109
					break;
			}
8c57155b   Benjamin Renard   Integration for t...
110
111
112
113
114
115
116
117
118
119
120
			
			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.');
22521f1c   Benjamin Renard   First commit
121
122
			}
			
8c57155b   Benjamin Renard   Integration for t...
123
			switch ($tab->{'page-mode'})
22521f1c   Benjamin Renard   First commit
124
			{
8c57155b   Benjamin Renard   Integration for t...
125
126
127
128
129
130
131
132
				case 'color' :
					$pageNode->setMode(RequestOutputPlotPageModeEnum::COLOR);
					break;
				case 'grayscale' :
					$pageNode->setMode(RequestOutputPlotPageModeEnum::GRAYSCALE);
					break;
				default:
					throw new Exception('Page mode not implemented.');
22521f1c   Benjamin Renard   First commit
133
			}
8c57155b   Benjamin Renard   Integration for t...
134
135
136
137
138
139
140
141
142
143
144
145
			
			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);
			
98881737   Benjamin Renard   Add post processi...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
			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 ".$resultFile." -rotate -90 ".$resultFile;
			
8c57155b   Benjamin Renard   Integration for t...
172
			++$requestIndex;
22521f1c   Benjamin Renard   First commit
173
		}
8c57155b   Benjamin Renard   Integration for t...
174
		
98881737   Benjamin Renard   Add post processi...
175
176
		$this->paramsData->setBatchEnable(!(($fileFormat == RequestOutputPlotPageFormatEnum::PNG) && $this->isInteractiveRequest));
		$this->paramsData->setPostCmd($postProcessCmd);
8c57155b   Benjamin Renard   Integration for t...
177
		
22521f1c   Benjamin Renard   First commit
178
		//determine extension and add post processing if needed
8c57155b   Benjamin Renard   Integration for t...
179
180
181
182
183
184
		return $this->paramsData;
	}
	
	protected function unmarshallTitle($inputData, $keyPrefix, $titleNode)
	{
		if ($inputData->{$keyPrefix.'-text'} != '')
22521f1c   Benjamin Renard   First commit
185
		{
8c57155b   Benjamin Renard   Integration for t...
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
			$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);
22521f1c   Benjamin Renard   First commit
215
		}
8c57155b   Benjamin Renard   Integration for t...
216
217
218
219
220
	}
	
	protected function unmarshallLabel($inputData, $keyPrefix, $labelNode)
	{
		if ($inputData->{$keyPrefix.'-font-activated'})
22521f1c   Benjamin Renard   First commit
221
		{
8c57155b   Benjamin Renard   Integration for t...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
			$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);
22521f1c   Benjamin Renard   First commit
239
		}
8c57155b   Benjamin Renard   Integration for t...
240
241
242
243
244
245
		
	}
	
	protected function unmarshallFont($inputData, $keyPrefix, $fontNode)
	{
		if ($inputData->{$keyPrefix.'-font-activated'})
22521f1c   Benjamin Renard   First commit
246
		{
8c57155b   Benjamin Renard   Integration for t...
247
248
249
250
251
252
253
254
255
256
257
258
			$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);
22521f1c   Benjamin Renard   First commit
259
		}
22521f1c   Benjamin Renard   First commit
260
261
262
263
264
265
266
	}

	/*
	 * @brief method to marshall the result of a download request
	*/
	protected function marshallResult($data)
	{
98881737   Benjamin Renard   Add post processi...
267
		if (!$this->isInteractiveRequest)
22521f1c   Benjamin Renard   First commit
268
269
		{
			//add to job
98881737   Benjamin Renard   Add post processi...
270
			$commonRes = $this->commonMarshallResult($data,PLOT_RESULT_FILE_KEY."_0");
22521f1c   Benjamin Renard   First commit
271
272
273
274
275
276
277
278
279
280
281
		
			return $commonRes;
		}
		
		if (!$data->getSuccess())
			return array(
					'success' => false,
					'message' => $data->getLastErrorMessage());
		
		switch ($data->getStatus())
		{
98881737   Benjamin Renard   Add post processi...
282
			case ProcessStatusEnumClass::DONE :
22521f1c   Benjamin Renard   First commit
283
284
285
286
				return array(
					'success'     => true,
					'id'          => $data->getId(),
					'folder'      => $this->getWorkingDirName(),
98881737   Benjamin Renard   Add post processi...
287
					'result'      => $data->getWaitingResults(),
22521f1c   Benjamin Renard   First commit
288
289
290
291
292
293
294
295
296
297
298
					'format'      => 'PNG',
					'compression' => 'unknown',
					'status'      => 'done',
					'children'    => array(
							array(
									"xmin" => "0",
									"xmax" => "1",
									"ymin" => "0",
									"umax" => "1"
							)
					),
22521f1c   Benjamin Renard   First commit
299
300
301
302
303
304
305
306
307
308
					'startDate'   => $this->startTime,
					'stopDate'    => $this->stopTime
				);
			default :
				return array(
				'success' => false,
				'message'   => 'Error to process the interractive request');
		}
	}
	
8c57155b   Benjamin Renard   Integration for t...
309
310
311
312
313
314
315
316
317
318
	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."]";
	}
	
22521f1c   Benjamin Renard   First commit
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
	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));
	}
}
?>