Blame view

src/InputOutput/IHMImpl/Tools/IHMParamTemplateClass.php 12 KB
bf27ba04   Benjamin Renard   Add templated par...
1
2
3
4
5
6
7
8
9
10
<?php

/**
 * @class IHMParamTemplateClass
 * @brief Class used to manage templated parameters
 * @details
 */
class IHMParamTemplateClass
{
	protected $paramTemplateList = null;
ffc5cb81   Elena.Budnik   temporary commit
11
	protected $paramTemplateListFilePath;
bf27ba04   Benjamin Renard   Add templated par...
12
	
ffc5cb81   Elena.Budnik   temporary commit
13
14
15
16
17
18
19
20
	protected static $paramTemplateNode                    = 'paramTemplate';
	protected static $paramTemplateIdAtt                   = 'paramId';
	protected static $paramTemplateFileNameAtt             = 'fileName';
	protected static $paramTemplateArgumentsNode           = 'arguments';
	protected static $paramTemplateArgumentNode            = 'argument';
	protected static $paramTemplateArgumentKeyAtt          = 'key';
	protected static $paramTemplateArgumentNameAtt         = 'name';
	protected static $paramTemplateArgumentTypeAtt         = 'type';
1e653e6f   Benjamin Renard   Add subtype defin...
21
	protected static $paramTemplateArgumentSubTypeAtt      = 'subtype';
a7011f4d   Benjamin Renard   Generated list fo...
22
23
24
	protected static $paramTemplateArgumentMinKeyAtt       = 'minkey';
	protected static $paramTemplateArgumentMaxKeyAtt       = 'maxkey';
	protected static $paramTemplateArgumentNameTplAtt      = 'nametpl';
ffc5cb81   Elena.Budnik   temporary commit
25
26
27
28
	protected static $paramTemplateArgumentDefaultAtt      = 'default';
	protected static $paramTemplateArgumentListItemNode    = 'item';
	protected static $paramTemplateArgumentListItemKeyAtt  = 'key';
	protected static $paramTemplateArgumentListItemNameAtt = 'name';
bf27ba04   Benjamin Renard   Add templated par...
29
30
31
32
33
34
	
	/*
	 * @brief Constructor
	*/
	function __construct()
	{
ffc5cb81   Elena.Budnik   temporary commit
35
		$this->paramTemplateListFilePath = IHMConfigClass::getParamTemplateListFilePath();
bf27ba04   Benjamin Renard   Add templated par...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
	}
	
	/*
	 * @brief Get list of templated parameters
	 */
	public function getParamTemplates() {
		$this->loadUserParamManagerFile();
		return $this->paramTemplateList;
	}
	
	/*
	 * @brief Check if param_id is a templated parameter
	*/
	public function isTemplatedParam($param_id) {
		$list = $this->getParamTemplates();
		return array_key_exists($param_id, $list);
	}
	
