Blame view

php/classes/TimeTableCacheMgr.php 9.1 KB
16035364   Benjamin Renard   First commit
1
<?php
16035364   Benjamin Renard   First commit
2

342b20ca   Benjamin Renard   Fix inheritence b...
3
4
5
6
/**
 * @class CatalogCacheMgr
 */

d18b535d   elena   catalog draft + c...
7
 class TimeTableCacheMgr
16035364   Benjamin Renard   First commit
8
{
0fea5567   Benjamin Renard   First step for re...
9
	protected $objectMgr = null;
d18b535d   elena   catalog draft + c...
10
	protected $cache = null;
16035364   Benjamin Renard   First commit
11

0fea5567   Benjamin Renard   First step for re...
12
13
  function __construct() {
		$this->objectMgr = new TimeTableMgr();
16035364   Benjamin Renard   First commit
14
15
	}

0fea5567   Benjamin Renard   First step for re...
16
17
18
19
20
21
22
23
  protected function getCacheFileName() {
    return "cacheTT";
  }

  protected function getCacheDirectory() {
    return USERTTDIR;
  }

ba9b1b7d   Benjamin Renard   Rework of TT and ...
24
  protected function resetCache($options = array()) {
0fea5567   Benjamin Renard   First step for re...
25
    $this->cache = new TimeTableCacheObject();
ba9b1b7d   Benjamin Renard   Rework of TT and ...
26
27
28
    return array();
  }

42e2e019   Benjamin Renard   Next step for cac...
29
  protected function loadAdditionalDescription($options) {
ba9b1b7d   Benjamin Renard   Rework of TT and ...
30
    return array();
0fea5567   Benjamin Renard   First step for re...
31
32
33
  }

	public function initObjectCache($options = array()) {
16035364   Benjamin Renard   First commit
34
		//Create new cache
ba9b1b7d   Benjamin Renard   Rework of TT and ...
35
		$info = $this->resetCache($options);
5446b8f0   Benjamin Renard   Move CacheTools i...
36

16035364   Benjamin Renard   First commit
37
		//Save cache file
ba9b1b7d   Benjamin Renard   Rework of TT and ...
38
		return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus()) + $info;
16035364   Benjamin Renard   First commit
39
40
	}

0fea5567   Benjamin Renard   First step for re...
41
	public function initFromObject($id, $type) {
53048303   Benjamin Renard   Cleanup TimeTable...
42
    $options = $this->loadAdditionalDescription(array('id' => $id, 'type' => $type));
16035364   Benjamin Renard   First commit
43
		//Create new cache
ba9b1b7d   Benjamin Renard   Rework of TT and ...
44
		$info = $this->resetCache($options);
16035364   Benjamin Renard   First commit
45

0fea5567   Benjamin Renard   First step for re...
46
47
		//Load intervals from object file and add to cache
		$intervals_res = $this->objectMgr->loadIntervalsFromObject($id,$type);
5446b8f0   Benjamin Renard   Move CacheTools i...
48

16035364   Benjamin Renard   First commit
49
50
51
52
53
54
		if (!$intervals_res['success'])
			return $intervals_res;

		foreach ($intervals_res['intervals'] as $interval)
		{
			//Add interval
342b20ca   Benjamin Renard   Fix inheritence b...
55
      $this->cache->addInterval($interval);
16035364   Benjamin Renard   First commit
56
		}
5446b8f0   Benjamin Renard   Move CacheTools i...
57

16035364   Benjamin Renard   First commit
58
59
60
61
62
63
		unset($intervals_res);

		//Update cache
		$this->cache->updateIndexes();

		//Save cache file
ba9b1b7d   Benjamin Renard   Rework of TT and ...
64
		return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus()) + $info;
16035364   Benjamin Renard   First commit
65
66
	}

0fea5567   Benjamin Renard   First step for re...
67
       public function initFromCatalog($id, $type) {
d547a559   Hacene SI HADJ MOHAND   rm_6903 ok
68
                //Create new cache
0fea5567   Benjamin Renard   First step for re...
69
            $this->resetCache();
d547a559   Hacene SI HADJ MOHAND   rm_6903 ok
70

0fea5567   Benjamin Renard   First step for re...
71
72
73
                //Load intervals from object file and add to cache
                $catMgr = new CatalogMgr();
                $intervals_res = $catMgr->loadIntervalsFromObject($id,$type);
d547a559   Hacene SI HADJ MOHAND   rm_6903 ok
74
75
76
77
78
79
80

                if (!$intervals_res['success'])
                        return $intervals_res;

                foreach ($intervals_res['intervals'] as $interval)
                {
                        //Add interval
342b20ca   Benjamin Renard   Fix inheritence b...
81
                        $this->cache->addInterval($interval);
d547a559   Hacene SI HADJ MOHAND   rm_6903 ok
82
83
84
85
86
87
88
89
90
91
92
93
                }

                unset($intervals_res);

                //Update cache
                $this->cache->updateIndexes();

                //Save cache file
                return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus());
        }


