Skip to content

Commit

Permalink
Mail: Move message decoding code for folder view to Message class
Browse files Browse the repository at this point in the history
  • Loading branch information
bartv2 committed Oct 5, 2012
1 parent 33c2e0a commit d96cf6b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
19 changes: 4 additions & 15 deletions mail/lib/mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
62 changes: 58 additions & 4 deletions mail/lib/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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() {

Expand Down Expand Up @@ -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();
//
Expand Down Expand Up @@ -189,8 +231,9 @@ private function get_attachment_info() {
}

public function as_array() {
$this->getmsg();
$mail_body = $this->plainmsg;
$mail_body = ereg_replace("\n","<br>",$mail_body);
$mail_body = nl2br($mail_body);

if (empty($this->plainmsg) && !empty($this->htmlmsg)) {
$mail_body = "<br/><h2>Only Html body available!</h2><br/>";
Expand All @@ -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;
}
}

0 comments on commit d96cf6b

Please sign in to comment.