Blame view

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

/**
 * @class CatalogMgr
 */

0fea5567   Benjamin Renard   First step for re...
7
8
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
		$this->contentRootId = 'catalog-treeRootNode';
		$this->contentRootTag = 'catalogList';
0fea5567   Benjamin Renard   First step for re...
13
		$this->attributes = array('name' => '', 'intervals' => ''); // +  'parameters'
d18b535d   elena   catalog draft + c...
14
15
16
17
		$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
			  $this->createDom();
0fea5567   Benjamin Renard   First step for re...
20
			  $this->xp = new domxpath($this->contentDom);
d18b535d   elena   catalog draft + c...
21
		}
0fea5567   Benjamin Renard   First step for re...
22
23
	}

0fea5567   Benjamin Renard   First step for re...
24
25
	public function getTmpObject($folderId, $name, $onlyDescription = false)
	{
53048303   Benjamin Renard   Cleanup TimeTable...
26
27
28
		$result = parent::getTmpObject($folderId, $name, $onlyDescription);
		if (!isset($result['success']) || !$result['success']) {
			return $result;
4a438b99   elena   catalog draft
29
		}
53048303   Benjamin Renard   Cleanup TimeTable...
30
		return $result + array('parameters' => $this->getCatalogParamDescription(array('folder' => $folderId, 'name' => $name)));
d18b535d   elena   catalog draft + c...
31
32
	}

0fea5567   Benjamin Renard   First step for re...
33
34

	public function loadIntervalsFromObject($id, $type, $start = NULL, $limit = NULL)
e82af71b   elena   catalog draft
35
	{
53048303   Benjamin Renard   Cleanup TimeTable...
36
37
38
		$result = parent::loadIntervalsFromObject($id, $type, $start, $limit);
		if (!isset($result['success']) || !$result['success']) {
			return $result;
4a438b99   elena   catalog draft
39
		}
0fea5567   Benjamin Renard   First step for re...
40

53048303   Benjamin Renard   Cleanup TimeTable...
41
42
		return $result + array('parameters' => $this->getCatalogParamDescription(array('id' => $id, 'type' => $type)));
  }
d18b535d   elena   catalog draft + c...
43
44
45
46

        /*
        *   catalog header
        */
0fea5567   Benjamin Renard   First step for re...
47
48
	protected function setParamDescription($params)
	{
40890ad7   elena   parameters
49
		$paramsElement = $this->objectDom->createElement('parameters');
0fea5567   Benjamin Renard   First step for re...
50
		foreach ($params as $param)
4a438b99   elena   catalog draft
51
		{
40890ad7   elena   parameters
52
			$paramElement = $this->objectDom->createElement('parameter');
0fea5567   Benjamin Renard   First step for re...
53
			$attrArray = (array)$param;
4a438b99   elena   catalog draft
54
55
			foreach ($attrArray as $key => $value)
				$paramElement->setAttribute($key, $value);
0fea5567   Benjamin Renard   First step for re...
56
			$paramsElement->appendChild($paramElement);
4a438b99   elena   catalog draft
57
		}
0fea5567   Benjamin Renard   First step for re...
58

4a438b99   elena   catalog draft
59
	return $paramsElement;
d18b535d   elena   catalog draft + c...
60
	}
0fea5567   Benjamin Renard   First step for re...
61

42e2e019   Benjamin Renard   Next step for cac...
62
      protected function createIntervalElement($interval, $options = array())
4a438b99   elena   catalog draft
63
64
65
66
      {
		$newInterval = $this->objectDom->createElement('intervals');
		$newInterval->appendChild($this->objectDom->createElement('start',$interval['start']));
		$newInterval->appendChild($this->objectDom->createElement('stop',$interval['stop']));
42e2e019   Benjamin Renard   Next step for cac...
67
68
69
70
		if (!empty($options['parameters'])) {
			foreach ($options['parameters'] as $parameter) {
				$newInterval->appendChild($this->objectDom->createElement('param', $interval[$parameter['id']]));
			}
4a438b99   elena   catalog draft
71
		}
d18b535d   elena   catalog draft + c...
72
73
	return $newInterval;
      }
