Blame view

php/classes/TimeTableMgr.php 24.7 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
<?php

/**
 * @class TimeTableMgr
 * @version $Id: TimeTableMgr.php 2809 2015-03-05 09:50:52Z natacha $
 * @todo MANAGE TT created by Search and Plot interactive - HERE?
 *
 */

4a438b99   elena   catalog draft
10
class TimeTableMgr extends AmdaObjectMgr
139a8045   Nathanaël Jourdane   autocleanup
11
{
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
12
13
14
15
16
17
	/**
	 * TimeTableMgr constructor.
	 * @param null $user The user
	 * @param bool $sharedObject Shared object
	 */
	public function __construct($user = null, $sharedObject = false)
4a438b99   elena   catalog draft
18
	{
16035364   Benjamin Renard   First commit
19
20
21
		parent::__construct('Tt.xml');
		$this->contentRootId = 'timeTable-treeRootNode';
		$this->contentRootTag = 'timetabList';
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
22
23
		$this->attributes = ['name' => '', 'intervals' => ''];
		$this->optionalAttributes = [];
16035364   Benjamin Renard   First commit
24
25
26
		$this->objTagName = 'timetab';
		$this->id_prefix = 'tt_';

169f14d2   Benjamin Renard   Add shared object...
27
		if (!$sharedObject && !file_exists($this->xmlName)) {
139a8045   Nathanaël Jourdane   autocleanup
28
29
			$this->createDom();
			$this->xp = new domxpath($this->contentDom);
16035364   Benjamin Renard   First commit
30
31
		}
	}
139a8045   Nathanaël Jourdane   autocleanup
32
33

	protected function createDom()
3ed056c4   Elena.Budnik   redmine #5278
34
	{
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
35
		$types = ['timetab' => 'timeTable', 'catalog' => 'catalog'];
16035364   Benjamin Renard   First commit
36
37

		$rootElement = $this->contentDom->createElement('ws');
139a8045   Nathanaël Jourdane   autocleanup
38

3ed056c4   Elena.Budnik   redmine #5278
39
		foreach ($types as $key => $value) {
139a8045   Nathanaël Jourdane   autocleanup
40
41
			$contentId = $value . '-treeRootNode';
			$contentTag = $key . 'List';
3ed056c4   Elena.Budnik   redmine #5278
42
43
44
			$typeElement = $this->contentDom->createElement($contentTag);
			$typeElement->setAttribute('xml:id', $contentId);
			$rootElement->appendChild($typeElement);
16035364   Benjamin Renard   First commit
45
		}
4a438b99   elena   catalog draft
46
		$this->contentDom->appendChild($rootElement);
16035364   Benjamin Renard   First commit
47
		$this->contentDom->save($this->xmlName);
3ed056c4   Elena.Budnik   redmine #5278
48
	}
16035364   Benjamin Renard   First commit
49

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
50
51
52
53
54
55
56
	/**
	 * Get object
	 * @param $id
	 * @param $nodeType
	 * @return array
	 */
	public function getObject($id, $nodeType = null)
4a438b99   elena   catalog draft
57
	{
139a8045   Nathanaël Jourdane   autocleanup
58
59
60
61
62
63
64
		if (substr($nodeType, 0, 6) == 'shared') {
			//Shared object
			$sharedObjMgr = new SharedObjectsMgr();
			$path = $sharedObjMgr->getDataFilePath(str_replace('shared', '', $nodeType), $id);
		} else {
			$path = USERTTDIR . $id . '.xml';
		}
16035364   Benjamin Renard   First commit
65

139a8045   Nathanaël Jourdane   autocleanup
66
		if (!file_exists($path)) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
67
			return ['error' => NO_OBJECT_FILE];
139a8045   Nathanaël Jourdane   autocleanup
68
69
70
		}
		$this->objectDom->load($path);
		if (!($objToGet = $this->objectDom->getElementById($id))) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
71
			return ['error' => NO_SUCH_ID];
139a8045   Nathanaël Jourdane   autocleanup
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
		}
		$attributesToReturn['id'] = $objToGet->getAttribute('xml:id');
		$attributes = $objToGet->childNodes;

		$nbInt = 0;
		foreach ($attributes as $attribute) {
			if ($attribute->nodeType == XML_ELEMENT_NODE) {
				/*if ($attribute->tagName == 'intervals') {
					$start = $attribute -> getElementsByTagName('start')->item(0) -> nodeValue;
					$stop = $attribute -> getElementsByTagName('stop')->item(0) -> nodeValue;
					$attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop);
				}
				else
				$attributesToReturn[$attribute->tagName] =  $attribute->nodeValue;*/
				//BRE - load all except intervals - Intervals will be loaded later with 'loadIntervalsFromTT' function
				if ($attribute->tagName != 'intervals') {
					$attributesToReturn[$attribute->tagName] = $attribute->nodeValue;
				} else {
					$nbInt++;
				}
			}
		}
139a8045   Nathanaël Jourdane   autocleanup
94
95
96
		$attributesToReturn['nbIntervals'] = $nbInt;

		return $attributesToReturn;
16035364   Benjamin Renard   First commit
97
98
	}

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
99
100
101
102
103
	/**
	 * Modify an object
	 * @param $p
	 * @return array
	 */
139a8045   Nathanaël Jourdane   autocleanup
104
	public function modifyObject($p)
4a438b99   elena   catalog draft
105
	{
139a8045   Nathanaël Jourdane   autocleanup
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
		$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");
			}
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
127
128
			return ['id' => $p->id, 'info' => $result['info']];
		} catch (Exception $exception) {
139a8045   Nathanaël Jourdane   autocleanup
129
130
131
132
133
			//Restore TT file
			if ($tmpFileExist) {
				copy($ttFilePath . ".tmp", $ttFilePath);
				unlink($ttFilePath . ".tmp");
			}
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
134
			return ['error' => $exception->getMessage()];
139a8045   Nathanaël Jourdane   autocleanup
135
		}
