Blame view

php/classes/CatalogMgr.php 12 KB
d18b535d   elena   catalog draft + c...
1
2
3
4
5
6
7
8
<?php

/**
 * @class CatalogMgr
 */

class CatalogMgr extends TimeTableMgr {	
             
169f14d2   Benjamin Renard   Add shared object...
9
10
	function __construct($sharedObject = FALSE) {
		parent::__construct('Tt.xml', $sharedObject);
d18b535d   elena   catalog draft + c...
11
12
13
14
15
16
17
		$this->contentRootId = 'catalog-treeRootNode';
		$this->contentRootTag = 'catalogList';
		$this->attributes = array('name' => '', 'intervals' => ''); // +  'parameters' 
		$this->optionalAttributes = array();
		$this->objTagName =   'catalog';
		$this->id_prefix = 'cat_'; // 'tt_' ?

169f14d2   Benjamin Renard   Add shared object...
18
		if (!$sharedObject && !file_exists($this->xmlName)) {
d18b535d   elena   catalog draft + c...
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
			  $this->createDom();
			  $this->xp = new domxpath($this->contentDom); 
		}
	}	
 
	public function getTmpObject($folderId, $name, $onlyDescription = false) {
	
		$filePath = USERWORKINGDIR.$folderId.'/'.$name.'.xml';
		
		if (!file_exists($filePath)) 
			return array('error' => 'Cannot find result file');
		
		$dom = new DomDocument('1.0'); 
		$dom->formatOutput = true;
		
		if (!$dom -> load($filePath))
			return array('error' => 'Cannot load result file');
		$nameNodes = $dom->getElementsByTagName('name');
		if ($nameNodes->length > 0)
			$attributesToReturn['name'] = $nameNodes->item(0)->nodeValue;
		
		$descNodes = $dom->getElementsByTagName('description');
		if ($descNodes->length > 0)
			$attributesToReturn['description'] = $descNodes->item(0)->nodeValue;
		
		$creatNodes = $dom->getElementsByTagName('created');
		if ($creatNodes->length > 0)
			$attributesToReturn['created'] = $creatNodes->item(0)->nodeValue;
		
		$histNodes = $dom->getElementsByTagName('history');
		if ($histNodes->length > 0)
			$attributesToReturn['history'] = $histNodes->item(0)->nodeValue;
	
		$attributesToReturn['objName'] = $name;
		$attributesToReturn['folderId'] = $folderId;
		$attributesToReturn['success'] = true;
		
		if (!$onlyDescription)
		{
			$intNodes = $dom->getElementsByTagName('intervals');
			foreach ($intNodes as $intNode)
			{
				$startNodes = $intNode->getElementsByTagName('start');
				if ($startNodes->length <= 0)
					return array('error' => 'Error detected in result file');
					
				$stopNodes = $intNode->getElementsByTagName('stop');
				if ($stopNodes->length <= 0)
					return array('error' => 'Error detected in result file');
					
				// for catalog	
				$paramNodes = $intNode->getElementsByTagName('param');
				$params = array();
				if ($paramNodes->length > 0) 
					foreach ( $paramNodes as $paramNode ) $params[] = $paramNode->nodeValue;
					
					
				$attributesToReturn['intervals'][] = array('start' => $startNodes->item(0)->nodeValue,
									    'stop' => $stopNodes->item(0)->nodeValue,
									    'paramTable' =>  $params);
			}
			// for catalog
			$paramsNodes = $dom->getElementsByTagName('parameter');
			
			if ($paramsNodes->length > 0){
			
			  $paramsArray = array();		 
			  foreach ($paramsNodes as $paramNode) {
			  
			    $oneParam = array();
			    foreach ($paramNode->attributes as $attr) 			    
				    $oneParam[$attr->nodeName] = $attr->nodeValue;
			  
			     if (substr($paramNode->getAttribute('id'),0,8) == 'stat_cov') {		    
				    $oneParam['size'] = '1';
				    $oneParam['name'] = 'Flag';
			      }
			      
			    $paramsArray[] = $oneParam;
			  }
			$attributesToReturn['success'] = true;
			$attributesToReturn['parameters'] = $paramsArray;
			}
			else 
4a438b99   elena   catalog draft
103
104
			    return array('error' => 'No information on parameters in result file');	
		}
d18b535d   elena   catalog draft + c...
105
106
107
108
109
	
		return  $attributesToReturn;
	}

	
e82af71b   elena   catalog draft
110
111
	public function loadIntervalsFromTT($id, $typeTT, $start = NULL, $limit = NULL)
	{
169f14d2   Benjamin Renard   Add shared object...
112
113
114
115
		if ($typeTT == 'sharedcatalog') {
	    	//Shared object
			$sharedObjMgr = new SharedObjectsMgr();
			$path = $sharedObjMgr->getDataFilePath('catalog', $id);
e82af71b   elena   catalog draft
116
117
	      }
	    else {
169f14d2   Benjamin Renard   Add shared object...
118
		    $path = USERTTDIR.$id.'.xml';	
e82af71b   elena   catalog draft
119
120
121
	    }
			  
	      //load intervals from TT id
169f14d2   Benjamin Renard   Add shared object...
122
123
		if (!file_exists($path))
			return array('success' => false, 'message' => "Cannot find Catalog file ".$typeTT);
e82af71b   elena   catalog draft
124
		
169f14d2   Benjamin Renard   Add shared object...
125
	      $this->objectDom->load($path);
e82af71b   elena   catalog draft
126
	      
4a438b99   elena   catalog draft
127
128
		if (!($objToGet = $this->objectDom->getElementById($id)))
			return array('success' => false, 'message' => NO_SUCH_ID." ".$id);
e82af71b   elena   catalog draft
129
130
131
132
133
134
	      
	      $xpath = new DOMXPath($this->objectDom);
	      $intervals =  $xpath->query('//intervals');
	      
	      $result = array();
	      
4a438b99   elena   catalog draft
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
		if (!isset($start) || !isset($limit))
		{
			foreach ($intervals as $interval)
			{
				$startTime = $interval->getElementsByTagName('start')->item(0)->nodeValue;
				$stopTime  = $interval->getElementsByTagName('stop')->item(0)->nodeValue;
				// for catalog	
				$paramNodes = $interval->getElementsByTagName('param');
				$params = array();
				if ($paramNodes->length > 0) 
					foreach ( $paramNodes as $paramNode ) $params[] = $paramNode->nodeValue;
									
				array_push($result, array('start' => $startTime, 'stop' => $stopTime,'paramTable' =>  $params));
				   
			}
		}
		else
e82af71b   elena   catalog draft
152
	      {
4a438b99   elena   catalog draft
153
154
			for ($i = 0; $i < $limit; ++$i)
			{
e82af71b   elena   catalog draft
155
156
			  if ($start+$i >= $intervals->length)
				  break;
4a438b99   elena   catalog draft
157
158
159
160
161
162
163
164
165
166
167
168
				$startTime = $intervals->item($start+$i)->getElementsByTagName('start')->item(0)->nodeValue;
				$stopTime  = $intervals->item($start+$i)->getElementsByTagName('stop')->item(0)->nodeValue;
				// for catalog
				$paramNodes = $intervals->item($start+$i)->getElementsByTagName('param');
				$params = array();
				if ($paramNodes->length > 0) 
					foreach ( $paramNodes as $paramNode ) $params[] = $paramNode->nodeValue;
									
				array_push($result, array('start' => $startTime, 'stop' => $stopTime,'paramTable' =>  $params));
			}
		}
		// for catalog : params header
40890ad7   elena   parameters
169
			$paramsNodes =  $xpath->query('//parameters/parameter');
4a438b99   elena   catalog draft
170
171
172
173
174
175
176
177
178
179
180
181
182
183
			$paramsArray = array();
			
			if ($paramsNodes->length > 0)
			{									 
				foreach ($paramsNodes as $paramNode) 
				{			  
					$oneParam = array();
					foreach ($paramNode->attributes as $attr) 			    
						$oneParam[$attr->nodeName] = $attr->nodeValue;			  			     			      
					$paramsArray[] = $oneParam;
				}
			}			
			 			
		return array(
e82af71b   elena   catalog draft
184
185
			  'totalCount' => $intervals->length,
			  'intervals'  => $result,
4a438b99   elena   catalog draft
186
			  'parameters' => $paramsArray,
e82af71b   elena   catalog draft
187
188
189
190
191
192
			  'start' => isset($start) ? $start : 0,
			  'limit' => isset($limit) ? $limit : 0,
			  'success'    => true
	      );
	  
      }
d18b535d   elena   catalog draft + c...
193
194
195
196

