1 <?php
2 3 4 5 6 7 8 9 10
11
12 MainFactory::load_class('EmailWriterInterface');
13
14 15 16 17 18 19 20 21 22
23 class EmailWriter implements EmailWriterInterface
24 {
25 26 27 28
29 protected $db;
30
31
32 33 34 35 36
37 public function __construct(CI_DB_query_builder $db)
38 {
39 $this->db = $db;
40 }
41
42
43 44 45 46 47
48 public function write(EmailInterface $email)
49 {
50 if($email->getId() === null)
51 {
52 $this->_insert($email);
53 }
54 else
55 {
56 $this->_update($email);
57 }
58
59 $this->_writeContactsAndAttachments($email);
60 }
61
62
63 64 65 66 67
68 protected function _insert(EmailInterface $email)
69 {
70
71 $record = array(
72 'subject' => (string)$email->getSubject(),
73 'content_plain' => (string)$email->getContentPlain(),
74 'content_html' => htmlentities_wrapper((string)$email->getContentHtml()),
75 'is_pending' => (int)$email->isPending(),
76 'creation_date' => $email->getCreationDate()->format('Y-m-d H:i:s')
77 );
78
79 if($email->getSentDate() !== null)
80 {
81 $record['sent_date'] = $email->getSentDate()->format('Y-m-d H:i:s');
82 }
83
84 $this->db->insert('emails', $record);
85 $email->setId(new IdType($this->db->insert_id()));
86 }
87
88
89 90 91 92 93
94 protected function _update(EmailInterface $email)
95 {
96
97 $record = array(
98 'subject' => (string)$email->getSubject(),
99 'content_plain' => (string)$email->getContentPlain(),
100 'content_html' => htmlentities_wrapper((string)$email->getContentHtml()),
101 'is_pending' => (int)$email->isPending(),
102 'creation_date' => $email->getCreationDate()->format('Y-m-d H:i:s')
103 );
104
105 if($email->getSentDate() !== null)
106 {
107 $record['sent_date'] = $email->getSentDate()->format('Y-m-d H:i:s');
108 }
109
110 $this->db->update('emails', $record, array('email_id' => (int)(string)$email->getId()));
111 }
112
113
114 115 116 117 118 119 120 121
122 protected function _writeContactsAndAttachments(EmailInterface $email)
123 {
124 if($email->getId() == null)
125 {
126 throw new UnexpectedValueException('$email object does not have an ID set.');
127 }
128
129 $emailId = (int)(string)$email->getId();
130
131
132 $this->db->delete('email_contacts', array('email_id' => $emailId));
133 $this->db->delete('email_attachments', array('email_id' => $emailId));
134
135
136 $contacts = array_merge($email->getBcc()->getArray(), $email->getCc()->getArray(),
137 array($email->getSender(), $email->getRecipient(), $email->getReplyTo()));
138 foreach($contacts as $contact)
139 {
140 if($contact == null)
141 {
142 continue;
143 }
144 $record = array(
145 'email_id' => $emailId,
146 'email_address' => (string)$contact->getEmailAddress(),
147 'contact_type' => (string)$contact->getContactType(),
148 'contact_name' => (string)$contact->getContactName()
149 );
150 $this->db->insert('email_contacts', $record);
151 }
152
153
154 foreach($email->getAttachments()->getArray() as $attachment)
155 {
156 $record = array(
157 'email_id' => $emailId,
158 'path' => (string)$attachment->getPath(false),
159 'name' => (string)$attachment->getName()
160 );
161 $this->db->insert('email_attachments', $record);
162 }
163 }
164 }