Commit 139a8045726ec6e43caa7adcf090567c81a7f345

Authored by Nathanaël Jourdane
1 parent 5f8bd1f5

autocleanup

Showing 1 changed file with 627 additions and 555 deletions   Show diff stats
php/classes/TimeTableMgr.php
@@ -7,22 +7,22 @@ @@ -7,22 +7,22 @@
7 * 7 *
8 */ 8 */
9 9
10 -function timeFormat($myString) 10 +function timeFormat($myString)
11 { 11 {
12 - if (format == "Y z H i s") { 12 + if (format == "Y z H i s") {
13 $tt = getdate(strtotime($myString)); 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"]); 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 } 19 }
20 - return date(format,strtotime($myString)); 20 + return date(format, strtotime($myString));
21 } 21 }
22 22
23 class TimeTableMgr extends AmdaObjectMgr 23 class TimeTableMgr extends AmdaObjectMgr
24 -{  
25 - function __construct($user = NULL, $sharedObject = FALSE) 24 +{
  25 + function __construct($user = null, $sharedObject = false)
26 { 26 {
27 parent::__construct('Tt.xml'); 27 parent::__construct('Tt.xml');
28 $this->contentRootId = 'timeTable-treeRootNode'; 28 $this->contentRootId = 'timeTable-treeRootNode';
@@ -33,20 +33,20 @@ class TimeTableMgr extends AmdaObjectMgr @@ -33,20 +33,20 @@ class TimeTableMgr extends AmdaObjectMgr
33 $this->id_prefix = 'tt_'; 33 $this->id_prefix = 'tt_';
34 34
35 if (!$sharedObject && !file_exists($this->xmlName)) { 35 if (!$sharedObject && !file_exists($this->xmlName)) {
36 - $this->createDom();  
37 - $this->xp = new domxpath($this->contentDom); 36 + $this->createDom();
  37 + $this->xp = new domxpath($this->contentDom);
38 } 38 }
39 } 39 }
40 -  
41 - protected function createDom() 40 +
  41 + protected function createDom()
42 { 42 {
43 $types = array('timetab' => 'timeTable', 'catalog' => 'catalog'); 43 $types = array('timetab' => 'timeTable', 'catalog' => 'catalog');
44 44
45 $rootElement = $this->contentDom->createElement('ws'); 45 $rootElement = $this->contentDom->createElement('ws');
46 - 46 +
47 foreach ($types as $key => $value) { 47 foreach ($types as $key => $value) {
48 - $contentId = $value.'-treeRootNode';  
49 - $contentTag = $key.'List'; 48 + $contentId = $value . '-treeRootNode';
  49 + $contentTag = $key . 'List';
50 $typeElement = $this->contentDom->createElement($contentTag); 50 $typeElement = $this->contentDom->createElement($contentTag);
51 $typeElement->setAttribute('xml:id', $contentId); 51 $typeElement->setAttribute('xml:id', $contentId);
52 $rootElement->appendChild($typeElement); 52 $rootElement->appendChild($typeElement);
@@ -58,360 +58,557 @@ class TimeTableMgr extends AmdaObjectMgr @@ -58,360 +58,557 @@ class TimeTableMgr extends AmdaObjectMgr
58 /* 58 /*
59 * rename Time Table in id.xml 59 * rename Time Table in id.xml
60 */ 60 */
61 - protected function renameInResource($name, $id) 61 +
  62 +
  63 + function getObject($id, $nodeType)
62 { 64 {
63 - if (!file_exists(USERTTDIR.$id.'.xml')) return false; 65 + if (substr($nodeType, 0, 6) == 'shared') {
  66 + //Shared object
  67 + $sharedObjMgr = new SharedObjectsMgr();
  68 + $path = $sharedObjMgr->getDataFilePath(str_replace('shared', '', $nodeType), $id);
  69 + } else {
  70 + $path = USERTTDIR . $id . '.xml';
  71 + }
64 72
65 - $this->objectDom -> load(USERTTDIR.$id.'.xml');  
66 - if (!($objToRename = $this->objectDom->getElementById($id))) return false;  
67 - $objToRename -> getElementsByTagName('name')->item(0)->nodeValue = $name;  
68 - $this->objectDom ->save(USERTTDIR.$id.'.xml');  
69 -  
70 - return true; 73 + if (!file_exists($path)) {
  74 + return array('error' => NO_OBJECT_FILE);
  75 + }
  76 + $this->objectDom->load($path);
  77 + if (!($objToGet = $this->objectDom->getElementById($id))) {
  78 + return array('error' => NO_SUCH_ID);
  79 + }
  80 + $attributesToReturn['id'] = $objToGet->getAttribute('xml:id');
  81 + $attributes = $objToGet->childNodes;
  82 +
  83 + $nbInt = 0;
  84 + foreach ($attributes as $attribute) {
  85 + if ($attribute->nodeType == XML_ELEMENT_NODE) {
  86 + /*if ($attribute->tagName == 'intervals') {
  87 + $start = $attribute -> getElementsByTagName('start')->item(0) -> nodeValue;
  88 + $stop = $attribute -> getElementsByTagName('stop')->item(0) -> nodeValue;
  89 + $attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop);
  90 + }
  91 + else
  92 + $attributesToReturn[$attribute->tagName] = $attribute->nodeValue;*/
  93 + //BRE - load all except intervals - Intervals will be loaded later with 'loadIntervalsFromTT' function
  94 + if ($attribute->tagName != 'intervals') {
  95 + $attributesToReturn[$attribute->tagName] = $attribute->nodeValue;
  96 + } else {
  97 + $nbInt++;
  98 + }
  99 + }
  100 + }
  101 +
  102 + $attributesToReturn['nbIntervals'] = $nbInt;
  103 +
  104 + return $attributesToReturn;
71 } 105 }
72 106
73 - protected function deleteParameter($id) 107 + public function modifyObject($p)
74 { 108 {
75 - if (file_exists(USERTTDIR.$id.'.xml')) unlink(USERTTDIR.$id.'.xml'); 109 + $folder = $this->getObjectFolder($p->id);
  110 +
  111 + //Copy TT in a tempory file
  112 + $ttFilePath = USERTTDIR . $p->id . '.xml';
  113 + $tmpFileExist = false;
  114 + if (file_exists($ttFilePath)) {
  115 + $tmpFileExist = copy($ttFilePath, $ttFilePath . ".tmp");
  116 + }
  117 +
  118 + //Delete TT
  119 + $this->deleteObject($p);
  120 +
  121 + //Save modifications
  122 + try {
  123 + $result = $this->createObject($p, $folder);
  124 + if ($result['error']) {
  125 + throw new Exception($result['error']);
  126 + }
  127 + if ($tmpFileExist) {
  128 + unlink($ttFilePath . ".tmp");
  129 + }
  130 + return array('id' => $p->id, 'info' => $result['info']);
  131 + } catch (Exception $e) {
  132 + //Restore TT file
  133 + if ($tmpFileExist) {
  134 + copy($ttFilePath . ".tmp", $ttFilePath);
  135 + unlink($ttFilePath . ".tmp");
  136 + }
  137 + return array('error' => $e->getMessage());
  138 + }
76 } 139 }
77 140
78 /* 141 /*
79 * Check if difference is name and info only 142 * Check if difference is name and info only
80 */ 143 */
81 - protected function renameOnly($p) {  
82 - //if (!($p->intervals)) return true;  
83 - return false; 144 +
  145 + public function createObject($p, $folder)
  146 + {
  147 + if ($p->leaf) {
  148 + $result = $this->createParameter($p, $folder);
  149 + if ($result['error']) {
  150 + return $result;
  151 + }
  152 + $cacheMgr = new TimeTableCacheMgr();
  153 + if (isset($p->cacheToken) && ($p->cacheToken != '')) {
  154 + $resultSaveInt = $cacheMgr->saveInTT($result['id'], "update", $p->cacheToken);
  155 + if (!$resultSaveInt['success']) {
  156 + if ($resultSaveInt['message']) {
  157 + return array('error' => $resultSaveInt['message']);
  158 + } else {
  159 + return array('error' => 'Unknown error during intervals save');
  160 + }
  161 + }
  162 + }
  163 + 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');
  169 + }
84 } 170 }
85 -  
86 - /*  
87 - * In case of catalogs  
88 - */  
89 - protected function setParamDescription($param) { }  
90 171
91 -  
92 /* 172 /*
93 - * Create Time Table  
94 - */ 173 + * In case of catalogs
  174 + */
  175 +
95 protected function createParameter($p, $folder) 176 protected function createParameter($p, $folder)
96 { 177 {
97 - if ($this -> objectExistsByName($p->name)) {  
98 - $p -> id = $this -> getObjectIdByName($p->name);  
99 - $this -> deleteObject($p); 178 + if ($this->objectExistsByName($p->name)) {
  179 + $p->id = $this->getObjectIdByName($p->name);
  180 + $this->deleteObject($p);
100 } 181 }
101 - 182 +
102 $this->id = $this->setId(); 183 $this->id = $this->setId();
103 $this->created = date('Y-m-d\TH:i:s'); 184 $this->created = date('Y-m-d\TH:i:s');
104 - if (!$this->id) return array('error' => ID_CREATION_ERROR);  
105 -  
106 - $this->resFileName = USERTTDIR.$this->id.'.xml'; 185 + if (!$this->id) {
  186 + return array('error' => ID_CREATION_ERROR);
  187 + }
  188 +
  189 + $this->resFileName = USERTTDIR . $this->id . '.xml';
107 //TODO catalog root element = 'timetable' 190 //TODO catalog root element = 'timetable'
108 $rootElement = $this->objectDom->createElement('timetable'); 191 $rootElement = $this->objectDom->createElement('timetable');
109 - $rootElement->setAttribute('xml:id',$this->id);  
110 -  
111 - foreach ($p as $key => $value) 192 + $rootElement->setAttribute('xml:id', $this->id);
  193 +
  194 + foreach ($p as $key => $value) {
112 if ($key != 'id' && $key != 'leaf' && $key != 'nodeType' && 195 if ($key != 'id' && $key != 'leaf' && $key != 'nodeType' &&
113 $key != 'objName' && $key != 'objFormat' && $key != 'folderId' && $key != 'cacheToken') { 196 $key != 'objName' && $key != 'objFormat' && $key != 'folderId' && $key != 'cacheToken') {
114 - if ($key == 'created') {  
115 - $rootElement->appendChild($this->objectDom->createElement($key, $this->created));  
116 - }  
117 - // it is catalog  
118 - else if ($key == 'parameters') {  
119 - $paramsElement = $this->setParamDescription($value);  
120 - if ($paramsElement) $rootElement->appendChild($paramsElement);  
121 - 197 + if ($key == 'created') {
  198 + $rootElement->appendChild($this->objectDom->createElement($key, $this->created));
  199 + } // it is catalog
  200 + else {
  201 + if ($key == 'parameters') {
  202 + $paramsElement = $this->setParamDescription($value);
  203 + if ($paramsElement) {
  204 + $rootElement->appendChild($paramsElement);
  205 + }
  206 +
  207 + } else {
  208 + if ($key != 'intervals') {
  209 + $rootElement->appendChild($this->objectDom->createElement($key, htmlspecialchars($value)));
  210 + }
122 } 211 }
123 - else if ($key != 'intervals')  
124 - $rootElement->appendChild($this->objectDom->createElement($key, htmlspecialchars($value))); 212 + }
125 } 213 }
  214 + }
126 215
127 $this->objectDom->appendChild($rootElement); 216 $this->objectDom->appendChild($rootElement);
128 $this->objectDom->save($this->resFileName); 217 $this->objectDom->save($this->resFileName);
129 $obj = new stdClass(); 218 $obj = new stdClass();
130 $obj->name = $p->name; 219 $obj->name = $p->name;
131 $obj->intervals = $p->nbIntervals; 220 $obj->intervals = $p->nbIntervals;
132 - $this -> addToContent($obj, $folder);  
133 - return array('id' => $this->id,'created' => $this->created,'info' =>$obj->intervals.' intervals' ); 221 + $this->addToContent($obj, $folder);
  222 + return array('id' => $this->id, 'created' => $this->created, 'info' => $obj->intervals . ' intervals');
134 } 223 }
135 224
136 - protected function call_intersection($fst, $snd) 225 +
  226 + /*
  227 + * Create Time Table
  228 + */
  229 +
  230 + protected function setParamDescription($param)
137 { 231 {
138 - $inf = ( $fst[0] > $snd[0] ) ? $fst[0] : $snd[0];  
139 - $sup = ( $fst[1] < $snd[1] ) ? $fst[1] : $snd[1];  
140 - if ( $inf >= $sup ) { $inter[] = array(0,0); }  
141 - else {$inter[] = array($inf,$sup); }  
142 - return $inter;  
143 } 232 }
144 - 233 +
  234 + public function getUploadedObject($name, $format, $onlyDescription = false)
  235 + {
  236 + if (strpos($name, '.txt') !== false || strpos($name, '.asc') !== false || strpos($name, '.') == false) {
  237 + $attributesToReturn = $this->text2amda(USERTEMPDIR . $name, $onlyDescription);
  238 + $attributesToReturn['objName'] = $name;
  239 + $attributesToReturn['objFormat'] = $format;
  240 +
  241 + return $attributesToReturn;
  242 + }
  243 +
  244 + if ($format == 'VOT') {
  245 + $attributesToReturn = $this->vot2amda(USERTEMPDIR . $name, $onlyDescription);
  246 + $attributesToReturn['objName'] = $name;
  247 + $attributesToReturn['objFormat'] = $format;
  248 +
  249 + return $attributesToReturn;
  250 + }
  251 +
  252 + if (strpos($name, '.xml') !== false) {
  253 + $temp = explode('.xml', $name);
  254 + $name = $temp[0];
  255 + }
  256 +
  257 + if (!file_exists(USERTEMPDIR . $name . '.xml')) {
  258 + return array('error' => 'no such name');
  259 + }
  260 +
  261 + $this->objectDom->load(USERTEMPDIR . $name . '.xml');
  262 + if (!($objToGet = $this->objectDom->getElementsByTagName('timetable')->item(0)) &&
  263 + !($objToGet = $this->objectDom->getElementsByTagName('TimeTable')->item(0))) {
  264 + return array('error' => 'no time table');
  265 + }
  266 +
  267 + $attributes = $objToGet->childNodes;
  268 + $attributesToReturn['name'] = $name;
  269 + $attributesToReturn['objName'] = $name;
  270 + $attributesToReturn['objFormat'] = $format;
  271 +
  272 + foreach ($attributes as $attribute) {
  273 + if ($attribute->nodeType == XML_ELEMENT_NODE) {
  274 + if ($attribute->tagName == 'intervals') {
  275 + $start = $attribute->getElementsByTagName('start')->item(0)->nodeValue;
  276 + $stop = $attribute->getElementsByTagName('stop')->item(0)->nodeValue;
  277 + if (!$onlyDescription) {
  278 + $attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop);
  279 + }
  280 + } else {
  281 + if ($attribute->tagName == 'Interval') {
  282 + $start = $attribute->getElementsByTagName('Start')->item(0)->nodeValue;
  283 + $stop = $attribute->getElementsByTagName('Stop')->item(0)->nodeValue;
  284 + if (!$onlyDescription) {
  285 + $attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop);
  286 + }
  287 + } else {
  288 + switch (strtolower($attribute->tagName)) {
  289 + case 'created' :
  290 + $attributesToReturn['created'] = $attribute->nodeValue;
  291 + break;
  292 + case 'chain' :
  293 + case 'source' :
  294 + $attributesToReturn['description'] = $attribute->nodeValue;
  295 + break;
  296 + default:
  297 + break;
  298 + }
  299 + }
  300 + }
  301 + }
  302 + }
  303 + return $attributesToReturn;
  304 + }
  305 +
145 /* 306 /*
146 * Uploaded text file => convert to array 307 * Uploaded text file => convert to array
147 */ 308 */
148 - protected function text2amda($tmp_file, $onlyDescription = false)  
149 - { 309 +
  310 + protected function text2amda($tmp_file, $onlyDescription = false)
  311 + {
150 $suffix = explode('.', basename($tmp_file)); 312 $suffix = explode('.', basename($tmp_file));
151 - $lines = file($tmp_file,FILE_SKIP_EMPTY_LINES);  
152 - $description="Uploaded Time Table".PHP_EOL; 313 + $lines = file($tmp_file, FILE_SKIP_EMPTY_LINES);
  314 + $description = "Uploaded Time Table" . PHP_EOL;
153 315
154 $recordsNumber = count($lines); 316 $recordsNumber = count($lines);
155 $descNumber = 0; 317 $descNumber = 0;
156 -  
157 - foreach ($lines as $line)  
158 - { 318 +
  319 + foreach ($lines as $line) {
159 if ($line[0] == '#') { 320 if ($line[0] == '#') {
160 - $description=$description.PHP_EOL.substr($line,1,-1);  
161 - }  
162 - else {  
163 - $date = explode(' ', trim(preg_replace('!\s+!', ' ',$line)));  
164 - 321 + $description = $description . PHP_EOL . substr($line, 1, -1);
  322 + } else {
  323 + $date = explode(' ', trim(preg_replace('!\s+!', ' ', $line)));
  324 +
165 if (!strtotime(trim($date[0]))) { 325 if (!strtotime(trim($date[0]))) {
166 - $description=$description.PHP_EOL.$line; 326 + $description = $description . PHP_EOL . $line;
167 $descNumber++; 327 $descNumber++;
168 continue; 328 continue;
169 } 329 }
170 // check if it is ISO format 330 // check if it is ISO format
171 - if (!isset($isIso)) 331 + if (!isset($isIso)) {
172 $isIso = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})$/', trim($date[0])); 332 $isIso = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})$/', trim($date[0]));
173 - 333 + }
  334 +
174 if (!$isIso) { 335 if (!$isIso) {
175 // y-m-d h:m:s for example 336 // y-m-d h:m:s for example
176 $dateLength = count($date) / 2; 337 $dateLength = count($date) / 2;
177 338
178 $tempStart = $date[0]; 339 $tempStart = $date[0];
179 $tempStop = $date[$dateLength]; 340 $tempStop = $date[$dateLength];
180 - 341 +
181 if ($dateLength > 1) { 342 if ($dateLength > 1) {
182 - for ($i = 1; $i < $dateLength; $i++)  
183 - $tempStart.= $date[$i];  
184 -  
185 - for ($i = $dateLength + 1; $i < $dateLength * 2; $i++) 343 + for ($i = 1; $i < $dateLength; $i++) {
  344 + $tempStart .= $date[$i];
  345 + }
  346 +
  347 + for ($i = $dateLength + 1; $i < $dateLength * 2; $i++) {
186 $tempStop .= $date[$i]; 348 $tempStop .= $date[$i];
  349 + }
187 } 350 }
188 351
189 - $startDate = date('Y-m-d\TH:i:s',strtotime($tempStart));  
190 - $stopDate = date('Y-m-d\TH:i:s',strtotime($tempStop));  
191 -  
192 - if (!$onlyDescription)  
193 - $attributesToReturn['intervals'][] = array('start' => $startDate, 'stop' => $stopDate);  
194 - }  
195 - else {  
196 - if (!$onlyDescription) 352 + $startDate = date('Y-m-d\TH:i:s', strtotime($tempStart));
  353 + $stopDate = date('Y-m-d\TH:i:s', strtotime($tempStop));
  354 +
  355 + if (!$onlyDescription) {
  356 + $attributesToReturn['intervals'][] = array('start' => $startDate, 'stop' => $stopDate);
  357 + }
  358 + } else {
  359 + if (!$onlyDescription) {
197 $attributesToReturn['intervals'][] = array('start' => trim($date[0]), 'stop' => trim($date[1])); 360 $attributesToReturn['intervals'][] = array('start' => trim($date[0]), 'stop' => trim($date[1]));
198 - } 361 + }
  362 + }
199 } 363 }
200 } 364 }
201 - if ( $recordsNumber == $descNumber )  
202 - $description = "Looks like we can not read your time format...".PHP_EOL.$description;  
203 - 365 + if ($recordsNumber == $descNumber) {
  366 + $description = "Looks like we can not read your time format..." . PHP_EOL . $description;
  367 + }
  368 +
204 $attributesToReturn['description'] = $description; 369 $attributesToReturn['description'] = $description;
205 - $attributesToReturn['name'] = basename($tmp_file, '.'.$suffix[1]); 370 + $attributesToReturn['name'] = basename($tmp_file, '.' . $suffix[1]);
206 $attributesToReturn['created'] = date('Y-m-d\TH:i:s'); 371 $attributesToReturn['created'] = date('Y-m-d\TH:i:s');
207 372
208 return $attributesToReturn; 373 return $attributesToReturn;
209 } 374 }
210 375
211 - /*  
212 - * Uploaded vot TT => convert to array  
213 - */  
214 - protected function vot2amda($tmp_file, $onlyDescription = false) {  
215 - // Load Time table  
216 - $this->objectDom -> load($tmp_file); 376 + /*
  377 + * Uploaded vot TT => convert to array
  378 + */
  379 + protected function vot2amda($tmp_file, $onlyDescription = false)
  380 + {
  381 + // Load Time table
  382 + $this->objectDom->load($tmp_file);
217 $objToGet = $this->objectDom->getElementsByTagName('TABLEDATA')->item(0); 383 $objToGet = $this->objectDom->getElementsByTagName('TABLEDATA')->item(0);
218 $attributesToReturn['name'] = $tmp_file; 384 $attributesToReturn['name'] = $tmp_file;
219 - $attributes = $objToGet -> childNodes;  
220 - foreach($attributes as $attribute) 385 + $attributes = $objToGet->childNodes;
  386 + foreach ($attributes as $attribute) {
221 if ($attribute->tagName == 'TR') { 387 if ($attribute->tagName == 'TR') {
222 - $start = $attribute -> getElementsByTagName('TD')->item(0) -> nodeValue;  
223 - $stop = $attribute -> getElementsByTagName('TD')->item(1) -> nodeValue;  
224 - if (!$onlyDescription)  
225 - $attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop); 388 + $start = $attribute->getElementsByTagName('TD')->item(0)->nodeValue;
  389 + $stop = $attribute->getElementsByTagName('TD')->item(1)->nodeValue;
  390 + if (!$onlyDescription) {
  391 + $attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop);
  392 + }
226 } 393 }
227 - $suffix = explode('.', basename($tmp_file));  
228 - $attributesToReturn['name'] = basename($tmp_file, '.'.$suffix[1]);  
229 - $attributesToReturn['created'] = date('Y-m-d')."T".date('H:i:s');  
230 - $attributesToReturn['description'] = htmlspecialchars($this->objectDom->getElementsByTagName('DESCRIPTION')->item(0) -> nodeValue);  
231 - return($attributesToReturn); 394 + }
  395 + $suffix = explode('.', basename($tmp_file));
  396 + $attributesToReturn['name'] = basename($tmp_file, '.' . $suffix[1]);
  397 + $attributesToReturn['created'] = date('Y-m-d') . "T" . date('H:i:s');
  398 + $attributesToReturn['description'] = htmlspecialchars($this->objectDom->getElementsByTagName('DESCRIPTION')->item(0)->nodeValue);
  399 + return ($attributesToReturn);
232 } 400 }
233 - 401 +
234 /***************************************************************** 402 /*****************************************************************
235 * PUBLIC FUNCTIONS 403 * PUBLIC FUNCTIONS
236 *****************************************************************/ 404 *****************************************************************/
237 - 405 +
238 /* 406 /*
239 * Get Object into Edit 407 * Get Object into Edit
240 */ 408 */
241 - function getObject($id, $nodeType) {  
242 - if (substr($nodeType,0,6) == 'shared') {  
243 - //Shared object  
244 - $sharedObjMgr = new SharedObjectsMgr();  
245 - $path = $sharedObjMgr->getDataFilePath(str_replace('shared', '', $nodeType), $id); 409 + public function getTmpObject($folderId, $name, $onlyDescription = false)
  410 + {
  411 + $filePath = USERWORKINGDIR . $folderId . '/' . $name . '.xml';
  412 + if (!file_exists($filePath)) {
  413 + return array('error' => 'Cannot find result file');
246 } 414 }
247 - else {  
248 - $path = USERTTDIR.$id.'.xml'; 415 +
  416 + $dom = new DomDocument('1.0');
  417 + $dom->formatOutput = true;
  418 +
  419 + if (!$dom->load($filePath)) {
  420 + return array('error' => 'Cannot load result file');
249 } 421 }
250 422
251 - if (!file_exists($path)) return array('error' => NO_OBJECT_FILE);  
252 - $this->objectDom -> load($path);  
253 - if (!($objToGet = $this->objectDom->getElementById($id))) return array('error' => NO_SUCH_ID);  
254 - $attributesToReturn['id'] = $objToGet->getAttribute('xml:id');  
255 - $attributes = $objToGet -> childNodes; 423 + $descNodes = $dom->getElementsByTagName('description');
  424 + if ($descNodes->length > 0) {
  425 + $attributesToReturn['description'] = $descNodes->item(0)->nodeValue;
  426 + }
256 427
257 - $nbInt = 0;  
258 - foreach($attributes as $attribute)  
259 - if($attribute->nodeType == XML_ELEMENT_NODE){  
260 - /*if ($attribute->tagName == 'intervals') {  
261 - $start = $attribute -> getElementsByTagName('start')->item(0) -> nodeValue;  
262 - $stop = $attribute -> getElementsByTagName('stop')->item(0) -> nodeValue;  
263 - $attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop);  
264 - }  
265 - else  
266 - $attributesToReturn[$attribute->tagName] = $attribute->nodeValue;*/  
267 - //BRE - load all except intervals - Intervals will be loaded later with 'loadIntervalsFromTT' function  
268 - if ($attribute->tagName != 'intervals')  
269 - $attributesToReturn[$attribute->tagName] = $attribute->nodeValue;  
270 - else  
271 - $nbInt++;  
272 - }  
273 -  
274 - $attributesToReturn['nbIntervals'] = $nbInt;  
275 -  
276 - return $attributesToReturn;  
277 - }  
278 -  
279 - public function createObject($p, $folder)  
280 - {  
281 - if ($p -> leaf){  
282 - $result = $this->createParameter($p, $folder);  
283 - if ($result['error'])  
284 - return $result;  
285 - $cacheMgr = new TimeTableCacheMgr();  
286 - if (isset($p->cacheToken) && ($p->cacheToken != ''))  
287 - {  
288 - $resultSaveInt = $cacheMgr->saveInTT($result['id'], "update", $p->cacheToken);  
289 - if (!$resultSaveInt['success'])  
290 - {  
291 - if ($resultSaveInt['message'])  
292 - return array('error' => $resultSaveInt['message']);  
293 - else  
294 - return array('error' => 'Unknown error during intervals save'); 428 + $creatNodes = $dom->getElementsByTagName('created');
  429 + if ($creatNodes->length > 0) {
  430 + $attributesToReturn['created'] = $creatNodes->item(0)->nodeValue;
  431 + }
  432 +
  433 + $histNodes = $dom->getElementsByTagName('history');
  434 + if ($histNodes->length > 0) {
  435 + $attributesToReturn['history'] = $histNodes->item(0)->nodeValue;
  436 + }
  437 +
  438 + $attributesToReturn['objName'] = $name;
  439 + $attributesToReturn['folderId'] = $folderId;
  440 +
  441 + if (!$onlyDescription) {
  442 + $intNodes = $dom->getElementsByTagName('intervals');
  443 + foreach ($intNodes as $intNode) {
  444 + $startNodes = $intNode->getElementsByTagName('start');
  445 + if ($startNodes->length <= 0) {
  446 + return array('error' => 'Error detected in result file');
  447 + }
  448 + $stopNodes = $intNode->getElementsByTagName('stop');
  449 + if ($stopNodes->length <= 0) {
  450 + return array('error' => 'Error detected in result file');
295 } 451 }
  452 + $attributesToReturn['intervals'][] = array(
  453 + 'start' => $startNodes->item(0)->nodeValue,
  454 + 'stop' => $stopNodes->item(0)->nodeValue
  455 + );
296 } 456 }
297 - return $result;  
298 } 457 }
299 - // else return $this->createFolder($p);  
300 - //TODO check if this is possible?  
301 - else return array('error' => 'createFolder should be called from RENAME'); 458 +
  459 + return $attributesToReturn;
302 } 460 }
303 -  
304 -  
305 - public function modifyObject($p) {  
306 - $folder = $this->getObjectFolder($p->id);  
307 -  
308 - //Copy TT in a tempory file  
309 - $ttFilePath = USERTTDIR.$p->id.'.xml';  
310 - $tmpFileExist = FALSE;  
311 - if (file_exists($ttFilePath))  
312 - $tmpFileExist = copy($ttFilePath,$ttFilePath.".tmp");  
313 -  
314 - //Delete TT  
315 - $this->deleteObject($p);  
316 -  
317 - //Save modifications  
318 - try {  
319 - $result = $this->createObject($p, $folder);  
320 - if ($result['error'])  
321 - throw new Exception($result['error']);  
322 - if ($tmpFileExist)  
323 - unlink($ttFilePath.".tmp");  
324 - return array('id' => $p->id, 'info' => $result['info']); 461 +
  462 + public function merge($obj)
  463 + {
  464 + /**
  465 + * Array of intervals, used like :
  466 + * [{start:'2010-01-01T23:00:00',stop:'2011-01-01T20:00:00'},{start:'2009-01-01T23:00:00',stop:'2010-01-01T20:00:00'}]
  467 + * $attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop);
  468 + */
  469 +
  470 + $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'];
  477 + }
  478 + $intervals += count($interval[$i]);
325 } 479 }
326 - catch (Exception $e) {  
327 - //Restore TT file  
328 - if ($tmpFileExist)  
329 - {  
330 - copy($ttFilePath.".tmp", $ttFilePath);  
331 - unlink($ttFilePath.".tmp"); 480 + if ($intervals > 10000) {
  481 + set_time_limit(1800);
  482 + }
  483 +
  484 + $final = array();
  485 + for ($i = 0; $i < count($obj->ids); $i++) {
  486 + $final = array_merge($final, $interval[$i]);
  487 + }
  488 + sort($final);
  489 +
  490 + // Algorithm of union
  491 + $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);
  497 + } else {
  498 + if (($final[$i + 1][0] <= $final[$i][1]) && ($final[$i + 1][1] >= $final[$i][1])) {
  499 + $i++;
  500 + } else {
  501 + $start[$line] = $a;
  502 + $stop[$line] = $final[$i][1];
  503 + $i++;
  504 + $line++;
  505 + $a = $final[$i][0];
  506 + }
332 } 507 }
333 - return array ('error' => $e->getMessage());  
334 } 508 }
  509 + $start[$line] = $a;
  510 + $stop[$line] = $final[$i][1];
  511 + $line++;
  512 +
  513 + $objTT = new stdClass();
  514 + $objTT->name = $obj->name;
  515 + $objTT->nodeType = 'timeTable';
  516 + $objTT->leaf = true;
  517 + $objTT->created = null;
  518 + $objTT->history = $obj->history;
  519 + for ($i = 0; $i < count($start); $i++) {
  520 + $inter = new stdClass();
  521 + $inter->start = $start[$i];
  522 + $inter->stop = $stop[$i];
  523 + $objTT->intervals[] = $inter;
  524 + }
  525 + $objTT->nbIntervals = count($start);
  526 + $this->objectDom = new DomDocument('1.0');
  527 + $this->objectDom->formatOutput = true;
  528 +
  529 + $res = $this->createParameter($objTT, $folder);
  530 + if ($res['error']) {
  531 + return $res;
  532 + }
  533 +
  534 + $this->saveIntervals($res['id'], $objTT->intervals, 'merge');
  535 +
  536 + return $res;
  537 +