	/*
e4545ed5   Benjamin Renard   Implement templat...
55
56
57
58
59
60
61
62
63
64
65
66
67
	 * 
	 */
	public function parseTemplatedParam($param_expression) {
		$exploded_param_expression = explode('_',$param_expression);
		
		$tmp_paramid = "";
		$real_param_id = "";
		
		foreach ($exploded_param_expression as $expression_part) {
			//add one by one each part of the expression to check if it's a templated parameter
			if ($tmp_paramid != "")
				$tmp_paramid .= "_";
			$tmp_paramid .= $expression_part;
9cce1f21   Benjamin Renard   Fix bug in param ...
68
			if ($this->isTemplatedParam($tmp_paramid) && strlen($tmp_paramid) > strlen($real_param_id)) {
e4545ed5   Benjamin Renard   Implement templat...
69
				$real_param_id = $tmp_paramid;
e4545ed5   Benjamin Renard   Implement templat...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
			}
		}
		
		if (empty($real_param_id))
			return FALSE;
		
		$template_args_str = str_replace($real_param_id, "", $param_expression);
		
		if ($template_args_str == "")
			//A templated param need at least one argument
			return FALSE;
		
		$template_args_info = $this->paramTemplateList[$real_param_id];
		
		$args_format = "";
		foreach ($template_args_info["arguments"] as $arg) {
			$args_format .= "_";
			switch($arg["type"]) {
				case 'float' :
				case 'double' :
4c191dce   Erdogan Furkan   Fix on #10557
90
				case 'date' :
e4545ed5   Benjamin Renard   Implement templat...
91
92
93
					$args_format .= "%f";
					break;
				case 'int' :
c76ef7b2   Benjamin Renard   Add boolean argum...
94
				case 'bool' :
e4545ed5   Benjamin Renard   Implement templat...
95
96
					$args_format .= "%d";
					break;
1e653e6f   Benjamin Renard   Add subtype defin...
97
98
99
100
101
102
103
104
105
106
107
108
109
110
				case 'list' :
					switch($arg["subtype"]) {
						case 'float' :
						case 'double' :
							$args_format .= "%f";
							break;
						case 'int' :
						case 'bool' :
							$args_format .= "%d";
							break;
						default:
							$args_format .= "%s";
					}
					break;
e4545ed5   Benjamin Renard   Implement templat...
111
112
113
114
115
116
				default:
					$args_format .= "%s";
			}
		}
		
		$exploded_args = sscanf($template_args_str , $args_format);
e4545ed5   Benjamin Renard   Implement templat...
117
118
119
120
121
122
123
		
		if ($exploded_args === -1)
			return FALSE;
		
		$template_args = array();
		$i = 0;
		foreach ($template_args_info["arguments"] as $arg_key => $arg) {
8ef738e8   Benjamin Renard   Fix templated par...
124
125
126
127
128
			if ($arg["type"] == "list") {
				if (!array_key_exists($exploded_args[$i], $arg['items'])) {
					return FALSE;
				}
			}
e4545ed5   Benjamin Renard   Implement templat...
129
130
131
132
133
134
135
136
137
138
139
140
			$template_args[$arg_key] = $exploded_args[$i];
			++$i;
		}
		
		return array(
			"paramid"       => $real_param_id,
			"fullparamid" => $param_expression,
			"template_args" => $template_args
		);
	}
	
	/*
bf27ba04   Benjamin Renard   Add templated par...
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
	 * @brief Get templated parameter id
	*/
	public function getTemplatedParamId($param_id, $template_args) {
		$templatePath = $this->getTemplatePath($param_id);
		
		if (empty($templatePath))
			return "";
		
		if (!isset($template_args))
			$template_args = array();
		
		//Enrich template args with default values
		$this->addDefaultValues($param_id, $template_args);
		
		return basename($this->replaceArgs($templatePath, $template_args), ".xml");
	}
	