        /*
        *   catalog header
        */
4a438b99   elena   catalog draft
197
198
	protected function setParamDescription($params) 
	{		
40890ad7   elena   parameters
199
		$paramsElement = $this->objectDom->createElement('parameters');
4a438b99   elena   catalog draft
200
201
		foreach ($params as $param) 
		{
40890ad7   elena   parameters
202
			$paramElement = $this->objectDom->createElement('parameter');
4a438b99   elena   catalog draft
203
204
205
206
207
208
209
			$attrArray = (array)$param;            
			foreach ($attrArray as $key => $value)
				$paramElement->setAttribute($key, $value);
			$paramsElement->appendChild($paramElement);    
		}
	
	return $paramsElement;
d18b535d   elena   catalog draft + c...
210
	}
4a438b99   elena   catalog draft
211
212
213
214
215
216
217
218
219
220
221
        
      protected function createIntervalElement($interval) 
      {
		$newInterval = $this->objectDom->createElement('intervals');
		$newInterval->appendChild($this->objectDom->createElement('start',$interval['start']));
		$newInterval->appendChild($this->objectDom->createElement('stop',$interval['stop']));
		foreach ($interval as $key =>$value) {
			if (substr($key,0,5) == 'param')
			$newInterval->appendChild($this->objectDom->createElement('param', $value));
		
		}
d18b535d   elena   catalog draft + c...
222
223
224
	return $newInterval;
      }
      
4a438b99   elena   catalog draft
225
226
	public function createObject($p, $folder)
	{	 
d18b535d   elena   catalog draft + c...
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
		if ($p -> leaf)
		{
			$result = $this->createParameter($p, $folder);
			if ($result['error'])
				return $result;
				
			$cacheMgr = new CatalogCacheMgr();
			
			if (isset($p->cacheToken) && ($p->cacheToken != ''))
			{
				$resultSaveInt = $cacheMgr->saveInTT($result['id'], "update", $p->cacheToken);
				if (!$resultSaveInt['success'])
				{
					if ($resultSaveInt['message'])
						return array('error' => $resultSaveInt['message']);
					else
						return array('error' => 'Unknown error during intervals save');
				}
			}
			return $result;
		}
		//      else return $this->createFolder($p);
		//TODO check if this is possible?
		else return array('error' => 'createFolder should be called from RENAME');
	
	}
	
9d412dda   elena   catalog tmp and r...
254
	public function initForChart($id, $name, $isTmpObject, $typeTT) 
86263051   elena   visu second draft
255
	{
9d412dda   elena   catalog tmp and r...
256
	       $intervals_res = $this->getCatalogParamDescription($id, $name, $isTmpObject, $typeTT);
86263051   elena   visu second draft
257
258
259
260
		
		if (!$intervals_res['success'])
			return $intervals_res;

9d412dda   elena   catalog tmp and r...
261
		$paramHeaders = array();
86263051   elena   visu second draft
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
		
		foreach ( $intervals_res['parameters'] as $param ) {
		 
		  if ($param['size'] > 1) {
		  
			for ($i = 0; $i < $param['size']; $i++) {
				$paramComp = array();
				$paramComp['id'] = $param['id'].'_'.$i;
				$paramComp['name'] = $param['name'].'_'.$i;
			//	$paramComp['size'] = 1;
				
				$paramHeaders[] = $paramComp;
			}
		  }
		  else {
		       $paramHeaders[] = $param;    
		  }
		}
			
9d412dda   elena   catalog tmp and r...
281
	//	unset($intervals_res);
86263051   elena   visu second draft
282
		 
9d412dda   elena   catalog tmp and r...
283
284
		return array('success' => true, 'parameters' => $paramHeaders, 
				'totalCount' =>  $intervals_res['totalCount'], 'name' => $intervals_res['name']);
86263051   elena   visu second draft
285
286
	}
	
9d412dda   elena   catalog tmp and r...
287
288
	   	