335 } 538 }
336 -  
337 - public function loadIntervalsFromTT($id,$typeTT,$start = NULL, $limit = NULL) 539 +
  540 + public function loadIntervalsFromTT($id, $typeTT, $start = null, $limit = null)
338 { 541 {
339 if ($typeTT == 'sharedtimeTable') { 542 if ($typeTT == 'sharedtimeTable') {
340 //Shared object 543 //Shared object
341 $sharedObjMgr = new SharedObjectsMgr(); 544 $sharedObjMgr = new SharedObjectsMgr();
342 $path = $sharedObjMgr->getDataFilePath('timeTable', $id); 545 $path = $sharedObjMgr->getDataFilePath('timeTable', $id);
  546 + } else {
  547 + $path = USERTTDIR . $id . '.xml';
343 } 548 }
344 - else {  
345 - $path = USERTTDIR.$id.'.xml';  
346 - }  
347 -  
348 - //load intervals from TT id  
349 - if (!file_exists($path))  
350 - return array('success' => false, 'message' => "Cannot find TT file ".$id);  
351 -  
352 - $this->objectDom -> load($path);  
353 - if (!($objToGet = $this->objectDom->getElementById($id)))  
354 - return array('success' => false, 'message' => NO_SUCH_ID." ".$id);  
355 - 549 +
  550 + //load intervals from TT id
  551 + if (!file_exists($path)) {
  552 + return array('success' => false, 'message' => "Cannot find TT file " . $id);
  553 + }
  554 +
  555 + $this->objectDom->load($path);
  556 + if (!($objToGet = $this->objectDom->getElementById($id))) {
  557 + return array('success' => false, 'message' => NO_SUCH_ID . " " . $id);
  558 + }
  559 +
356 $xpath = new DOMXPath($this->objectDom); 560 $xpath = new DOMXPath($this->objectDom);
357 - $intervals = $xpath->query('//intervals');  
358 - 561 + $intervals = $xpath->query('//intervals');
  562 +
359 $result = array(); 563 $result = array();
360 - 564 +
361 if (!isset($start) || !isset($limit)) { 565 if (!isset($start) || !isset($limit)) {
362 foreach ($intervals as $interval) { 566 foreach ($intervals as $interval) {
363 $startTime = $interval->getElementsByTagName('start')->item(0)->nodeValue; 567 $startTime = $interval->getElementsByTagName('start')->item(0)->nodeValue;
364 - $stopTime = $interval->getElementsByTagName('stop')->item(0)->nodeValue; 568 + $stopTime = $interval->getElementsByTagName('stop')->item(0)->nodeValue;
365 array_push($result, array('start' => $startTime, 'stop' => $stopTime)); 569 array_push($result, array('start' => $startTime, 'stop' => $stopTime));
366 } 570 }
367 - }  
368 - else { 571 + } else {
369 for ($i = 0; $i < $limit; ++$i) { 572 for ($i = 0; $i < $limit; ++$i) {
370 - if ($start+$i >= $intervals->length) 573 + if ($start + $i >= $intervals->length) {
371 break; 574 break;
372 - $startTime = $intervals->item($start+$i)->getElementsByTagName('start')->item(0)->nodeValue;  
373 - $stopTime = $intervals->item($start+$i)->getElementsByTagName('stop')->item(0)->nodeValue; 575 + }
  576 + $startTime = $intervals->item($start + $i)->getElementsByTagName('start')->item(0)->nodeValue;
  577 + $stopTime = $intervals->item($start + $i)->getElementsByTagName('stop')->item(0)->nodeValue;
374 array_push($result, array('start' => $startTime, 'stop' => $stopTime)); 578 array_push($result, array('start' => $startTime, 'stop' => $stopTime));
375 } 579 }
376 } 580 }
377 - 581 +
378 return array( 582 return array(
379 - 'totalCount' => $intervals->length,  
380 - 'intervals' => $result,  
381 - 'start' => isset($start) ? $start : 0,  
382 - 'limit' => isset($limit) ? $limit : 0,  
383 - 'success' => true  
384 - );  
385 - }  
386 -  
387 - protected function createIntervalElement($interval)  
388 - {  
389 - $newInterval = $this->objectDom->createElement('intervals');  
390 - $newInterval->appendChild($this->objectDom->createElement('start',$interval->start));  
391 - $newInterval->appendChild($this->objectDom->createElement('stop',$interval->stop));  
392 - return $newInterval; 583 + 'totalCount' => $intervals->length,
  584 + 'intervals' => $result,
  585 + 'start' => isset($start) ? $start : 0,
  586 + 'limit' => isset($limit) ? $limit : 0,
  587 + 'success' => true
  588 + );
393 } 589 }
394 -  
395 - public function saveIntervals($id,$intervals,$action) 590 +
  591 + public function saveIntervals($id, $intervals, $action)
