Commit d8ec16238fd8c36b0daf77df7a6219cad1062ab2

Authored by Nathanaël Jourdane
1 parent 139a8045

fix phpcs warnings

Showing 1 changed file with 237 additions and 166 deletions   Show diff stats
php/classes/TimeTableMgr.php
@@ -7,28 +7,20 @@ @@ -7,28 +7,20 @@
7 * 7 *
8 */ 8 */
9 9
10 -function timeFormat($myString)  
11 -{  
12 - if (format == "Y z H i s") {  
13 - $tt = getdate(strtotime($myString));  
14 - return sprintf("%04d", $tt["year"]) . " "  
15 - . sprintf("%03d", $tt["yday"] + 1) . " "  
16 - . sprintf("%02d", $tt["hours"]) . " "  
17 - . sprintf("%02d", $tt["minutes"]) . " "  
18 - . sprintf("%02d", $tt["seconds"]);  
19 - }  
20 - return date(format, strtotime($myString));  
21 -}  
22 -  
23 class TimeTableMgr extends AmdaObjectMgr 10 class TimeTableMgr extends AmdaObjectMgr
24 { 11 {
25 - function __construct($user = null, $sharedObject = false) 12 + /**
  13 + * TimeTableMgr constructor.
  14 + * @param null $user The user
  15 + * @param bool $sharedObject Shared object
  16 + */
  17 + public function __construct($user = null, $sharedObject = false)
26 { 18 {
27 parent::__construct('Tt.xml'); 19 parent::__construct('Tt.xml');
28 $this->contentRootId = 'timeTable-treeRootNode'; 20 $this->contentRootId = 'timeTable-treeRootNode';
29 $this->contentRootTag = 'timetabList'; 21 $this->contentRootTag = 'timetabList';
30 - $this->attributes = array('name' => '', 'intervals' => '');  
31 - $this->optionalAttributes = array(); 22 + $this->attributes = ['name' => '', 'intervals' => ''];
  23 + $this->optionalAttributes = [];
32 $this->objTagName = 'timetab'; 24 $this->objTagName = 'timetab';
33 $this->id_prefix = 'tt_'; 25 $this->id_prefix = 'tt_';
34 26
@@ -40,7 +32,7 @@ class TimeTableMgr extends AmdaObjectMgr @@ -40,7 +32,7 @@ class TimeTableMgr extends AmdaObjectMgr
40 32
41 protected function createDom() 33 protected function createDom()
42 { 34 {
43 - $types = array('timetab' => 'timeTable', 'catalog' => 'catalog'); 35 + $types = ['timetab' => 'timeTable', 'catalog' => 'catalog'];
44 36
45 $rootElement = $this->contentDom->createElement('ws'); 37 $rootElement = $this->contentDom->createElement('ws');
46 38
@@ -55,12 +47,13 @@ class TimeTableMgr extends AmdaObjectMgr @@ -55,12 +47,13 @@ class TimeTableMgr extends AmdaObjectMgr
55 $this->contentDom->save($this->xmlName); 47 $this->contentDom->save($this->xmlName);
56 } 48 }
57 49
58 - /*  
59 - * rename Time Table in id.xml  
60 - */  
61 -  
62 -  
63 - function getObject($id, $nodeType) 50 + /**
  51 + * Get object
  52 + * @param $id
  53 + * @param $nodeType
  54 + * @return array
  55 + */
  56 + public function getObject($id, $nodeType = null)
64 { 57 {
65 if (substr($nodeType, 0, 6) == 'shared') { 58 if (substr($nodeType, 0, 6) == 'shared') {
66 //Shared object 59 //Shared object
@@ -71,11 +64,11 @@ class TimeTableMgr extends AmdaObjectMgr @@ -71,11 +64,11 @@ class TimeTableMgr extends AmdaObjectMgr
71 } 64 }
72 65
73 if (!file_exists($path)) { 66 if (!file_exists($path)) {
74 - return array('error' => NO_OBJECT_FILE); 67 + return ['error' => NO_OBJECT_FILE];
75 } 68 }
76 $this->objectDom->load($path); 69 $this->objectDom->load($path);
77 if (!($objToGet = $this->objectDom->getElementById($id))) { 70 if (!($objToGet = $this->objectDom->getElementById($id))) {
78 - return array('error' => NO_SUCH_ID); 71 + return ['error' => NO_SUCH_ID];
79 } 72 }
80 $attributesToReturn['id'] = $objToGet->getAttribute('xml:id'); 73 $attributesToReturn['id'] = $objToGet->getAttribute('xml:id');
81 $attributes = $objToGet->childNodes; 74 $attributes = $objToGet->childNodes;
@@ -98,12 +91,16 @@ class TimeTableMgr extends AmdaObjectMgr @@ -98,12 +91,16 @@ class TimeTableMgr extends AmdaObjectMgr
98 } 91 }
99 } 92 }
100 } 93 }
101 -  
102 $attributesToReturn['nbIntervals'] = $nbInt; 94 $attributesToReturn['nbIntervals'] = $nbInt;
103 95
104 return $attributesToReturn; 96 return $attributesToReturn;
105 } 97 }
106 98
  99 + /**
  100 + * Modify an object
  101 + * @param $p
  102 + * @return array
  103 + */
