Move out HTML logic to Helper/TimeHelper.php. The Utility/CakeTime.php timeAgoInWords() now returns a string with the time, while the same function in Helper/TimeHelper.php may return an HTML element.

This commit is contained in:
Magnus Johansson 2012-05-09 20:00:22 +02:00 committed by mark_story
parent 9fa531d6e3
commit 352fbde2ec
2 changed files with 47 additions and 27 deletions

View file

@ -666,7 +666,6 @@ class CakeTime {
$timezone = null; $timezone = null;
$format = self::$wordFormat; $format = self::$wordFormat;
$end = self::$wordEnd; $end = self::$wordEnd;
$element = null;
$accuracy = self::$wordAccuracy; $accuracy = self::$wordAccuracy;
if (is_array($options)) { if (is_array($options)) {
@ -686,24 +685,6 @@ class CakeTime {
} }
} }
if (isset($options['element'])) {
$element_options = array(
'tag' => 'span',
'class' => 'timeAgoInWords',
'title' => $dateTime
);
if (is_array($options['element'])) {
$element = array_merge($element_options, $options['element']);
} else {
if ($options['element']) {
$element = $element_options;
$element['tag'] = $options['element'];
} else {
$element = null;
}
}
}
if (isset($options['format'])) { if (isset($options['format'])) {
$format = $options['format']; $format = $options['format'];
unset($options['format']); unset($options['format']);
@ -850,13 +831,6 @@ class CakeTime {
$relativeDate = __d('cake', 'just now', 'just now'); $relativeDate = __d('cake', 'just now', 'just now');
} }
// Apply HTML element
if ($element) {
$title = isset($element['title']) ? ' title="'.$element['title'].'"' : '';
$class = isset($element['class']) ? ' class="'.$element['class'].'"' : '';
$relativeDate = '<'.$element['tag'].''.$title.$class.'>'.$relativeDate.'</'.$element['tag'].'>';
}
return $relativeDate; return $relativeDate;
} }

View file

@ -349,7 +349,39 @@ class TimeHelper extends AppHelper {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
*/ */
public function timeAgoInWords($dateTime, $options = array()) { public function timeAgoInWords($dateTime, $options = array()) {
return $this->_engine->timeAgoInWords($dateTime, $options); $element = null;
$stringDate = '';
if (isset($options['element'])) {
$element_options = array(
'tag' => 'span',
'class' => 'time-ago-in-words',
'title' => $dateTime
);
if (is_array($options['element'])) {
$element = array_merge($element_options, $options['element']);
} else {
if ($options['element']) {
$element = $element_options;
$element['tag'] = $options['element'];
} else {
$element = null;
}
}
}
$relativeDate = $this->_engine->timeAgoInWords($dateTime, $options);
// Apply HTML element
if ($element) {
$title = isset($element['title']) ? ' title="'.$element['title'].'"' : '';
$class = isset($element['class']) ? ' class="'.$element['class'].'"' : '';
$relativeDate = '<'.$element['tag'].$title.$class.'>'.$relativeDate.'</'.$element['tag'].'>';
}
return $relativeDate;
} }
/** /**
@ -366,6 +398,20 @@ class TimeHelper extends AppHelper {
return $this->_engine->wasWithinLast($timeInterval, $dateString, $timezone); return $this->_engine->wasWithinLast($timeInterval, $dateString, $timezone);
} }
/**
* @see CakeTime::isWithinLast()
*
* @param mixed $timeInterval the numeric value with space then time type.
* Example of valid types: 6 hours, 2 days, 1 minute.
* @param mixed $dateString the datestring or unix timestamp to compare
* @param mixed $timezone User's timezone string or DateTimeZone object
* @return boolean
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
*/
public function isWithinNext($timeInterval, $dateString, $timezone = null) {
return $this->_engine->isWithinNext($timeInterval, $dateString, $timezone);
}
/** /**
* @see CakeTime::gmt() * @see CakeTime::gmt()
* *