16035364   Benjamin Renard   First commit
94
	public function initFromTmpObject($folderId, $name) {
53048303   Benjamin Renard   Cleanup TimeTable...
95
    $options = $this->loadAdditionalDescription(array('folder' => USERWORKINGDIR.$folderId, 'name' => $name));
16035364   Benjamin Renard   First commit
96
		//Create new cache
42e2e019   Benjamin Renard   Next step for cac...
97
		$info = $this->resetCache($options);
16035364   Benjamin Renard   First commit
98
99

		//Load intervals from TmpObject file (DD_Search output)
0fea5567   Benjamin Renard   First step for re...
100
		$intervals_res = $this->objectMgr->getTmpObject($folderId, $name);
16035364   Benjamin Renard   First commit
101
102
103
104
105
106
107
108
109

		if (!isset($intervals_res))
			return array('success' => false, 'message' => 'Cannot get Tmp Object');

		if (array_key_exists('intervals', $intervals_res))
		{
			foreach ($intervals_res['intervals'] as $interval)
			{
				//Add interval
342b20ca   Benjamin Renard   Fix inheritence b...
110
				$this->cache->addInterval($interval);
16035364   Benjamin Renard   First commit
111
112
113
114
			}
		}

		$this->cache->setIsModified(true);
5446b8f0   Benjamin Renard   Move CacheTools i...
115

16035364   Benjamin Renard   First commit
116
		unset($intervals_res);
5446b8f0   Benjamin Renard   Move CacheTools i...
117

16035364   Benjamin Renard   First commit
118
119
120
121
		//Update cache
		$this->cache->updateIndexes();

		//Save cache file
42e2e019   Benjamin Renard   Next step for cac...
122
		return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus()) + $info;
16035364   Benjamin Renard   First commit
123
124
125
	}

	public function initFromUploadedFile($name, $format) {
53048303   Benjamin Renard   Cleanup TimeTable...
126
    $options = $this->loadAdditionalDescription(array('folder' => USERTEMPDIR, 'name' => $name));
16035364   Benjamin Renard   First commit
127
		//Create new cache
42e2e019   Benjamin Renard   Next step for cac...
128
		$info = $this->resetCache($options);
16035364   Benjamin Renard   First commit
129
130

		//Load intervals from uploaded file
0fea5567   Benjamin Renard   First step for re...
131
		$intervals_res = $this->objectMgr->getUploadedObject($name, $format);
16035364   Benjamin Renard   First commit
132
133
134
135
136
137
138
139
140

		if (!isset($intervals_res))
			return array('success' => false, 'message' => 'Cannot get Tmp Object');

		if (array_key_exists('intervals', $intervals_res))
		{
			foreach ($intervals_res['intervals'] as $interval)
			{
				//Add interval
342b20ca   Benjamin Renard   Fix inheritence b...
141
				$this->cache->addInterval($interval);
16035364   Benjamin Renard   First commit
142
143
144
145
			}
		}

		$this->cache->setIsModified(true);
5446b8f0   Benjamin Renard   Move CacheTools i...
146

16035364   Benjamin Renard   First commit
147
		unset($intervals_res);
5446b8f0   Benjamin Renard   Move CacheTools i...
148

16035364   Benjamin Renard   First commit
149
150
151
152
		//Update cache
		$this->cache->updateIndexes();

		//Save cache file
42e2e019   Benjamin Renard   Next step for cac...
153
		return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus()) + $info;
16035364   Benjamin Renard   First commit
154
155
	}