16035364   Benjamin Renard   First commit
136
137
	}

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
138
139
140
141
142
143
	/**
	 * Create an object
	 * @param $p
	 * @param $folder
	 * @return array
	 */
139a8045   Nathanaël Jourdane   autocleanup
144
145
146
147
148
149
150
151
152
153
154
155
	public function createObject($p, $folder)
	{
		if ($p->leaf) {
			$result = $this->createParameter($p, $folder);
			if ($result['error']) {
				return $result;
			}
			$cacheMgr = new TimeTableCacheMgr();
			if (isset($p->cacheToken) && ($p->cacheToken != '')) {
				$resultSaveInt = $cacheMgr->saveInTT($result['id'], "update", $p->cacheToken);
				if (!$resultSaveInt['success']) {
					if ($resultSaveInt['message']) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
156
						return ['error' => $resultSaveInt['message']];
139a8045   Nathanaël Jourdane   autocleanup
157
					} else {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
158
						return ['error' => 'Unknown error during intervals save'];
139a8045   Nathanaël Jourdane   autocleanup
159
160
161
162
					}
				}
			}
			return $result;
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
163
164
165
166
		} else {
			return ['error' => 'createFolder should be called from RENAME'];
			// return $this->createFolder($p);
			// TODO check if this is possible?
139a8045   Nathanaël Jourdane   autocleanup
167
		}
16035364   Benjamin Renard   First commit
168
	}
16035364   Benjamin Renard   First commit
169

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
170
171
172
173
174
175
	/**
	 * Create parameter (in case of catalogs)
	 * @param $p
	 * @param $folder
	 * @return array
	 */
4a438b99   elena   catalog draft
176
177
	protected function createParameter($p, $folder)
	{
139a8045   Nathanaël Jourdane   autocleanup
178
179
180
		if ($this->objectExistsByName($p->name)) {
			$p->id = $this->getObjectIdByName($p->name);
			$this->deleteObject($p);
16035364   Benjamin Renard   First commit
181
		}
139a8045   Nathanaël Jourdane   autocleanup
182

16035364   Benjamin Renard   First commit
183
184
		$this->id = $this->setId();
		$this->created = date('Y-m-d\TH:i:s');
139a8045   Nathanaël Jourdane   autocleanup
185
		if (!$this->id) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
186
			return ['error' => ID_CREATION_ERROR];
139a8045   Nathanaël Jourdane   autocleanup
187
188
189
		}

		$this->resFileName = USERTTDIR . $this->id . '.xml';
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
190
		// TODO catalog root element = 'timetable'
16035364   Benjamin Renard   First commit
191
		$rootElement = $this->objectDom->createElement('timetable');
139a8045   Nathanaël Jourdane   autocleanup
192
193
194
		$rootElement->setAttribute('xml:id', $this->id);

		foreach ($p as $key => $value) {
3ed056c4   Elena.Budnik   redmine #5278
195
196
			if ($key != 'id' && $key != 'leaf' && $key != 'nodeType' &&
				$key != 'objName' && $key != 'objFormat' && $key != 'folderId' && $key != 'cacheToken') {
139a8045   Nathanaël Jourdane   autocleanup
197
198
199
200
201
202
203
204
205
				if ($key == 'created') {
					$rootElement->appendChild($this->objectDom->createElement($key, $this->created));
				} // it is catalog
				else {
					if ($key == 'parameters') {
						$paramsElement = $this->setParamDescription($value);
						if ($paramsElement) {
							$rootElement->appendChild($paramsElement);
						}
139a8045   Nathanaël Jourdane   autocleanup
206
207
208
209
					} else {
						if ($key != 'intervals') {
							$rootElement->appendChild($this->objectDom->createElement($key, htmlspecialchars($value)));
						}
3ed056c4   Elena.Budnik   redmine #5278
210
					}
139a8045   Nathanaël Jourdane   autocleanup
211
				}
d18b535d   elena   catalog draft + c...
212
			}
139a8045   Nathanaël Jourdane   autocleanup
213
		}
16035364   Benjamin Renard   First commit
214
215
216
217
218
219

		$this->objectDom->appendChild($rootElement);
		$this->objectDom->save($this->resFileName);
		$obj = new stdClass();
		$obj->name = $p->name;
		$obj->intervals = $p->nbIntervals;
139a8045   Nathanaël Jourdane   autocleanup
220
		$this->addToContent($obj, $folder);
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
221
222
		// FIXME field created is undefined
		return ['id' => $this->id, 'created' => $this->created, 'info' => $obj->intervals . ' intervals'];
16035364   Benjamin Renard   First commit
223
224
	}

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
225
226
227
228
229
	/**
	 * Set parameter description
	 * TODO
	 * @param $param string The parameter
	 * @return DOMNode
139a8045   Nathanaël Jourdane   autocleanup
230
	 */
139a8045   Nathanaël Jourdane   autocleanup
231
	protected function setParamDescription($param)
4a438b99   elena   catalog draft
232
	{
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
233
		return new DOMNode();
4a438b99   elena   catalog draft
234
	}
139a8045   Nathanaël Jourdane   autocleanup
235

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
236
237
238
239
240
241
242
	/**
	 * Get uploaded object
	 * @param $name
	 * @param $format
	 * @param bool $onlyDescription
	 * @return mixed
	 */