	/*
	 * @brief Generate parameter file from template
	 */
52859c27   Benjamin Renard   Give the possibil...
161
	public function generateTemplatedParamFile($param_id, $template_args, $table_link = NULL) {
bf27ba04   Benjamin Renard   Add templated par...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
		$templatePath = $this->getTemplatePath($param_id);
		if (empty($templatePath))
			return "";
		
		if (!isset($template_args))
			$template_args = array();
		
		//Enrich template args with default values
		$this->addDefaultValues($param_id, $template_args);
		
		$dst_param_id = $this->getTemplatedParamId($param_id, $template_args);
			
		//Build destination file path
		$dstDir = IHMConfigClass::getTemplateParamGeneratePath();
		if (!is_dir($dstDir))
			mkdir($dstDir);		
		$dstFilePath = $dstDir."/".$dst_param_id.".xml";
		
		//Check if it's necessary to generate the parameter file
		if (file_exists($dstFilePath)) {
			$dateModifDst = filemtime($dstFilePath);
			$dateModifTemplate = filemtime($templatePath);
d2a2763f   Benjamin Renard   Fix test used to ...
184
			if ($dateModifTemplate == $dateModifDst)
bf27ba04   Benjamin Renard   Add templated par...
185
186
187
188
189
190
191
192
193
194
				//no modification - reuse old generated file
				return $dstFilePath;
		}
		
		//Generate file
		$templateHandle = fopen($templatePath, "r");
		$dstHandle = fopen($dstFilePath, "w");
		if (!$templateHandle || !$dstHandle)
			return "";
		while (($line = fgets($templateHandle)) !== false) {
6f98ec26   Benjamin Renard   Write argument it...
195
			fwrite($dstHandle, $this->replaceArgs($line, $template_args, $this->getArguments($param_id)));
bf27ba04   Benjamin Renard   Add templated par...
196
197
198
		}
		fclose($templateHandle);
		fclose($dstHandle);
52859c27   Benjamin Renard   Give the possibil...
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

		if (isset($table_link)) {
			//Inject table link
			$xml = new DOMDocument();
			$xml->preserveWhiteSpace = TRUE;
			$xml->load($dstFilePath);

			$infoNodes = $xml->documentElement->getElementsByTagName('info');
			if ($infoNodes->length == 0) {
				$infoNode = $xml->createElement('info');
				$infoNode = $xml->documentElement->insertBefore($infoNode, $xml->documentElement->firstChild);
			}
			else {
				$infoNode = $infoNodes->item(0);
			}
		
			$tableNodes = $infoNode->getElementsByTagName('table');
			if ($tableNodes->length == 0) {
				$tableNode = $xml->createElement('table');
				$tableNode = $infoNode->appendChild($tableNode);
			}
			else {
				$tableNode = $tableNodes->item(0);
			}

			$linkTableNode = $xml->createElement('linkTable');
			$linkTableNode->setAttribute('originParamId', $table_link['paramid']);
			$linkTableNode->setAttribute('originTableDim', $table_link['relateddim']);
			$tableNode->appendChild($linkTableNode);

			$xml->save($dstFilePath);
		}
bf27ba04   Benjamin Renard   Add templated par...
231
232
233
234
235
236
237
238
239
240
		
		$dateModifTemplate = filemtime($templatePath);
		touch($dstFilePath, $dateModifTemplate);
		
		return $dstFilePath;
	}
	
	/*
	 * @brief Get template file path
	 */
4fe90834   Benjamin Renard   Fix bug with get ...
241
	public function getTemplatePath($param_id) {
bf27ba04   Benjamin Renard   Add templated par...
242
243
244
245
		if (!$this->isTemplatedParam($param_id))
			return "";
		return IHMConfigClass::getParamTemplateFilePath($this->paramTemplateList[$param_id]['fileName']);
	}
7911f4bc   Benjamin Renard   Another bug with ...
246
247
248
249
250
251
252
253
254
255
256
257
258

	/*
	 *  @brief Get template id by template file name
	 */
	public function getTemplateId($template_name) {
		$list = $this->getParamTemplates();
		foreach ($list as $param_id => $param_info) {
			if ($param_info['fileName'] == $template_name) {
				return $param_id;
			}
		}
		return FALSE;
	}
bf27ba04   Benjamin Renard   Add templated par...
259
260
	
	/*
4fe90834   Benjamin Renard   Fix bug with get ...
261
262
263
264
265
266
267
268
269
	 * 
	 */
	public function getTemplateFileName($param_id) {
		if (!$this->isTemplatedParam($param_id))
			return "";
		return $this->paramTemplateList[$param_id]['fileName'];
	}
	