107 public function modifyObject($p) 104 public function modifyObject($p)
108 { 105 {
109 $folder = $this->getObjectFolder($p->id); 106 $folder = $this->getObjectFolder($p->id);
@@ -127,21 +124,23 @@ class TimeTableMgr extends AmdaObjectMgr @@ -127,21 +124,23 @@ class TimeTableMgr extends AmdaObjectMgr
127 if ($tmpFileExist) { 124 if ($tmpFileExist) {
128 unlink($ttFilePath . ".tmp"); 125 unlink($ttFilePath . ".tmp");
129 } 126 }
130 - return array('id' => $p->id, 'info' => $result['info']);  
131 - } catch (Exception $e) { 127 + return ['id' => $p->id, 'info' => $result['info']];
  128 + } catch (Exception $exception) {
132 //Restore TT file 129 //Restore TT file
133 if ($tmpFileExist) { 130 if ($tmpFileExist) {
134 copy($ttFilePath . ".tmp", $ttFilePath); 131 copy($ttFilePath . ".tmp", $ttFilePath);
135 unlink($ttFilePath . ".tmp"); 132 unlink($ttFilePath . ".tmp");
136 } 133 }
137 - return array('error' => $e->getMessage()); 134 + return ['error' => $exception->getMessage()];
138 } 135 }
139 } 136 }
140 137
141 - /*  
142 - * Check if difference is name and info only  
143 - */  
144 - 138 + /**
  139 + * Create an object
  140 + * @param $p
  141 + * @param $folder
  142 + * @return array
  143 + */
145 public function createObject($p, $folder) 144 public function createObject($p, $folder)
146 { 145 {
147 if ($p->leaf) { 146 if ($p->leaf) {
@@ -154,25 +153,26 @@ class TimeTableMgr extends AmdaObjectMgr @@ -154,25 +153,26 @@ class TimeTableMgr extends AmdaObjectMgr
154 $resultSaveInt = $cacheMgr->saveInTT($result['id'], "update", $p->cacheToken); 153 $resultSaveInt = $cacheMgr->saveInTT($result['id'], "update", $p->cacheToken);
155 if (!$resultSaveInt['success']) { 154 if (!$resultSaveInt['success']) {
156 if ($resultSaveInt['message']) { 155 if ($resultSaveInt['message']) {
157 - return array('error' => $resultSaveInt['message']); 156 + return ['error' => $resultSaveInt['message']];
158 } else { 157 } else {
159 - return array('error' => 'Unknown error during intervals save'); 158 + return ['error' => 'Unknown error during intervals save'];
160 } 159 }
161 } 160 }
162 } 161 }
163 return $result; 162 return $result;
164 - }  
165 - // else return $this->createFolder($p);  
166 - //TODO check if this is possible?  
167 - else {  
168 - return array('error' => 'createFolder should be called from RENAME'); 163 + } else {
  164 + return ['error' => 'createFolder should be called from RENAME'];
  165 + // return $this->createFolder($p);
  166 + // TODO check if this is possible?
169 } 167 }
170 } 168 }
171 169
172 - /*  
173 - * In case of catalogs  
174 - */  
175 - 170 + /**
  171 + * Create parameter (in case of catalogs)
  172 + * @param $p
  173 + * @param $folder
  174 + * @return array
  175 + */