139a8045   Nathanaël Jourdane   autocleanup
243
244
245
	public function getUploadedObject($name, $format, $onlyDescription = false)
	{
		if (strpos($name, '.txt') !== false || strpos($name, '.asc') !== false || strpos($name, '.') == false) {
e4fba9d2   Nathanaël Jourdane   Import tt: parse ...
246
			$attributesToReturn = $this->textToAmda(USERTEMPDIR . $name, $onlyDescription);
139a8045   Nathanaël Jourdane   autocleanup
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
			$attributesToReturn['objName'] = $name;
			$attributesToReturn['objFormat'] = $format;

			return $attributesToReturn;
		}

		if ($format == 'VOT') {
			$attributesToReturn = $this->vot2amda(USERTEMPDIR . $name, $onlyDescription);
			$attributesToReturn['objName'] = $name;
			$attributesToReturn['objFormat'] = $format;

			return $attributesToReturn;
		}

		if (strpos($name, '.xml') !== false) {
			$temp = explode('.xml', $name);
			$name = $temp[0];
		}

		if (!file_exists(USERTEMPDIR . $name . '.xml')) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
267
			return ['error' => 'no such name'];
139a8045   Nathanaël Jourdane   autocleanup
268
269
270
271
272
		}

		$this->objectDom->load(USERTEMPDIR . $name . '.xml');
		if (!($objToGet = $this->objectDom->getElementsByTagName('timetable')->item(0)) &&
			!($objToGet = $this->objectDom->getElementsByTagName('TimeTable')->item(0))) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
273
			return ['error' => 'no time table'];
139a8045   Nathanaël Jourdane   autocleanup
274
275
276
277
278
279
280
		}

		$attributes = $objToGet->childNodes;
		$attributesToReturn['name'] = $name;
		$attributesToReturn['objName'] = $name;
		$attributesToReturn['objFormat'] = $format;

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
281
		/** @var DOMElement $attribute */
139a8045   Nathanaël Jourdane   autocleanup
282
283
284
285
286
287
		foreach ($attributes as $attribute) {
			if ($attribute->nodeType == XML_ELEMENT_NODE) {
				if ($attribute->tagName == 'intervals') {
					$start = $attribute->getElementsByTagName('start')->item(0)->nodeValue;
					$stop = $attribute->getElementsByTagName('stop')->item(0)->nodeValue;
					if (!$onlyDescription) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
288
						$attributesToReturn['intervals'][] = ['start' => $start, 'stop' => $stop];
139a8045   Nathanaël Jourdane   autocleanup
289
290
291
292
293
294
					}
				} else {
					if ($attribute->tagName == 'Interval') {
						$start = $attribute->getElementsByTagName('Start')->item(0)->nodeValue;
						$stop = $attribute->getElementsByTagName('Stop')->item(0)->nodeValue;
						if (!$onlyDescription) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
295
							$attributesToReturn['intervals'][] = ['start' => $start, 'stop' => $stop];
139a8045   Nathanaël Jourdane   autocleanup
296
297
298
						}
					} else {
						switch (strtolower($attribute->tagName)) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
299
							case 'created':
139a8045   Nathanaël Jourdane   autocleanup
300
301
								$attributesToReturn['created'] = $attribute->nodeValue;
								break;
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
302
303
							case 'chain':
							case 'source':
139a8045   Nathanaël Jourdane   autocleanup
304
305
306
307
308
309
310
311
312
313
314
315
								$attributesToReturn['description'] = $attribute->nodeValue;
								break;
							default:
								break;
						}
					}
				}
			}
		}
		return $attributesToReturn;
	}

3ed056c4   Elena.Budnik   redmine #5278
316
317
318
	/*
	* Uploaded text file => convert to array
	*/
139a8045   Nathanaël Jourdane   autocleanup
319

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
320
321
322
323
324
325
	/**
	 * Convert text to AMDA attributes
	 * @param $tmp_file
	 * @param bool $onlyDescription
	 * @return mixed
	 */
e4fba9d2   Nathanaël Jourdane   Import tt: parse ...
326
	protected function textToAmda($tmp_file, $onlyDescription = false)
139a8045   Nathanaël Jourdane   autocleanup
327
	{
4a438b99   elena   catalog draft
328
		$suffix = explode('.', basename($tmp_file));
139a8045   Nathanaël Jourdane   autocleanup
329
330
		$lines = file($tmp_file, FILE_SKIP_EMPTY_LINES);
		$description = "Uploaded Time Table" . PHP_EOL;
4a438b99   elena   catalog draft
331

f8cd7d84   Elena.Budnik   error msg if cann...
332
333
		$recordsNumber = count($lines);
		$descNumber = 0;
139a8045   Nathanaël Jourdane   autocleanup
334
335

		foreach ($lines as $line) {
f9b34ed9   Nathanaël Jourdane   import : parse da...
336
			$line = preg_replace('/[-:\/\s]+/', ' ', trim($line));
e4fba9d2   Nathanaël Jourdane   Import tt: parse ...
337
			if ($line[0] == '#') { // Comment
139a8045   Nathanaël Jourdane   autocleanup
338
339
				$description = $description . PHP_EOL . substr($line, 1, -1);
			} else {
e4fba9d2   Nathanaël Jourdane   Import tt: parse ...
340
341
				$isoFormat = 'Y-m-dTH:i:s';
				$doyFormat = 'Y z H i s';
f9b34ed9   Nathanaël Jourdane   import : parse da...
342
				$doyRegex = '(\d{4}) (\d{3}) (\d{2}) (\d{2}) (\d{2})( \d{2})?';
e4fba9d2   Nathanaël Jourdane   Import tt: parse ...
343

efb57954   Nathanaël Jourdane   Parse imported ti...
344
				if (preg_match('/^' . $doyRegex . ' ' . $doyRegex . '$/', $line)) {
a525e8c1   Nathanaël Jourdane   import tt: Substr...
345
346
347
348
					$start = DateTime::createFromFormat($doyFormat, substr($line, 0, 17));
					$stop = DateTime::createFromFormat($doyFormat, substr($line, 18));
					$startDate = $start->sub(new DateInterval('P1D'))->format($isoFormat);
					$stopDate = $stop->sub(new DateInterval('P1D'))->format($isoFormat);
e4fba9d2   Nathanaël Jourdane   Import tt: parse ...
349
				} else {
efb57954   Nathanaël Jourdane   Parse imported ti...
350
351
					$dateLength = round((strlen($line)-1) / 2);

f9b34ed9   Nathanaël Jourdane   import : parse da...
352
353
354
355
356
					$start = explode(' ', substr($line, 0, $dateLength) . ' 00');
					$startTime = strtotime("$start[0]/$start[1]/$start[2] $start[3]:$start[4]:$start[5]");

					$stop = explode(' ', substr($line, $dateLength + 1) . ' 00');
					$stopTime = strtotime("$stop[0]/$stop[1]/$stop[2] $stop[3]:$stop[4]:$stop[5]");
efb57954   Nathanaël Jourdane   Parse imported ti...
357
358
359
360
361
362
363
364
					if (is_numeric($startTime) && is_numeric($stopTime)) {
						$startDate = date($isoFormat, $startTime);
						$stopDate = date($isoFormat, $stopTime);
					} else {
						$description = $description . PHP_EOL . $line;
						$descNumber++;
						continue;
					}
4a438b99   elena   catalog draft
365
				}
139a8045   Nathanaël Jourdane   autocleanup
366

e4fba9d2   Nathanaël Jourdane   Import tt: parse ...
367
368
				if (!$onlyDescription) {
					$attributesToReturn['intervals'][] = ['start' => $startDate, 'stop' => $stopDate];
139a8045   Nathanaël Jourdane   autocleanup
369
				}
4a438b99   elena   catalog draft
370
			}
3ed056c4   Elena.Budnik   redmine #5278
371
		}
139a8045   Nathanaël Jourdane   autocleanup
372
		if ($recordsNumber == $descNumber) {
e4fba9d2   Nathanaël Jourdane   Import tt: parse ...
373
			$description = 'Looks like we can not read your time format...' . PHP_EOL . $description;
139a8045   Nathanaël Jourdane   autocleanup
374
375
		}

4a438b99   elena   catalog draft
376
		$attributesToReturn['description'] = $description;
139a8045   Nathanaël Jourdane   autocleanup
377
		$attributesToReturn['name'] = basename($tmp_file, '.' . $suffix[1]);
dca1b251   Elena.Budnik   non standard time...
378
		$attributesToReturn['created'] = date('Y-m-d\TH:i:s');
3ed056c4   Elena.Budnik   redmine #5278
379
		return $attributesToReturn;
16035364   Benjamin Renard   First commit
380
	}
