Skip to content

Commit

Permalink
Consolidate all message date handling code in a single class
Browse files Browse the repository at this point in the history
Allows us to share caching of date data and provides a single location
to ensure the timezone is set if TZ data will be displayed to the user.
  • Loading branch information
slusarz committed Jul 7, 2014
1 parent f28d594 commit 8cc5af0
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 116 deletions.
7 changes: 2 additions & 5 deletions imp/lib/Ajax/Application/ListMessages.php
Expand Up @@ -40,7 +40,7 @@ class IMP_Ajax_Application_ListMessages
*/
public function listMessages($args)
{
global $injector, $notification, $registry;
global $injector, $notification;

$initial = $args['initial'];
$is_search = false;
Expand Down Expand Up @@ -110,9 +110,6 @@ public function listMessages($args)
$is_search = $mbox->search;
}

/* Set the current time zone. */
$registry->setTimeZone();

/* Run filters now. */
if (!empty($args['applyfilter'])) {
$mbox->filter();
Expand Down Expand Up @@ -453,7 +450,7 @@ private function _getOverviewData($mbox, $msglist)
$msg['size'] = IMP::sizeFormat($ob['size']);

/* Format the Date: Header. */
$msg['date'] = $imp_ui->getDate($ob['envelope']->date);
$msg['date'] = strval(new IMP_Message_Date($ob['envelope']->date));

/* Format the From: Header. */
$getfrom = $imp_ui->getFrom($ob['envelope']);
Expand Down
6 changes: 2 additions & 4 deletions imp/lib/Ajax/Application/ShowMessage.php
Expand Up @@ -134,9 +134,6 @@ public function showMessage($args)
'js' => array()
);

/* Set the current time zone. */
$registry->setTimeZone();

