Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * Time Helper class file.
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://cakephp.org CakePHP(tm) Project
14
 * @package       Cake.View.Helper
15
 * @since         CakePHP(tm) v 0.10.0.1076
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('CakeTime', 'Utility');
20
App::uses('Multibyte', 'I18n');
21
App::uses('AppHelper', 'View/Helper');
22
 
23
/**
24
 * Time Helper class for easy use of time data.
25
 *
26
 * Manipulation of time data.
27
 *
28
 * @package       Cake.View.Helper
29
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html
30
 * @see CakeTime
31
 */
32
class TimeHelper extends AppHelper {
33
 
34
/**
35
 * CakeTime instance
36
 *
37
 * @var stdClass
38
 */
39
	protected $_engine = null;
40
 
41
/**
42
 * Constructor
43
 *
44
 * ### Settings:
45
 *
46
 * - `engine` Class name to use to replace CakeTime functionality
47
 *            The class needs to be placed in the `Utility` directory.
48
 *
49
 * @param View $View the view object the helper is attached to.
50
 * @param array $settings Settings array
51
 * @throws CakeException When the engine class could not be found.
52
 */
53
	public function __construct(View $View, $settings = array()) {
54
		$settings = Hash::merge(array('engine' => 'CakeTime'), $settings);
55
		parent::__construct($View, $settings);
56
		list($plugin, $engineClass) = pluginSplit($settings['engine'], true);
57
		App::uses($engineClass, $plugin . 'Utility');
58
		if (class_exists($engineClass)) {
59
			$this->_engine = new $engineClass($settings);
60
		} else {
61
			throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
62
		}
63
	}
64
 
65
/**
66
 * Magic accessor for deprecated attributes.
67
 *
68
 * @param string $name Name of the attribute to set.
69
 * @param string $value Value of the attribute to set.
70
 * @return void
71
 */
72
	public function __set($name, $value) {
73
		switch ($name) {
74
			case 'niceFormat':
75
				$this->_engine->{$name} = $value;
76
				break;
77
			default:
78
				$this->{$name} = $value;
79
		}
80
	}
81
 
82
/**
83
 * Magic isset check for deprecated attributes.
84
 *
85
 * @param string $name Name of the attribute to check.
86
 * @return boolean
87
 */
88
	public function __isset($name) {
89
		if (isset($this->{$name})) {
90
			return true;
91
		}
92
		$magicGet = array('niceFormat');
93
		if (in_array($name, $magicGet)) {
94
			return $this->__get($name) !== null;
95
		}
96
		return null;
97
	}
98
 
99
/**
100
 * Magic accessor for attributes that were deprecated.
101
 *
102
 * @param string $name Name of the attribute to get.
103
 * @return mixed
104
 */
105
	public function __get($name) {
106
		if (isset($this->_engine->{$name})) {
107
			return $this->_engine->{$name};
108
		}
109
		$magicGet = array('niceFormat');
110
		if (in_array($name, $magicGet)) {
111
			return $this->_engine->{$name};
112
		}
113
		return null;
114
	}
115
 
116
/**
117
 * Call methods from CakeTime utility class
118
 * @return mixed Whatever is returned by called method, or false on failure
119
 */
120
	public function __call($method, $params) {
121
		return call_user_func_array(array($this->_engine, $method), $params);
122
	}
123
 
124
/**
125
 * @see CakeTime::convertSpecifiers()
126
 *
127
 * @param string $format Format with specifiers for strftime function.
128
 *    Accepts the special specifier %S which mimics the modifier S for date()
129
 * @param string $time UNIX timestamp
130
 * @return string windows safe and date() function compatible format for strftime
131
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
132
 */
133
	public function convertSpecifiers($format, $time = null) {
134
		return $this->_engine->convertSpecifiers($format, $time);
135
	}
136
 
137
/**
138
 * @see CakeTime::convert()
139
 *
140
 * @param string $serverTime UNIX timestamp
141
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
142
 * @return integer UNIX timestamp
143
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
144
 */
145
	public function convert($serverTime, $timezone) {
146
		return $this->_engine->convert($serverTime, $timezone);
147
	}
148
 
149
/**
150
 * @see CakeTime::serverOffset()
151
 *
152
 * @return integer Offset
153
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
154
 */
155
	public function serverOffset() {
156
		return $this->_engine->serverOffset();
157
	}
158
 
159
/**
160
 * @see CakeTime::fromString()
161
 *
162
 * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
163
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
164
 * @return string Parsed timestamp
165
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
166
 */
167
	public function fromString($dateString, $timezone = null) {
168
		return $this->_engine->fromString($dateString, $timezone);
169
	}
170
 
171
/**
172
 * @see CakeTime::nice()
173
 *
174
 * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
175
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
176
 * @param string $format The format to use. If null, `TimeHelper::$niceFormat` is used
177
 * @return string Formatted date string
178
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
179
 */
180
	public function nice($dateString = null, $timezone = null, $format = null) {
181
		return $this->_engine->nice($dateString, $timezone, $format);
182
	}
183
 
184
/**
185
 * @see CakeTime::niceShort()
186
 *
187
 * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime objectp
188
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
189
 * @return string Described, relative date string
190
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
191
 */
192
	public function niceShort($dateString = null, $timezone = null) {
193
		return $this->_engine->niceShort($dateString, $timezone);
194
	}
195
 
196
/**
197
 * @see CakeTime::daysAsSql()
198
 *
199
 * @param integer|string|DateTime $begin UNIX timestamp, strtotime() valid string or DateTime object
200
 * @param integer|string|DateTime $end UNIX timestamp, strtotime() valid string or DateTime object
201
 * @param string $fieldName Name of database field to compare with
202
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
203
 * @return string Partial SQL string.
204
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
205
 */
206
	public function daysAsSql($begin, $end, $fieldName, $timezone = null) {
207
		return $this->_engine->daysAsSql($begin, $end, $fieldName, $timezone);
208
	}
209
 
210
/**
211
 * @see CakeTime::dayAsSql()
212
 *
213
 * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
214
 * @param string $fieldName Name of database field to compare with
215
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
216
 * @return string Partial SQL string.
217
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
218
 */
219
	public function dayAsSql($dateString, $fieldName, $timezone = null) {
220
		return $this->_engine->dayAsSql($dateString, $fieldName, $timezone);
221
	}
222
 
223
/**
224
 * @see CakeTime::isToday()
225
 *
226
 * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
227
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
228
 * @return boolean True if datetime string is today
229
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
230
 */
231
	public function isToday($dateString, $timezone = null) {
232
		return $this->_engine->isToday($dateString, $timezone);
233
	}
234
 
235
/**
236
 * @see CakeTime::isThisWeek()
237
 *
238
 * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
239
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
240
 * @return boolean True if datetime string is within current week
241
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
242
 */
243
	public function isThisWeek($dateString, $timezone = null) {
244
		return $this->_engine->isThisWeek($dateString, $timezone);
245
	}
246
 
247
/**
248
 * @see CakeTime::isThisMonth()
249
 *
250
 * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
251
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
252
 * @return boolean True if datetime string is within current month
253
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
254
 */
255
	public function isThisMonth($dateString, $timezone = null) {
256
		return $this->_engine->isThisMonth($dateString, $timezone);
257
	}
258
 
259
/**
260
 * @see CakeTime::isThisYear()
261
 *
262
 * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
263
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
264
 * @return boolean True if datetime string is within current year
265
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
266
 */
267
	public function isThisYear($dateString, $timezone = null) {
268
		return $this->_engine->isThisYear($dateString, $timezone);
269
	}
270
 
271
/**
272
 * @see CakeTime::wasYesterday()
273
 *
274
 * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
275
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
276
 * @return boolean True if datetime string was yesterday
277
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
278
 *
279
 */
280
	public function wasYesterday($dateString, $timezone = null) {
281
		return $this->_engine->wasYesterday($dateString, $timezone);
282
	}
283
 
284
/**
285
 * @see CakeTime::isTomorrow()
286
 *
287
 * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
288
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
289
 * @return boolean True if datetime string was yesterday
290
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
291
 */
292
	public function isTomorrow($dateString, $timezone = null) {
293
		return $this->_engine->isTomorrow($dateString, $timezone);
294
	}
295
 
296
/**
297
 * @see CakeTime::toQuarter()
298
 *
299
 * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
300
 * @param boolean $range if true returns a range in Y-m-d format
301
 * @return mixed 1, 2, 3, or 4 quarter of year or array if $range true
302
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
303
 */
304
	public function toQuarter($dateString, $range = false) {
305
		return $this->_engine->toQuarter($dateString, $range);
306
	}
307
 
308
/**
309
 * @see CakeTime::toUnix()
310
 *
311
 * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
312
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
313
 * @return integer Unix timestamp
314
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
315
 */
316
	public function toUnix($dateString, $timezone = null) {
317
		return $this->_engine->toUnix($dateString, $timezone);
318
	}
319
 
320
/**
321
 * @see CakeTime::toAtom()
322
 *
323
 * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
324
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
325
 * @return string Formatted date string
326
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
327
 */
328
	public function toAtom($dateString, $timezone = null) {
329
		return $this->_engine->toAtom($dateString, $timezone);
330
	}
331
 
332
/**
333
 * @see CakeTime::toRSS()
334
 *
335
 * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
336
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
337
 * @return string Formatted date string
338
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
339
 */
340
	public function toRSS($dateString, $timezone = null) {
341
		return $this->_engine->toRSS($dateString, $timezone);
342
	}
343
 
344
/**
345
 * @see CakeTime::timeAgoInWords()
346
 *
347
 * ## Addition options
348
 *
349
 * - `element` - The element to wrap the formatted time in.
350
 *   Has a few additional options:
351
 *   - `tag` - The tag to use, defaults to 'span'.
352
 *   - `class` - The class name to use, defaults to `time-ago-in-words`.
353
 *   - `title` - Defaults to the $dateTime input.
354
 *
355
 * @param integer|string|DateTime $dateTime UNIX timestamp, strtotime() valid string or DateTime object
356
 * @param array $options Default format if timestamp is used in $dateString
357
 * @return string Relative time string.
358
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
359
 */
360
	public function timeAgoInWords($dateTime, $options = array()) {
361
		$element = null;
362
 
363
		if (!empty($options['element'])) {
364
			$element = array(
365
				'tag' => 'span',
366
				'class' => 'time-ago-in-words',
367
				'title' => $dateTime
368
			);
369
 
370
			if (is_array($options['element'])) {
371
				$element = array_merge($element, $options['element']);
372
			} else {
373
				$element['tag'] = $options['element'];
374
			}
375
			unset($options['element']);
376
		}
377
		$relativeDate = $this->_engine->timeAgoInWords($dateTime, $options);
378
 
379
		if ($element) {
380
			$relativeDate = sprintf(
381
				'<%s%s>%s</%s>',
382
				$element['tag'],
383
				$this->_parseAttributes($element, array('tag')),
384
				$relativeDate,
385
				$element['tag']
386
			);
387
		}
388
		return $relativeDate;
389
	}
390
 
391
/**
392
 * @see CakeTime::wasWithinLast()
393
 *
394
 * @param string|integer $timeInterval the numeric value with space then time type.
395
 *    Example of valid types: 6 hours, 2 days, 1 minute.
396
 * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
397
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
398
 * @return boolean
399
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
400
 */
401
	public function wasWithinLast($timeInterval, $dateString, $timezone = null) {
402
		return $this->_engine->wasWithinLast($timeInterval, $dateString, $timezone);
403
	}
404
 
405
/**
406
 * @see CakeTime::isWithinLast()
407
 *
408
 * @param string|integer $timeInterval the numeric value with space then time type.
409
 *    Example of valid types: 6 hours, 2 days, 1 minute.
410
 * @param integer|string|DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
411
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
412
 * @return boolean
413
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
414
 */
415
	public function isWithinNext($timeInterval, $dateString, $timezone = null) {
416
		return $this->_engine->isWithinNext($timeInterval, $dateString, $timezone);
417
	}
418
 
419
/**
420
 * @see CakeTime::gmt()
421
 *
422
 * @param integer|string|DateTime $string UNIX timestamp, strtotime() valid string or DateTime object
423
 * @return integer UNIX timestamp
424
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
425
 */
426
	public function gmt($string = null) {
427
		return $this->_engine->gmt($string);
428
	}
429
 
430
/**
431
 * @see CakeTime::format()
432
 *
433
 * @param integer|string|DateTime $format date format string (or a UNIX timestamp, strtotime() valid string or DateTime object)
434
 * @param integer|string|DateTime $date UNIX timestamp, strtotime() valid string or DateTime object (or a date format string)
435
 * @param boolean $invalid flag to ignore results of fromString == false
436
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
437
 * @return string Formatted date string
438
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
439
 */
440
	public function format($format, $date = null, $invalid = false, $timezone = null) {
441
		return $this->_engine->format($format, $date, $invalid, $timezone);
442
	}
443
 
444
/**
445
 * @see CakeTime::i18nFormat()
446
 *
447
 * @param integer|string|DateTime $date UNIX timestamp, strtotime() valid string or DateTime object
448
 * @param string $format strftime format string.
449
 * @param boolean $invalid flag to ignore results of fromString == false
450
 * @param string|DateTimeZone $timezone User's timezone string or DateTimeZone object
451
 * @return string Formatted and translated date string
452
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
453
 */
454
	public function i18nFormat($date, $format = null, $invalid = false, $timezone = null) {
455
		return $this->_engine->i18nFormat($date, $format, $invalid, $timezone);
456
	}
457
 
458
}