6207ec14   Elena.Budnik   #5919 non standar...
381

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
382
383
384
385
386
387
	/**
	 * Convert VOTable time table to AMDA attributes
	 * @param $tmp_file
	 * @param bool $onlyDescription
	 * @return mixed
	 */
139a8045   Nathanaël Jourdane   autocleanup
388
389
390
391
	protected function vot2amda($tmp_file, $onlyDescription = false)
	{
		// Load Time table
		$this->objectDom->load($tmp_file);
3ed056c4   Elena.Budnik   redmine #5278
392
393
		$objToGet = $this->objectDom->getElementsByTagName('TABLEDATA')->item(0);
		$attributesToReturn['name'] = $tmp_file;
139a8045   Nathanaël Jourdane   autocleanup
394
		$attributes = $objToGet->childNodes;
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
395
396

		/** @var DOMElement $attribute */
139a8045   Nathanaël Jourdane   autocleanup
397
		foreach ($attributes as $attribute) {
3ed056c4   Elena.Budnik   redmine #5278
398
			if ($attribute->tagName == 'TR') {
139a8045   Nathanaël Jourdane   autocleanup
399
400
401
				$start = $attribute->getElementsByTagName('TD')->item(0)->nodeValue;
				$stop = $attribute->getElementsByTagName('TD')->item(1)->nodeValue;
				if (!$onlyDescription) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
402
					$attributesToReturn['intervals'][] = ['start' => $start, 'stop' => $stop];
139a8045   Nathanaël Jourdane   autocleanup
403
				}
3ed056c4   Elena.Budnik   redmine #5278
404
			}
139a8045   Nathanaël Jourdane   autocleanup
405
406
407
408
		}
		$suffix = explode('.', basename($tmp_file));
		$attributesToReturn['name'] = basename($tmp_file, '.' . $suffix[1]);
		$attributesToReturn['created'] = date('Y-m-d') . "T" . date('H:i:s');
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
409
410
		$description = $this->objectDom->getElementsByTagName('DESCRIPTION')->item(0)->nodeValue;
		$attributesToReturn['description'] = htmlspecialchars($description);
139a8045   Nathanaël Jourdane   autocleanup
411
		return ($attributesToReturn);
16035364   Benjamin Renard   First commit
412
	}
139a8045   Nathanaël Jourdane   autocleanup
413

16035364   Benjamin Renard   First commit
414
415
416
	/*****************************************************************
	 *                           PUBLIC FUNCTIONS
	 *****************************************************************/
139a8045   Nathanaël Jourdane   autocleanup
417

16035364   Benjamin Renard   First commit
418
419
420
	/*
	 *   Get Object into Edit
	 */
139a8045   Nathanaël Jourdane   autocleanup
421
422
423
424
	public function getTmpObject($folderId, $name, $onlyDescription = false)
	{
		$filePath = USERWORKINGDIR . $folderId . '/' . $name . '.xml';
		if (!file_exists($filePath)) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
425
			return ['error' => 'Cannot find result file'];
16035364   Benjamin Renard   First commit
426
		}
139a8045   Nathanaël Jourdane   autocleanup
427
428
429
430
431

		$dom = new DomDocument('1.0');
		$dom->formatOutput = true;

		if (!$dom->load($filePath)) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
432
			return ['error' => 'Cannot load result file'];
16035364   Benjamin Renard   First commit
433
434
		}

139a8045   Nathanaël Jourdane   autocleanup
435
436
437
438
		$descNodes = $dom->getElementsByTagName('description');
		if ($descNodes->length > 0) {
			$attributesToReturn['description'] = $descNodes->item(0)->nodeValue;
		}
16035364   Benjamin Renard   First commit
439