	/*
bf27ba04   Benjamin Renard   Add templated par...
270
271
	 * @brief Replace args in string
	*/
6f98ec26   Benjamin Renard   Write argument it...
272
	public function replaceArgs($string, $template_args, $arguments = array()) {
bf27ba04   Benjamin Renard   Add templated par...
273
		$result = $string;
6f98ec26   Benjamin Renard   Write argument it...
274
275
276
		if (empty($template_args)) {
			return $result;
		}
bf27ba04   Benjamin Renard   Add templated par...
277
278
		foreach ($template_args as $template_arg_key => $template_arg_value) {
			$result = str_replace("##".$template_arg_key."##", $template_arg_value, $result);
6f98ec26   Benjamin Renard   Write argument it...
279
280
281
282
			if (array_key_exists($template_arg_key, $arguments) && ($arguments[$template_arg_key]['type'] == 'list')) {
				$item_name = array_key_exists($template_arg_value, $arguments[$template_arg_key]['items']) ? $arguments[$template_arg_key]['items'][$template_arg_value] : 'Unknown';
				$result = str_replace("@@".$template_arg_key."@@", $item_name, $result);
			}
bf27ba04   Benjamin Renard   Add templated par...
283
284
285
286
287
288
289
290
		}
		return $result;
	}
	
	/*
	 * @brief Enrich Template args with default values
	 */
	private function addDefaultValues($param_id, &$template_args) {
6f98ec26   Benjamin Renard   Write argument it...
291
292
		$arguments = $this->getArguments($param_id);
		if (empty($arguments))
bf27ba04   Benjamin Renard   Add templated par...
293
			return;
bf27ba04   Benjamin Renard   Add templated par...
294
		foreach ($arguments as $arg_key => $arg_def) {
a44cf065   Benjamin Renard   Add indexes suppo...
295
296
297
298
299
300
301
302
			if (!array_key_exists($arg_key, $template_args)) {
				if (is_array($template_args)) {
					$template_args[$arg_key] = $arg_def['default'];
				}
				else {
					$template_args->{$arg_key} = $arg_def['default'];
				}
			}
bf27ba04   Benjamin Renard   Add templated par...
303
304
		}
	}
6f98ec26   Benjamin Renard   Write argument it...
305
306
307
308
309
310
311
312
313
314
315

	/*
	 * @brief Get list of arguments for a given parameter
	 */
	protected function getArguments($param_id) {
		$list = $this->getParamTemplates();

		if (!array_key_exists($param_id, $list) || !isset($list[$param_id]['arguments']))
			return array();
		return $list[$param_id]['arguments'];
	}
bf27ba04   Benjamin Renard   Add templated par...
316
317
318
319
	