	public function getIntervalsForChart($id, $name, $isTmpObject, $type) {
86263051   elena   visu second draft
289
		
9d412dda   elena   catalog tmp and r...
290
291
292
293
		if ($isTmpObject)
			$intervals_res = $this->getTmpObject($id, $name); 
		else
			$intervals_res = $this->loadIntervalsFromTT($id,$type);  
86263051   elena   visu second draft
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
		
		if (!$intervals_res['success'])
			return $intervals_res;
			
		$newIntervals = array();
		 
		foreach ($intervals_res['intervals'] as $interval)
		{
			$newIntervalComp = array();
			$k = 0;
			
			for ( $j = 0; $j < count($interval['paramTable']); $j++ ) {
			 
				$param = $interval['paramTable'][$j];
				$tempArr = explode(',',$param);

				if (count($tempArr) > 1) {
					for ($i = 0; $i < count($tempArr); $i++) {
						$newIntervalComp['param'.$k] = $tempArr[$i];
						$k++;						
					}					 
				} 
				else {
				       $newIntervalComp['param'.$k] = $param;
					$k++; 
				}
			}
			$newIntervals[] = $newIntervalComp; 
		}
		
		return array('success' => true, 'intervals' => $newIntervals);
			
	}
d18b535d   elena   catalog draft + c...
327
	
9d412dda   elena   catalog tmp and r...
328
329
	public function getCatalogParamDescription($id, $name, $isTmpObject, $typeTT)
	{
169f14d2   Benjamin Renard   Add shared object...
330
331
332
333
		if ($typeTT == 'sharedcatalog') {	
			//Shared object
			$sharedObjMgr = new SharedObjectsMgr();
			$path = $sharedObjMgr->getDataFilePath('catalog', $id);
9d412dda   elena   catalog tmp and r...
334
335
		}
		elseif (!$isTmpObject) {
169f14d2   Benjamin Renard   Add shared object...
336
			$path = USERTTDIR.$id.'.xml';		
9d412dda   elena   catalog tmp and r...
337
338
		}
		else {
169f14d2   Benjamin Renard   Add shared object...
339
			$path = USERWORKINGDIR.$id.'/'.$name.'.xml';
9d412dda   elena   catalog tmp and r...
340
341
342
		}
		
	      //load intervals from Catalog id
169f14d2   Benjamin Renard   Add shared object...
343
		if (!file_exists($path))
9d412dda   elena   catalog tmp and r...
344
345
			return array('success' => false, 'message' => "Cannot find Catalog file ".$id);
		
169f14d2   Benjamin Renard   Add shared object...
346
		if (!$this->objectDom->load($path)) 
9d412dda   elena   catalog tmp and r...
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
			return array('success' => false, 'message' => "Cannot load Catalog file ".$id);
	      
// 		if (!($objToGet = $this->objectDom->getElementById($id)))
// 			return array('success' => false, 'message' => NO_SUCH_ID." ".$id);

	      $nameNodes = $this->objectDom->getElementsByTagName('name');
	      
	      if ($nameNodes->length > 0)
			$nameReal = $nameNodes->item(0)->nodeValue;
		else 			
			$nameReal = $name;
			
	      $xpath = new DOMXPath($this->objectDom);
	      $intervals =  $xpath->query('//intervals');

	      
		// params header
		$paramsNodes =  $xpath->query('//parameters/parameter');
		$paramsArray = array();
		
		if ($paramsNodes->length > 0)
		{									 
			foreach ($paramsNodes as $paramNode) 
			{			  
				$oneParam = array();
				foreach ($paramNode->attributes as $attr) 			    
					$oneParam[$attr->nodeName] = $attr->nodeValue;			  			     			      
				$paramsArray[] = $oneParam;
			}
		}			
					
		return array(
			  'name'      =>  $nameReal,
			  'totalCount' => $intervals->length,
			  'parameters' => $paramsArray,
			  'success'    => true
	      );
	  
      }
f9c8b272   elena   edit catalog
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
      
// 	public function modifyObject($p) {
// 		$folder = $this->getObjectFolder($p->id);
// 		
// 		//Copy TT in a tempory file
// 		$ttFilePath = USERTTDIR.$p->id.'.xml';
// 		$tmpFileExist = FALSE;
// 		if (file_exists($ttFilePath))
// 			$tmpFileExist = copy($ttFilePath,$ttFilePath.".tmp");
// 		
// 		//Delete TT
// 		$this->deleteObject($p);
// 		
// 		//Save modifications
// 		try {
// 			$result = $this->createObject($p, $folder);
// 			if ($result['error'])
// 				throw new Exception($result['error']);
// 			if ($tmpFileExist)
// 				unlink($ttFilePath.".tmp");
// 			return array('id' => $p->id, 'info' => $result['nbIntervals'].' intervals' );
// 		}
// 		catch (Exception $e) {
// 			//Restore TT file
// 			if ($tmpFileExist)
// 			{
// 				copy($ttFilePath.".tmp", $ttFilePath);
// 				unlink($ttFilePath.".tmp");
// 			}
// 			return array ('error' => $e->getMessage());
// 		}
// 	}
d18b535d   elena   catalog draft + c...
418
}
40890ad7   elena   parameters
419
?>