139a8045   Nathanaël Jourdane   autocleanup
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
		$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;

		if (!$onlyDescription) {
			$intNodes = $dom->getElementsByTagName('intervals');
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
455
456

			/** @var DOMElement $intNode */
139a8045   Nathanaël Jourdane   autocleanup
457
458
459
			foreach ($intNodes as $intNode) {
				$startNodes = $intNode->getElementsByTagName('start');
				if ($startNodes->length <= 0) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
460
					return ['error' => 'Error detected in result file'];
139a8045   Nathanaël Jourdane   autocleanup
461
462
463
				}
				$stopNodes = $intNode->getElementsByTagName('stop');
				if ($stopNodes->length <= 0) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
464
					return ['error' => 'Error detected in result file'];
16035364   Benjamin Renard   First commit
465
				}
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
466
				$attributesToReturn['intervals'][] = [
139a8045   Nathanaël Jourdane   autocleanup
467
468
					'start' => $startNodes->item(0)->nodeValue,
					'stop' => $stopNodes->item(0)->nodeValue
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
469
				];
16035364   Benjamin Renard   First commit
470
			}
16035364   Benjamin Renard   First commit
471
		}
139a8045   Nathanaël Jourdane   autocleanup
472
473

		return $attributesToReturn;
16035364   Benjamin Renard   First commit
474
	}
139a8045   Nathanaël Jourdane   autocleanup
475

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
476
477
478
479
480
	/**
	 * Merge time tables
	 * @param $obj
	 * @return array
	 */
139a8045   Nathanaël Jourdane   autocleanup
481
482
483
484
485
486
487
488
489
	public function merge($obj)
	{
		/**
		 * Array of intervals, used like :
		 * [{start:'2010-01-01T23:00:00',stop:'2011-01-01T20:00:00'},{start:'2009-01-01T23:00:00',stop:'2010-01-01T20:00:00'}]
		 * $attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop);
		 */

		$intervals = 0;
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
490
491
492
493
494
		for ($iId = 0; $iId < count($obj->ids); $iId++) {
			$table[$iId] = $this->loadIntervalsFromTT($obj->ids[$iId]);
			for ($jId = 0; $jId < count($table[$iId]['intervals']); $jId++) {
				$interval[$iId][$jId][0] = $table[$iId]['intervals'][$jId]['start'];
				$interval[$iId][$jId][1] = $table[$iId]['intervals'][$jId]['stop'];
139a8045   Nathanaël Jourdane   autocleanup
495
			}
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
496
			$intervals += count($interval[$iId]);
16035364   Benjamin Renard   First commit
497
		}
139a8045   Nathanaël Jourdane   autocleanup
498
499
500
501
		if ($intervals > 10000) {
			set_time_limit(1800);
		}

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
502
503
504
		$final = [];
		for ($iId = 0; $iId < count($obj->ids); $iId++) {
			$final = array_merge($final, $interval[$iId]);
139a8045   Nathanaël Jourdane   autocleanup
505
506
507
508
509
		}
		sort($final);

		// Algorithm of union
		$line = 0;
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
510
511
512
513
514
		$iId = 0;
		$val = $final[$iId][0];
		while ($iId < count($final) - 1) {
			if ($final[$iId + 1][1] <= $final[$iId][1]) {
				array_splice($final, $iId + 1, 1);
139a8045   Nathanaël Jourdane   autocleanup
515
			} else {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
516
517
				if (($final[$iId + 1][0] <= $final[$iId][1]) && ($final[$iId + 1][1] >= $final[$iId][1])) {
					$iId++;
139a8045   Nathanaël Jourdane   autocleanup
518
				} else {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
519
520
521
					$start[$line] = $val;
					$stop[$line] = $final[$iId][1];
					$iId++;
139a8045   Nathanaël Jourdane   autocleanup
522
					$line++;
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
523
					$val = $final[$iId][0];
139a8045   Nathanaël Jourdane   autocleanup
524
				}
16035364   Benjamin Renard   First commit
525
			}
16035364   Benjamin Renard   First commit
526
		}
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
527
528
		$start[$line] = $val;
		$stop[$line] = $final[$iId][1];
139a8045   Nathanaël Jourdane   autocleanup
529
530
531
532
533
534
535

		$objTT = new stdClass();
		$objTT->name = $obj->name;
		$objTT->nodeType = 'timeTable';
		$objTT->leaf = true;
		$objTT->created = null;
		$objTT->history = $obj->history;
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
536
		for ($iId = 0; $iId < count($start); $iId++) {
139a8045   Nathanaël Jourdane   autocleanup
537
			$inter = new stdClass();
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
538
539
			$inter->start = $start[$iId];
			$inter->stop = $stop[$iId];
139a8045   Nathanaël Jourdane   autocleanup
540
541
542
543
544
545
			$objTT->intervals[] = $inter;
		}
		$objTT->nbIntervals = count($start);
		$this->objectDom = new DomDocument('1.0');
		$this->objectDom->formatOutput = true;

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
546
		$res = $this->createParameter($objTT, $folder); // FIXME $folder is undefined
139a8045   Nathanaël Jourdane   autocleanup
547
548
549
550
551
		if ($res['error']) {
			return $res;
		}

		$this->saveIntervals($res['id'], $objTT->intervals, 'merge');
139a8045   Nathanaël Jourdane   autocleanup
552
		return $res;
16035364   Benjamin Renard   First commit
553
	}
139a8045   Nathanaël Jourdane   autocleanup
554

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
555
556
557
558
559
560
561
562
563
	/**
	 * Load intervals from time table
	 * @param $id
	 * @param $typeTT
	 * @param null $start
	 * @param null $limit
	 * @return array
	 */
	public function loadIntervalsFromTT($id, $typeTT = '', $start = null, $limit = null)