396 { 592 {
397 - if (substr($id,0,6) == 'shared') { 593 + if (substr($id, 0, 6) == 'shared') {
398 return array('success' => false, 'message' => "Cannot save shared TimeTable"); 594 return array('success' => false, 'message' => "Cannot save shared TimeTable");
  595 + } else {
  596 + $path = USERTTDIR . $id . '.xml';
  597 + }
  598 + if (!file_exists($path)) {
  599 + return array('success' => false, 'message' => "Cannot find TT file " . $id);
  600 + }
  601 + $this->objectDom->load($path);
  602 +
  603 + if (!($objToGet = $this->objectDom->getElementById($id))) {
  604 + return array('success' => false, 'message' => NO_SUCH_ID . " " . $id);
399 } 605 }
400 - else {  
401 - $path = USERTTDIR.$id.'.xml';  
402 - }  
403 - if (!file_exists($path))  
404 - return array('success' => false, 'message' => "Cannot find TT file ".$id);  
405 - $this->objectDom -> load($path);  
406 -  
407 - if (!($objToGet = $this->objectDom->getElementById($id)))  
408 - return array('success' => false, 'message' => NO_SUCH_ID." ".$id);  
409 606
410 //remove old intervals 607 //remove old intervals
411 $crtNode = $objToGet->firstChild; 608 $crtNode = $objToGet->firstChild;
412 - 609 +
413 while ($crtNode) { 610 while ($crtNode) {
414 - if (($crtNode->nodeType != XML_ELEMENT_NODE) || ($crtNode->tagName != 'intervals')) { 611 + if (($crtNode->nodeType != XML_ELEMENT_NODE) || ($crtNode->tagName != 'intervals')) {
415 $crtNode = $crtNode->nextSibling; 612 $crtNode = $crtNode->nextSibling;
416 continue; 613 continue;
417 } 614 }
@@ -420,357 +617,232 @@ class TimeTableMgr extends AmdaObjectMgr @@ -420,357 +617,232 @@ class TimeTableMgr extends AmdaObjectMgr
420 $objToGet->removeChild($toRemove); 617 $objToGet->removeChild($toRemove);
421 unset($toRemove); 618 unset($toRemove);
422 } 619 }
423 - 620 +
424 //add new intervals 621 //add new intervals
425 - foreach ($intervals as $interval) {  
426 - $newInterval = $this-> createIntervalElement($interval); 622 + foreach ($intervals as $interval) {
  623 + $newInterval = $this->createIntervalElement($interval);
427 $this->objectDom->documentElement->appendChild($newInterval); 624 $this->objectDom->documentElement->appendChild($newInterval);
428 } 625 }
429 626
430 //save modifications 627 //save modifications
431 $this->id = $id; 628 $this->id = $id;
432 - $this->resFileName = USERTTDIR.$this->id.'.xml'; 629 + $this->resFileName = USERTTDIR . $this->id . '.xml';
433 $this->objectDom->save($this->resFileName); 630 $this->objectDom->save($this->resFileName);
434 - 631 +
435 unset($this->objectDom); 632 unset($this->objectDom);
436 -  
437 - return array('success' => true,'action' => $action, 'nbIntervals' => count($intervals)); 633 +
  634 + return array('success' => true, 'action' => $action, 'nbIntervals' => count($intervals));
438 } 635 }
439 -  
440 -  
441 - public function getUploadedObject($name, $format, $onlyDescription = false) 636 +
  637 + protected function createIntervalElement($interval)