176 protected function createParameter($p, $folder) 176 protected function createParameter($p, $folder)
177 { 177 {
178 if ($this->objectExistsByName($p->name)) { 178 if ($this->objectExistsByName($p->name)) {
@@ -183,11 +183,11 @@ class TimeTableMgr extends AmdaObjectMgr @@ -183,11 +183,11 @@ class TimeTableMgr extends AmdaObjectMgr
183 $this->id = $this->setId(); 183 $this->id = $this->setId();
184 $this->created = date('Y-m-d\TH:i:s'); 184 $this->created = date('Y-m-d\TH:i:s');
185 if (!$this->id) { 185 if (!$this->id) {
186 - return array('error' => ID_CREATION_ERROR); 186 + return ['error' => ID_CREATION_ERROR];
187 } 187 }
188 188
189 $this->resFileName = USERTTDIR . $this->id . '.xml'; 189 $this->resFileName = USERTTDIR . $this->id . '.xml';
190 - //TODO catalog root element = 'timetable' 190 + // TODO catalog root element = 'timetable'
191 $rootElement = $this->objectDom->createElement('timetable'); 191 $rootElement = $this->objectDom->createElement('timetable');
192 $rootElement->setAttribute('xml:id', $this->id); 192 $rootElement->setAttribute('xml:id', $this->id);
193 193
@@ -203,7 +203,6 @@ class TimeTableMgr extends AmdaObjectMgr @@ -203,7 +203,6 @@ class TimeTableMgr extends AmdaObjectMgr
203 if ($paramsElement) { 203 if ($paramsElement) {
204 $rootElement->appendChild($paramsElement); 204 $rootElement->appendChild($paramsElement);
205 } 205 }
206 -  
207 } else { 206 } else {
208 if ($key != 'intervals') { 207 if ($key != 'intervals') {
209 $rootElement->appendChild($this->objectDom->createElement($key, htmlspecialchars($value))); 208 $rootElement->appendChild($this->objectDom->createElement($key, htmlspecialchars($value)));
@@ -219,18 +218,28 @@ class TimeTableMgr extends AmdaObjectMgr @@ -219,18 +218,28 @@ class TimeTableMgr extends AmdaObjectMgr
219 $obj->name = $p->name; 218 $obj->name = $p->name;
220 $obj->intervals = $p->nbIntervals; 219 $obj->intervals = $p->nbIntervals;
221 $this->addToContent($obj, $folder); 220 $this->addToContent($obj, $folder);
222 - return array('id' => $this->id, 'created' => $this->created, 'info' => $obj->intervals . ' intervals'); 221 + // FIXME field created is undefined
  222 + return ['id' => $this->id, 'created' => $this->created, 'info' => $obj->intervals . ' intervals'];
223 } 223 }
224 224
225 -  
226 - /*  
227 - * Create Time Table 225 + /**
  226 + * Set parameter description
  227 + * TODO
  228 + * @param $param string The parameter
  229 + * @return DOMNode
228 */ 230 */
229 -  
230 protected function setParamDescription($param) 231 protected function setParamDescription($param)
231 { 232 {
  233 + return new DOMNode();
232 } 234 }
233 235
  236 + /**
  237 + * Get uploaded object
  238 + * @param $name
  239 + * @param $format
  240 + * @param bool $onlyDescription
  241 + * @return mixed
  242 + */
234 public function getUploadedObject($name, $format, $onlyDescription = false) 243 public function getUploadedObject($name, $format, $onlyDescription = false)
235 { 244 {
236 if (strpos($name, '.txt') !== false || strpos($name, '.asc') !== false || strpos($name, '.') == false) { 245 if (strpos($name, '.txt') !== false || strpos($name, '.asc') !== false || strpos($name, '.') == false) {
@@ -255,13 +264,13 @@ class TimeTableMgr extends AmdaObjectMgr @@ -255,13 +264,13 @@ class TimeTableMgr extends AmdaObjectMgr
255 } 264 }
256 265
257 if (!file_exists(USERTEMPDIR . $name . '.xml')) { 266 if (!file_exists(USERTEMPDIR . $name . '.xml')) {
258 - return array('error' => 'no such name'); 267 + return ['error' => 'no such name'];
259 } 268 }
260 269
261 $this->objectDom->load(USERTEMPDIR . $name . '.xml'); 270 $this->objectDom->load(USERTEMPDIR . $name . '.xml');
262 if (!($objToGet = $this->objectDom->getElementsByTagName('timetable')->item(0)) && 271 if (!($objToGet = $this->objectDom->getElementsByTagName('timetable')->item(0)) &&
263 !($objToGet = $this->objectDom->getElementsByTagName('TimeTable')->item(0))) { 272 !($objToGet = $this->objectDom->getElementsByTagName('TimeTable')->item(0))) {
264 - return array('error' => 'no time table'); 273 + return ['error' => 'no time table'];
265 } 274 }
266 275
267 $attributes = $objToGet->childNodes; 276 $attributes = $objToGet->childNodes;
@@ -269,28 +278,29 @@ class TimeTableMgr extends AmdaObjectMgr @@ -269,28 +278,29 @@ class TimeTableMgr extends AmdaObjectMgr
269 $attributesToReturn['objName'] = $name; 278 $attributesToReturn['objName'] = $name;
270 $attributesToReturn['objFormat'] = $format; 279 $attributesToReturn['objFormat'] = $format;
271 280
  281 + /** @var DOMElement $attribute */
272 foreach ($attributes as $attribute) { 282 foreach ($attributes as $attribute) {
273 if ($attribute->nodeType == XML_ELEMENT_NODE) { 283 if ($attribute->nodeType == XML_ELEMENT_NODE) {
274 if ($attribute->tagName == 'intervals') { 284 if ($attribute->tagName == 'intervals') {
275 $start = $attribute->getElementsByTagName('start')->item(0)->nodeValue; 285 $start = $attribute->getElementsByTagName('start')->item(0)->nodeValue;
276 $stop = $attribute->getElementsByTagName('stop')->item(0)->nodeValue; 286 $stop = $attribute->getElementsByTagName('stop')->item(0)->nodeValue;
277 if (!$onlyDescription) { 287 if (!$onlyDescription) {
278 - $attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop); 288 + $attributesToReturn['intervals'][] = ['start' => $start, 'stop' => $stop];
279 } 289 }
280 } else { 290 } else {
281 if ($attribute->tagName == 'Interval') { 291 if ($attribute->tagName == 'Interval') {
282 $start = $attribute->getElementsByTagName('Start')->item(0)->nodeValue; 292 $start = $attribute->getElementsByTagName('Start')->item(0)->nodeValue;
283 $stop = $attribute->getElementsByTagName('Stop')->item(0)->nodeValue; 293 $stop = $attribute->getElementsByTagName('Stop')->item(0)->nodeValue;
284 if (!$onlyDescription) { 294 if (!$onlyDescription) {
285 - $attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop); 295 + $attributesToReturn['intervals'][] = ['start' => $start, 'stop' => $stop];
286 } 296 }
287 } else { 297 } else {
288 switch (strtolower($attribute->tagName)) { 298 switch (strtolower($attribute->tagName)) {
289 - case 'created' : 299 + case 'created':
290 $attributesToReturn['created'] = $attribute->nodeValue; 300 $attributesToReturn['created'] = $attribute->nodeValue;
291 break; 301 break;
292 - case 'chain' :  
293 - case 'source' : 302 + case 'chain':
  303 + case 'source':
294 $attributesToReturn['description'] = $attribute->nodeValue; 304 $attributesToReturn['description'] = $attribute->nodeValue;
295 break; 305 break;
296 default: 306 default:
@@ -307,6 +317,12 @@ class TimeTableMgr extends AmdaObjectMgr @@ -307,6 +317,12 @@ class TimeTableMgr extends AmdaObjectMgr
307 * Uploaded text file => convert to array 317 * Uploaded text file => convert to array
308 */ 318 */
309 319
  320 + /**
  321 + * Convert text to AMDA attributes
  322 + * @param $tmp_file
  323 + * @param bool $onlyDescription
  324 + * @return mixed
  325 + */
310 protected function text2amda($tmp_file, $onlyDescription = false) 326 protected function text2amda($tmp_file, $onlyDescription = false)
311 { 327 {
312 $suffix = explode('.', basename($tmp_file)); 328 $suffix = explode('.', basename($tmp_file));
@@ -340,12 +356,12 @@ class TimeTableMgr extends AmdaObjectMgr @@ -340,12 +356,12 @@ class TimeTableMgr extends AmdaObjectMgr
340 $tempStop = $date[$dateLength]; 356 $tempStop = $date[$dateLength];
341 357
342 if ($dateLength > 1) { 358 if ($dateLength > 1) {
343 - for ($i = 1; $i < $dateLength; $i++) {  
344 - $tempStart .= $date[$i]; 359 + for ($iDate = 1; $iDate < $dateLength; $iDate++) {
  360 + $tempStart .= $date[$iDate];
345 } 361 }
346 362
347 - for ($i = $dateLength + 1; $i < $dateLength * 2; $i++) {  
348 - $tempStop .= $date[$i]; 363 + for ($iDate = $dateLength + 1; $iDate < $dateLength * 2; $iDate++) {
  364 + $tempStop .= $date[$iDate];
349 } 365 }
350 } 366 }
351 367
@@ -353,11 +369,11 @@ class TimeTableMgr extends AmdaObjectMgr @@ -353,11 +369,11 @@ class TimeTableMgr extends AmdaObjectMgr
353 $stopDate = date('Y-m-d\TH:i:s', strtotime($tempStop)); 369 $stopDate = date('Y-m-d\TH:i:s', strtotime($tempStop));
354 370
355 if (!$onlyDescription) { 371 if (!$onlyDescription) {
356 - $attributesToReturn['intervals'][] = array('start' => $startDate, 'stop' => $stopDate); 372 + $attributesToReturn['intervals'][] = ['start' => $startDate, 'stop' => $stopDate];
357 } 373 }
358 } else { 374 } else {
359 if (!$onlyDescription) { 375 if (!$onlyDescription) {
360 - $attributesToReturn['intervals'][] = array('start' => trim($date[0]), 'stop' => trim($date[1])); 376 + $attributesToReturn['intervals'][] = ['start' => trim($date[0]), 'stop' => trim($date[1])];
361 } 377 }
362 } 378 }
363 } 379 }
@@ -373,9 +389,12 @@ class TimeTableMgr extends AmdaObjectMgr @@ -373,9 +389,12 @@ class TimeTableMgr extends AmdaObjectMgr
373 return $attributesToReturn; 389 return $attributesToReturn;
374 } 390 }
375 391
376 - /*  
377 - * Uploaded vot TT => convert to array  
378 - */ 392 + /**
  393 + * Convert VOTable time table to AMDA attributes
  394 + * @param $tmp_file
  395 + * @param bool $onlyDescription
  396 + * @return mixed
  397 + */
379 protected function vot2amda($tmp_file, $onlyDescription = false) 398 protected function vot2amda($tmp_file, $onlyDescription = false)
380 { 399 {
381 // Load Time table 400 // Load Time table
@@ -383,19 +402,22 @@ class TimeTableMgr extends AmdaObjectMgr @@ -383,19 +402,22 @@ class TimeTableMgr extends AmdaObjectMgr
383 $objToGet = $this->objectDom->getElementsByTagName('TABLEDATA')->item(0); 402 $objToGet = $this->objectDom->getElementsByTagName('TABLEDATA')->item(0);
384 $attributesToReturn['name'] = $tmp_file; 403 $attributesToReturn['name'] = $tmp_file;
385 $attributes = $objToGet->childNodes; 404 $attributes = $objToGet->childNodes;
  405 +
  406 + /** @var DOMElement $attribute */
386 foreach ($attributes as $attribute) { 407 foreach ($attributes as $attribute) {
387 if ($attribute->tagName == 'TR') { 408 if ($attribute->tagName == 'TR') {
388 $start = $attribute->getElementsByTagName('TD')->item(0)->nodeValue; 409 $start = $attribute->getElementsByTagName('TD')->item(0)->nodeValue;
389 $stop = $attribute->getElementsByTagName('TD')->item(1)->nodeValue; 410 $stop = $attribute->getElementsByTagName('TD')->item(1)->nodeValue;
390 if (!$onlyDescription) { 411 if (!$onlyDescription) {
391 - $attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop); 412 + $attributesToReturn['intervals'][] = ['start' => $start, 'stop' => $stop];
392 } 413 }
393 } 414 }
394 } 415 }
395 $suffix = explode('.', basename($tmp_file)); 416 $suffix = explode('.', basename($tmp_file));
396 $attributesToReturn['name'] = basename($tmp_file, '.' . $suffix[1]); 417 $attributesToReturn['name'] = basename($tmp_file, '.' . $suffix[1]);
397 $attributesToReturn['created'] = date('Y-m-d') . "T" . date('H:i:s'); 418 $attributesToReturn['created'] = date('Y-m-d') . "T" . date('H:i:s');
398 - $attributesToReturn['description'] = htmlspecialchars($this->objectDom->getElementsByTagName('DESCRIPTION')->item(0)->nodeValue); 419 + $description = $this->objectDom->getElementsByTagName('DESCRIPTION')->item(0)->nodeValue;
  420 + $attributesToReturn['description'] = htmlspecialchars($description);
399 return ($attributesToReturn); 421 return ($attributesToReturn);
400 } 422 }
401 423
@@ -410,14 +432,14 @@ class TimeTableMgr extends AmdaObjectMgr @@ -410,14 +432,14 @@ class TimeTableMgr extends AmdaObjectMgr
410 { 432 {
411 $filePath = USERWORKINGDIR . $folderId . '/' . $name . '.xml'; 433 $filePath = USERWORKINGDIR . $folderId . '/' . $name . '.xml';
412 if (!file_exists($filePath)) { 434 if (!file_exists($filePath)) {
413 - return array('error' => 'Cannot find result file'); 435 + return ['error' => 'Cannot find result file'];
414 } 436 }
415 437
416 $dom = new DomDocument('1.0'); 438 $dom = new DomDocument('1.0');
417 $dom->formatOutput = true; 439 $dom->formatOutput = true;
418 440
419 if (!$dom->load($filePath)) { 441 if (!$dom->load($filePath)) {
420 - return array('error' => 'Cannot load result file'); 442 + return ['error' => 'Cannot load result file'];
421 } 443 }
422 444
423 $descNodes = $dom->getElementsByTagName('description'); 445 $descNodes = $dom->getElementsByTagName('description');
@@ -440,25 +462,32 @@ class TimeTableMgr extends AmdaObjectMgr @@ -440,25 +462,32 @@ class TimeTableMgr extends AmdaObjectMgr
440 462
441 if (!$onlyDescription) { 463 if (!$onlyDescription) {
442 $intNodes = $dom->getElementsByTagName('intervals'); 464 $intNodes = $dom->getElementsByTagName('intervals');
  465 +
  466 + /** @var DOMElement $intNode */
443 foreach ($intNodes as $intNode) { 467 foreach ($intNodes as $intNode) {
444 $startNodes = $intNode->getElementsByTagName('start'); 468 $startNodes = $intNode->getElementsByTagName('start');
445 if ($startNodes->length <= 0) { 469 if ($startNodes->length <= 0) {
446 - return array('error' => 'Error detected in result file'); 470 + return ['error' => 'Error detected in result file'];
447 } 471 }
448 $stopNodes = $intNode->getElementsByTagName('stop'); 472 $stopNodes = $intNode->getElementsByTagName('stop');
449 if ($stopNodes->length <= 0) { 473 if ($stopNodes->length <= 0) {
450 - return array('error' => 'Error detected in result file'); 474 + return ['error' => 'Error detected in result file'];
451 } 475 }
452 - $attributesToReturn['intervals'][] = array( 476 + $attributesToReturn['intervals'][] = [
453 'start' => $startNodes->item(0)->nodeValue, 477 'start' => $startNodes->item(0)->nodeValue,
454 'stop' => $stopNodes->item(0)->nodeValue 478 'stop' => $stopNodes->item(0)->nodeValue
455 - ); 479 + ];
456 } 480 }
457 } 481 }
458 482
459 return $attributesToReturn; 483 return $attributesToReturn;
460 } 484 }
461 485
  486 + /**
  487 + * Merge time tables
  488 + * @param $obj
  489 + * @return array
  490 + */
462 public function merge($obj) 491 public function merge($obj)
463 { 492 {
464 /** 493 /**
@@ -468,47 +497,45 @@ class TimeTableMgr extends AmdaObjectMgr @@ -468,47 +497,45 @@ class TimeTableMgr extends AmdaObjectMgr
468 */ 497 */
469 498
470 $intervals = 0; 499 $intervals = 0;
471 -  
472 - for ($i = 0; $i < count($obj->ids); $i++) {  
473 - $table[$i] = $this->loadIntervalsFromTT($obj->ids[$i]);  
474 - for ($j = 0; $j < count($table[$i]['intervals']); $j++) {  
475 - $interval[$i][$j][0] = $table[$i]['intervals'][$j]['start'];  
476 - $interval[$i][$j][1] = $table[$i]['intervals'][$j]['stop']; 500 + for ($iId = 0; $iId < count($obj->ids); $iId++) {
  501 + $table[$iId] = $this->loadIntervalsFromTT($obj->ids[$iId]);
  502 + for ($jId = 0; $jId < count($table[$iId]['intervals']); $jId++) {
  503 + $interval[$iId][$jId][0] = $table[$iId]['intervals'][$jId]['start'];
  504 + $interval[$iId][$jId][1] = $table[$iId]['intervals'][$jId]['stop'];
477 } 505 }
478 - $intervals += count($interval[$i]); 506 + $intervals += count($interval[$iId]);
479 } 507 }
480 if ($intervals > 10000) { 508 if ($intervals > 10000) {
481 set_time_limit(1800); 509 set_time_limit(1800);
482 } 510 }
483 511
484 - $final = array();  
485 - for ($i = 0; $i < count($obj->ids); $i++) {  
486 - $final = array_merge($final, $interval[$i]); 512 + $final = [];
  513 + for ($iId = 0; $iId < count($obj->ids); $iId++) {
  514 + $final = array_merge($final, $interval[$iId]);
487 } 515 }
488 sort($final); 516 sort($final);
489 517
490 // Algorithm of union 518 // Algorithm of union
491 $line = 0; 519 $line = 0;
492 - $i = 0;  
493 - $a = $final[$i][0];  
494 - while ($i < count($final) - 1) {  
495 - if ($final[$i + 1][1] <= $final[$i][1]) {  
496 - array_splice($final, $i + 1, 1); 520 + $iId = 0;
  521 + $val = $final[$iId][0];
  522 + while ($iId < count($final) - 1) {
  523 + if ($final[$iId + 1][1] <= $final[$iId][1]) {
  524 + array_splice($final, $iId + 1, 1);
497 } else { 525 } else {
498 - if (($final[$i + 1][0] <= $final[$i][1]) && ($final[$i + 1][1] >= $final[$i][1])) {  
499 - $i++; 526 + if (($final[$iId + 1][0] <= $final[$iId][1]) && ($final[$iId + 1][1] >= $final[$iId][1])) {
  527 + $iId++;
500 } else { 528 } else {
501 - $start[$line] = $a;  
502 - $stop[$line] = $final[$i][1];  
503 - $i++; 529 + $start[$line] = $val;
  530 + $stop[$line] = $final[$iId][1];
  531 + $iId++;
504 $line++; 532 $line++;
505 - $a = $final[$i][0]; 533 + $val = $final[$iId][0];
506 } 534 }
507 } 535 }
508 } 536 }
509 - $start[$line] = $a;  
510 - $stop[$line] = $final[$i][1];  
511 - $line++; 537 + $start[$line] = $val;
  538 + $stop[$line] = $final[$iId][1];
512 539
513 $objTT = new stdClass(); 540 $objTT = new stdClass();
514 $objTT->name = $obj->name; 541 $objTT->name = $obj->name;
@@ -516,28 +543,34 @@ class TimeTableMgr extends AmdaObjectMgr @@ -516,28 +543,34 @@ class TimeTableMgr extends AmdaObjectMgr
516 $objTT->leaf = true; 543 $objTT->leaf = true;
517 $objTT->created = null; 544 $objTT->created = null;
518 $objTT->history = $obj->history; 545 $objTT->history = $obj->history;
519 - for ($i = 0; $i < count($start); $i++) { 546 + for ($iId = 0; $iId < count($start); $iId++) {
520 $inter = new stdClass(); 547 $inter = new stdClass();
521 - $inter->start = $start[$i];  
522 - $inter->stop = $stop[$i]; 548 + $inter->start = $start[$iId];
  549 + $inter->stop = $stop[$iId];
523 $objTT->intervals[] = $inter; 550 $objTT->intervals[] = $inter;
524 } 551 }
525 $objTT->nbIntervals = count($start); 552 $objTT->nbIntervals = count($start);
526 $this->objectDom = new DomDocument('1.0'); 553 $this->objectDom = new DomDocument('1.0');
527 $this->objectDom->formatOutput = true; 554 $this->objectDom->formatOutput = true;
528 555
529 - $res = $this->createParameter($objTT, $folder); 556 + $res = $this->createParameter($objTT, $folder); // FIXME $folder is undefined
530 if ($res['error']) { 557 if ($res['error']) {
531 return $res; 558 return $res;
532 } 559 }
533 560
534 $this->saveIntervals($res['id'], $objTT->intervals, 'merge'); 561 $this->saveIntervals($res['id'], $objTT->intervals, 'merge');
535 -  
536 return $res; 562 return $res;
537 -  
538 } 563 }
539 564
540 - public function loadIntervalsFromTT($id, $typeTT, $start = null, $limit = null) 565 + /**
  566 + * Load intervals from time table
  567 + * @param $id
  568 + * @param $typeTT
  569 + * @param null $start
  570 + * @param null $limit
  571 + * @return array
  572 + */
  573 + public function loadIntervalsFromTT($id, $typeTT = '', $start = null, $limit = null)
541 { 574 {
542 if ($typeTT == 'sharedtimeTable') { 575 if ($typeTT == 'sharedtimeTable') {
543 //Shared object 576 //Shared object
@@ -549,59 +582,68 @@ class TimeTableMgr extends AmdaObjectMgr @@ -549,59 +582,68 @@ class TimeTableMgr extends AmdaObjectMgr
549 582
550 //load intervals from TT id 583 //load intervals from TT id
551 if (!file_exists($path)) { 584 if (!file_exists($path)) {
552 - return array('success' => false, 'message' => "Cannot find TT file " . $id); 585 + return ['success' => false, 'message' => "Cannot find TT file " . $id];
553 } 586 }
554 587
555 $this->objectDom->load($path); 588 $this->objectDom->load($path);
556 if (!($objToGet = $this->objectDom->getElementById($id))) { 589 if (!($objToGet = $this->objectDom->getElementById($id))) {
557 - return array('success' => false, 'message' => NO_SUCH_ID . " " . $id); 590 + return ['success' => false, 'message' => NO_SUCH_ID . " " . $id];
558 } 591 }
559 592
560 $xpath = new DOMXPath($this->objectDom); 593 $xpath = new DOMXPath($this->objectDom);
561 $intervals = $xpath->query('//intervals'); 594 $intervals = $xpath->query('//intervals');
562 595
563 - $result = array(); 596 + $result = [];
564 597
565 if (!isset($start) || !isset($limit)) { 598 if (!isset($start) || !isset($limit)) {
  599 +
  600 + /** @var DOMElement $interval */
566 foreach ($intervals as $interval) { 601 foreach ($intervals as $interval) {
567 $startTime = $interval->getElementsByTagName('start')->item(0)->nodeValue; 602 $startTime = $interval->getElementsByTagName('start')->item(0)->nodeValue;
568 $stopTime = $interval->getElementsByTagName('stop')->item(0)->nodeValue; 603 $stopTime = $interval->getElementsByTagName('stop')->item(0)->nodeValue;
569 - array_push($result, array('start' => $startTime, 'stop' => $stopTime)); 604 + array_push($result, ['start' => $startTime, 'stop' => $stopTime]);
570 } 605 }
571 } else { 606 } else {
572 - for ($i = 0; $i < $limit; ++$i) {  
573 - if ($start + $i >= $intervals->length) { 607 + for ($iInt = 0; $iInt < $limit; ++$iInt) {
  608 + if ($start + $iInt >= $intervals->length) {
574 break; 609 break;
575 } 610 }
576 - $startTime = $intervals->item($start + $i)->getElementsByTagName('start')->item(0)->nodeValue;  
577 - $stopTime = $intervals->item($start + $i)->getElementsByTagName('stop')->item(0)->nodeValue;  
578 - array_push($result, array('start' => $startTime, 'stop' => $stopTime)); 611 + $startTime = $intervals->item($start + $iInt)->getElementsByTagName('start')->item(0)->nodeValue;
  612 + $stopTime = $intervals->item($start + $iInt)->getElementsByTagName('stop')->item(0)->nodeValue;
  613 + array_push($result, ['start' => $startTime, 'stop' => $stopTime]);
579 } 614 }
580 } 615 }
581 616
582 - return array( 617 + return [
583 'totalCount' => $intervals->length, 618 'totalCount' => $intervals->length,
584 'intervals' => $result, 619 'intervals' => $result,
585 'start' => isset($start) ? $start : 0, 620 'start' => isset($start) ? $start : 0,
586 'limit' => isset($limit) ? $limit : 0, 621 'limit' => isset($limit) ? $limit : 0,
587 'success' => true 622 'success' => true
588 - ); 623 + ];
589 } 624 }
590 625
  626 + /**
  627 + * Save intervals
  628 + * @param $id
  629 + * @param $intervals
  630 + * @param $action
  631 + * @return array
  632 + */
591 public function saveIntervals($id, $intervals, $action) 633 public function saveIntervals($id, $intervals, $action)
592 { 634 {
593 if (substr($id, 0, 6) == 'shared') { 635 if (substr($id, 0, 6) == 'shared') {
594 - return array('success' => false, 'message' => "Cannot save shared TimeTable"); 636 + return ['success' => false, 'message' => "Cannot save shared TimeTable"];
595 } else { 637 } else {
596 $path = USERTTDIR . $id . '.xml'; 638 $path = USERTTDIR . $id . '.xml';
597 } 639 }
598 if (!file_exists($path)) { 640 if (!file_exists($path)) {
599 - return array('success' => false, 'message' => "Cannot find TT file " . $id); 641 + return ['success' => false, 'message' => "Cannot find TT file " . $id];
600 } 642 }
601 $this->objectDom->load($path); 643 $this->objectDom->load($path);
602 644
603 if (!($objToGet = $this->objectDom->getElementById($id))) { 645 if (!($objToGet = $this->objectDom->getElementById($id))) {
604 - return array('success' => false, 'message' => NO_SUCH_ID . " " . $id); 646 + return ['success' => false, 'message' => NO_SUCH_ID . " " . $id];
605 } 647 }
606 648
607 //remove old intervals 649 //remove old intervals
@@ -631,9 +673,14 @@ class TimeTableMgr extends AmdaObjectMgr @@ -631,9 +673,14 @@ class TimeTableMgr extends AmdaObjectMgr
631 673
632 unset($this->objectDom); 674 unset($this->objectDom);
633 675
634 - return array('success' => true, 'action' => $action, 'nbIntervals' => count($intervals)); 676 + return ['success' => true, 'action' => $action, 'nbIntervals' => count($intervals)];
635 } 677 }
636 678
  679 + /**
  680 + * Create interval element
  681 + * @param $interval
  682 + * @return DOMElement
  683 + */
637 protected function createIntervalElement($interval) 684 protected function createIntervalElement($interval)
638 { 685 {
639 $newInterval = $this->objectDom->createElement('intervals'); 686 $newInterval = $this->objectDom->createElement('intervals');
@@ -642,16 +689,21 @@ class TimeTableMgr extends AmdaObjectMgr @@ -642,16 +689,21 @@ class TimeTableMgr extends AmdaObjectMgr
642 return $newInterval; 689 return $newInterval;
643 } 690 }
644 691
  692 + /**
  693 + * Intersect time tables
  694 + * @param $obj
  695 + * @return array|string
  696 + */
645 public function intersect($obj) 697 public function intersect($obj)
646 { 698 {
647 -  
648 - for ($i = 0; $i < count($obj->ids); $i++) {  
649 - $table[$i] = $this->loadIntervalsFromTT($obj->ids[$i]);  
650 - for ($j = 0; $j < count($table[$i]['intervals']); $j++) {  
651 - $interval[$i][$j][0] = $table[$i]['intervals'][$j]['start'];  
652 - $interval[$i][$j][1] = $table[$i]['intervals'][$j]['stop']; 699 + $intervals = 0;
  700 + for ($iId = 0; $iId < count($obj->ids); $iId++) {
  701 + $table[$iId] = $this->loadIntervalsFromTT($obj->ids[$iId]);
  702 + for ($jId = 0; $jId < count($table[$iId]['intervals']); $jId++) {
  703 + $interval[$iId][$jId][0] = $table[$iId]['intervals'][$jId]['start'];
  704 + $interval[$iId][$jId][1] = $table[$iId]['intervals'][$jId]['stop'];
653 } 705 }
654 - $intervals += count($interval[$i]); 706 + $intervals += count($interval[$iId]);
655 } 707 }
656 if ($intervals > 10000) { 708 if ($intervals > 10000) {
657 set_time_limit(1800); 709 set_time_limit(1800);
@@ -661,12 +713,12 @@ class TimeTableMgr extends AmdaObjectMgr @@ -661,12 +713,12 @@ class TimeTableMgr extends AmdaObjectMgr
661 sort($interval[0]); 713 sort($interval[0]);
662 sort($interval[1]); 714 sort($interval[1]);
663 715
664 - $i = 0;  
665 - $j = 0; 716 + $iId = 0;
  717 + $jId = 0;
666 $line = 0; 718 $line = 0;
667 719
668 - while (($i < count($interval[0])) && ($j < count($interval[1]))) {  
669 - $inter = $this->call_intersection($interval[0][$i], $interval[1][$j]); 720 + while (($iId < count($interval[0])) && ($jId < count($interval[1]))) {
  721 + $inter = $this->callIntersection($interval[0][$iId], $interval[1][$jId]);
670 722
671 if ($inter[0][0] != 0 && $inter[0][1] != 0) { 723 if ($inter[0][0] != 0 && $inter[0][1] != 0) {
672 $start[$line] = $inter[0][0]; 724 $start[$line] = $inter[0][0];
@@ -674,10 +726,10 @@ class TimeTableMgr extends AmdaObjectMgr @@ -674,10 +726,10 @@ class TimeTableMgr extends AmdaObjectMgr
674 $line++; 726 $line++;
675 } 727 }
676 728
677 - if ($interval[0][$i][1] < $interval[1][$j][1]) {  
678 - $i++; 729 + if ($interval[0][$iId][1] < $interval[1][$jId][1]) {
  730 + $iId++;
679 } else { 731 } else {
680 - $j++; 732 + $jId++;
681 } 733 }
682 } 734 }
683 735
@@ -685,15 +737,15 @@ class TimeTableMgr extends AmdaObjectMgr @@ -685,15 +737,15 @@ class TimeTableMgr extends AmdaObjectMgr
685 if ($line == 0) { 737 if ($line == 0) {
686 $result = "empty"; 738 $result = "empty";
687 } else { 739 } else {
688 - $objTT->name = $obj->name; 740 + $objTT->name = $obj->name; // FIXME $objTT is undefined
689 $objTT->nodeType = 'timeTable'; 741 $objTT->nodeType = 'timeTable';
690 $objTT->leaf = true; 742 $objTT->leaf = true;
691 $objTT->created = null; 743 $objTT->created = null;
692 $objTT->history = $obj->history; 744 $objTT->history = $obj->history;
693 - for ($i = 0; $i < count($start); $i++) { 745 + for ($iId = 0; $iId < count($start); $iId++) {
694 $inter = new stdClass(); 746 $inter = new stdClass();
695 - $inter->start = $start[$i];  
696 - $inter->stop = $stop[$i]; 747 + $inter->start = $start[$iId];
  748 + $inter->stop = $stop[$iId];
697 $objTT->intervals[] = $inter; 749 $objTT->intervals[] = $inter;
698 } 750 }
699 $objTT->nbIntervals = count($start); 751 $objTT->nbIntervals = count($start);
@@ -703,32 +755,37 @@ class TimeTableMgr extends AmdaObjectMgr @@ -703,32 +755,37 @@ class TimeTableMgr extends AmdaObjectMgr
703 } else { 755 } else {
704 $this->objectDom = new DomDocument('1.0'); 756 $this->objectDom = new DomDocument('1.0');
705 $this->objectDom->formatOutput = true; 757 $this->objectDom->formatOutput = true;
706 - $result = $this->createObject($objTT, $folder); 758 + $result = $this->createObject($objTT, $folder); // FIXME $folder is undefined
707 759
708 if (!isset($result['error'])) { 760 if (!isset($result['error'])) {
709 $this->saveIntervals($result['id'], $objTT->intervals, 'intersect'); 761 $this->saveIntervals($result['id'], $objTT->intervals, 'intersect');
710 } 762 }
711 -  
712 } 763 }
713 } 764 }
714 return $result; 765 return $result;
715 } 766 }
716 767
717 - protected function call_intersection($fst, $snd) 768 + /**
  769 + * Call intersection
  770 + * @param $fst
  771 + * @param $snd
  772 + * @return array
  773 + */
  774 + protected function callIntersection($fst, $snd)
718 { 775 {
719 $inf = ($fst[0] > $snd[0]) ? $fst[0] : $snd[0]; 776 $inf = ($fst[0] > $snd[0]) ? $fst[0] : $snd[0];
720 $sup = ($fst[1] < $snd[1]) ? $fst[1] : $snd[1]; 777 $sup = ($fst[1] < $snd[1]) ? $fst[1] : $snd[1];
721 - if ($inf >= $sup) {  
722 - $inter[] = array(0, 0);  
723 - } else {  
724 - $inter[] = array($inf, $sup);  
725 - } 778 + $inter[] = ($inf >= $sup) ? [0, 0] : [$inf, $sup];
726 return $inter; 779 return $inter;
727 } 780 }
728 781
729 - //TODO getObject only!!!! => change DD_Search output  
730 -  
731 - function validNameObject($p) 782 + /**
  783 + * Valid name object
  784 + * TODO getObject only!!!! => change DD_Search output
  785 + * @param $p
  786 + * @return array
  787 + */
  788 + public function validNameObject($p)
732 { 789 {
733 // overwritten 790 // overwritten
734 $res = parent::validNameObject($p); 791 $res = parent::validNameObject($p);
@@ -739,16 +796,21 @@ class TimeTableMgr extends AmdaObjectMgr @@ -739,16 +796,21 @@ class TimeTableMgr extends AmdaObjectMgr
739 796
740 //no space 797 //no space
741 if (strpos($p->name, ' ') === false) { 798 if (strpos($p->name, ' ') === false) {
742 - return array('valid' => true); 799 + return ['valid' => true];
743 } 800 }
744 801
745 - return array('valid' => false, 'error' => 'Space character is not allowed'); 802 + return ['valid' => false, 'error' => 'Space character is not allowed'];
746 } 803 }
747 804
748 - /*  
749 - * merge time tables  
750 - */  
751 - 805 + /**
  806 + * Copy time table
  807 + * @param $src_path
  808 + * @param $dst_path
  809 + * @param $newId
  810 + * @param $newName
  811 + * @param null $newDescription
  812 + * @return bool
  813 + */
752 public function copyTT($src_path, $dst_path, $newId, $newName, $newDescription = null) 814 public function copyTT($src_path, $dst_path, $newId, $newName, $newDescription = null)
753 { 815 {
754 if (!file_exists($src_path)) { 816 if (!file_exists($src_path)) {
@@ -800,21 +862,23 @@ class TimeTableMgr extends AmdaObjectMgr @@ -800,21 +862,23 @@ class TimeTableMgr extends AmdaObjectMgr
800 $descriptionNode->nodeValue = $newDescription; 862 $descriptionNode->nodeValue = $newDescription;
801 } 863 }
802 864
803 - $dst_file_path = $dst_path . "/" . $newId . ".xml";  
804 - if ($dom->save($dst_file_path) === false) { 865 + $dstFilePath = $dst_path . "/" . $newId . ".xml";
  866 + if ($dom->save($dstFilePath) === false) {
805 return false; 867 return false;
806 } 868 }
807 869
808 - chgrp($dst_file_path, APACHE_USER);  
809 - chmod($dst_file_path, 0775); 870 + chgrp($dstFilePath, APACHE_USER);
  871 + chmod($dstFilePath, 0775);
810 872
811 return true; 873 return true;
812 } 874 }
813 875
814 - /*  
815 - * intersect time tables 876 + /**
  877 + * Rename in resource
  878 + * @param $name
  879 + * @param $id
  880 + * @return bool
816 */ 881 */
817 -  
818 protected function renameInResource($name, $id) 882 protected function renameInResource($name, $id)
819 { 883 {
820 if (!file_exists(USERTTDIR . $id . '.xml')) { 884 if (!file_exists(USERTTDIR . $id . '.xml')) {
@@ -831,6 +895,10 @@ class TimeTableMgr extends AmdaObjectMgr @@ -831,6 +895,10 @@ class TimeTableMgr extends AmdaObjectMgr
831 return true; 895 return true;
832 } 896 }
833 897
  898 + /**
  899 + * Delete parameter
  900 + * @param $id
  901 + */
834 protected function deleteParameter($id) 902 protected function deleteParameter($id)
835 { 903 {
836 if (file_exists(USERTTDIR . $id . '.xml')) { 904 if (file_exists(USERTTDIR . $id . '.xml')) {
@@ -838,11 +906,14 @@ class TimeTableMgr extends AmdaObjectMgr @@ -838,11 +906,14 @@ class TimeTableMgr extends AmdaObjectMgr
838 } 906 }
839 } 907 }
840 908
  909 + /**
  910 + * Rename only
  911 + * @param $p
  912 + * @return bool
  913 + */
841 protected function renameOnly($p) 914 protected function renameOnly($p)
842 { 915 {
843 //if (!($p->intervals)) return true; 916 //if (!($p->intervals)) return true;
844 return false; 917 return false;
845 } 918 }
846 } 919 }
847 -  
848 -?>