3ed056c4   Elena.Budnik   redmine #5278
564
565
566
567
568
	{
		if ($typeTT == 'sharedtimeTable') {
			//Shared object
			$sharedObjMgr = new SharedObjectsMgr();
			$path = $sharedObjMgr->getDataFilePath('timeTable', $id);
139a8045   Nathanaël Jourdane   autocleanup
569
570
		} else {
			$path = USERTTDIR . $id . '.xml';
3ed056c4   Elena.Budnik   redmine #5278
571
		}
139a8045   Nathanaël Jourdane   autocleanup
572
573
574

		//load intervals from TT id
		if (!file_exists($path)) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
575
			return ['success' => false, 'message' => "Cannot find TT file " . $id];
139a8045   Nathanaël Jourdane   autocleanup
576
577
578
579
		}

		$this->objectDom->load($path);
		if (!($objToGet = $this->objectDom->getElementById($id))) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
580
			return ['success' => false, 'message' => NO_SUCH_ID . " " . $id];
139a8045   Nathanaël Jourdane   autocleanup
581
582
		}

3ed056c4   Elena.Budnik   redmine #5278
583
		$xpath = new DOMXPath($this->objectDom);
139a8045   Nathanaël Jourdane   autocleanup
584
585
		$intervals = $xpath->query('//intervals');

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
586
		$result = [];
139a8045   Nathanaël Jourdane   autocleanup
587

3ed056c4   Elena.Budnik   redmine #5278
588
		if (!isset($start) || !isset($limit)) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
589
590

			/** @var DOMElement $interval */
3ed056c4   Elena.Budnik   redmine #5278
591
592
			foreach ($intervals as $interval) {
				$startTime = $interval->getElementsByTagName('start')->item(0)->nodeValue;
139a8045   Nathanaël Jourdane   autocleanup
593
				$stopTime = $interval->getElementsByTagName('stop')->item(0)->nodeValue;
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
594
				array_push($result, ['start' => $startTime, 'stop' => $stopTime]);
3ed056c4   Elena.Budnik   redmine #5278
595
			}
139a8045   Nathanaël Jourdane   autocleanup
596
		} else {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
597
598
			for ($iInt = 0; $iInt < $limit; ++$iInt) {
				if ($start + $iInt >= $intervals->length) {
3ed056c4   Elena.Budnik   redmine #5278
599
					break;
139a8045   Nathanaël Jourdane   autocleanup
600
				}
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
601
602
603
				$startTime = $intervals->item($start + $iInt)->getElementsByTagName('start')->item(0)->nodeValue;
				$stopTime = $intervals->item($start + $iInt)->getElementsByTagName('stop')->item(0)->nodeValue;
				array_push($result, ['start' => $startTime, 'stop' => $stopTime]);
3ed056c4   Elena.Budnik   redmine #5278
604
			}
16035364   Benjamin Renard   First commit
605
		}
139a8045   Nathanaël Jourdane   autocleanup
606

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
607
		return [
139a8045   Nathanaël Jourdane   autocleanup
608
609
610
611
612
			'totalCount' => $intervals->length,
			'intervals' => $result,
			'start' => isset($start) ? $start : 0,
			'limit' => isset($limit) ? $limit : 0,
			'success' => true
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
613
		];
169f14d2   Benjamin Renard   Add shared object...
614
	}
139a8045   Nathanaël Jourdane   autocleanup
615

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
616
617
618
619
620
621
622
	/**
	 * Save intervals
	 * @param $id
	 * @param $intervals
	 * @param $action
	 * @return array
	 */
139a8045   Nathanaël Jourdane   autocleanup
623
	public function saveIntervals($id, $intervals, $action)
3ed056c4   Elena.Budnik   redmine #5278
624
	{
139a8045   Nathanaël Jourdane   autocleanup
625
		if (substr($id, 0, 6) == 'shared') {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
626
			return ['success' => false, 'message' => "Cannot save shared TimeTable"];
139a8045   Nathanaël Jourdane   autocleanup
627
628
629
630
		} else {
			$path = USERTTDIR . $id . '.xml';
		}
		if (!file_exists($path)) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
631
			return ['success' => false, 'message' => "Cannot find TT file " . $id];
139a8045   Nathanaël Jourdane   autocleanup
632
633
634
635
		}
		$this->objectDom->load($path);

		if (!($objToGet = $this->objectDom->getElementById($id))) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
636
			return ['success' => false, 'message' => NO_SUCH_ID . " " . $id];
3ed056c4   Elena.Budnik   redmine #5278
637
		}
16035364   Benjamin Renard   First commit
638

3ed056c4   Elena.Budnik   redmine #5278
639
640
		//remove old intervals
		$crtNode = $objToGet->firstChild;
139a8045   Nathanaël Jourdane   autocleanup
641

3ed056c4   Elena.Budnik   redmine #5278
642
		while ($crtNode) {
139a8045   Nathanaël Jourdane   autocleanup
643
			if (($crtNode->nodeType != XML_ELEMENT_NODE) || ($crtNode->tagName != 'intervals')) {
3ed056c4   Elena.Budnik   redmine #5278
644
645
646
647
648
649
650
651
				$crtNode = $crtNode->nextSibling;
				continue;
			}
			$toRemove = $crtNode;
			$crtNode = $crtNode->nextSibling;
			$objToGet->removeChild($toRemove);
			unset($toRemove);
		}
139a8045   Nathanaël Jourdane   autocleanup
652

3ed056c4   Elena.Budnik   redmine #5278
653
		//add new intervals
139a8045   Nathanaël Jourdane   autocleanup
654
655
		foreach ($intervals as $interval) {
			$newInterval = $this->createIntervalElement($interval);
3ed056c4   Elena.Budnik   redmine #5278
656
657
			$this->objectDom->documentElement->appendChild($newInterval);
		}
16035364   Benjamin Renard   First commit
658

3ed056c4   Elena.Budnik   redmine #5278
659
660
		//save modifications
		$this->id = $id;
139a8045   Nathanaël Jourdane   autocleanup
661
		$this->resFileName = USERTTDIR . $this->id . '.xml';
3ed056c4   Elena.Budnik   redmine #5278
662
		$this->objectDom->save($this->resFileName);
139a8045   Nathanaël Jourdane   autocleanup
663

3ed056c4   Elena.Budnik   redmine #5278
664
		unset($this->objectDom);
