Blame view

php/classes/DerivedParamMgr.php 11 KB
16035364   Benjamin Renard   First commit
1
2
3
4
<?php
/**
 * @class DerivedParamMgr
 * @version $Id: DerivedParamMgr.php 2914 2015-05-19 10:31:38Z elena $
16035364   Benjamin Renard   First commit
5
6
 */

996f73e5   Elena.Budnik   excluded SimuParams
7
8
9
10
class DerivedParamMgr extends AmdaObjectMgr 
{ 
	protected $type;
	protected $BaseManager;
16035364   Benjamin Renard   First commit
11

996f73e5   Elena.Budnik   excluded SimuParams
12
13
	function __construct($type) 
	{ 
16035364   Benjamin Renard   First commit
14
		parent::__construct('WsParams.xml');
16035364   Benjamin Renard   First commit
15
16
		$this->type = $type;

996f73e5   Elena.Budnik   excluded SimuParams
17
18
19
20
21
		if ($type == 'derivedParam') 
		{
			$this->contentRootId = 'derivedParam-treeRootNode';
			$this->contentRootTag = 'paramList';
			//node attributes  and object XML tags 
8ba47f72   Benjamin Renard   rest actualisatio...
22
			$this->attributes = array('name' => '', 'buildchain' => '', 'timestep' => '', 'dim_1' => '1', 'dim_2' => '1');
996f73e5   Elena.Budnik   excluded SimuParams
23
24
25
26
			// + tags in object XML
			$this->optionalAttributes = array('units' => 'undefined','description' => 'undefined', 'ytitle' => 'undefined');
			$this->objTagName = 'param';
			$this->id_prefix = 'ws_';
16035364   Benjamin Renard   First commit
27
		}
996f73e5   Elena.Budnik   excluded SimuParams
28
29
30
31
32
		else 
		{
			$this->contentRootId = 'myDataParam-treeRootNode';
			$this->contentRootTag = 'myDataList';
			//node attributes and object XML tags 
19dcd963   Elena.Budnik   redmine #4903 : s...
33
34
35
36
37
38
39
			$this->attributes = array('name' => '',  
					'size' => ' ', 
					'format' => '', 
					'desc' => '', 
					'mask' => '', 
					'minsampling' => '',
					'plottype' => '');
fdb53872   Elena.Budnik   formattage
40
			$this->optionalAttributes = array('vi' => 'undefined', 
19dcd963   Elena.Budnik   redmine #4903 : s...
41
42
43
44
45
46
47
					'realvar' => '', 
					'fillvalue' => -1.e31, 
					'units' => 'undefined',
					'ytitle' => 'undefined',  
					'file' => '', 
					'type' => '');
					
996f73e5   Elena.Budnik   excluded SimuParams
48
49
50
			$this->objTagName = 'mydata';
			$this->id_prefix = 'wsd_';
			$this->type = 'myDataParam';
16035364   Benjamin Renard   First commit
51
52
		}

996f73e5   Elena.Budnik   excluded SimuParams
53
54
55
56
57
		if (!file_exists($this->xmlName)) 
		{
			$this->createDom();
			$this->xp = new domxpath($this->contentDom); 
		}
16035364   Benjamin Renard   First commit
58
	}
16035364   Benjamin Renard   First commit
59
   
996f73e5   Elena.Budnik   excluded SimuParams
60
61
62
63
64
	protected function param2dd($paramID)
	{
		$pairs = array(":" => "_");    
		return strtr($paramID, $pairs); 
	}
16035364   Benjamin Renard   First commit
65

996f73e5   Elena.Budnik   excluded SimuParams
66
67
68
	protected function createDom() 
	{
		$types = array('param' => 'derived', 'mydata' => 'myData');
16035364   Benjamin Renard   First commit
69
70

		$rootElement = $this->contentDom->createElement('ws');
996f73e5   Elena.Budnik   excluded SimuParams
71
72
73
74
75
76
77
		foreach ($types as $key => $value)
		{
			$contentId = $value.'Param-treeRootNode';
			$contentTag = $key.'List';
			$typeElement = $this->contentDom->createElement($contentTag);
			$typeElement->setAttribute('xml:id', $contentId);
			$rootElement->appendChild($typeElement);
16035364   Benjamin Renard   First commit
78
		}
996f73e5   Elena.Budnik   excluded SimuParams
79
80
		
		$this->contentDom->appendChild($rootElement);
16035364   Benjamin Renard   First commit
81
		$this->contentDom->save($this->xmlName);
996f73e5   Elena.Budnik   excluded SimuParams
82
83
	}

16035364   Benjamin Renard   First commit
84
/*
996f73e5   Elena.Budnik   excluded SimuParams
85
*       Rename PARAM in id.xml
16035364   Benjamin Renard   First commit
86
*/
996f73e5   Elena.Budnik   excluded SimuParams
87
88
89
90
91
92
93
94
95
96
97
	protected function renameInResource($name, $id) 
	{
		if (!file_exists(USERWSDIR.$id.'.xml')) return false; 

		$this->objectDom -> load(USERWSDIR.$id.'.xml');
		if (!($objToRename = $this->objectDom->getElementById($id))) return false; 
		$objToRename -> getElementsByTagName('name')->item(0)->nodeValue = $name; 
		$this->objectDom ->save(USERWSDIR.$id.'.xml');
				
		return true;
	}
16035364   Benjamin Renard   First commit
98
99
100
101

/*
*        Check file id.xml if difference is name only
*/
996f73e5   Elena.Budnik   excluded SimuParams
102
103
104
105
106
107
108
109
110
111
112
113
114
	protected function renameOnly($p)
	{
		$oldObject = new DomDocument("1.0");
		$oldObject -> load(USERWSDIR.$p->id.".xml");

		foreach ($p as $key => $value) 
		{
			if ($key != 'id') 
			{
				if ($oldObject->getElementsByTagName($key)->length == 0) return false;
				if ($key != "name" && $oldObject->getElementsByTagName($key)->item(0)->nodeValue != $value) return false;
			}
		}
16035364   Benjamin Renard   First commit
115

996f73e5   Elena.Budnik   excluded SimuParams
116
117
		return true;
	}
16035364   Benjamin Renard   First commit
118

16035364   Benjamin Renard   First commit
119
120
121
/*
*         Create Parameter
*/
996f73e5   Elena.Budnik   excluded SimuParams
122
123
124
125
126
127
128
129
	public function createParameter($p, $folder)
	{
		// overwritten                
		if ($this->objectExistsByName($p->name)) 
		{
			$p->id  = $this->getObjectIdByName($p->name); 		      
			$this->deleteObject($p);
		}
16035364   Benjamin Renard   First commit
130
        
996f73e5   Elena.Budnik   excluded SimuParams
131
132
133
		$this->id = $this->setId();
		if (!$this->id) return array('error' => ID_CREATION_ERROR);
		$this->descFileName = USERWSDIR.$this->id.'.xml';
16035364   Benjamin Renard   First commit
134
            
996f73e5   Elena.Budnik   excluded SimuParams
135
136
137
138
139
140
		$fileExists = -1;
		//if alias exists, replace alias name by parameter name   
		if ($this->type == 'derivedParam') 
		{
			if (file_exists(USERWSDIR.'Alias.xml')) {
				$p->buildchain =  $this->resetAlias($p->buildchain);
7cab5983   Benjamin Renard   Add modif. time f...
141
142
			}
			$p->last_update = time(); 
996f73e5   Elena.Budnik   excluded SimuParams
143
144
145
146
			// switch between myData and Derived 		                  
			$this->createObjectDescription($p);
			$this->addToContent($p, $folder);
			
7cab5983   Benjamin Renard   Add modif. time f...
147
			return array('id' => $this->id, 'info' => $p->buildchain, 'dim_1' => $p->dim_1, 'dim_2' => $p->dim_2, 'last_update' => $p->last_update);   
996f73e5   Elena.Budnik   excluded SimuParams
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
		}
		// myData parameter
		else 
		{   
			$isSpectra = $p->plottype === 'Spectra';
			$this->BaseManager = new BaseManager();
			$oldMask = $this->BaseManager->getOldMask($p->mask);	

			$myFileMgr = new FilesMgr();

			if($oldMask) 
			{                                                                                                                              
				$myFileMgr->delMask($oldMask);
				$this->BaseManager->delVI($oldMask);                                     
			} 
			// separate files without mask - check that file exists
			else 
			{
				$fileExists = $this->BaseManager->getVI($p->mask);                      
			}
16035364   Benjamin Renard   First commit
168

996f73e5   Elena.Budnik   excluded SimuParams
169
170
171
172
173
174
175
176
177
178
179
180
181
			if ($fileExists < 0) 
			{                         
				$p->vi = $this->BaseManager->addVI($p->mask, $p->format); 
				$p->desc = $this->BaseManager->getViDesc($p->vi);                    
			}
			else 
			{
				if (!$oldMask) 
				{
					$p->vi = $this->BaseManager->getVI($p->mask); 
					$p->desc = $this->BaseManager->getViDesc($p->vi); 
				}
			}
16035364   Benjamin Renard   First commit
182

996f73e5   Elena.Budnik   excluded SimuParams
183
184
185
186
			if ($oldMask) 
			{
				$this->updateMaskMydata($oldMask, $p->mask,  $p->vi, $p->desc);
			}
16035364   Benjamin Renard   First commit
187

19dcd963   Elena.Budnik   redmine #4903 : s...
188
			$this->createObjectDescription($p);
0a089287   Benjamin Renard   Add table definit...
189
                
996f73e5   Elena.Budnik   excluded SimuParams
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
			//Add table definition
			if (isset($p->tableDef))
			{
				$tableNodes = $this->objectDom->getElementsByTagName('tableDef');
				if ($tableNodes->length > 0)
					$tableNode = $tableNodes->item(0);
				else
				{
					$tableNode = $this->objectDom->createElement('tableDef');
					$this->objectDom->documentElement->appendChild($tableNode);
				}
				//remove previous definition if exist
				while ($tableNode->firstChild)
					$tableNode->removeChild($tableNode->firstChild);
				//add definition
				$tableNode->setAttribute('tableDefType', $p->tableDef->tableDefType);
				$tableNode->setAttribute('channelsDefType', $p->tableDef->channelsDefType);
8edd6346   Benjamin Renard   Table name and un...
207
208
				$tableNode->setAttribute('tableName', $p->tableDef->tableName);
				$tableNode->setAttribute('tableUnits', $p->tableDef->tableUnits);
996f73e5   Elena.Budnik   excluded SimuParams
209
210
211
212
213
214
215
216
217
218
219
220
221
				if (isset($p->tableDef->data))
				{
					foreach ($p->tableDef->data as $key => $value)
					{
						$tablePropNode = $this->objectDom->createElement($key);
						$tablePropNode->nodeValue = $value;
						$tableNode -> appendChild($tablePropNode);
					}
				}
				//save 
				$this->objectDom->save($this->descFileName);
			}
			
19dcd963   Elena.Budnik   redmine #4903 : s...
222
			$this->addToContent($p, $folder);
16035364   Benjamin Renard   First commit
223

19dcd963   Elena.Budnik   redmine #4903 : s...
224
			$info =  "Size: ".$p->size."<br/>".$p->desc."<br/> Sampling: ".$p->minsampling."<br/> Mask: ".$p->mask;
16035364   Benjamin Renard   First commit
225

996f73e5   Elena.Budnik   excluded SimuParams
226
227
228
229
230
231
232
			if ($myFileMgr -> addMask($p->mask)){
				return array('id' => $this->id, 'size' => $p->size, 'mask' => $p->mask, 'info' => $info, 'isSpectra' => $isSpectra, 'updateMyData' => true); 
			}
			return array('id' => $this->id, 'size' => $p->size, 'mask' => $p->mask, 'info' => $info, 'isSpectra' => $isSpectra);  
		}                
	} 
 
16035364   Benjamin Renard   First commit
233
234
235
/*
*  Start-Stop was changed - update in in MyDataParam
*/ 
996f73e5   Elena.Budnik   excluded SimuParams
236
237
238
239
240
241
242
	public function updateMydata($mask, $desc) 
	{
		$mydatas = $this->xp->query("//mydata[@mask = '".$mask."']"); 
		
		if ($mydatas->length > 0) 
			foreach ($mydatas as $mydata)  
					$mydata->setAttribute("desc", $desc);  
16035364   Benjamin Renard   First commit
243

996f73e5   Elena.Budnik   excluded SimuParams
244
245
246
		$this->contentDom->save($this->xmlName);
					
	}   
16035364   Benjamin Renard   First commit
247
248
249
250

/*
*      Mask was changed - update in MyDataParam   
*/
996f73e5   Elena.Budnik   excluded SimuParams
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
	public function updateMaskMydata($oldMask, $newMask, $viId, $description)
	{     
		$mydatas = $this->xp->query("//mydata[@mask = '".$oldMask."']");
		if ($mydatas->length == 0) return;

		foreach ($mydatas as $mydata)  
		{
			$id = $mydata->getAttribute('xml:id');
			$mydata->setAttribute("mask", $newMask); 
			$mydata->setAttribute("desc", $description);

			if (file_exists(USERWSDIR.$id.'.xml')) 
			{
				$xml = new DomDocument("1.0");
				$xml->load(USERWSDIR.$id.'.xml');

				$mask = $xml->getElementsByTagName('mask');
				$mask->item(0)->nodeValue = $newMask;

				$vi = $xml->getElementsByTagName('vi');
				$vi->item(0)->nodeValue = $viId;

				$desc = $xml->getElementsByTagName('desc');
				$desc->item(0)->nodeValue = $description; 
				$xml->save(USERWSDIR.$id.'.xml');                        
			}
		}    
	}
16035364   Benjamin Renard   First commit
279
280
281
282
283

/*
*        Delete Description, Resource file, mark deleted parameter AS UNDEFINED in requests/ conditions/ aliases (?)
*/
//TODO return to client number of requests touches???
996f73e5   Elena.Budnik   excluded SimuParams
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
	public function deleteParameter($id)
	{ 
		//delete VI if there is no params with this mask ??????  
		$isDeletedMask = false;
		
		if ($this->type == 'myDataParam') 
		{
			if (file_exists(USERWSDIR.$id.'.xml')) 
			{
				$xml = new DomDocument("1.0");
				$xml->load(USERWSDIR.$id.'.xml');
				$mask = $xml->getElementsByTagName('mask'); 
				if (!$mask) return;
				$maskName = $mask->item(0)->nodeValue;
				$params = $this->xp->query("//mydata[@mask='".$maskName."']"); 
				if ($params->length == 1) {
					$this->BaseManager = new BaseManager();
					$this->BaseManager->delVI($maskName); 
					$FilesMgr = new FilesMgr();
					$FilesMgr->delMask($maskName);
					$isDeletedMask = true;
16035364   Benjamin Renard   First commit
305
				}                      
16035364   Benjamin Renard   First commit
306
			}
996f73e5   Elena.Budnik   excluded SimuParams
307
308
309
		}
		if (file_exists(USERWSDIR.$id.'.xml')) unlink(USERWSDIR.$id.'.xml');
			
ae3373c4   Benjamin Renard   php/classes/Deriv...
310
		$requestMgr = new RequestMgr('request');
996f73e5   Elena.Budnik   excluded SimuParams
311
312
313
		$n_requests = $requestMgr -> markAsUndefined($id);
		return $isDeletedMask;
	}
16035364   Benjamin Renard   First commit
314
315
316
317
   
/*
*   Get Object into Edit
*/
996f73e5   Elena.Budnik   excluded SimuParams
318
319
320
	public function getObject($id) 
	{
		if (!file_exists(USERWSDIR.$id.'.xml')) return array('error' => NO_OBJECT_FILE); 
7cab5983   Benjamin Renard   Add modif. time f...
321

996f73e5   Elena.Budnik   excluded SimuParams
322
323
324
325
326
		$this->objectDom -> load(USERWSDIR.$id.'.xml');
		if (!($objToGet = $this->objectDom->getElementById($id))) return array('error' => NO_SUCH_ID); 
				
		$attributesToReturn['id'] = $objToGet->getAttribute('xml:id');
		$attributes = $objToGet -> childNodes;
996f73e5   Elena.Budnik   excluded SimuParams
327
328
329
330
331
332
333
334
		foreach($attributes as $attribute) 
		{
			if ($attribute->tagName === "buildchain") $attributesToReturn[$attribute->tagName] = $this->setAlias($attribute->nodeValue);
			else if ($attribute->tagName === "tableDef")
			{
				//load table definition
				$tableDefType = $attribute->getAttribute('tableDefType');
				$channelsDefType = $attribute->getAttribute('channelsDefType');
8edd6346   Benjamin Renard   Table name and un...
335
336
				$tableName = $attribute->getAttribute('tableName');
				$tableUnits = $attribute->getAttribute('tableUnits');
996f73e5   Elena.Budnik   excluded SimuParams
337
338
339
340
341
342
				$tableData = array();
				foreach ($attribute->childNodes as $tableDataNode)
				{
					if ($tableDataNode->nodeType != XML_ELEMENT_NODE) continue;
					$tableData[$tableDataNode->tagName] = $tableDataNode->nodeValue;
				}
8edd6346   Benjamin Renard   Table name and un...
343
				$attributesToReturn[$attribute->tagName] = array('tableDefType' => $tableDefType, 'channelsDefType' => $channelsDefType, 'tableName' => $tableName, 'tableUnits' => $tableUnits, 'data' => $tableData);
996f73e5   Elena.Budnik   excluded SimuParams
344
345
346
			}
			else $attributesToReturn[$attribute->tagName] =  $attribute->nodeValue;
		}	
7cab5983   Benjamin Renard   Add modif. time f...
347
348
349
350
351

		if (empty($attributesToReturn['last_update'])) {
			$attributesToReturn['last_update'] = filemtime(USERWSDIR.$id.'.xml');
		}

996f73e5   Elena.Budnik   excluded SimuParams
352
353
		return  $attributesToReturn;   
	}
16035364   Benjamin Renard   First commit
354
  
996f73e5   Elena.Budnik   excluded SimuParams
355
356
357
358
359
360
361
362
	public function getObjectByName($name) 
	{
		if (!$this->objectExistsByName($name))
			return array('error' => NO_SUCH_ID);
		
		$objId = $this->getObjectIdByName($name);
		return $this->getObject($objId);
	}
16035364   Benjamin Renard   First commit
363
    
996f73e5   Elena.Budnik   excluded SimuParams
364
365
366
367
368
369
370
371
	public function validNameObject($p)
	{
	// overwritten
		$res = parent::validNameObject($p);
		
		if (!$res['valid']) return $res;
		
		//only low case
e915a87c   Elena.Budnik   upper cases in De...
372
373
// 		if (strtolower($p->name) != $p->name)
// 			return array('valid' => false, 'error' => 'Use only low case');
996f73e5   Elena.Budnik   excluded SimuParams
374
375
376
		
		return array('valid' => true);
	}
16035364   Benjamin Renard   First commit
377
378
}
?>