0fea5567   Benjamin Renard   First step for re...
156
	public function saveInObject($id, $action, $token) {
16035364   Benjamin Renard   First commit
157
158
159
160
161
		if (!$this->loadFromFile())
			return array('success' => false, 'message' => 'Cannot load cache file');

		if ($token != $this->cache->getToken())
			return array('success' => false, 'message' => 'Cache token check error');
5446b8f0   Benjamin Renard   Move CacheTools i...
162

16035364   Benjamin Renard   First commit
163
164
		$this->cache->updateIndexes();
		$this->saveToFile();
5446b8f0   Benjamin Renard   Move CacheTools i...
165

16035364   Benjamin Renard   First commit
166
		$intervals = $this->cache->getIntervalsArray(NULL,NULL,true);
5446b8f0   Benjamin Renard   Move CacheTools i...
167

16035364   Benjamin Renard   First commit
168
		$this->cache->reset();
5446b8f0   Benjamin Renard   Move CacheTools i...
169

42e2e019   Benjamin Renard   Next step for cac...
170
171
172
    $options = $this->loadAdditionalDescription(array('id' => $id));

		return $this->objectMgr->saveIntervals($id, $intervals, $action, $options);
16035364   Benjamin Renard   First commit
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
	}

	public function getIntervals($start,$limit,$sort_obj,$filter_json) {
		if (!$this->loadFromFile())
			return array('success' => false, 'message' => 'Cannot load cache file');

		$needToUpdate = false;
		if (isset($filter_json))
		{
			if (!$this->cache->getFilter()->isSameFromJSON($filter_json))
			{
				$needToUpdate = true;
				$this->cache->getFilter()->loadFromJSON($filter_json);
			}
		}
		else
		{
			if (!$this->cache->getFilter()->isEmpty())
			{
				$needToUpdate = true;
				$this->cache->getFilter()->reset();
			}
		}
5446b8f0   Benjamin Renard   Move CacheTools i...
196

16035364   Benjamin Renard   First commit
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
		if (isset($sort_obj))
		{
			if (!$this->cache->getSort()->isSameFromObject($sort_obj))
			{
				$needToUpdate = true;
				$this->cache->getSort()->loadFromObject($sort_obj);
			}
		}
		else
		{
			if (!$this->cache->getSort()->isEmpty())
			{
				$needToUpdate = true;
				$this->cache->getSort()->reset();
			}
		}
5446b8f0   Benjamin Renard   Move CacheTools i...
213

16035364   Benjamin Renard   First commit
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
		if ($needToUpdate)
		{
			$this->cache->updateIndexes();
			$this->saveToFile();
		}

		return array(
				'token'      => $this->cache->getToken(),
				'totalCount' => $this->cache->getLength(),
				'intervals'  => $this->cache->getIntervalsArray($start, $limit),
				'start' => isset($start) ? $start : 0,
				'limit' => isset($limit) ? $limit : 0,
				'status' => $this->cache->getStatus(),
				'success'    => true
		);
	}

1a0151a5   Benjamin Renard   wip
231
232
233
234
  public function getIntervalsForChart() {
    return $this->getIntervals(NULL, NULL, NULL, NULL);
  }

16035364   Benjamin Renard   First commit
235
236
237
238
239
240
241
242
	public function addInterval($index, $start, $stop) {
		if (!$this->loadFromFile())
			return array('success' => false, 'message' => 'Cannot load cache file');

		if (!isset($index))
			$index = 0;

		if (!isset($start))
1fe67f2c   Hacene SI HADJ MOHAND   progress
243
			$start = date('Y-m-d\TH:i:s.u');
16035364   Benjamin Renard   First commit
244
245

		if (!isset($stop))
1fe67f2c   Hacene SI HADJ MOHAND   progress
246
			$stop = date('Y-m-d\TH:i:s.u');
5446b8f0   Benjamin Renard   Move CacheTools i...
247

342b20ca   Benjamin Renard   Fix inheritence b...
248
		$this->cache->addInterval(array('start' => $start, 'stop' => $stop), true, $index);
5446b8f0   Benjamin Renard   Move CacheTools i...
249

16035364   Benjamin Renard   First commit
250
		//$this->cache->updateIndexes();
5446b8f0   Benjamin Renard   Move CacheTools i...
251

16035364   Benjamin Renard   First commit
252
		$this->saveToFile();
5446b8f0   Benjamin Renard   Move CacheTools i...
253

16035364   Benjamin Renard   First commit
254
255
256
257
258
259
260
261
		return array('success' => true, 'index' => $index, 'status' => $this->cache->getStatus());
	}

	public function removeIntervalFromId($id) {
		if (!$this->loadFromFile())
			return array('success' => false, 'message' => 'Cannot load cache file');

		$this->cache->removeIntervalFromId($id);
5446b8f0   Benjamin Renard   Move CacheTools i...
262

16035364   Benjamin Renard   First commit
263
264
265
266
267
268
269
		$this->cache->updateIndexes();

		$this->saveToFile();

		return array('success' => true, 'status' => $this->cache->getStatus());
	}