139a8045   Nathanaël Jourdane   autocleanup
665

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
666
		return ['success' => true, 'action' => $action, 'nbIntervals' => count($intervals)];
3ed056c4   Elena.Budnik   redmine #5278
667
	}
139a8045   Nathanaël Jourdane   autocleanup
668

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
669
670
671
672
673
	/**
	 * Create interval element
	 * @param $interval
	 * @return DOMElement
	 */
139a8045   Nathanaël Jourdane   autocleanup
674
	protected function createIntervalElement($interval)
3ed056c4   Elena.Budnik   redmine #5278
675
	{
139a8045   Nathanaël Jourdane   autocleanup
676
677
678
679
680
681
		$newInterval = $this->objectDom->createElement('intervals');
		$newInterval->appendChild($this->objectDom->createElement('start', $interval->start));
		$newInterval->appendChild($this->objectDom->createElement('stop', $interval->stop));
		return $newInterval;
	}

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
682
683
684
685
686
	/**
	 * Intersect time tables
	 * @param $obj
	 * @return array|string
	 */
139a8045   Nathanaël Jourdane   autocleanup
687
688
	public function intersect($obj)
	{
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
689
690
691
692
693
694
		$intervals = 0;
		for ($iId = 0; $iId < count($obj->ids); $iId++) {
			$table[$iId] = $this->loadIntervalsFromTT($obj->ids[$iId]);
			for ($jId = 0; $jId < count($table[$iId]['intervals']); $jId++) {
				$interval[$iId][$jId][0] = $table[$iId]['intervals'][$jId]['start'];
				$interval[$iId][$jId][1] = $table[$iId]['intervals'][$jId]['stop'];
139a8045   Nathanaël Jourdane   autocleanup
695
			}
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
696
			$intervals += count($interval[$iId]);
16035364   Benjamin Renard   First commit
697
		}
139a8045   Nathanaël Jourdane   autocleanup
698
699
		if ($intervals > 10000) {
			set_time_limit(1800);
16035364   Benjamin Renard   First commit
700
		}
16035364   Benjamin Renard   First commit
701

139a8045   Nathanaël Jourdane   autocleanup
702
703
704
		// Sort intervals in time tables
		sort($interval[0]);
		sort($interval[1]);
16035364   Benjamin Renard   First commit
705

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
706
707
		$iId = 0;
		$jId = 0;
139a8045   Nathanaël Jourdane   autocleanup
708
709
		$line = 0;

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
710
711
		while (($iId < count($interval[0])) && ($jId < count($interval[1]))) {
			$inter = $this->callIntersection($interval[0][$iId], $interval[1][$jId]);
139a8045   Nathanaël Jourdane   autocleanup
712
713
714
715
716
717
718

			if ($inter[0][0] != 0 && $inter[0][1] != 0) {
				$start[$line] = $inter[0][0];
				$stop[$line] = $inter[0][1];
				$line++;
			}

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
719
720
			if ($interval[0][$iId][1] < $interval[1][$jId][1]) {
				$iId++;
139a8045   Nathanaël Jourdane   autocleanup
721
			} else {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
722
				$jId++;
139a8045   Nathanaël Jourdane   autocleanup
723
724
725
726
727
728
729
			}
		}

		// Intersection is empty
		if ($line == 0) {
			$result = "empty";
		} else {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
730
			$objTT->name = $obj->name;  // FIXME $objTT is undefined
139a8045   Nathanaël Jourdane   autocleanup
731
732
733
734
			$objTT->nodeType = 'timeTable';
			$objTT->leaf = true;
			$objTT->created = null;
			$objTT->history = $obj->history;
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
735
			for ($iId = 0; $iId < count($start); $iId++) {
139a8045   Nathanaël Jourdane   autocleanup
736
				$inter = new stdClass();
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
737
738
				$inter->start = $start[$iId];
				$inter->stop = $stop[$iId];
139a8045   Nathanaël Jourdane   autocleanup
739
740
741
742
743
744
745
746
747
				$objTT->intervals[] = $inter;
			}
			$objTT->nbIntervals = count($start);

			if (count($objTT->intervals) == 0) {
				$result = "empty";
			} else {
				$this->objectDom = new DomDocument('1.0');
				$this->objectDom->formatOutput = true;
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
748
				$result = $this->createObject($objTT, $folder); // FIXME $folder is undefined
139a8045   Nathanaël Jourdane   autocleanup
749
750
751

				if (!isset($result['error'])) {
					$this->saveIntervals($result['id'], $objTT->intervals, 'intersect');
3ed056c4   Elena.Budnik   redmine #5278
752
				}
16035364   Benjamin Renard   First commit
753
			}
3ed056c4   Elena.Budnik   redmine #5278
754
		}
139a8045   Nathanaël Jourdane   autocleanup
755
756
757
		return $result;
	}

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
758
759
760
761
762
763
764
	/**
	 * Call intersection
	 * @param $fst
	 * @param $snd
	 * @return array
	 */
	protected function callIntersection($fst, $snd)
139a8045   Nathanaël Jourdane   autocleanup
765
766
767
	{
		$inf = ($fst[0] > $snd[0]) ? $fst[0] : $snd[0];
		$sup = ($fst[1] < $snd[1]) ? $fst[1] : $snd[1];
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
768
		$inter[] = ($inf >= $sup) ? [0, 0] : [$inf, $sup];
139a8045   Nathanaël Jourdane   autocleanup
769
		return $inter;
16035364   Benjamin Renard   First commit
770
771
	}

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
772
773
774
775
776
777
778
	/**
	 * Valid name object
	 * TODO getObject only!!!! => change DD_Search output
	 * @param $p
	 * @return array
	 */
	public function validNameObject($p)