442 { 638 {
443 - if (strpos($name,'.txt') !== false || strpos($name,'.asc') !== false || strpos($name,'.') == false) {  
444 - $attributesToReturn = $this->text2amda(USERTEMPDIR.$name, $onlyDescription);  
445 - $attributesToReturn['objName'] = $name;  
446 - $attributesToReturn['objFormat'] = $format;  
447 -  
448 - return $attributesToReturn; 639 + $newInterval = $this->objectDom->createElement('intervals');
  640 + $newInterval->appendChild($this->objectDom->createElement('start', $interval->start));
  641 + $newInterval->appendChild($this->objectDom->createElement('stop', $interval->stop));
  642 + return $newInterval;
  643 + }
  644 +
  645 + public function intersect($obj)
  646 + {
  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'];
  653 + }
  654 + $intervals += count($interval[$i]);
449 } 655 }
450 -  
451 - if ($format == 'VOT') {  
452 - $attributesToReturn = $this->vot2amda(USERTEMPDIR.$name, $onlyDescription);  
453 - $attributesToReturn['objName'] = $name;  
454 - $attributesToReturn['objFormat'] = $format;  
455 -  
456 - return $attributesToReturn;  
457 - }  
458 -  
459 - if (strpos($name,'.xml') !== false) {  
460 - $temp = explode('.xml', $name);  
461 - $name = $temp[0]; 656 + if ($intervals > 10000) {
  657 + set_time_limit(1800);
462 } 658 }
463 -  
464 - if (!file_exists(USERTEMPDIR.$name.'.xml'))  
465 - return array('error' => 'no such name');  
466 -  
467 - $this->objectDom->load(USERTEMPDIR.$name.'.xml');  
468 - if (!($objToGet = $this->objectDom->getElementsByTagName('timetable')->item(0)) &&  
469 - !($objToGet = $this->objectDom->getElementsByTagName('TimeTable')->item(0)))  
470 - return array('error' => 'no time table');  
471 659
472 - $attributes = $objToGet -> childNodes;  
473 - $attributesToReturn['name'] = $name;  
474 - $attributesToReturn['objName'] = $name;  
475 - $attributesToReturn['objFormat'] = $format; 660 + // Sort intervals in time tables
  661 + sort($interval[0]);
  662 + sort($interval[1]);