342b20ca   Benjamin Renard   Fix inheritence b...
270
	public function modifyIntervalFromId($cacheId, $data) {
16035364   Benjamin Renard   First commit
271
272
273
		if (!$this->loadFromFile())
			return array('success' => false, 'message' => 'Cannot load cache file');

342b20ca   Benjamin Renard   Fix inheritence b...
274
		$this->cache->modifyIntervalFromId($cacheId, $data);
5446b8f0   Benjamin Renard   Move CacheTools i...
275

16035364   Benjamin Renard   First commit
276
		$this->saveToFile();
5446b8f0   Benjamin Renard   Move CacheTools i...
277

16035364   Benjamin Renard   First commit
278
279
		return array('success' => true, 'status' => $this->cache->getStatus());
	}
5446b8f0   Benjamin Renard   Move CacheTools i...
280

16035364   Benjamin Renard   First commit
281
282
283
	public function operationIntervals($extendTime, $shiftTime) {
		if (!$this->loadFromFile())
			return array('success' => false, 'message' => 'Cannot load cache file');
5446b8f0   Benjamin Renard   Move CacheTools i...
284

16035364   Benjamin Renard   First commit
285
		$this->cache->operationIntervals($extendTime, $shiftTime);
5446b8f0   Benjamin Renard   Move CacheTools i...
286

16035364   Benjamin Renard   First commit
287
		$this->saveToFile();
5446b8f0   Benjamin Renard   Move CacheTools i...
288

16035364   Benjamin Renard   First commit
289
290
		return array('success' => true, 'status' => $this->cache->getStatus());
	}
5446b8f0   Benjamin Renard   Move CacheTools i...
291

16035364   Benjamin Renard   First commit
292
293
294
	public function mergeIntervals() {
		if (!$this->loadFromFile())
			return array('success' => false, 'message' => 'Cannot load cache file');
5446b8f0   Benjamin Renard   Move CacheTools i...
295

16035364   Benjamin Renard   First commit
296
		$this->cache->mergeIntervals();
5446b8f0   Benjamin Renard   Move CacheTools i...
297

16035364   Benjamin Renard   First commit
298
		$this->saveToFile();
5446b8f0   Benjamin Renard   Move CacheTools i...
299

16035364   Benjamin Renard   First commit
300
301
		return array('success' => true, 'status' => $this->cache->getStatus());
	}
5446b8f0   Benjamin Renard   Move CacheTools i...
302

16035364   Benjamin Renard   First commit
303
304
305
	public function getStatistics() {
		if (!$this->loadFromFile())
			return array('success' => false, 'message' => 'Cannot load cache file');
5446b8f0   Benjamin Renard   Move CacheTools i...
306

16035364   Benjamin Renard   First commit
307
308
309
310
311
312
313
314
315
316
317
		return array('success' => true, "result" => $this->cache->getStatistics(), 'status' => $this->cache->getStatus());
	}

	public function dump() {
		if (!$this->loadFromFile())
		{
			echo "ERROR to load cache file : ".$this->getCacheFilePath().PHP_EOL;
			return;
		}
		$this->cache->dump();
	}
5446b8f0   Benjamin Renard   Move CacheTools i...
318

0fea5567   Benjamin Renard   First step for re...
319
320
  protected function getCacheFilePath() {
		return $this->getCacheDirectory().'/'.$this->getCacheFileName();
16035364   Benjamin Renard   First commit
321
	}
5446b8f0   Benjamin Renard   Move CacheTools i...
322

d18b535d   elena   catalog draft + c...
323
	protected function saveToFile() {
16035364   Benjamin Renard   First commit
324
325
326
327
328
329
330
331
332
333
334
335
336
		if (!isset($this->cache))
			return false;
		$handle = fopen($this->getCacheFilePath(), 'wb');
		$result = false;
		if (flock($handle, LOCK_EX))
		{
			$this->cache->writeBin($handle);
			flock( $handle, LOCK_UN );
			$result = true;
		}
		fclose($handle);
		return $result;
	}
5446b8f0   Benjamin Renard   Move CacheTools i...
337

d18b535d   elena   catalog draft + c...
338
	protected function loadFromFile() {
16035364   Benjamin Renard   First commit
339
340
		if (!file_exists($this->getCacheFilePath()))
			return false;
0fea5567   Benjamin Renard   First step for re...
341
		$this->resetCache();
16035364   Benjamin Renard   First commit
342
343
344
345
346
347
348
349
350
351
352
353
354
355
		$handle = fopen($this->getCacheFilePath(), 'rb');
		$result = false;
		if (flock($handle, LOCK_SH))
		{
			$this->cache->loadBin($handle);
			flock( $handle, LOCK_UN );
			$result = true;
		}
		fclose($handle);
		return $result;
	}
}

?>