139a8045   Nathanaël Jourdane   autocleanup
779
780
781
782
783
784
785
786
787
788
	{
		// overwritten
		$res = parent::validNameObject($p);

		if (!$res['valid']) {
			return $res;
		}

		//no space
		if (strpos($p->name, ' ') === false) {
d8ec1623   Nathanaël Jourdane   fix phpcs warnings
789
			return ['valid' => true];
16035364   Benjamin Renard   First commit
790
		}
139a8045   Nathanaël Jourdane   autocleanup
791

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
792
		return ['valid' => false, 'error' => 'Space character is not allowed'];
16035364   Benjamin Renard   First commit
793
794
	}

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
795
796
797
798
799
800
801
802
803
	/**
	 * Copy time table
	 * @param $src_path
	 * @param $dst_path
	 * @param $newId
	 * @param $newName
	 * @param null $newDescription
	 * @return bool
	 */
139a8045   Nathanaël Jourdane   autocleanup
804
	public function copyTT($src_path, $dst_path, $newId, $newName, $newDescription = null)
3ed056c4   Elena.Budnik   redmine #5278
805
	{
139a8045   Nathanaël Jourdane   autocleanup
806
807
808
809
810
811
812
813
		if (!file_exists($src_path)) {
			return false;
		}

		if (!is_dir($dst_path)) {
			return false;
		}

169f14d2   Benjamin Renard   Add shared object...
814
815
		$dom = new DomDocument('1.0');
		$dom->formatOutput = true;
139a8045   Nathanaël Jourdane   autocleanup
816
817
818
819
820

		if (!$dom->load($src_path)) {
			return false;
		}

169f14d2   Benjamin Renard   Add shared object...
821
		$timeTableNodes = $dom->getElementsByTagName('timetable');
139a8045   Nathanaël Jourdane   autocleanup
822
823
824
825
		if ($timeTableNodes->length <= 0) {
			return false;
		}

169f14d2   Benjamin Renard   Add shared object...
826
		$timeTableNode = $timeTableNodes->item(0);
139a8045   Nathanaël Jourdane   autocleanup
827

169f14d2   Benjamin Renard   Add shared object...
828
		$timeTableNode->setAttribute('xml:id', $newId);
139a8045   Nathanaël Jourdane   autocleanup
829

169f14d2   Benjamin Renard   Add shared object...
830
		$nameNodes = $timeTableNode->getElementsByTagName('name');
139a8045   Nathanaël Jourdane   autocleanup
831

247b38e9   Benjamin Renard   Fix function used...
832
		if ($nameNodes->length <= 0) {
169f14d2   Benjamin Renard   Add shared object...
833
834
835
			//create name node (normally never append)
			$nameNode = $dom->createElement('name');
			$timeTableNode->appendChild($nameNode);
139a8045   Nathanaël Jourdane   autocleanup
836
		} else {
169f14d2   Benjamin Renard   Add shared object...
837
			$nameNode = $nameNodes->item(0);
139a8045   Nathanaël Jourdane   autocleanup
838
839
		}

169f14d2   Benjamin Renard   Add shared object...
840
		$nameNode->nodeValue = $newName;
139a8045   Nathanaël Jourdane   autocleanup
841

09eefb2d   Benjamin Renard   Shared TT and Cat...
842
843
		if (isset($newDescription) && !empty($newDescription)) {
			$descriptionNodes = $timeTableNode->getElementsByTagName('description');
247b38e9   Benjamin Renard   Fix function used...
844
			if ($descriptionNodes->length <= 0) {
09eefb2d   Benjamin Renard   Shared TT and Cat...
845
846
847
				//create description node (normally never append)
				$descriptionNode = $dom->createElement('description');
				$timeTableNode->appendChild($descriptionNode);
139a8045   Nathanaël Jourdane   autocleanup
848
			} else {
09eefb2d   Benjamin Renard   Shared TT and Cat...
849
				$descriptionNode = $descriptionNodes->item(0);
139a8045   Nathanaël Jourdane   autocleanup
850
851
			}

09eefb2d   Benjamin Renard   Shared TT and Cat...
852
853
			$descriptionNode->nodeValue = $newDescription;
		}
139a8045   Nathanaël Jourdane   autocleanup
854

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
855
856
		$dstFilePath = $dst_path . "/" . $newId . ".xml";
		if ($dom->save($dstFilePath) === false) {
139a8045   Nathanaël Jourdane   autocleanup
857
858
859
			return false;
		}

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
860
861
		chgrp($dstFilePath, APACHE_USER);
		chmod($dstFilePath, 0775);
139a8045   Nathanaël Jourdane   autocleanup
862
863
864
865

		return true;
	}

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
866
867
868
869
870
	/**
	 * Rename in resource
	 * @param $name
	 * @param $id
	 * @return bool
139a8045   Nathanaël Jourdane   autocleanup
871
	 */
139a8045   Nathanaël Jourdane   autocleanup
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
	protected function renameInResource($name, $id)
	{
		if (!file_exists(USERTTDIR . $id . '.xml')) {
			return false;
		}

		$this->objectDom->load(USERTTDIR . $id . '.xml');
		if (!($objToRename = $this->objectDom->getElementById($id))) {
			return false;
		}
		$objToRename->getElementsByTagName('name')->item(0)->nodeValue = $name;
		$this->objectDom->save(USERTTDIR . $id . '.xml');

		return true;
	}

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
888
889
890
891
	/**
	 * Delete parameter
	 * @param $id
	 */
139a8045   Nathanaël Jourdane   autocleanup
892
893
894
895
896
897
898
	protected function deleteParameter($id)
	{
		if (file_exists(USERTTDIR . $id . '.xml')) {
			unlink(USERTTDIR . $id . '.xml');
		}
	}

d8ec1623   Nathanaël Jourdane   fix phpcs warnings
899
900
901
902
903
	/**
	 * Rename only
	 * @param $p
	 * @return bool
	 */
139a8045   Nathanaël Jourdane   autocleanup
904
905
906
907
	protected function renameOnly($p)
	{
		//if (!($p->intervals)) return true;
		return false;
3ed056c4   Elena.Budnik   redmine #5278
908
	}
16035364   Benjamin Renard   First commit
909
}