Blame view

php/classes/TimeTableCacheMgr.php 8.42 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
24
25
26
27
28
  protected function getCacheFileName() {
    return "cacheTT";
  }

  protected function getCacheDirectory() {
    return USERTTDIR;
  }

  protected function resetCache() {
    $this->cache = new TimeTableCacheObject();
  }

	public function initObjectCache($options = array()) {
16035364   Benjamin Renard   First commit
29
		//Create new cache
0fea5567   Benjamin Renard   First step for re...
30
		$this->resetCache();
5446b8f0   Benjamin Renard   Move CacheTools i...
31

16035364   Benjamin Renard   First commit
32
33
34
35
		//Save cache file
		return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus());
	}

0fea5567   Benjamin Renard   First step for re...
36
	public function initFromObject($id, $type) {
16035364   Benjamin Renard   First commit
37
		//Create new cache
0fea5567   Benjamin Renard   First step for re...
38
		$this->resetCache();
16035364   Benjamin Renard   First commit
39

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

16035364   Benjamin Renard   First commit
43
44
45
46
47
48
		if (!$intervals_res['success'])
			return $intervals_res;

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

16035364   Benjamin Renard   First commit
52
53
54
55
56
57
58
59
60
		unset($intervals_res);

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

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

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

0fea5567   Benjamin Renard   First step for re...
65
66
67
                //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
68
69
70
71
72
73
74

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

                foreach ($intervals_res['intervals'] as $interval)
                {
                        //Add interval
342b20ca   Benjamin Renard   Fix inheritence b...
75
                        $this->cache->addInterval($interval);
d547a559   Hacene SI HADJ MOHAND   rm_6903 ok
76
77
78
79
80
81
82
83
84
85
86
87
                }

                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
88
89
	public function initFromTmpObject($folderId, $name) {
		//Create new cache
0fea5567   Benjamin Renard   First step for re...
90
		$this->resetCache();
16035364   Benjamin Renard   First commit
91
92

		//Load intervals from TmpObject file (DD_Search output)
0fea5567   Benjamin Renard   First step for re...
93
		$intervals_res = $this->objectMgr->getTmpObject($folderId, $name);
16035364   Benjamin Renard   First commit
94
95
96
97
98
99
100
101
102

		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...
103
				$this->cache->addInterval($interval);
16035364   Benjamin Renard   First commit
104
105
106
107
			}
		}

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

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

16035364   Benjamin Renard   First commit
111
112
113
114
115
116
117
118
119
		//Update cache
		$this->cache->updateIndexes();

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

	public function initFromUploadedFile($name, $format) {
		//Create new cache
0fea5567   Benjamin Renard   First step for re...
120
		$this->resetCache();
16035364   Benjamin Renard   First commit
121
122

		//Load intervals from uploaded file
0fea5567   Benjamin Renard   First step for re...
123
		$intervals_res = $this->objectMgr->getUploadedObject($name, $format);
16035364   Benjamin Renard   First commit
124
125
126
127
128
129
130
131
132

		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...
133
				$this->cache->addInterval($interval);
16035364   Benjamin Renard   First commit
134
135
136
137
			}
		}

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

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

16035364   Benjamin Renard   First commit
141
142
143
144
145
146
147
		//Update cache
		$this->cache->updateIndexes();

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

0fea5567   Benjamin Renard   First step for re...
148
	public function saveInObject($id, $action, $token) {
16035364   Benjamin Renard   First commit
149
150
151
152
153
		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...
154

16035364   Benjamin Renard   First commit
155
156
		$this->cache->updateIndexes();
		$this->saveToFile();
5446b8f0   Benjamin Renard   Move CacheTools i...
157

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

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

342b20ca   Benjamin Renard   Fix inheritence b...
162
		return $this->objectMgr->saveIntervals($id, $intervals, $action);
16035364   Benjamin Renard   First commit
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
	}

	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...
186

16035364   Benjamin Renard   First commit
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
		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...
203

16035364   Benjamin Renard   First commit
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
		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...
233

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

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

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

16035364   Benjamin Renard   First commit
240
241
242
243
244
245
246
247
		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...
248

16035364   Benjamin Renard   First commit
249
250
251
252
253
254
255
		$this->cache->updateIndexes();

		$this->saveToFile();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

16035364   Benjamin Renard   First commit
293
294
295
296
297
298
299
300
301
302
303
		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...
304

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

d18b535d   elena   catalog draft + c...
309
	protected function saveToFile() {
16035364   Benjamin Renard   First commit
310
311
312
313
314
315
316
317
318
319
320
321
322
		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...
323

d18b535d   elena   catalog draft + c...
324
	protected function loadFromFile() {
16035364   Benjamin Renard   First commit
325
326
		if (!file_exists($this->getCacheFilePath()))
			return false;
0fea5567   Benjamin Renard   First step for re...
327
		$this->resetCache();
16035364   Benjamin Renard   First commit
328
329
330
331
332
333
334
335
336
337
338
339
340
341
		$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;
	}
}

?>