From d96cf6b761d674845b0bbca437815c2a44d07e6d Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Fri, 5 Oct 2012 23:46:44 +0200 Subject: [PATCH] Mail: Move message decoding code for folder view to Message class --- mail/lib/mail.php | 19 +++----------- mail/lib/message.php | 62 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 19 deletions(-) diff --git a/mail/lib/mail.php b/mail/lib/mail.php index c402fd5616..c737ecaa0e 100644 --- a/mail/lib/mail.php +++ b/mail/lib/mail.php @@ -184,21 +184,10 @@ public static function getMessages($user_id, $account_id, $folder_id, $from = 0, $headers = $conn->fetch($folder_id, $fetch_query); ob_start(); // fix for Horde warnings - foreach ($headers as $header) { - $flags = array('SEEN' => True, 'ANSWERED' => False, 'FORWARDED' => False, 'DRAFT' => False, 'HAS_ATTACHMENTS' => True); -// \Horde_Imap_Client_Data_Fetch::HEADER_PARSE - - $f = $header->getFlags(); - $date = $header->getImapDate()->format('U'); - $id = $header->getUid(); - $e = $header->getEnvelope(); - $flags = array('unseen' => !in_array("\seen", $f)); - $to = $e->to_decoded[0]; - $to = $to['personal']; //."<".$to['mailbox']."@".$to['host'].">"; - $from = $e->from_decoded[0]; - $from = $from['personal']; //."<".$from['mailbox']."@".$from['host'].">"; - $messages[] = array('id' => $id, 'from' => $from, 'to' => $to, 'subject' => $e->subject_decoded, - 'date' => $date, 'size' => $header->getSize(), 'flags' => $flags); + foreach ($headers as $message_id => $header) { + $message = new Message($conn, $folder_id, $message_id); + $message->setInfo($header); + $messages[] = $message->getListArray(); } ob_clean(); diff --git a/mail/lib/message.php b/mail/lib/message.php index d101f4eefa..33bcc147fd 100644 --- a/mail/lib/message.php +++ b/mail/lib/message.php @@ -22,15 +22,13 @@ */ namespace OCA\Mail; -class Message{ +class Message { // input $mbox = IMAP conn, $mid = message id function __construct($conn, $folder_id, $message_id) { $this->conn = $conn; $this->folder_id = $folder_id; $this->message_id = $message_id; - - $this->getmsg(); } // output all the following: @@ -42,7 +40,50 @@ function __construct($conn, $folder_id, $message_id) { public $attachments = array(); private $conn, $folder_id, $message_id; + private $fetch; + + public function setInfo($info) { + $this->fetch = $info; + } + + public function getUid() { + return $this->fetch->getUid(); + } + + public function getFlags() { + $flags = $this->fetch->getFlags(); + return array('unseen' => !in_array("\seen", $flags)); + } + + public function getEnvelope() { + return $this->fetch->getEnvelope(); + } + + public function getFrom() { + $e = $this->getEnvelope(); + $from = $e->from_decoded[0]; + return $from['personal']; //."<".$from['mailbox']."@".$from['host'].">"; + } + + public function getTo() { + $e = $this->getEnvelope(); + $to = $e->to_decoded[0]; + return $to['personal']; //."<".$to['mailbox']."@".$to['host'].">"; + } + public function getSubject() { + $e = $this->getEnvelope(); + return $e->subject_decoded; + } + + public function getSentDate() { + // TODO: Use internal imap date for now + return $this->fetch->getImapDate(); + } + + public function getSize() { + return $this->fetch->getSize(); + } private function getmsg() { @@ -73,6 +114,7 @@ private function getmsg() { // $list is an array of Horde_Imap_Client_Data_Fetch objects. $ids = new \Horde_Imap_Client_Ids($this->message_id); $headers = $this->conn->fetch($this->folder_id, $fetch_query, array('ids' => $ids)); + $this->fetch = $headers[$this->message_id]; $this->plainmsg = $headers[$this->message_id]->getBodyText(); // @@ -189,8 +231,9 @@ private function get_attachment_info() { } public function as_array() { + $this->getmsg(); $mail_body = $this->plainmsg; - $mail_body = ereg_replace("\n","
",$mail_body); + $mail_body = nl2br($mail_body); if (empty($this->plainmsg) && !empty($this->htmlmsg)) { $mail_body = "

Only Html body available!


"; @@ -216,4 +259,15 @@ public function as_array() { 'header' => 'TODO: add the header' ); } + + public function getListArray() { + $data = array(); + $data['id'] = $this->getUid(); + $data['from'] = $this->getFrom(); + $data['subject'] = $this->getSubject(); + $data['date'] = $this->getSentDate()->format('U'); + $data['size'] = $this->getSize(); + $data['flags'] = $this->getFlags(); + return $data; + } }