Blame view

php/classes/TimeTableMgr.php 23.5 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
11
12
13
14
15
16
17
18
19
20
21
function timeFormat($myString) 
{
	if (format == "Y z H i s")  {
		$tt = getdate(strtotime($myString));
		return sprintf("%04d",$tt["year"])." "
			.sprintf("%03d",$tt["yday"]+1)." "
			.sprintf("%02d",$tt["hours"])." "
			.sprintf("%02d",$tt["minutes"])." "
			.sprintf("%02d",$tt["seconds"]);
	}
	return date(format,strtotime($myString));     
}
16035364   Benjamin Renard   First commit
22

4a438b99   elena   catalog draft
23
24
class TimeTableMgr extends AmdaObjectMgr
{     
e57cb025   Benjamin Renard   Fix most of error...
25
	function __construct($user = NULL, $sharedObject = FALSE) 
4a438b99   elena   catalog draft
26
	{
16035364   Benjamin Renard   First commit
27
28
29
30
31
32
33
34
		parent::__construct('Tt.xml');
		$this->contentRootId = 'timeTable-treeRootNode';
		$this->contentRootTag = 'timetabList';
		$this->attributes = array('name' => '', 'intervals' => '');
		$this->optionalAttributes = array();
		$this->objTagName = 'timetab';
		$this->id_prefix = 'tt_';

169f14d2   Benjamin Renard   Add shared object...
35
		if (!$sharedObject && !file_exists($this->xmlName)) {
16035364   Benjamin Renard   First commit
36
37
38
39
40
			  $this->createDom();
			  $this->xp = new domxpath($this->contentDom); 
		}
	}
	
3ed056c4   Elena.Budnik   redmine #5278
41
42
	protected function createDom() 
	{
16035364   Benjamin Renard   First commit
43
44
45
		$types = array('timetab' => 'timeTable', 'catalog' => 'catalog');

		$rootElement = $this->contentDom->createElement('ws');
3ed056c4   Elena.Budnik   redmine #5278
46
47
48
49
50
51
52
		
		foreach ($types as $key => $value) {
			$contentId = $value.'-treeRootNode';
			$contentTag = $key.'List';
			$typeElement = $this->contentDom->createElement($contentTag);
			$typeElement->setAttribute('xml:id', $contentId);
			$rootElement->appendChild($typeElement);
16035364   Benjamin Renard   First commit
53
		}
4a438b99   elena   catalog draft
54
		$this->contentDom->appendChild($rootElement);
16035364   Benjamin Renard   First commit
55
		$this->contentDom->save($this->xmlName);
3ed056c4   Elena.Budnik   redmine #5278
56
	}
16035364   Benjamin Renard   First commit
57

3ed056c4   Elena.Budnik   redmine #5278
58
59
60
	/*
	*       rename  Time Table in  id.xml
	*/
4a438b99   elena   catalog draft
61
62
	protected function renameInResource($name, $id) 
	{
16035364   Benjamin Renard   First commit
63
64
65
66
67
68
69
70
71
72
		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;
	}

4a438b99   elena   catalog draft
73
74
	protected function deleteParameter($id)
	{
16035364   Benjamin Renard   First commit
75
76
77
78
		if (file_exists(USERTTDIR.$id.'.xml')) unlink(USERTTDIR.$id.'.xml');
	}

	/*
4a438b99   elena   catalog draft
79
80
	*        Check if difference is name and info only
	*/
16035364   Benjamin Renard   First commit
81
82
83
84
	protected function renameOnly($p) {
		//if (!($p->intervals)) return true;
		return false;
	}
d18b535d   elena   catalog draft + c...
85
86
87
88
89
         
