1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
<?php
/* --------------------------------------------------------------
EmailReader.inc.php 2015-01-29 gm
Gambio GmbH
http://www.gambio.de
Copyright (c) 2015 Gambio GmbH
Released under the GNU General Public License (Version 2)
[http://www.gnu.org/licenses/gpl-2.0.html]
--------------------------------------------------------------
*/
MainFactory::load_class('EmailReaderInterface');
/**
* Class EmailReader
*
* Reads email records from the database. This class provides a customizable interface
* for reading operations so that it is possible to build different variations in the
* EmailRepository class (e.g. "getPending", "findById", "getAll").
*
* @category System
* @package Email
* @subpackage Repository
*/
class EmailReader implements EmailReaderInterface
{
/**
* Query builder.
* @var CI_DB_query_builder
*/
protected $db;
/**
* E-Mail factory.
* @var EmailFactory
*/
protected $factory;
/**
* Class Constructor
*
* @param CI_DB_query_builder $db Will be used for database operations.
* @param EmailFactoryInterface $factory Will be used for the creation of returned objects.
*/
public function __construct(CI_DB_query_builder $db, EmailFactoryInterface $factory)
{
$this->db = $db;
$this->factory = $factory;
}
/**
* Get email records filtered by conditions.
*
* Example:
* $reader->get(array('email_id' => $customerId), 10, array( array('email_id', 'asc') ));
*
* @param array $conditions (optional) Contains conditions with column => value pairs.
* @param array $limit (optional) Array that contains LIMIT and OFFSET value
* e.g. array( 'limit' => 10, 'offset' => 5 )
* @param array $order (optional) Contains arrays with column, direction pairs
* e.g. array( 'column' => 'direction' )
*
* @return EmailCollection Returns a collection containing the email records.
*/
public function get(array $conditions = array(), array $limit = array(), array $order = array())
{
$this->_limit($limit);
$this->_order($order);
$results = $this->db->get_where('emails', $conditions)->result_array();
$collection = MainFactory::create('EmailCollection');
foreach($results as &$item)
{
$item['contacts'] = $this->db->get_where('email_contacts', array('email_id' => $item['email_id']))
->result_array();
$item['attachments'] = $this->db->get_where('email_attachments', array('email_id' => $item['email_id']))
->result_array();
$collection->add($this->_createEmailByArray($item));
}
return $collection;
}
/**
* Filter email records with provided keyword string.
*
* @param string $p_keyword String to be used for filtering the email records.
* @param array $limit (optional) Array that contains LIMIT and OFFSET value
* e.g. array( 'limit' => 10, 'offset' => 5 )
* @param array $order (optional) Contains arrays with column, direction pairs
* e.g. array( 'column' => 'direction' )
*
* @return EmailCollection Returns a collection containing the email records.
*/
public function filter($p_keyword, array $limit = array(), array $order = array())
{
$this->_filter($p_keyword);
$this->_limit($limit);
$this->_order($order);
$results = $this->db->get()->result_array();
$collection = MainFactory::create('EmailCollection');
foreach($results as &$item)
{
$item['contacts'] = $this->db->get_where('email_contacts', array('email_id' => $item['email_id']))
->result_array();
$item['attachments'] = $this->db->get_where('email_attachments', array('email_id' => $item['email_id']))
->result_array();
$collection->add($this->_createEmailByArray($item));
}
return $collection;
}
/**
* Get the current count of the email records in the database.
*
* This method will quickly return the record count of the "emails" table. It must
* be used when we just need the number and not the data, because the "get" or "find"
* methods need more time to load and parse the records.
*
* @param string $p_filterKeyword (optional) If provided the records will be filtered.
*
* @throws InvalidArgumentException If the provided argument is not a string.
*
* @return int Returns the row number of the email table.
*/
public function getRecordCount($p_filterKeyword = '')
{
if(!is_string($p_filterKeyword))
{
throw new InvalidArgumentException('Invalid argument provided (string expected): '
. gettype($p_filterKeyword));
}
if(!empty($p_filterKeyword))
{
$this->_filter($p_filterKeyword);
$count = $this->db->count_all_results();
}
else
{
$count = $this->db->count_all('emails');
}
return (int)$count;
}
/**
* Creates an email object out of an array.
*
* This method expects the following values to be present in the array: 'email_id', 'subject',
* 'content', 'is_pending', 'contacts', 'attachments'. It uses the EmailFactory for creating
* email objects.
*
* @param array $emailDataArray Contains the database record information.
*
* @throws UnexpectedValueException If the 'creation_date' of the email is empty.
*
* @return Email Returns an object that represents the database record.
*/
protected function _createEmailByArray(array $emailDataArray)
{
// Required email fields must always have a value.
$emailId = new IdType((int)$emailDataArray['email_id']);
$subject = (!empty($emailDataArray['subject'])) ? MainFactory::create('EmailSubject',
$emailDataArray['subject']) : null;
$isPending = (bool)$emailDataArray['is_pending'];
// Optional email fields might be empty. In that case we simply set a NULL value.
$contentHtml = (!empty($emailDataArray['content_html'])) ? MainFactory::create('EmailContent',
html_entity_decode_wrapper($emailDataArray['content_html'])) : null;
$contentPlain = (!empty($emailDataArray['content_plain'])) ? MainFactory::create('EmailContent',
$emailDataArray['content_plain']) : null;
$contacts = MainFactory::create('ContactCollection');
foreach($emailDataArray['contacts'] as $contactDataArray)
{
// Required Fields
$emailAddress = MainFactory::create('EmailAddress', $contactDataArray['email_address']);
$contactType = MainFactory::create('ContactType', $contactDataArray['contact_type']);
// Optional Field
$contactName = (!empty($contactDataArray['contact_name'])) ? MainFactory::create('ContactName',
$contactDataArray['contact_name']) : null;
$contacts->add($this->factory->createContact($emailAddress, $contactType, $contactName));
}
$attachments = MainFactory::create('AttachmentCollection');
foreach($emailDataArray['attachments'] as $attachmentDataArray)
{
// Required Field
$path = MainFactory::create('AttachmentPath', $attachmentDataArray['path']);
// Optional Field
$name = (!empty($attachmentDataArray['name'])) ? MainFactory::create('AttachmentName',
$attachmentDataArray['name']) : null;
$attachments->add(MainFactory::create('EmailAttachment', $path, $name));
}
$email = $this->factory->createEmail($emailId, $subject, $contentHtml, $contentPlain, $isPending, $contacts,
$attachments);
// All registered emails must have a creation date. If there is a record with no
// creation date then something wrong happened during the "send" or "queue" operations.
if($emailDataArray['creation_date'] === null)
{
throw new UnexpectedValueException('Email "creation_date" field must not be null.');
}
$email->setCreationDate(MainFactory::create('DateTime', $emailDataArray['creation_date']));
if($emailDataArray['sent_date'] !== null)
{
$email->setSentDate(MainFactory::create('DateTime', $emailDataArray['sent_date']));
}
return $email;
}
/**
* Apply filter rules to email records.
*
* This method will set the SQL filters depending the provided keyword so that one
* can "get" the filtered records.
*
* @param string $p_keyword Filtering keyword to be applied in the query.
*
* @throws InvalidArgumentException If the $keyword argument is not a string.
*/
protected function _filter($p_keyword)
{
if(!is_string($p_keyword))
{
throw new InvalidArgumentException('Invalid argument provided (string expected) $keyword: ' . $p_keyword);
}
$this->db->select('emails.*')
->distinct()
->from('emails')
->join('email_contacts', 'email_contacts.email_id = emails.email_id', 'left')
->join('email_attachments', 'email_attachments.email_id = emails.email_id', 'left')
->like('emails.subject', $p_keyword)
->or_like('emails.content_html', $p_keyword)
->or_like('emails.content_plain', $p_keyword)
->or_like('emails.creation_date', $p_keyword)
->or_like('emails.sent_date', $p_keyword)
->or_like('email_contacts.email_address', $p_keyword)
->or_like('email_contacts.contact_name', $p_keyword)
->or_like('email_attachments.path', $p_keyword)
->or_like('email_attachments.name', $p_keyword);
}
/**
* Apply LIMIT clause to query.
*
* Example: $this->_limit( array( 'limit' => 10, 'offset' => 0 ) );
*
* @link http://www.codeigniter.com/userguide3/database/query_builder.html#limiting-or-counting-results
*
* @param array $rule Must be an array that contains 'limit' and 'offset' values.
*/
protected function _limit(array $rule)
{
if(!empty($rule))
{
$this->db->limit((int)$rule['limit'], (int)$rule['offset']);
}
}
/**
* Apply ORDER BY clause to query.
*
* Example: $this->_order( array( 'email_id' => 'desc' ) );
*
* @link http://www.codeigniter.com/userguide3/database/query_builder.html#ordering-results
*
* @param array $rule Contains column, direction arrays for ordering results.
*/
protected function _order(array $rule)
{
foreach($rule as $column => $direction)
{
$this->db->order_by($column, $direction);
}
}
}