$mime_headers = $this->_peek
? $this->_contents->getHeader()
: $this->_contents->getHeaderAndMarkAsSeen();
Expand Down Expand Up @@ -172,7 +169,8 @@ public function showMessage($args)
if ($val = $mime_headers->getValue($head)) {
if ($head == 'date') {
/* Add local time to date header. */
$val = htmlspecialchars($imp_ui->getLocalTime($this->_envelope->date));
$date_ob = new IMP_Message_Date($this->_envelope->date);
$val = htmlspecialchars($date_ob->format($date_ob::DATE_LOCAL));
if ($preview) {
$result['localdate'] = $val;
}
Expand Down
2 changes: 1 addition & 1 deletion imp/lib/Basic/Mailbox.php
Expand Up @@ -867,7 +867,7 @@ protected function _init()
'bg' => '',
'buid' => $imp_mailbox->getBuid($ob['mailbox'], $ob['uid']),
'class' => '',
'date' => $imp_ui->getDate($ob['envelope']->date),
'date' => strval(new IMP_Message_Date($ob['envelope']->date)),
'preview' => '',
'status' => '',
'size' => IMP::sizeFormat($ob['size'])
Expand Down
4 changes: 2 additions & 2 deletions imp/lib/Basic/Message.php
Expand Up @@ -252,8 +252,8 @@ protected function _init()
$basic_headers = $imp_ui->basicHeaders();
$display_headers = $msgAddresses = array();

$format_date = $imp_ui->getLocalTime($envelope->date);
if (!empty($format_date)) {
$date_ob = new IMP_Message_Date($envelope->date);
if ($format_date = $date_ob->format($date_ob::DATE_LOCAL)) {
$display_headers['date'] = $format_date;
}

Expand Down
3 changes: 2 additions & 1 deletion imp/lib/Basic/Thread.php
Expand Up @@ -102,7 +102,8 @@ protected function _init()
$curr_msg['idx'] = $idx;

/* Get headers for the message. */
$curr_msg['date'] = $imp_ui->getLocalTime($envelope->date);
$date_ob = new IMP_Message_Date($envelope->date);
$curr_msg['date'] = $date_ob->format($date_ob::DATE_LOCAL);

if ($this->indices->mailbox->special_outgoing) {
$curr_msg['addr_to'] = true;
Expand Down
2 changes: 1 addition & 1 deletion imp/lib/Block/Newmail.php
Expand Up @@ -87,7 +87,7 @@ protected function _content()
foreach ($fetch_ret as $uid => $ob) {
$envelope = $ob->getEnvelope();

$date = $imp_ui->getDate($envelope->date);
$date = new IMP_Message_Date($envelope->date);
$from = $imp_ui->getFrom($envelope);
$subject = $imp_ui->getSubject($envelope->subject, true);

Expand Down
4 changes: 2 additions & 2 deletions imp/lib/Contents/View.php
Expand Up @@ -237,8 +237,8 @@ public function printAttach($id)
if ($hdr_val = $headerob->getValue($key)) {
/* Format date string. */
if ($key == 'date') {
$registry->setTimeZone();
$hdr_val = $imp_ui_mbox->getDate($hdr_val, $imp_ui_mbox::DATE_FORCE | $imp_ui_mbox::DATE_FULL);
$date_ob = new IMP_Message_Date($hdr_val);
$hdr_val = $date_ob->format($date_ob::DATE_FORCE);
}

$headers[] = array(
Expand Down
79 changes: 10 additions & 69 deletions imp/lib/Mailbox/Ui.php
Expand Up @@ -22,22 +22,19 @@
*/
class IMP_Mailbox_Ui
{
const DATE_FORCE = 1;
const DATE_FULL = 2;

/**
* The current mailbox.
* Cached drafts sent-mail mailbox..
*
* @var IMP_Mailbox
* @var array
*/
private $_mailbox;
private $_draftsSent = array();

/**
* Cached data.
* The current mailbox.
*
* @var array
* @var IMP_Mailbox
*/
private $_cache = array();
private $_mailbox;

/**
* Constructor.
Expand Down Expand Up @@ -69,12 +66,12 @@ public function getFrom($ob)
'to' => false
);

if (!isset($this->_cache['drafts_sm_folder'])) {
$this->_cache['drafts_sm_folder'] = $this->_mailbox->special_outgoing;
if (!isset($this->_draftsSent)) {
$this->_draftsSent = $this->_mailbox->special_outgoing;
}

if ($GLOBALS['injector']->getInstance('IMP_Identity')->hasAddress($ob->from)) {
if (!$this->_cache['drafts_sm_folder']) {
if (!$this->_draftsSent) {
$ret['from'] = _("To:") . ' ';
}
$ret['to'] = true;
Expand All @@ -87,7 +84,7 @@ public function getFrom($ob)
}
} else {
$addrs = $ob->from;
if ($this->_cache['drafts_sm_folder']) {
if ($this->_draftsSent) {
$ret['from'] = _("From:") . ' ';
}

Expand All @@ -113,62 +110,6 @@ public function getFrom($ob)
return $ret;
}

/**
* Formats the date header.
*
* @param mixed $date The date object. Either a DateTime object or a
* date string.
* @param integer $format Mask of formatting options:
* - IMP_Mailbox_Ui::DATE_FORCE - Force use of date formatting, instead
* of time formatting, for all dates.
* - IMP_Mailbox_Ui::DATE_FULL - Use full representation of date,
* including time information.
*
* @return string The formatted date header.
*/
public function getDate($date, $format = 0)
{
if (!is_object($date)) {
if (is_null($date)) {
return _("Unknown Date");
}

$date = new Horde_Imap_Client_DateTime($date);
}

if (!($format & self::DATE_FORCE) &&
!isset($this->_cache['today_start'])) {
$this->_cache['today_start'] = new DateTime('today');
$this->_cache['today_end'] = new DateTime('today + 1 day');
}

$udate = null;
if (!$date->error()) {
try {
$udate = $date->format('U');
} catch (Exception $e) {}
}

if (is_null($udate)) {
return _("Unknown Date");
}

if (($format & self::DATE_FORCE) ||
($udate < $this->_cache['today_start']->format('U')) ||
($udate > $this->_cache['today_end']->format('U'))) {
/* Not today, use the date. */
if ($format & self::DATE_FULL) {
return strftime($GLOBALS['prefs']->getValue('date_format'), $udate) .
' [' . strftime($GLOBALS['prefs']->getValue('time_format'), $udate) . ']';
}

return strftime($GLOBALS['prefs']->getValue('date_format_mini'), $udate);
}

/* Else, it's today, use the time. */
return strftime($GLOBALS['prefs']->getValue('time_format'), $udate);
}

/**
* Formats the subject header.
*
Expand Down
154 changes: 154 additions & 0 deletions imp/lib/Message/Date.php
@@ -0,0 +1,154 @@
<?php
/**
* Copyright 2006-2014 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @category Horde
* @copyright 2006-2014 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/

/**
* Common code dealing with date formatting for messages.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2006-2014 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
class IMP_Message_Date
{
const DATE_FORCE = 1;
const DATE_FULL = 2;
const DATE_LOCAL = 3;

/**
* Shared cache.
*
* @var array
*/
static private $_cache = array();

/**
* The date object.
*
* @var Horde_Imap_Client_DateTime
*/
private $_date;

/**
* Constructor.
*
* @param Horde_Imap_Client_DateTime $date The date object.
*/
public function __construct($date = null)
{
if (!is_object($date) && !is_null($date)) {
$date = new Horde_Imap_Client_DateTime($date);
}

$this->_date = $date;
}

/**
*/
public function __toString()
{
return $this->format();
}

/**
* Formats the date.
*
* @param integer $format Formatting options:
* - DATE_FORCE - Force use of date formatting, instead of time
* formatting, for all dates.
* - DATE_FULL - Use full representation of date, including time
* information.
* - DATE_LOCAL - Display localized formatting (with timezone
* information). Displays "Today" for the current date.
*
* @return string The formatted date string.
*/
public function format($format = 0)
{
global $prefs, $registry;

$udate = null;
if ($this->_date && !$this->_date->error()) {
try {
$udate = $this->_date->format('U');
$time_str = strftime($prefs->getValue('time_format'), $udate);

if (empty(self::$_cache['tz'])) {
$registry->setTimeZone();
self::$_cache['tz'] = true;
}
} catch (Exception $e) {}
}

switch ($format) {
case self::DATE_LOCAL:
if (is_null($udate)) {
return '';
}

$this->_buildCache();
$tz = strftime('%Z');

if (($udate < self::$_cache['today_start']) ||
($udate > self::$_cache['today_end'])) {
/* Not today, use the date. */
return sprintf(
'%s (%s %s)',
strftime($prefs->getValue('date_format'), $udate),
$time_str,
$tz
);
}

/* Else, it's today, use the time only. */
return sprintf(_("Today, %s %s"), $time_str, $tz);
}

if (is_null($udate)) {
return _("Unknown Date");
}

if ($format === self::DATE_FORCE) {
return strftime($prefs->getValue('date_format'), $udate) . ' [' . $time_str . ']';
}

$this->_buildCache();

if (($udate < self::$_cache['today_start']) ||
($udate > self::$_cache['today_end'])) {
/* Not today, use the date. */
return ($format === self::DATE_FULL)
? strftime($prefs->getValue('date_format'), $udate) . ' [' . $time_str . ']'
: strftime($prefs->getValue('date_format_mini'), $udate);
}

/* It's today, use the time. */
return $time_str;
}

/**
* Build the date cache.
*/
private function _buildCache()
{
if (!isset(self::$_cache['today_start'])) {
$date = new DateTime('today');
self::$_cache['today_start'] = $date->format('U');

$date = new DateTime('today + 1 day');
self::$_cache['today_end'] = $date->format('U');
}
}

}

0 comments on commit 8cc5af0

Please sign in to comment.