476 663
477 - foreach($attributes as $attribute) {  
478 - if($attribute->nodeType == XML_ELEMENT_NODE){  
479 - if ($attribute->tagName == 'intervals') {  
480 - $start = $attribute -> getElementsByTagName('start')->item(0) -> nodeValue;  
481 - $stop = $attribute -> getElementsByTagName('stop')->item(0) -> nodeValue;  
482 - if (!$onlyDescription)  
483 - $attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop);  
484 - }  
485 - else if ($attribute->tagName == 'Interval') {  
486 - $start = $attribute -> getElementsByTagName('Start')->item(0) -> nodeValue;  
487 - $stop = $attribute -> getElementsByTagName('Stop')->item(0) -> nodeValue;  
488 - if (!$onlyDescription)  
489 - $attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop);  
490 - }  
491 - else {  
492 - switch (strtolower($attribute->tagName)) {  
493 - case 'created' :  
494 - $attributesToReturn['created'] = $attribute->nodeValue;  
495 - break;  
496 - case 'chain' :  
497 - case 'source' :  
498 - $attributesToReturn['description'] = $attribute->nodeValue;  
499 - break;  
500 - default: break;  
501 - } 664 + $i = 0;
  665 + $j = 0;
  666 + $line = 0;
  667 +
  668 + while (($i < count($interval[0])) && ($j < count($interval[1]))) {
  669 + $inter = $this->call_intersection($interval[0][$i], $interval[1][$j]);
  670 +
  671 + if ($inter[0][0] != 0 && $inter[0][1] != 0) {
  672 + $start[$line] = $inter[0][0];
  673 + $stop[$line] = $inter[0][1];
  674 + $line++;
  675 + }
  676 +
  677 + if ($interval[0][$i][1] < $interval[1][$j][1]) {
  678 + $i++;
  679 + } else {
  680 + $j++;
  681 + }
  682 + }
  683 +
  684 + // Intersection is empty
  685 + if ($line == 0) {
  686 + $result = "empty";
  687 + } else {
  688 + $objTT->name = $obj->name;
  689 + $objTT->nodeType = 'timeTable';
  690 + $objTT->leaf = true;
  691 + $objTT->created = null;
  692 + $objTT->history = $obj->history;
  693 + for ($i = 0; $i < count($start); $i++) {
  694 + $inter = new stdClass();
  695 + $inter->start = $start[$i];
  696 + $inter->stop = $stop[$i];
  697 + $objTT->intervals[] = $inter;
  698 + }
  699 + $objTT->nbIntervals = count($start);
  700 +
  701 + if (count($objTT->intervals) == 0) {
  702 + $result = "empty";
  703 + } else {
  704 + $this->objectDom = new DomDocument('1.0');
  705 + $this->objectDom->formatOutput = true;
  706 + $result = $this->createObject($objTT, $folder);
  707 +
  708 + if (!isset($result['error'])) {
  709 + $this->saveIntervals($result['id'], $objTT->intervals, 'intersect');
502 } 710 }
  711 +
503 } 712 }
504 } 713 }
505 - return $attributesToReturn; 714 + return $result;
  715 + }
  716 +
  717 + protected function call_intersection($fst, $snd)
  718 + {
  719 + $inf = ($fst[0] > $snd[0]) ? $fst[0] : $snd[0];
  720 + $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 + }
  726 + return $inter;