	/*
	 * @brief Load list of templated parameters
	*/
ffc5cb81   Elena.Budnik   temporary commit
320
	protected function loadUserParamManagerFile()
bf27ba04   Benjamin Renard   Add templated par...
321
322
323
324
325
	{
		if (isset($this->paramTemplateList))
			return;
		
		$this->paramTemplateList = array();
ffc5cb81   Elena.Budnik   temporary commit
326
	 
bf27ba04   Benjamin Renard   Add templated par...
327
328
		//load xml file
		$dom = new DomDocument("1.0");
ffc5cb81   Elena.Budnik   temporary commit
329
		if (!$dom->load($this->paramTemplateListFilePath))
bf27ba04   Benjamin Renard   Add templated par...
330
			return;
ffc5cb81   Elena.Budnik   temporary commit
331
			
bf27ba04   Benjamin Renard   Add templated par...
332
		$paramTemplateNodes = $dom->getElementsByTagName(self::$paramTemplateNode);
ffc5cb81   Elena.Budnik   temporary commit
333

bf27ba04   Benjamin Renard   Add templated par...
334
335
336
337
338
339
		foreach ($paramTemplateNodes as $paramTemplateNode) {
			$paramId       = $paramTemplateNode->getAttribute(self::$paramTemplateIdAtt);
			$paramFileName = $paramTemplateNode->getAttribute(self::$paramTemplateFileNameAtt);
			
			if (empty($paramId) || empty($paramFileName))
				continue;
ffc5cb81   Elena.Budnik   temporary commit
340
				
bf27ba04   Benjamin Renard   Add templated par...
341
342
343
344
345
346
347
348
349
350
351
352
			$arguments = array();
			
			$argumentsNode = $paramTemplateNode->getElementsByTagName(self::$paramTemplateArgumentsNode);
			
			if (count($argumentsNode) > 0) {
				$argumentsNode = $argumentsNode->item(0);
				$argumentNodes = $argumentsNode->getElementsByTagName(self::$paramTemplateArgumentNode);
				
				foreach($argumentNodes as $argumentNode) {
					$argumentKey  = $argumentNode->getAttribute(self::$paramTemplateArgumentKeyAtt);
					$argumentName = $argumentNode->getAttribute(self::$paramTemplateArgumentNameAtt);
					$argumentType = $argumentNode->getAttribute(self::$paramTemplateArgumentTypeAtt);
1e653e6f   Benjamin Renard   Add subtype defin...
353
					$argumentSubType = $argumentNode->getAttribute(self::$paramTemplateArgumentSubTypeAtt);
bf27ba04   Benjamin Renard   Add templated par...
354
					$argumentDefault = $argumentNode->getAttribute(self::$paramTemplateArgumentDefaultAtt);
a7011f4d   Benjamin Renard   Generated list fo...
355
356
357
					$argumentMinKey = $argumentNode->getAttribute(self::$paramTemplateArgumentMinKeyAtt);
					$argumentMaxKey = $argumentNode->getAttribute(self::$paramTemplateArgumentMaxKeyAtt);
					$argumentNameTpl = $argumentNode->getAttribute(self::$paramTemplateArgumentNameTplAtt);
bf27ba04   Benjamin Renard   Add templated par...
358
359
360
361
362
363
364
					
					if (empty($argumentKey) || empty($argumentType))
						continue;
					
					$arguments[$argumentKey] = array(
						"name" => (empty($argumentName) ? $argumentKey : $argumentName),
						"type" => $argumentType,
1e653e6f   Benjamin Renard   Add subtype defin...
365
						"subtype" => $argumentSubType,
bf27ba04   Benjamin Renard   Add templated par...
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
						"default" => $argumentDefault
					);
					
					switch ($argumentType) {
						case "list" :
							$arguments[$argumentKey]['items'] = array();
							
							$itemNodes = $argumentNode->getElementsByTagName(self::$paramTemplateArgumentListItemNode);
							foreach ($itemNodes as $itemNode) {
								$itemKey  = $itemNode->getAttribute(self::$paramTemplateArgumentListItemKeyAtt);
								$itemName = $itemNode->getAttribute(self::$paramTemplateArgumentListItemNameAtt);
								if ($itemKey == "")
									continue;
								$arguments[$argumentKey]['items'][$itemKey] = (empty($itemName) ? $itemKey : $itemName);
							}
a7011f4d   Benjamin Renard   Generated list fo...
381
382
383
384
385
386
387
388
							break;
						case "generated-list":
							$arguments[$argumentKey]['type'] = 'list';
							$arguments[$argumentKey]['items'] = array();
							for ($key = intval($argumentMinKey) ; $key <= intval($argumentMaxKey); ++$key) {
								$itemName = str_replace('##key##',$key,$argumentNameTpl);
								$arguments[$argumentKey]['items'][strval($key)] = $itemName;
							}
bf27ba04   Benjamin Renard   Add templated par...
389
390
391
392
393
394
395
396
397
398
399
400
401
402
							break;
						default:
							//Nothing to do
					}
				}
			}
			
			$this->paramTemplateList[$paramId] = array(
				"fileName"  => $paramFileName,
				"arguments" => $arguments
			);
			
		}
	}
a7011f4d   Benjamin Renard   Generated list fo...
403
}