        /*
        *  In case of catalogs
        */
        protected function setParamDescription($param) { }
16035364   Benjamin Renard   First commit
90

d18b535d   elena   catalog draft + c...
91
        
16035364   Benjamin Renard   First commit
92
93
94
	/*
	 *         Create Time Table
	 */
4a438b99   elena   catalog draft
95
96
	protected function createParameter($p, $folder)
	{
16035364   Benjamin Renard   First commit
97
98
99
100
		if ($this -> objectExistsByName($p->name)) {
			$p -> id  = $this -> getObjectIdByName($p->name);
			$this -> deleteObject($p);
		}
3ed056c4   Elena.Budnik   redmine #5278
101
		
16035364   Benjamin Renard   First commit
102
103
104
		$this->id = $this->setId();
		$this->created = date('Y-m-d\TH:i:s');
		if (!$this->id) return array('error' => ID_CREATION_ERROR);
d18b535d   elena   catalog draft + c...
105
		
16035364   Benjamin Renard   First commit
106
		$this->resFileName = USERTTDIR.$this->id.'.xml';
d18b535d   elena   catalog draft + c...
107
		//TODO catalog root element = 'timetable'
16035364   Benjamin Renard   First commit
108
109
110
111
		$rootElement = $this->objectDom->createElement('timetable');
		$rootElement->setAttribute('xml:id',$this->id);
	 
		foreach ($p as $key => $value)		
3ed056c4   Elena.Budnik   redmine #5278
112
113
114
115
116
			if ($key != 'id' && $key != 'leaf' && $key != 'nodeType' &&
				$key != 'objName' && $key != 'objFormat' && $key != 'folderId' && $key != 'cacheToken') {
					if ($key == 'created') {
						$rootElement->appendChild($this->objectDom->createElement($key, $this->created));
					}
3ed056c4   Elena.Budnik   redmine #5278
117
118
119
120
121
122
123
124
					// it is catalog 
					else if ($key == 'parameters') {
					$paramsElement = $this->setParamDescription($value);
					if ($paramsElement) $rootElement->appendChild($paramsElement);
					
					}
					else if ($key != 'intervals')
						$rootElement->appendChild($this->objectDom->createElement($key, htmlspecialchars($value)));
d18b535d   elena   catalog draft + c...
125
			}
16035364   Benjamin Renard   First commit
126
127
128
129
130
131
132
133

		$this->objectDom->appendChild($rootElement);
		$this->objectDom->save($this->resFileName);
		$obj = new stdClass();
		$obj->name = $p->name;
		$obj->intervals = $p->nbIntervals;
		$this -> addToContent($obj, $folder);
		return array('id' => $this->id,'created' => $this->created,'info' =>$obj->intervals.' intervals' );
16035364   Benjamin Renard   First commit
134
135
	}

4a438b99   elena   catalog draft
136
137
138
139
140
141
142
143
	protected function call_intersection($fst, $snd) 
	{
		$inf = ( $fst[0] > $snd[0] ) ? $fst[0] : $snd[0];
		$sup = ( $fst[1] < $snd[1] ) ? $fst[1] : $snd[1];
		if ( $inf >= $sup ) { $inter[] = array(0,0); }
		else {$inter[] = array($inf,$sup); }
		return $inter;
	}
16035364   Benjamin Renard   First commit
144
    
3ed056c4   Elena.Budnik   redmine #5278
145
146
147
	/*
	* Uploaded text file => convert to array
	*/
4a438b99   elena   catalog draft
148
149
150
151
152
153
	protected function text2amda($tmp_file, $onlyDescription = false) 
	{  
		$suffix = explode('.', basename($tmp_file));
		$lines = file($tmp_file,FILE_SKIP_EMPTY_LINES);
		$description="Uploaded Time Table".PHP_EOL; 

f8cd7d84   Elena.Budnik   error msg if cann...
154
155
156
		$recordsNumber = count($lines);
		$descNumber = 0;
		
6207ec14   Elena.Budnik   #5919 non standar...
157
158
159
160
161
		foreach ($lines as $line)
		{  
			if ($line[0] == '#') {
				$description=$description.PHP_EOL.substr($line,1,-1);
			}
4a438b99   elena   catalog draft
162
			else {
6207ec14   Elena.Budnik   #5919 non standar...
163
164
				$date = explode(' ', trim(preg_replace('!\s+!', ' ',$line)));
				
4a438b99   elena   catalog draft
165
				if (!strtotime(trim($date[0]))) {
dca1b251   Elena.Budnik   non standard time...
166
					$description=$description.PHP_EOL.$line;
f8cd7d84   Elena.Budnik   error msg if cann...
167
					$descNumber++;
4a438b99   elena   catalog draft
168
169
					continue;
				}
3ed056c4   Elena.Budnik   redmine #5278
170
				// check if it is ISO format
6207ec14   Elena.Budnik   #5919 non standar...
171
172
173
				if (!isset($isIso)) 
					$isIso = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})$/', trim($date[0]));
					
3ed056c4   Elena.Budnik   redmine #5278
174
				if (!$isIso) {