506 } 727 }
507 728
508 //TODO getObject only!!!! => change DD_Search output 729 //TODO getObject only!!!! => change DD_Search output
509 - public function getTmpObject($folderId, $name, $onlyDescription = false) {  
510 - $filePath = USERWORKINGDIR.$folderId.'/'.$name.'.xml';  
511 - if (!file_exists($filePath))  
512 - return array('error' => 'Cannot find result file');  
513 -  
514 - $dom = new DomDocument('1.0');  
515 - $dom->formatOutput = true;  
516 -  
517 - if (!$dom -> load($filePath))  
518 - return array('error' => 'Cannot load result file');  
519 -  
520 - $descNodes = $dom->getElementsByTagName('description');  
521 - if ($descNodes->length > 0)  
522 - $attributesToReturn['description'] = $descNodes->item(0)->nodeValue;  
523 -  
524 - $creatNodes = $dom->getElementsByTagName('created');  
525 - if ($creatNodes->length > 0)  
526 - $attributesToReturn['created'] = $creatNodes->item(0)->nodeValue;  
527 -  
528 - $histNodes = $dom->getElementsByTagName('history');  
529 - if ($histNodes->length > 0)  
530 - $attributesToReturn['history'] = $histNodes->item(0)->nodeValue;  
531 -  
532 - $attributesToReturn['objName'] = $name;  
533 - $attributesToReturn['folderId'] = $folderId;  
534 -  
535 - if (!$onlyDescription)  
536 - {  
537 - $intNodes = $dom->getElementsByTagName('intervals');  
538 - foreach ($intNodes as $intNode)  
539 - {  
540 - $startNodes = $intNode->getElementsByTagName('start');  
541 - if ($startNodes->length <= 0)  
542 - return array('error' => 'Error detected in result file');  
543 - $stopNodes = $intNode->getElementsByTagName('stop');  
544 - if ($stopNodes->length <= 0)  
545 - return array('error' => 'Error detected in result file');  
546 - $attributesToReturn['intervals'][] = array('start' => $startNodes->item(0)->nodeValue,  
547 - 'stop' => $stopNodes->item(0)->nodeValue);  
548 - } 730 +
  731 + function validNameObject($p)
  732 + {
  733 + // overwritten
  734 + $res = parent::validNameObject($p);
  735 +
  736 + if (!$res['valid']) {
  737 + return $res;
  738 + }
  739 +
  740 + //no space
  741 + if (strpos($p->name, ' ') === false) {
  742 + return array('valid' => true);
549 } 743 }
550 -  
551 - return $attributesToReturn; 744 +
  745 + return array('valid' => false, 'error' => 'Space character is not allowed');
552 } 746 }
553 747
554 /* 748 /*
555 * merge time tables 749 * merge time tables
556 */ 750 */
557 - public function merge($obj) {  
558 - /**  
559 - * Array of intervals, used like :  
560 - * [{start:'2010-01-01T23:00:00',stop:'2011-01-01T20:00:00'},{start:'2009-01-01T23:00:00',stop:'2010-01-01T20:00:00'}]  
561 - * $attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop);  
562 - */  
563 -  
564 - $intervals = 0;  
565 -  
566 - for ( $i = 0; $i < count($obj->ids); $i++ ) {  
567 - $table[$i] = $this->loadIntervalsFromTT($obj->ids[$i]);  
568 - for ($j=0; $j < count($table[$i]['intervals']); $j++) {  
569 - $interval[$i][$j][0] = $table[$i]['intervals'][$j]['start'];  
570 - $interval[$i][$j][1] = $table[$i]['intervals'][$j]['stop'];  
571 - }  
572 - $intervals += count($interval[$i]);  
573 - }  
574 - if ( $intervals > 10000) set_time_limit(1800);  
575 -  
576 - $final = array();  
577 - for ( $i = 0; $i < count($obj->ids); $i++ ) {  
578 - $final = array_merge($final, $interval[$i]);  
579 - }  
580 - sort($final);  
581 -  
582 - // Algorithm of union  
583 - $line = 0;  
584 - $i = 0;  
585 - $a = $final[$i][0];  
586 - while ($i < count($final)-1) {  
587 - if ($final[$i+1][1] <= $final[$i][1])  
588 - {  
589 - array_splice($final,$i+1,1);  
590 - }  
591 - else if (($final[$i+1][0] <= $final[$i][1]) && ($final[$i+1][1] >= $final[$i][1]))  
592 - {  
593 - $i++;  
594 - }  
595 - else {  
596 - $start[$line] = $a;  
597 - $stop[$line] = $final[$i][1];  
598 - $i++;  
599 - $line++;  
600 - $a = $final[$i][0];  
601 - }  
602 - }  
603 - $start[$line] = $a;  
604 - $stop[$line] = $final[$i][1];  
605 - $line++;  
606 -  
607 - $objTT = new stdClass();  
608 - $objTT->name = $obj->name;  
609 - $objTT->nodeType = 'timeTable';  
610 - $objTT->leaf = true;  
611 - $objTT->created = null;  
612 - $objTT->history = $obj->history;  
613 - for ($i=0; $i < count($start); $i++) {  
614 - $inter = new stdClass();  
615 - $inter->start = $start[$i];  
616 - $inter->stop = $stop[$i];  
617 - $objTT->intervals[] = $inter;  
618 - }  
619 - $objTT->nbIntervals = count($start);  
620 - $this->objectDom = new DomDocument('1.0');  
621 - $this->objectDom->formatOutput = true;  
622 -  
623 - $res = $this->createParameter($objTT, $folder);  
624 - if ($res['error'])  
625 - return $res;  
626 -  
627 - $this->saveIntervals($res['id'],$objTT->intervals,'merge');  
628 -  
629 - return $res;  
630 -  
631 - }  
632 -  
633 -/*  
634 - * intersect time tables  
635 - */  
636 - public function intersect($obj) {  
637 -  
638 - for ( $i = 0; $i < count($obj->ids); $i++ ) {  
639 - $table[$i] = $this->loadIntervalsFromTT($obj->ids[$i]);  
640 - for ($j=0; $j < count($table[$i]['intervals']); $j++) {  
641 - $interval[$i][$j][0] = $table[$i]['intervals'][$j]['start'];  
642 - $interval[$i][$j][1] = $table[$i]['intervals'][$j]['stop'];  
643 - }  
644 - $intervals += count($interval[$i]);  
645 - }  
646 - if ( $intervals > 10000) set_time_limit(1800);  
647 -  
648 - // Sort intervals in time tables  
649 - sort($interval[0]);  
650 - sort($interval[1]);  
651 -  
652 - $i = 0;  
653 - $j = 0;  
654 - $line = 0;  
655 -  
656 - while ( ($i < count($interval[0])) && ($j < count($interval[1])) ) {  
657 - $inter = $this->call_intersection($interval[0][$i], $interval[1][$j]);  
658 -  
659 - if ($inter[0][0] != 0 && $inter[0][1] != 0)  
660 - {  
661 - $start[$line] = $inter[0][0];  
662 - $stop[$line] = $inter[0][1];  
663 - $line++;  
664 - }  
665 -  
666 - if ( $interval[0][$i][1] < $interval[1][$j][1] ) { $i++; }  
667 - else { $j++; }  
668 - }  
669 -  
670 - // Intersection is empty  
671 - if ( $line == 0 ) {  
672 - $result ="empty";  
673 - }  
674 - else {  
675 - $objTT->name = $obj->name;  
676 - $objTT->nodeType = 'timeTable';  
677 - $objTT->leaf = true;  
678 - $objTT->created = null;  
679 - $objTT->history = $obj->history;  
680 - for ($i=0; $i < count($start); $i++) {  
681 - $inter = new stdClass();  
682 - $inter->start = $start[$i];  
683 - $inter->stop = $stop[$i];  
684 - $objTT->intervals[] = $inter;  
685 - }  
686 - $objTT->nbIntervals = count($start);  
687 -  
688 - if (count($objTT->intervals) == 0)  
689 - $result ="empty";  
690 - else  
691 - {  
692 - $this->objectDom = new DomDocument('1.0');  
693 - $this->objectDom->formatOutput = true;  
694 - $result = $this->createObject($objTT, $folder);  
695 -  
696 - if (!isset($result['error']))  
697 - $this->saveIntervals($result['id'],$objTT->intervals,'intersect');  
698 -  
699 - }  
700 - }  
701 - return $result;  
702 - }  
703 -  
704 -  
705 - function validNameObject($p){  
706 - // overwritten  
707 - $res = parent::validNameObject($p);  
708 -  
709 - if (!$res['valid'])  
710 - return $res;  
711 -  
712 - //no space  
713 - if (strpos($p->name, ' ') === FALSE)  
714 - return array('valid' => true);  
715 -  
716 - return array('valid' => false, 'error' => 'Space character is not allowed');  
717 - }  
718 -  
719 - public function copyTT($src_path, $dst_path, $newId, $newName, $newDescription = NULL) 751 +
  752 + public function copyTT($src_path, $dst_path, $newId, $newName, $newDescription = null)
