Blame view

php/classes/CatalogMgr.php 6.95 KB
d18b535d   elena   catalog draft + c...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php

/**
 * @class CatalogMgr
 */

class CatalogMgr extends TimeTableMgr {	
             
	function __construct() {
		parent::__construct('Tt.xml');
		$this->contentRootId = 'catalog-treeRootNode';
		$this->contentRootTag = 'catalogList';
		$this->attributes = array('name' => '', 'intervals' => ''); // +  'parameters' 
		$this->optionalAttributes = array();
		$this->objTagName =   'catalog';
		$this->id_prefix = 'cat_'; // 'tt_' ?

		if (!file_exists($this->xmlName)) {
			  $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 
			    return array('error' => 'No information on parameters in result file');
		
				
			}
	
		return  $attributesToReturn;
	}

	
// 	public function loadIntervalsFromTT($id,$typeTT,$start = NULL, $limit = NULL)
// 	{
// 
// 	    if ($typeTT == 'sharedtimeTable') {			
// 		    $pathid = SHAREDPATH.'TT/'.$id;
// 	      }
// 	    else {
// 		    $pathid = USERTTDIR.$id;		
// 	    }
// 			  
// 	      //load intervals from TT id
// 	      if (!file_exists($pathid.'.xml'))
// 		return array('success' => false, 'message' => "Cannot find TT file ".$id);
// 		
// 	      $this->objectDom -> load($pathid.'.xml');
// 	      
// 	      if (!($objToGet = $this->objectDom->getElementById($id)))
// 		return array('success' => false, 'message' => NO_SUCH_ID." ".$id);
// 	      
// 	      $xpath = new DOMXPath($this->objectDom);
// 	      $intervals =  $xpath->query('//intervals');
// 	      
// 	      $result = array();
// 	      
// 	      if (!isset($start) || !isset($limit))
// 	      {
// 		  foreach ($intervals as $interval)
// 		  {
// 				  $startTime = $interval->getElementsByTagName('start')->item(0)->nodeValue;
// 				  $stopTime  = $interval->getElementsByTagName('stop')->item(0)->nodeValue;
// 				  array_push($result, array('start' => $startTime, 'stop' => $stopTime));
// 			  }
// 	      }
// 	      else
// 	      {
// 		  for ($i = 0; $i < $limit; ++$i)
// 		  {
// 			  if ($start+$i >= $intervals->length)
// 				  break;
// 			  $startTime = $intervals->item($start+$i)->getElementsByTagName('start')->item(0)->nodeValue;
// 			  $stopTime  = $intervals->item($start+$i)->getElementsByTagName('stop')->item(0)->nodeValue;
// 			  array_push($result, array('start' => $startTime, 'stop' => $stopTime));
// 		  }
// 	      }
// 	      
// 	      return array(
// 			  'totalCount' => $intervals->length,
// 			  'intervals'  => $result,
// 			  'start' => isset($start) ? $start : 0,
// 			  'limit' => isset($limit) ? $limit : 0,
// 			  'success'    => true
// 	      );
// 	  
//       }

        /*
        *   catalog header
        */
        protected function setParamDescription($params) {
                  
          $paramsElement = $this->objectDom->createElement('parametres');
          foreach ($params as $param) {
             $paramElement = $this->objectDom->createElement('param');
             $attrArray = (array)$param;            
             foreach ($attrArray as $key => $value)
		    $paramElement->setAttribute($key, $value);
	     $paramsElement->appendChild($paramElement);    
          }
          
          return $paramsElement;
        }
        
      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));
	    
	}
	return $newInterval;
      }
      
	public function createObject($p, $folder){
	 
		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');
	
	}
	
	
}
?>