3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
30 * @copyright CiviCRM LLC (c) 2004-2015
34 class CRM_Mailing_BAO_Query
{
36 static $_mailingFields = NULL;
41 public static function &getFields() {
42 if (!self
::$_mailingFields) {
43 self
::$_mailingFields = array();
44 $_mailingFields['mailing_id'] = array(
45 'name' => 'mailing_id',
46 'title' => 'Mailing ID',
47 'where' => 'civicrm_mailing.id',
50 return self
::$_mailingFields;
54 * If mailings are involved, add the specific Mailing fields
60 public static function select(&$query) {
61 // if Mailing mode add mailing id
62 if ($query->_mode
& CRM_Contact_BAO_Query
::MODE_MAILING
) {
63 $query->_select
['mailing_id'] = "civicrm_mailing.id as mailing_id";
64 $query->_element
['mailing_id'] = 1;
66 // base table is contact, so join recipients to it
67 $query->_tables
['civicrm_mailing_recipients'] = $query->_whereTables
['civicrm_mailing_recipients']
68 = " INNER JOIN civicrm_mailing_recipients ON civicrm_mailing_recipients.contact_id = contact_a.id ";
70 $query->_tables
['civicrm_mailing'] = $query->_whereTables
['civicrm_mailing'] = 1;
73 if (!empty($query->_returnProperties
['mailing_name'])) {
74 $query->_select
['mailing_name'] = "civicrm_mailing.name as mailing_name";
75 $query->_element
['mailing_name'] = 1;
78 // get mailing subject
79 if (!empty($query->_returnProperties
['mailing_subject'])) {
80 $query->_select
['mailing_subject'] = "civicrm_mailing.subject as mailing_subject";
81 $query->_element
['mailing_subject'] = 1;
85 if (!empty($query->_returnProperties
['mailing_job_status'])) {
86 $query->_tables
['civicrm_mailing_job'] = $query->_whereTables
['civicrm_mailing_job']
87 = " LEFT JOIN civicrm_mailing_job ON civicrm_mailing_job.mailing_id = civicrm_mailing.id AND civicrm_mailing_job.parent_id IS NULL AND civicrm_mailing_job.is_test != 1 ";
88 $query->_select
['mailing_job_status'] = "civicrm_mailing_job.status as mailing_job_status";
89 $query->_element
['mailing_job_status'] = 1;
93 if (!empty($query->_returnProperties
['email_on_hold'])) {
94 $query->_select
['email_on_hold'] = "recipient_email.on_hold as email_on_hold";
95 $query->_element
['email_on_hold'] = 1;
96 $query->_tables
['recipient_email'] = $query->_whereTables
['recipient_email'] = 1;
99 // get recipient email
100 if (!empty($query->_returnProperties
['email'])) {
101 $query->_select
['email'] = "recipient_email.email as email";
102 $query->_element
['email'] = 1;
103 $query->_tables
['recipient_email'] = $query->_whereTables
['recipient_email'] = 1;
107 if (!empty($query->_returnProperties
['contact_opt_out'])) {
108 $query->_select
['contact_opt_out'] = "contact_a.is_opt_out as contact_opt_out";
109 $query->_element
['contact_opt_out'] = 1;
112 // mailing job end date / completed date
113 if (!empty($query->_returnProperties
['mailing_job_end_date'])) {
114 $query->_tables
['civicrm_mailing_job'] = $query->_whereTables
['civicrm_mailing_job']
115 = " LEFT JOIN civicrm_mailing_job ON civicrm_mailing_job.mailing_id = civicrm_mailing.id AND civicrm_mailing_job.parent_id IS NULL AND civicrm_mailing_job.is_test != 1 ";
116 $query->_select
['mailing_job_end_date'] = "civicrm_mailing_job.end_date as mailing_job_end_date";
117 $query->_element
['mailing_job_end_date'] = 1;
120 if (!empty($query->_returnProperties
['mailing_recipients_id'])) {
121 $query->_select
['mailing_recipients_id'] = " civicrm_mailing_recipients.id as mailing_recipients_id";
122 $query->_element
['mailing_recipients_id'] = 1;
130 public static function where(&$query) {
132 foreach (array_keys($query->_params
) as $id) {
133 if (empty($query->_params
[$id][0])) {
136 if (substr($query->_params
[$id][0], 0, 8) == 'mailing_') {
137 if ($query->_mode
== CRM_Contact_BAO_QUERY
::MODE_CONTACTS
) {
138 $query->_useDistinct
= TRUE;
140 $grouping = $query->_params
[$id][3];
141 self
::whereClauseSingle($query->_params
[$id], $query);
147 * @param string $name
151 * @return null|string
153 public static function from($name, $mode, $side) {
157 case 'civicrm_mailing_recipients':
158 $from = " $side JOIN civicrm_mailing_recipients ON civicrm_mailing_recipients.contact_id = contact_a.id";
161 case 'civicrm_mailing_event_queue':
162 // this is tightly binded so as to do a check WRT actual job recipients ('child' type jobs)
163 $from = " INNER JOIN civicrm_mailing_event_queue ON
164 civicrm_mailing_event_queue.contact_id = civicrm_mailing_recipients.contact_id
165 AND civicrm_mailing_event_queue.job_id = civicrm_mailing_job.id AND civicrm_mailing_job.job_type = 'child'";
168 case 'civicrm_mailing':
169 $from = " $side JOIN civicrm_mailing ON civicrm_mailing.id = civicrm_mailing_recipients.mailing_id ";
172 case 'civicrm_mailing_job':
173 $from = " $side JOIN civicrm_mailing_job ON civicrm_mailing_job.mailing_id = civicrm_mailing.id AND civicrm_mailing_job.is_test != 1 ";
176 case 'civicrm_mailing_event_bounce':
177 case 'civicrm_mailing_event_delivered':
178 case 'civicrm_mailing_event_opened':
179 case 'civicrm_mailing_event_reply':
180 case 'civicrm_mailing_event_unsubscribe':
181 case 'civicrm_mailing_event_forward':
182 case 'civicrm_mailing_event_trackable_url_open':
183 $from = " $side JOIN $name ON $name.event_queue_id = civicrm_mailing_event_queue.id";
186 case 'recipient_email':
187 $from = " $side JOIN civicrm_email recipient_email ON recipient_email.id = civicrm_mailing_recipients.email_id";
196 * @param bool $includeCustomFields
200 public static function defaultReturnProperties(
202 $includeCustomFields = TRUE
206 if ($mode & CRM_Contact_BAO_Query
::MODE_MAILING
) {
212 'mailing_subject' => 1,
213 'email_on_hold' => 1,
214 'contact_opt_out' => 1,
215 'mailing_job_status' => 1,
216 'mailing_job_end_date' => 1,
218 'contact_sub_type' => 1,
219 'mailing_recipients_id' => 1,
229 public static function whereClauseSingle(&$values, &$query) {
230 list($name, $op, $value, $grouping, $wildcard) = $values;
233 $fields = self
::getFields();
237 $selectedMailings = array_flip($value);
238 $value = "(" . implode(',', $value) . ")";
240 $query->_where
[$grouping][] = "civicrm_mailing.id $op $value";
242 $mailings = CRM_Mailing_BAO_Mailing
::getMailingsList();
243 foreach ($selectedMailings as $id => $dnc) {
244 $selectedMailings[$id] = $mailings[$id];
246 $selectedMailings = implode(' or ', $selectedMailings);
248 $query->_qill
[$grouping][] = "Mailing Name $op \"$selectedMailings\"";
249 $query->_tables
['civicrm_mailing'] = $query->_whereTables
['civicrm_mailing'] = 1;
250 $query->_tables
['civicrm_mailing_recipients'] = $query->_whereTables
['civicrm_mailing_recipients'] = 1;
254 $value = strtolower(addslashes($value));
259 $query->_where
[$grouping][] = "LOWER(civicrm_mailing.name) $op '$value'";
260 $query->_qill
[$grouping][] = "Mailing Namename $op \"$value\"";
261 $query->_tables
['civicrm_mailing'] = $query->_whereTables
['civicrm_mailing'] = 1;
262 $query->_tables
['civicrm_mailing_recipients'] = $query->_whereTables
['civicrm_mailing_recipients'] = 1;
266 case 'mailing_date_low':
267 case 'mailing_date_high':
268 // process to / from date
269 $query->_tables
['civicrm_mailing'] = $query->_whereTables
['civicrm_mailing'] = 1;
270 $query->_tables
['civicrm_mailing_event_queue'] = $query->_whereTables
['civicrm_mailing_event_queue'] = 1;
271 $query->_tables
['civicrm_mailing_job'] = $query->_whereTables
['civicrm_mailing_job'] = 1;
272 $query->_tables
['civicrm_mailing_recipients'] = $query->_whereTables
['civicrm_mailing_recipients'] = 1;
273 $query->dateQueryBuilder($values,
274 'civicrm_mailing_job', 'mailing_date', 'start_date', 'Mailing Delivery Date'
278 case 'mailing_delivery_status':
279 $options = CRM_Mailing_PseudoConstant
::yesNoOptions('delivered');
281 list($name, $op, $value, $grouping, $wildcard) = $values;
283 self
::mailingEventQueryBuilder($query, $values,
284 'civicrm_mailing_event_delivered',
285 'mailing_delivery_status',
286 ts('Mailing Delivery'),
290 elseif ($value == 'N') {
291 $options['Y'] = $options['N'];
292 $values = array($name, $op, 'Y', $grouping, $wildcard);
293 self
::mailingEventQueryBuilder($query, $values,
294 'civicrm_mailing_event_bounce',
295 'mailing_delivery_status',
296 ts('Mailing Delivery'),
302 case 'mailing_bounce_types':
304 $values = array($name, $op, $value, $grouping, $wildcard);
305 self
::mailingEventQueryBuilder($query, $values,
306 'civicrm_mailing_event_bounce',
308 ts('Bounce type(s)'),
309 CRM_Core_PseudoConstant
::get('CRM_Mailing_Event_DAO_Bounce', 'bounce_type_id', array(
311 'labelColumn' => 'name',
316 case 'mailing_open_status':
317 self
::mailingEventQueryBuilder($query, $values,
318 'civicrm_mailing_event_opened', 'mailing_open_status', ts('Mailing: Trackable Opens'), CRM_Mailing_PseudoConstant
::yesNoOptions('open')
322 case 'mailing_click_status':
323 self
::mailingEventQueryBuilder($query, $values,
324 'civicrm_mailing_event_trackable_url_open', 'mailing_click_status', ts('Mailing: Trackable URL Clicks'), CRM_Mailing_PseudoConstant
::yesNoOptions('click')
328 case 'mailing_reply_status':
329 self
::mailingEventQueryBuilder($query, $values,
330 'civicrm_mailing_event_reply', 'mailing_reply_status', ts('Mailing: Trackable Replies'), CRM_Mailing_PseudoConstant
::yesNoOptions('reply')
334 case 'mailing_optout':
335 $valueTitle = array(1 => ts('Opt-out Requests'));
336 // include opt-out events only
337 $query->_where
[$grouping][] = "civicrm_mailing_event_unsubscribe.org_unsubscribe = 1";
338 self
::mailingEventQueryBuilder($query, $values,
339 'civicrm_mailing_event_unsubscribe', 'mailing_unsubscribe',
340 ts('Mailing: '), $valueTitle
344 case 'mailing_unsubscribe':
345 $valueTitle = array(1 => ts('Unsubscribe Requests'));
346 // exclude opt-out events
347 $query->_where
[$grouping][] = "civicrm_mailing_event_unsubscribe.org_unsubscribe = 0";
348 self
::mailingEventQueryBuilder($query, $values,
349 'civicrm_mailing_event_unsubscribe', 'mailing_unsubscribe',
350 ts('Mailing: '), $valueTitle
354 case 'mailing_forward':
355 $valueTitle = array('Y' => ts('Forwards'));
356 // since its a checkbox
358 self
::mailingEventQueryBuilder($query, $values,
359 'civicrm_mailing_event_forward', 'mailing_forward',
360 ts('Mailing: '), $valueTitle
364 case 'mailing_job_status':
365 if (!empty($value)) {
366 if ($value != 'Scheduled' && $value != 'Canceled') {
367 $query->_tables
['civicrm_mailing_event_queue'] = $query->_whereTables
['civicrm_mailing_event_queue'] = 1;
369 $query->_tables
['civicrm_mailing'] = $query->_whereTables
['civicrm_mailing'] = 1;
370 $query->_tables
['civicrm_mailing_job'] = $query->_whereTables
['civicrm_mailing_job'] = 1;
371 $query->_tables
['civicrm_mailing_recipients'] = $query->_whereTables
['civicrm_mailing_recipients'] = 1;
373 $query->_where
[$grouping][] = " civicrm_mailing_job.status = '{$value}' ";
374 $query->_qill
[$grouping][] = "Mailing Job Status IS \"$value\"";
381 * Add all the elements shared between Mailing search and advnaced search.
384 * @param CRM_Core_Form $form
387 public static function buildSearchForm(&$form) {
389 $mailings = CRM_Mailing_BAO_Mailing
::getMailingsList();
391 if (!empty($mailings)) {
392 $form->add('select', 'mailing_id', ts('Mailing Name(s)'), $mailings, FALSE,
393 array('id' => 'mailing_id', 'multiple' => 'multiple', 'class' => 'crm-select2')
397 CRM_Core_Form_Date
::buildDateRange($form, 'mailing_date', 1, '_low', '_high', ts('From'), FALSE);
399 $mailingJobStatuses = array(
400 '' => ts('- select -'),
401 'Complete' => 'Complete',
402 'Scheduled' => 'Scheduled',
403 'Running' => 'Running',
404 'Canceled' => 'Canceled',
406 $form->addElement('select', 'mailing_job_status', ts('Mailing Job Status'), $mailingJobStatuses, FALSE);
408 $mailingBounceTypes = CRM_Core_PseudoConstant
::get(
409 'CRM_Mailing_Event_DAO_Bounce', 'bounce_type_id',
410 array('keyColumn' => 'id', 'labelColumn' => 'name')
412 $form->add('select', 'mailing_bounce_types', ts('Bounce Types'), $mailingBounceTypes, FALSE,
413 array('id' => 'mailing_bounce_types', 'multiple' => 'multiple', 'class' => 'crm-select2')
417 $form->addRadio('mailing_delivery_status', ts('Delivery Status'), CRM_Mailing_PseudoConstant
::yesNoOptions('delivered'), array('allowClear' => TRUE));
418 $form->addRadio('mailing_open_status', ts('Trackable Opens'), CRM_Mailing_PseudoConstant
::yesNoOptions('open'), array('allowClear' => TRUE));
419 $form->addRadio('mailing_click_status', ts('Trackable URLs'), CRM_Mailing_PseudoConstant
::yesNoOptions('click'), array('allowClear' => TRUE));
420 $form->addRadio('mailing_reply_status', ts('Trackable Replies'), CRM_Mailing_PseudoConstant
::yesNoOptions('reply'), array('allowClear' => TRUE));
422 $form->add('checkbox', 'mailing_unsubscribe', ts('Unsubscribe Requests'));
423 $form->add('checkbox', 'mailing_optout', ts('Opt-out Requests'));
424 $form->add('checkbox', 'mailing_forward', ts('Forwards'));
426 $form->assign('validCiviMailing', TRUE);
433 public static function searchAction(&$row, $id) {
439 public static function tableNames(&$tables) {
443 * Filter query results based on which contacts do (not) have a particular mailing event in their history.
447 * @param string $tableName
448 * @param string $fieldName
451 * @param $valueTitles
455 public static function mailingEventQueryBuilder(&$query, &$values, $tableName, $fieldName, $fieldTitle, &$valueTitles) {
456 list($name, $op, $value, $grouping, $wildcard) = $values;
458 if (empty($value) ||
$value == 'A') {
459 // don't do any filtering
464 $query->_where
[$grouping][] = $tableName . ".id is not null ";
466 elseif ($value == 'N') {
467 $query->_where
[$grouping][] = $tableName . ".id is null ";
470 if (is_array($value)) {
471 $query->_where
[$grouping][] = "$tableName.$fieldName $op (" . implode(',', $value) . ")";
472 $query->_qill
[$grouping][] = "$fieldTitle $op " . implode(', ', array_intersect_key($valueTitles, array_flip($value)));
475 $query->_qill
[$grouping][] = $fieldTitle . ' - ' . $valueTitles[$value];
478 $query->_tables
['civicrm_mailing'] = $query->_whereTables
['civicrm_mailing'] = 1;
479 $query->_tables
['civicrm_mailing_job'] = $query->_whereTables
['civicrm_mailing_job'] = 1;
480 $query->_tables
['civicrm_mailing_event_queue'] = $query->_whereTables
['civicrm_mailing_event_queue'] = 1;
481 $query->_tables
['civicrm_mailing_recipients'] = $query->_whereTables
['civicrm_mailing_recipients'] = 1;
482 $query->_tables
[$tableName] = $query->_whereTables
[$tableName] = 1;