720 { 753 {
721 - if (!file_exists($src_path))  
722 - return FALSE;  
723 -  
724 - if (!is_dir($dst_path))  
725 - return FALSE;  
726 - 754 + if (!file_exists($src_path)) {
  755 + return false;
  756 + }
  757 +
  758 + if (!is_dir($dst_path)) {
  759 + return false;
  760 + }
  761 +
727 $dom = new DomDocument('1.0'); 762 $dom = new DomDocument('1.0');
728 $dom->formatOutput = true; 763 $dom->formatOutput = true;
729 -  
730 - if (!$dom->load($src_path))  
731 - return FALSE;  
732 - 764 +
  765 + if (!$dom->load($src_path)) {
  766 + return false;
  767 + }
  768 +
733 $timeTableNodes = $dom->getElementsByTagName('timetable'); 769 $timeTableNodes = $dom->getElementsByTagName('timetable');
734 - if ($timeTableNodes->length <= 0)  
735 - return FALSE;  
736 - 770 + if ($timeTableNodes->length <= 0) {
  771 + return false;
  772 + }
  773 +
737 $timeTableNode = $timeTableNodes->item(0); 774 $timeTableNode = $timeTableNodes->item(0);
738 - 775 +
739 $timeTableNode->setAttribute('xml:id', $newId); 776 $timeTableNode->setAttribute('xml:id', $newId);
740 - 777 +
741 $nameNodes = $timeTableNode->getElementsByTagName('name'); 778 $nameNodes = $timeTableNode->getElementsByTagName('name');
742 - 779 +
743 if ($nameNodes->length <= 0) { 780 if ($nameNodes->length <= 0) {
744 //create name node (normally never append) 781 //create name node (normally never append)
745 $nameNode = $dom->createElement('name'); 782 $nameNode = $dom->createElement('name');
746 $timeTableNode->appendChild($nameNode); 783 $timeTableNode->appendChild($nameNode);
747 - }  
748 - else 784 + } else {
749 $nameNode = $nameNodes->item(0); 785 $nameNode = $nameNodes->item(0);
750 - 786 + }
  787 +
751 $nameNode->nodeValue = $newName; 788 $nameNode->nodeValue = $newName;
752 - 789 +
753 if (isset($newDescription) && !empty($newDescription)) { 790 if (isset($newDescription) && !empty($newDescription)) {
754 $descriptionNodes = $timeTableNode->getElementsByTagName('description'); 791 $descriptionNodes = $timeTableNode->getElementsByTagName('description');
755 if ($descriptionNodes->length <= 0) { 792 if ($descriptionNodes->length <= 0) {
756 //create description node (normally never append) 793 //create description node (normally never append)
757 $descriptionNode = $dom->createElement('description'); 794 $descriptionNode = $dom->createElement('description');
758 $timeTableNode->appendChild($descriptionNode); 795 $timeTableNode->appendChild($descriptionNode);
759 - }  
760 - else 796 + } else {
761 $descriptionNode = $descriptionNodes->item(0); 797 $descriptionNode = $descriptionNodes->item(0);
762 - 798 + }
  799 +
763 $descriptionNode->nodeValue = $newDescription; 800 $descriptionNode->nodeValue = $newDescription;
764 } 801 }
765 -  
766 - $dst_file_path = $dst_path."/".$newId.".xml";  
767 - if ($dom->save($dst_file_path) === FALSE)  
768 - return FALSE;  
769 - 802 +
  803 + $dst_file_path = $dst_path . "/" . $newId . ".xml";
  804 + if ($dom->save($dst_file_path) === false) {
  805 + return false;
  806 + }
  807 +
770 chgrp($dst_file_path, APACHE_USER); 808 chgrp($dst_file_path, APACHE_USER);
771 chmod($dst_file_path, 0775); 809 chmod($dst_file_path, 0775);
772 -  
773 - return TRUE; 810 +
  811 + return true;
  812 + }
  813 +
  814 + /*
  815 + * intersect time tables
  816 + */
  817 +
  818 + protected function renameInResource($name, $id)
  819 + {
  820 + if (!file_exists(USERTTDIR . $id . '.xml')) {
  821 + return false;
  822 + }
  823 +
  824 + $this->objectDom->load(USERTTDIR . $id . '.xml');
  825 + if (!($objToRename = $this->objectDom->getElementById($id))) {
  826 + return false;
  827 + }
  828 + $objToRename->getElementsByTagName('name')->item(0)->nodeValue = $name;
  829 + $this->objectDom->save(USERTTDIR . $id . '.xml');
  830 +
  831 + return true;
  832 + }
  833 +
  834 + protected function deleteParameter($id)
  835 + {
  836 + if (file_exists(USERTTDIR . $id . '.xml')) {
  837 + unlink(USERTTDIR . $id . '.xml');
  838 + }
  839 + }
  840 +
  841 + protected function renameOnly($p)
  842 + {
  843 + //if (!($p->intervals)) return true;
  844 + return false;
774 } 845 }
775 } 846 }
  847 +
776 ?> 848 ?>