Blame view

php/classes/CatalogMgr.php 6.02 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

9846ec70   Benjamin Renard   WS getDataset wit...
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

42e2e019   Benjamin Renard   Next step for cac...
104
	public function getCatalogParamDescription($options)
9d412dda   elena   catalog tmp and r...
105
	{
53048303   Benjamin Renard   Cleanup TimeTable...
106
107
108
109
110
111
112
113
		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...
114
115
		}
		else {
42e2e019   Benjamin Renard   Next step for cac...
116
			$path = $options['folder'].'/'.$options['name'].'.xml';
9d412dda   elena   catalog tmp and r...
117
		}
0fea5567   Benjamin Renard   First step for re...
118

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

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

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

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

9d412dda   elena   catalog tmp and r...
132
		if ($paramsNodes->length > 0)
0fea5567   Benjamin Renard   First step for re...
133
134
135
		{
			foreach ($paramsNodes as $paramNode)
			{
9d412dda   elena   catalog tmp and r...
136
				$oneParam = array();
0fea5567   Benjamin Renard   First step for re...
137
138
				foreach ($paramNode->attributes as $attr)
					$oneParam[$attr->nodeName] = $attr->nodeValue;
9d412dda   elena   catalog tmp and r...
139
140
				$paramsArray[] = $oneParam;
			}
0fea5567   Benjamin Renard   First step for re...
141
142
		}

9d412dda   elena   catalog tmp and r...
143
		return array(
9d412dda   elena   catalog tmp and r...
144
145
			  'parameters' => $paramsArray,
			  'success'    => true
53048303   Benjamin Renard   Cleanup TimeTable...
146
    );
0fea5567   Benjamin Renard   First step for re...
147

53048303   Benjamin Renard   Cleanup TimeTable...
148
    }
0fea5567   Benjamin Renard   First step for re...
149

ba9b1b7d   Benjamin Renard   Rework of TT and ...
150
151
			public function getUploadedObject($name, $format, $onlyDescription = false)
			{
278185de   Benjamin Renard   Upload catalog fr...
152
				$result = parent::getUploadedObject($name, $format, $onlyDescription);
ba9b1b7d   Benjamin Renard   Rework of TT and ...
153
154
155
156
157
				if (!isset($result['success']) || !$result['success']) {

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

ba9b1b7d   Benjamin Renard   Rework of TT and ...
158

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

53048303   Benjamin Renard   Cleanup TimeTable...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
			/**
			 * 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 ...
177

53048303   Benjamin Renard   Cleanup TimeTable...
178
				return $interval;
ba9b1b7d   Benjamin Renard   Rework of TT and ...
179
180
			}

f9c8b272   elena   edit catalog
181
182
// 	public function modifyObject($p) {
// 		$folder = $this->getObjectFolder($p->id);
0fea5567   Benjamin Renard   First step for re...
183
//
f9c8b272   elena   edit catalog
184
185
186
187
188
// 		//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...
189
//
f9c8b272   elena   edit catalog
190
191
// 		//Delete TT
// 		$this->deleteObject($p);
0fea5567   Benjamin Renard   First step for re...
192
//
f9c8b272   elena   edit catalog
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// 		//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
212

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

d18b535d   elena   catalog draft + c...
214
}
40890ad7   elena   parameters
215
?>