3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 and the CiviCRM Licensing Exception. |
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 and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
33 class CRM_Mailing_BAO_Query
{
35 static $_mailingFields = NULL;
40 public static function &getFields() {
41 if (!self
::$_mailingFields) {
42 self
::$_mailingFields = array();
43 $_mailingFields['mailing_id'] = array(
44 'name' => 'mailing_id',
45 'title' => ts('Mailing ID'),
46 'where' => 'civicrm_mailing.id',
49 return self
::$_mailingFields;
53 * If mailings are involved, add the specific Mailing fields
57 public static function select(&$query) {
58 // if Mailing mode add mailing id
59 if ($query->_mode
& CRM_Contact_BAO_Query
::MODE_MAILING
) {
60 $query->_select
['mailing_id'] = "civicrm_mailing.id as mailing_id";
61 $query->_element
['mailing_id'] = 1;
63 // base table is contact, so join recipients to it
64 $query->_tables
['civicrm_mailing_recipients'] = $query->_whereTables
['civicrm_mailing_recipients']
65 = " INNER JOIN civicrm_mailing_recipients ON civicrm_mailing_recipients.contact_id = contact_a.id ";
67 $query->_tables
['civicrm_mailing'] = $query->_whereTables
['civicrm_mailing'] = 1;
70 if (!empty($query->_returnProperties
['mailing_name'])) {
71 $query->_select
['mailing_name'] = "civicrm_mailing.name as mailing_name";
72 $query->_element
['mailing_name'] = 1;
75 // get mailing subject
76 if (!empty($query->_returnProperties
['mailing_subject'])) {
77 $query->_select
['mailing_subject'] = "civicrm_mailing.subject as mailing_subject";
78 $query->_element
['mailing_subject'] = 1;
82 if (!empty($query->_returnProperties
['mailing_job_status'])) {
83 $query->_tables
['civicrm_mailing_job'] = $query->_whereTables
['civicrm_mailing_job']
84 = " 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 ";
85 $query->_select
['mailing_job_status'] = "civicrm_mailing_job.status as mailing_job_status";
86 $query->_element
['mailing_job_status'] = 1;
90 if (!empty($query->_returnProperties
['email_on_hold'])) {
91 $query->_select
['email_on_hold'] = "recipient_email.on_hold as email_on_hold";
92 $query->_element
['email_on_hold'] = 1;
93 $query->_tables
['recipient_email'] = $query->_whereTables
['recipient_email'] = 1;
96 // get recipient email
97 if (!empty($query->_returnProperties
['email'])) {
98 $query->_select
['email'] = "recipient_email.email as email";
99 $query->_element
['email'] = 1;
100 $query->_tables
['recipient_email'] = $query->_whereTables
['recipient_email'] = 1;
104 if (!empty($query->_returnProperties
['contact_opt_out'])) {
105 $query->_select
['contact_opt_out'] = "contact_a.is_opt_out as contact_opt_out";
106 $query->_element
['contact_opt_out'] = 1;
109 // mailing job end date / completed date
110 if (!empty($query->_returnProperties
['mailing_job_end_date'])) {
111 $query->_tables
['civicrm_mailing_job'] = $query->_whereTables
['civicrm_mailing_job']
112 = " 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 ";
113 $query->_select
['mailing_job_end_date'] = "civicrm_mailing_job.end_date as mailing_job_end_date";
114 $query->_element
['mailing_job_end_date'] = 1;
117 if (!empty($query->_returnProperties
['mailing_recipients_id'])) {
118 $query->_select
['mailing_recipients_id'] = " civicrm_mailing_recipients.id as mailing_recipients_id";
119 $query->_element
['mailing_recipients_id'] = 1;
123 if (CRM_Utils_Array
::value('mailing_campaign_id', $query->_returnProperties
)) {
124 $query->_select
['mailing_campaign_id'] = 'civicrm_mailing.campaign_id as mailing_campaign_id';
125 $query->_element
['mailing_campaign_id'] = 1;
126 $query->_tables
['civicrm_campaign'] = 1;
133 public static function where(&$query) {
135 foreach (array_keys($query->_params
) as $id) {
136 if (empty($query->_params
[$id][0])) {
139 if (substr($query->_params
[$id][0], 0, 8) == 'mailing_') {
140 if ($query->_mode
== CRM_Contact_BAO_QUERY
::MODE_CONTACTS
) {
141 $query->_useDistinct
= TRUE;
143 $grouping = $query->_params
[$id][3];
144 self
::whereClauseSingle($query->_params
[$id], $query);
150 * @param string $name
154 * @return null|string
156 public static function from($name, $mode, $side) {
160 case 'civicrm_mailing_recipients':
161 $from = " $side JOIN civicrm_mailing_recipients ON civicrm_mailing_recipients.contact_id = contact_a.id";
164 case 'civicrm_mailing_event_queue':
165 // this is tightly binded so as to do a check WRT actual job recipients ('child' type jobs)
166 $from = " INNER JOIN civicrm_mailing_event_queue ON
167 civicrm_mailing_event_queue.contact_id = civicrm_mailing_recipients.contact_id
168 AND civicrm_mailing_event_queue.job_id = civicrm_mailing_job.id AND civicrm_mailing_job.job_type = 'child'";
171 case 'civicrm_mailing':
172 $from = " $side JOIN civicrm_mailing ON civicrm_mailing.id = civicrm_mailing_recipients.mailing_id ";
175 case 'civicrm_mailing_job':
176 $from = " $side JOIN civicrm_mailing_job ON civicrm_mailing_job.mailing_id = civicrm_mailing.id AND civicrm_mailing_job.is_test != 1 ";
179 case 'civicrm_mailing_event_bounce':
180 case 'civicrm_mailing_event_delivered':
181 case 'civicrm_mailing_event_opened':
182 case 'civicrm_mailing_event_reply':
183 case 'civicrm_mailing_event_unsubscribe':
184 case 'civicrm_mailing_event_forward':
185 case 'civicrm_mailing_event_trackable_url_open':
186 $from = " $side JOIN $name ON $name.event_queue_id = civicrm_mailing_event_queue.id";
189 case 'recipient_email':
190 $from = " $side JOIN civicrm_email recipient_email ON recipient_email.id = civicrm_mailing_recipients.email_id";
193 case 'civicrm_campaign':
194 $from = " $side JOIN civicrm_campaign ON civicrm_campaign.id = civicrm_mailing.campaign_id";
203 * @param bool $includeCustomFields
207 public static function defaultReturnProperties(
209 $includeCustomFields = TRUE
213 if ($mode & CRM_Contact_BAO_Query
::MODE_MAILING
) {
216 'mailing_campaign_id' => 1,
220 'mailing_subject' => 1,
221 'email_on_hold' => 1,
222 'contact_opt_out' => 1,
223 'mailing_job_status' => 1,
224 'mailing_job_end_date' => 1,
226 'contact_sub_type' => 1,
227 'mailing_recipients_id' => 1,
237 public static function whereClauseSingle(&$values, &$query) {
238 list($name, $op, $value, $grouping, $wildcard) = $values;
242 $selectedMailings = array_flip($value);
243 $value = "(" . implode(',', $value) . ")";
245 $query->_where
[$grouping][] = "civicrm_mailing.id $op $value";
247 $mailings = CRM_Mailing_BAO_Mailing
::getMailingsList();
248 foreach ($selectedMailings as $id => $dnc) {
249 $selectedMailings[$id] = $mailings[$id];
251 $selectedMailings = implode(' or ', $selectedMailings);
253 $query->_qill
[$grouping][] = "Mailing Name $op \"$selectedMailings\"";
254 $query->_tables
['civicrm_mailing'] = $query->_whereTables
['civicrm_mailing'] = 1;
255 $query->_tables
['civicrm_mailing_recipients'] = $query->_whereTables
['civicrm_mailing_recipients'] = 1;
259 $value = addslashes($value);
265 $query->_where
[$grouping][] = "civicrm_mailing.name $op '$value'";
266 $query->_qill
[$grouping][] = "Mailing Namename $op \"$value\"";
267 $query->_tables
['civicrm_mailing'] = $query->_whereTables
['civicrm_mailing'] = 1;
268 $query->_tables
['civicrm_mailing_recipients'] = $query->_whereTables
['civicrm_mailing_recipients'] = 1;
272 case 'mailing_date_low':
273 case 'mailing_date_high':
274 // process to / from date
275 $query->_tables
['civicrm_mailing'] = $query->_whereTables
['civicrm_mailing'] = 1;
276 $query->_tables
['civicrm_mailing_event_queue'] = $query->_whereTables
['civicrm_mailing_event_queue'] = 1;
277 $query->_tables
['civicrm_mailing_job'] = $query->_whereTables
['civicrm_mailing_job'] = 1;
278 $query->_tables
['civicrm_mailing_recipients'] = $query->_whereTables
['civicrm_mailing_recipients'] = 1;
279 $query->dateQueryBuilder($values,
280 'civicrm_mailing_job', 'mailing_date', 'start_date', 'Mailing Delivery Date'
284 case 'mailing_delivery_status':
285 $options = CRM_Mailing_PseudoConstant
::yesNoOptions('delivered');
287 list($name, $op, $value, $grouping, $wildcard) = $values;
289 self
::mailingEventQueryBuilder($query, $values,
290 'civicrm_mailing_event_delivered',
291 'mailing_delivery_status',
292 ts('Mailing Delivery'),
296 elseif ($value == 'N') {
297 $options['Y'] = $options['N'];
298 $values = array($name, $op, 'Y', $grouping, $wildcard);
299 self
::mailingEventQueryBuilder($query, $values,
300 'civicrm_mailing_event_bounce',
301 'mailing_delivery_status',
302 ts('Mailing Delivery'),
308 case 'mailing_bounce_types':
310 $values = array($name, $op, $value, $grouping, $wildcard);
311 self
::mailingEventQueryBuilder($query, $values,
312 'civicrm_mailing_event_bounce',
314 ts('Bounce type(s)'),
315 CRM_Core_PseudoConstant
::get('CRM_Mailing_Event_DAO_Bounce', 'bounce_type_id', array(
317 'labelColumn' => 'name',
322 case 'mailing_open_status':
323 self
::mailingEventQueryBuilder($query, $values,
324 'civicrm_mailing_event_opened', 'mailing_open_status', ts('Mailing: Trackable Opens'), CRM_Mailing_PseudoConstant
::yesNoOptions('open')
328 case 'mailing_click_status':
329 self
::mailingEventQueryBuilder($query, $values,
330 'civicrm_mailing_event_trackable_url_open', 'mailing_click_status', ts('Mailing: Trackable URL Clicks'), CRM_Mailing_PseudoConstant
::yesNoOptions('click')
334 case 'mailing_reply_status':
335 self
::mailingEventQueryBuilder($query, $values,
336 'civicrm_mailing_event_reply', 'mailing_reply_status', ts('Mailing: Trackable Replies'), CRM_Mailing_PseudoConstant
::yesNoOptions('reply')
340 case 'mailing_optout':
341 $valueTitle = array(1 => ts('Opt-out Requests'));
342 // include opt-out events only
343 $query->_where
[$grouping][] = "civicrm_mailing_event_unsubscribe.org_unsubscribe = 1";
344 self
::mailingEventQueryBuilder($query, $values,
345 'civicrm_mailing_event_unsubscribe', 'mailing_unsubscribe',
346 ts('Mailing: '), $valueTitle
350 case 'mailing_unsubscribe':
351 $valueTitle = array(1 => ts('Unsubscribe Requests'));
352 // exclude opt-out events
353 $query->_where
[$grouping][] = "civicrm_mailing_event_unsubscribe.org_unsubscribe = 0";
354 self
::mailingEventQueryBuilder($query, $values,
355 'civicrm_mailing_event_unsubscribe', 'mailing_unsubscribe',
356 ts('Mailing: '), $valueTitle
360 case 'mailing_forward':
361 $valueTitle = array('Y' => ts('Forwards'));
362 // since its a checkbox
364 self
::mailingEventQueryBuilder($query, $values,
365 'civicrm_mailing_event_forward', 'mailing_forward',
366 ts('Mailing: '), $valueTitle
370 case 'mailing_job_status':
371 if (!empty($value)) {
372 if ($value != 'Scheduled' && $value != 'Canceled') {
373 $query->_tables
['civicrm_mailing_event_queue'] = $query->_whereTables
['civicrm_mailing_event_queue'] = 1;
375 $query->_tables
['civicrm_mailing'] = $query->_whereTables
['civicrm_mailing'] = 1;
376 $query->_tables
['civicrm_mailing_job'] = $query->_whereTables
['civicrm_mailing_job'] = 1;
377 $query->_tables
['civicrm_mailing_recipients'] = $query->_whereTables
['civicrm_mailing_recipients'] = 1;
379 $query->_where
[$grouping][] = " civicrm_mailing_job.status = '{$value}' ";
380 $query->_qill
[$grouping][] = "Mailing Job Status IS \"$value\"";
384 case 'mailing_campaign_id':
385 $name = 'campaign_id';
386 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_mailing.$name", $op, $value, 'Integer');
387 list($op, $value) = CRM_Contact_BAO_Query
::buildQillForFieldValue('CRM_Mailing_DAO_Mailing', $name, $value, $op);
388 $query->_qill
[$grouping][] = ts('Campaign %1 %2', array(1 => $op, 2 => $value));
389 $query->_tables
['civicrm_mailing'] = $query->_whereTables
['civicrm_mailing'] = 1;
390 $query->_tables
['civicrm_mailing_recipients'] = $query->_whereTables
['civicrm_mailing_recipients'] = 1;
396 * Add all the elements shared between Mailing search and advnaced search.
399 * @param CRM_Core_Form $form
401 public static function buildSearchForm(&$form) {
403 $mailings = CRM_Mailing_BAO_Mailing
::getMailingsList();
405 if (!empty($mailings)) {
406 $form->add('select', 'mailing_id', ts('Mailing Name(s)'), $mailings, FALSE,
407 array('id' => 'mailing_id', 'multiple' => 'multiple', 'class' => 'crm-select2')
411 CRM_Core_Form_Date
::buildDateRange($form, 'mailing_date', 1, '_low', '_high', ts('From'), FALSE);
412 $form->addElement('hidden', 'mailing_date_range_error');
413 $form->addFormRule(array('CRM_Mailing_BAO_Query', 'formRule'), $form);
415 $mailingJobStatuses = array(
416 '' => ts('- select -'),
417 'Complete' => 'Complete',
418 'Scheduled' => 'Scheduled',
419 'Running' => 'Running',
420 'Canceled' => 'Canceled',
422 $form->addElement('select', 'mailing_job_status', ts('Mailing Job Status'), $mailingJobStatuses, FALSE);
424 $mailingBounceTypes = CRM_Core_PseudoConstant
::get(
425 'CRM_Mailing_Event_DAO_Bounce', 'bounce_type_id',
426 array('keyColumn' => 'id', 'labelColumn' => 'name')
428 $form->add('select', 'mailing_bounce_types', ts('Bounce Types'), $mailingBounceTypes, FALSE,
429 array('id' => 'mailing_bounce_types', 'multiple' => 'multiple', 'class' => 'crm-select2')
433 $form->addRadio('mailing_delivery_status', ts('Delivery Status'), CRM_Mailing_PseudoConstant
::yesNoOptions('delivered'), array('allowClear' => TRUE));
434 $form->addRadio('mailing_open_status', ts('Trackable Opens'), CRM_Mailing_PseudoConstant
::yesNoOptions('open'), array('allowClear' => TRUE));
435 $form->addRadio('mailing_click_status', ts('Trackable URLs'), CRM_Mailing_PseudoConstant
::yesNoOptions('click'), array('allowClear' => TRUE));
436 $form->addRadio('mailing_reply_status', ts('Trackable Replies'), CRM_Mailing_PseudoConstant
::yesNoOptions('reply'), array('allowClear' => TRUE));
438 $form->add('checkbox', 'mailing_unsubscribe', ts('Unsubscribe Requests'));
439 $form->add('checkbox', 'mailing_optout', ts('Opt-out Requests'));
440 $form->add('checkbox', 'mailing_forward', ts('Forwards'));
441 // Campaign select field
442 CRM_Campaign_BAO_Campaign
::addCampaignInComponentSearch($form, 'mailing_campaign_id');
444 $form->assign('validCiviMailing', TRUE);
451 public static function searchAction(&$row, $id) {
457 public static function tableNames(&$tables) {
461 * Filter query results based on which contacts do (not) have a particular mailing event in their history.
465 * @param string $tableName
466 * @param string $fieldName
469 * @param $valueTitles
471 public static function mailingEventQueryBuilder(&$query, &$values, $tableName, $fieldName, $fieldTitle, &$valueTitles) {
472 list($name, $op, $value, $grouping, $wildcard) = $values;
474 if (empty($value) ||
$value == 'A') {
475 // don't do any filtering
480 $query->_where
[$grouping][] = $tableName . ".id is not null ";
482 elseif ($value == 'N') {
483 $query->_where
[$grouping][] = $tableName . ".id is null ";
486 if (is_array($value)) {
487 $query->_where
[$grouping][] = "$tableName.$fieldName $op (" . implode(',', $value) . ")";
488 $query->_qill
[$grouping][] = "$fieldTitle $op " . implode(', ', array_intersect_key($valueTitles, array_flip($value)));
491 $query->_qill
[$grouping][] = $fieldTitle . ' - ' . $valueTitles[$value];
494 $query->_tables
['civicrm_mailing'] = $query->_whereTables
['civicrm_mailing'] = 1;
495 $query->_tables
['civicrm_mailing_job'] = $query->_whereTables
['civicrm_mailing_job'] = 1;
496 $query->_tables
['civicrm_mailing_event_queue'] = $query->_whereTables
['civicrm_mailing_event_queue'] = 1;
497 $query->_tables
['civicrm_mailing_recipients'] = $query->_whereTables
['civicrm_mailing_recipients'] = 1;
498 $query->_tables
[$tableName] = $query->_whereTables
[$tableName] = 1;
502 * Check if the values in the date range are in correct chronological order.
504 * @param array $fields
505 * @param array $files
506 * @param CRM_Core_Form $form
510 public static function formRule($fields, $files, $form) {
513 if (empty($fields['mailing_date_high']) ||
empty($fields['mailing_date_low'])) {
517 CRM_Utils_Rule
::validDateRange($fields, 'mailing_date', $errors, ts('Mailing Date'));
519 return empty($errors) ?
TRUE : $errors;