0fea5567   Benjamin Renard   First step for re...
74

4a438b99   elena   catalog draft
75
	public function createObject($p, $folder)
0fea5567   Benjamin Renard   First step for re...
76
	{
d18b535d   elena   catalog draft + c...
77
78
79
80
81
		if ($p -> leaf)
		{
			$result = $this->createParameter($p, $folder);
			if ($result['error'])
				return $result;
0fea5567   Benjamin Renard   First step for re...
82

d18b535d   elena   catalog draft + c...
83
			$cacheMgr = new CatalogCacheMgr();
0fea5567   Benjamin Renard   First step for re...
84

d18b535d   elena   catalog draft + c...
85
86
			if (isset($p->cacheToken) && ($p->cacheToken != ''))
			{
0fea5567   Benjamin Renard   First step for re...
87
				$resultSaveInt = $cacheMgr->saveInObject($result['id'], "update", $p->cacheToken);
d18b535d   elena   catalog draft + c...
88
89
90
91
92
93
94
95
96
97
98
99
100
				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');
0fea5567   Benjamin Renard   First step for re...
101

d18b535d   elena   catalog draft + c...
102
	}
0fea5567   Benjamin Renard   First step for re...
103

8be9a1a8   Benjamin Renard   Fix catalog visu
104
	public function initForChart($id, $folderId, $name, $isTmpObject, $type)
86263051   elena   visu second draft
105
	{
8be9a1a8   Benjamin Renard   Fix catalog visu
106
107
108
109
110
111
112
113
114
115
116
117
118
		if ($isTmpObject)
			$options = array(
				'name' => $name,
				'folder' => empty($folderId) ? USERTEMPDIR : USERWORKINGDIR.$folderId,
				'type' => $type,
			);
		else
			$options = array(
				'id' => $id,
				'type' => $type,
			);

	  $intervals_res = $this->getCatalogParamDescription($options);
0fea5567   Benjamin Renard   First step for re...
119

86263051   elena   visu second draft
120
121
122
		if (!$intervals_res['success'])
			return $intervals_res;

9d412dda   elena   catalog tmp and r...
123
		$paramHeaders = array();
0fea5567   Benjamin Renard   First step for re...
124

86263051   elena   visu second draft
125
		foreach ( $intervals_res['parameters'] as $param ) {
0fea5567   Benjamin Renard   First step for re...
126

86263051   elena   visu second draft
127
		  if ($param['size'] > 1) {
0fea5567   Benjamin Renard   First step for re...
128

86263051   elena   visu second draft
129
130
131
132
133
			for ($i = 0; $i < $param['size']; $i++) {
				$paramComp = array();
				$paramComp['id'] = $param['id'].'_'.$i;
				$paramComp['name'] = $param['name'].'_'.$i;
			//	$paramComp['size'] = 1;
0fea5567   Benjamin Renard   First step for re...
134

86263051   elena   visu second draft
135
136
137
138
				$paramHeaders[] = $paramComp;
			}
		  }
		  else {
0fea5567   Benjamin Renard   First step for re...
139
		       $paramHeaders[] = $param;
86263051   elena   visu second draft
140
141
		  }
		}
0fea5567   Benjamin Renard   First step for re...
142

9d412dda   elena   catalog tmp and r...
143
	//	unset($intervals_res);
0fea5567   Benjamin Renard   First step for re...
144
145

		return array('success' => true, 'parameters' => $paramHeaders,
9d412dda   elena   catalog tmp and r...
146
				'totalCount' =>  $intervals_res['totalCount'], 'name' => $intervals_res['name']);
86263051   elena   visu second draft
147
	}
0fea5567   Benjamin Renard   First step for re...
148
149


9d412dda   elena   catalog tmp and r...
150
	public function getIntervalsForChart($id, $name, $isTmpObject, $type) {
0fea5567   Benjamin Renard   First step for re...
151

9d412dda   elena   catalog tmp and r...
152
		if ($isTmpObject)
0fea5567   Benjamin Renard   First step for re...
153
			$intervals_res = $this->getTmpObject($id, $name);
9d412dda   elena   catalog tmp and r...
154
		else
0fea5567   Benjamin Renard   First step for re...
155
156
			$intervals_res = $this->loadIntervalsFromObject($id,$type);

86263051   elena   visu second draft
157
158
		if (!$intervals_res['success'])
			return $intervals_res;
0fea5567   Benjamin Renard   First step for re...
159

8be9a1a8   Benjamin Renard   Fix catalog visu
160

86263051   elena   visu second draft
161
		$newIntervals = array();
0fea5567   Benjamin Renard   First step for re...
162

86263051   elena   visu second draft
163
164
165
166
		foreach ($intervals_res['intervals'] as $interval)
		{
			$newIntervalComp = array();
			$k = 0;
0fea5567   Benjamin Renard   First step for re...
167

86263051   elena   visu second draft
168
			for ( $j = 0; $j < count($interval['paramTable']); $j++ ) {
0fea5567   Benjamin Renard   First step for re...
169

86263051   elena   visu second draft
170
171
172
173
174
175
				$param = $interval['paramTable'][$j];
				$tempArr = explode(',',$param);

				if (count($tempArr) > 1) {
					for ($i = 0; $i < count($tempArr); $i++) {
						$newIntervalComp['param'.$k] = $tempArr[$i];
0fea5567   Benjamin Renard   First step for re...
176
177
178
						$k++;
					}
				}
86263051   elena   visu second draft
179
180
				else {
				       $newIntervalComp['param'.$k] = $param;
0fea5567   Benjamin Renard   First step for re...
181
					$k++;
86263051   elena   visu second draft
182
183
				}
			}
0fea5567   Benjamin Renard   First step for re...
184
			$newIntervals[] = $newIntervalComp;
86263051   elena   visu second draft
185
		}
0fea5567   Benjamin Renard   First step for re...
186

86263051   elena   visu second draft
187
		return array('success' => true, 'intervals' => $newIntervals);
0fea5567   Benjamin Renard   First step for re...
188

86263051   elena   visu second draft
189
	}
0fea5567   Benjamin Renard   First step for re...
190

42e2e019   Benjamin Renard   Next step for cac...
191
	public function getCatalogParamDescription($options)
9d412dda   elena   catalog tmp and r...
192
	{
53048303   Benjamin Renard   Cleanup TimeTable...
193
194
195
196
197
198
199
200
		if (!empty($options['id'])) {
			if (isset($options['type']) && ($options['type'] == 'sharedcatalog')) {
				$sharedObjMgr = new SharedObjectsMgr();
				$path = $sharedObjMgr->getDataFilePath('catalog', $options['id']);
			}
			else {
				$path = USERTTDIR.$options['id'].'.xml';
			}
9d412dda   elena   catalog tmp and r...
201
202
		}
		else {
42e2e019   Benjamin Renard   Next step for cac...
203
			$path = $options['folder'].'/'.$options['name'].'.xml';
9d412dda   elena   catalog tmp and r...
204
		}
0fea5567   Benjamin Renard   First step for re...
205

53048303   Benjamin Renard   Cleanup TimeTable...
206
      //load intervals from Catalog id
169f14d2   Benjamin Renard   Add shared object...
207
		if (!file_exists($path))
8be9a1a8   Benjamin Renard   Fix catalog visu
208
			return array('success' => false, 'message' => "Cannot find Catalog file");
0fea5567   Benjamin Renard   First step for re...
209
210

		if (!$this->objectDom->load($path))
42e2e019   Benjamin Renard   Next step for cac...
211
			return array('success' => false, 'message' => "Cannot load Catalog file");
0fea5567   Benjamin Renard   First step for re...
212

53048303   Benjamin Renard   Cleanup TimeTable...
213
    $xpath = new DOMXPath($this->objectDom);
0fea5567   Benjamin Renard   First step for re...
214

9d412dda   elena   catalog tmp and r...
215
216
217
		// params header
		$paramsNodes =  $xpath->query('//parameters/parameter');
		$paramsArray = array();
0fea5567   Benjamin Renard   First step for re...
218

9d412dda   elena   catalog tmp and r...
219
		if ($paramsNodes->length > 0)
0fea5567   Benjamin Renard   First step for re...
220
221
222
		{
			foreach ($paramsNodes as $paramNode)
			{
9d412dda   elena   catalog tmp and r...
223
				$oneParam = array();
0fea5567   Benjamin Renard   First step for re...
224
225
				foreach ($paramNode->attributes as $attr)
					$oneParam[$attr->nodeName] = $attr->nodeValue;
9d412dda   elena   catalog tmp and r...
226
227
				$paramsArray[] = $oneParam;
			}
0fea5567   Benjamin Renard   First step for re...
228
229
		}

9d412dda   elena   catalog tmp and r...
230
		return array(
9d412dda   elena   catalog tmp and r...
231
232
			  'parameters' => $paramsArray,
			  'success'    => true
53048303   Benjamin Renard   Cleanup TimeTable...
233
    );
0fea5567   Benjamin Renard   First step for re...
234

53048303   Benjamin Renard   Cleanup TimeTable...
235
    }
0fea5567   Benjamin Renard   First step for re...
236

ba9b1b7d   Benjamin Renard   Rework of TT and ...
237
238
			public function getUploadedObject($name, $format, $onlyDescription = false)
			{
278185de   Benjamin Renard   Upload catalog fr...
239
				$result = parent::getUploadedObject($name, $format, $onlyDescription);
ba9b1b7d   Benjamin Renard   Rework of TT and ...
240
241
242
243
244
				if (!isset($result['success']) || !$result['success']) {

					return array('success' => FALSE, 'error' => 'Error to retrieve catalog info');
				}

ba9b1b7d   Benjamin Renard   Rework of TT and ...
245

278185de   Benjamin Renard   Upload catalog fr...
246
				return $result + array('parameters' => $this->getCatalogParamDescription(array('folder' => USERTEMPDIR, 'name' => $name)));
53048303   Benjamin Renard   Cleanup TimeTable...
247
			}
ba9b1b7d   Benjamin Renard   Rework of TT and ...
248

53048303   Benjamin Renard   Cleanup TimeTable...
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
			/**
			 * Load interval from DOMElement
			 * @param DOMElement
			 * @return interval
			 */
			protected function loadIntervalElement($intervalNode)
			{
				$interval = parent::loadIntervalElement($intervalNode);
				if ($interval === FALSE) {
					return FALSE;
				}
				$paramNodes = $intervalNode->getElementsByTagName('param');
				$interval['paramTable'] = array();
				if ($paramNodes->length > 0)
        	foreach ( $paramNodes as $paramNode ) $interval['paramTable'][] = $paramNode->nodeValue;
ba9b1b7d   Benjamin Renard   Rework of TT and ...
264

53048303   Benjamin Renard   Cleanup TimeTable...
265
				return $interval;
ba9b1b7d   Benjamin Renard   Rework of TT and ...
266
267
			}

f9c8b272   elena   edit catalog
268
269
// 	public function modifyObject($p) {
// 		$folder = $this->getObjectFolder($p->id);
0fea5567   Benjamin Renard   First step for re...
270
//
f9c8b272   elena   edit catalog
271
272
273
274
275
// 		//Copy TT in a tempory file
// 		$ttFilePath = USERTTDIR.$p->id.'.xml';
// 		$tmpFileExist = FALSE;
// 		if (file_exists($ttFilePath))
// 			$tmpFileExist = copy($ttFilePath,$ttFilePath.".tmp");
0fea5567   Benjamin Renard   First step for re...
276
//
f9c8b272   elena   edit catalog
277
278
// 		//Delete TT
// 		$this->deleteObject($p);
0fea5567   Benjamin Renard   First step for re...
279
//
f9c8b272   elena   edit catalog
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// 		//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());
// 		}
// 	}
901ba3f3   Elena.Budnik   upload catalog
299

0fea5567   Benjamin Renard   First step for re...
300

d18b535d   elena   catalog draft + c...
301
}
40890ad7   elena   parameters
302
?>