Blame view

php/classes/TimeTableCacheMgr.php 9 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
231
232
233
234
235
236
237
238
239
240
241
242
		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
		);
	}

	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))
			$start = date('Y-m-d\TH:i:s');

		if (!isset($stop))
			$stop = date('Y-m-d\TH:i:s');
5446b8f0   Benjamin Renard   Move CacheTools i...
243

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

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

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

16035364   Benjamin Renard   First commit
250
251
252
253
254
255
256
257
		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...
258

16035364   Benjamin Renard   First commit
259
260
261
262
263
264
265
		$this->cache->updateIndexes();

		$this->saveToFile();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

16035364   Benjamin Renard   First commit
303
304
305
306
307
308
309
310
311
312
313
		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...
314

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

d18b535d   elena   catalog draft + c...
319
	protected function saveToFile() {
16035364   Benjamin Renard   First commit
320
321
322
323
324
325
326
327
328
329
330
331
332
		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...
333

d18b535d   elena   catalog draft + c...
334
	protected function loadFromFile() {
16035364   Benjamin Renard   First commit
335
336
		if (!file_exists($this->getCacheFilePath()))
			return false;
0fea5567   Benjamin Renard   First step for re...
337
		$this->resetCache();
16035364   Benjamin Renard   First commit
338
339
340
341
342
343
344
345
346
347
348
349
350
351
		$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;
	}
}

?>