Merge pull request #15771 from eileenmcnaughton/partially
[civicrm-core.git] / CRM / Mailing / BAO / Query.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2020
32 */
33 class CRM_Mailing_BAO_Query {
34
35 /**
36 * Get fields for the mailing & mailing job entity.
37 *
38 * @return array
39 */
40 public static function &getFields() {
41 $mailingFields = CRM_Mailing_BAO_Mailing::fields();
42 $mailingJobFields = CRM_Mailing_BAO_MailingJob::fields();
43
44 // In general it's good to return as many fields as could possibly be searched, but
45 // with the limitation that if the fields do not have unique names they might
46 // clobber other fields :-(
47 $fields = [
48 'mailing_id' => $mailingFields['id'],
49 'mailing_job_start_date' => $mailingJobFields['mailing_job_start_date'],
50 ];
51 return $fields;
52 }
53
54 /**
55 * If mailings are involved, add the specific Mailing fields
56 *
57 * @param $query
58 */
59 public static function select(&$query) {
60 // if Mailing mode add mailing id
61 if ($query->_mode & CRM_Contact_BAO_Query::MODE_MAILING) {
62 $query->_select['mailing_id'] = "civicrm_mailing.id as mailing_id";
63 $query->_element['mailing_id'] = 1;
64
65 // base table is contact, so join recipients to it
66 $query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients']
67 = " INNER JOIN civicrm_mailing_recipients ON civicrm_mailing_recipients.contact_id = contact_a.id ";
68
69 $query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1;
70
71 // get mailing name
72 if (!empty($query->_returnProperties['mailing_name'])) {
73 $query->_select['mailing_name'] = "civicrm_mailing.name as mailing_name";
74 $query->_element['mailing_name'] = 1;
75 }
76
77 // get mailing subject
78 if (!empty($query->_returnProperties['mailing_subject'])) {
79 $query->_select['mailing_subject'] = "civicrm_mailing.subject as mailing_subject";
80 $query->_element['mailing_subject'] = 1;
81 }
82
83 // get mailing status
84 if (!empty($query->_returnProperties['mailing_job_status'])) {
85 $query->_tables['civicrm_mailing_job'] = $query->_whereTables['civicrm_mailing_job']
86 = " 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 ";
87 $query->_select['mailing_job_status'] = "civicrm_mailing_job.status as mailing_job_status";
88 $query->_element['mailing_job_status'] = 1;
89 }
90
91 // get email on hold
92 if (!empty($query->_returnProperties['email_on_hold'])) {
93 $query->_select['email_on_hold'] = "recipient_email.on_hold as email_on_hold";
94 $query->_element['email_on_hold'] = 1;
95 $query->_tables['recipient_email'] = $query->_whereTables['recipient_email'] = 1;
96 }
97
98 // get recipient email
99 if (!empty($query->_returnProperties['email'])) {
100 $query->_select['email'] = "recipient_email.email as email";
101 $query->_element['email'] = 1;
102 $query->_tables['recipient_email'] = $query->_whereTables['recipient_email'] = 1;
103 }
104
105 // get user opt out
106 if (!empty($query->_returnProperties['contact_opt_out'])) {
107 $query->_select['contact_opt_out'] = "contact_a.is_opt_out as contact_opt_out";
108 $query->_element['contact_opt_out'] = 1;
109 }
110
111 // mailing job end date / completed date
112 if (!empty($query->_returnProperties['mailing_job_end_date'])) {
113 $query->_tables['civicrm_mailing_job'] = $query->_whereTables['civicrm_mailing_job']
114 = " 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 ";
115 $query->_select['mailing_job_end_date'] = "civicrm_mailing_job.end_date as mailing_job_end_date";
116 $query->_element['mailing_job_end_date'] = 1;
117 }
118
119 if (!empty($query->_returnProperties['mailing_recipients_id'])) {
120 $query->_select['mailing_recipients_id'] = " civicrm_mailing_recipients.id as mailing_recipients_id";
121 $query->_element['mailing_recipients_id'] = 1;
122 }
123 }
124
125 if (CRM_Utils_Array::value('mailing_campaign_id', $query->_returnProperties)) {
126 $query->_select['mailing_campaign_id'] = 'civicrm_mailing.campaign_id as mailing_campaign_id';
127 $query->_element['mailing_campaign_id'] = 1;
128 $query->_tables['civicrm_campaign'] = 1;
129 }
130 }
131
132 /**
133 * Get the metadata for fields to be included on the mailing search form.
134 *
135 * @throws \CiviCRM_API3_Exception
136 *
137 * @todo ideally this would be a trait included on the mailing search & advanced search
138 * rather than a static function.
139 */
140 public static function getSearchFieldMetadata() {
141 $fields = ['mailing_job_start_date'];
142 $metadata = civicrm_api3('Mailing', 'getfields', [])['values'];
143 $metadata = array_merge($metadata, civicrm_api3('MailingJob', 'getfields', [])['values']);
144 return array_intersect_key($metadata, array_flip($fields));
145 }
146
147 /**
148 * @param $query
149 */
150 public static function where(&$query) {
151 $grouping = NULL;
152 foreach (array_keys($query->_params) as $id) {
153 if (empty($query->_params[$id][0])) {
154 continue;
155 }
156 if (substr($query->_params[$id][0], 0, 8) == 'mailing_') {
157 if ($query->_mode == CRM_Contact_BAO_Query::MODE_CONTACTS) {
158 $query->_useDistinct = TRUE;
159 }
160 $grouping = $query->_params[$id][3];
161 self::whereClauseSingle($query->_params[$id], $query);
162 }
163 }
164 }
165
166 /**
167 * @param string $name
168 * @param $mode
169 * @param $side
170 *
171 * @return null|string
172 */
173 public static function from($name, $mode, $side) {
174 $from = NULL;
175
176 switch ($name) {
177 case 'civicrm_mailing_recipients':
178 $from = " $side JOIN civicrm_mailing_recipients ON civicrm_mailing_recipients.contact_id = contact_a.id";
179 break;
180
181 case 'civicrm_mailing_event_queue':
182 // this is tightly binded so as to do a check WRT actual job recipients ('child' type jobs)
183 $from = " INNER JOIN civicrm_mailing_event_queue ON
184 civicrm_mailing_event_queue.contact_id = civicrm_mailing_recipients.contact_id
185 AND civicrm_mailing_event_queue.job_id = civicrm_mailing_job.id AND civicrm_mailing_job.job_type = 'child'";
186 break;
187
188 case 'civicrm_mailing':
189 $from = " $side JOIN civicrm_mailing ON civicrm_mailing.id = civicrm_mailing_recipients.mailing_id ";
190 break;
191
192 case 'civicrm_mailing_job':
193 $from = " $side JOIN civicrm_mailing_job ON civicrm_mailing_job.mailing_id = civicrm_mailing.id AND civicrm_mailing_job.is_test != 1 ";
194 break;
195
196 case 'civicrm_mailing_event_bounce':
197 case 'civicrm_mailing_event_delivered':
198 case 'civicrm_mailing_event_opened':
199 case 'civicrm_mailing_event_reply':
200 case 'civicrm_mailing_event_unsubscribe':
201 case 'civicrm_mailing_event_forward':
202 case 'civicrm_mailing_event_trackable_url_open':
203 $from = " $side JOIN $name ON $name.event_queue_id = civicrm_mailing_event_queue.id";
204 break;
205
206 case 'recipient_email':
207 $from = " $side JOIN civicrm_email recipient_email ON recipient_email.id = civicrm_mailing_recipients.email_id";
208 break;
209
210 case 'civicrm_campaign':
211 $from = " $side JOIN civicrm_campaign ON civicrm_campaign.id = civicrm_mailing.campaign_id";
212 break;
213 }
214
215 return $from;
216 }
217
218 /**
219 * @param $mode
220 * @param bool $includeCustomFields
221 *
222 * @return array|null
223 */
224 public static function defaultReturnProperties(
225 $mode,
226 $includeCustomFields = TRUE
227 ) {
228
229 $properties = NULL;
230 if ($mode & CRM_Contact_BAO_Query::MODE_MAILING) {
231 $properties = [
232 'mailing_id' => 1,
233 'mailing_campaign_id' => 1,
234 'mailing_name' => 1,
235 'sort_name' => 1,
236 'email' => 1,
237 'mailing_subject' => 1,
238 'email_on_hold' => 1,
239 'contact_opt_out' => 1,
240 'mailing_job_status' => 1,
241 'mailing_job_end_date' => 1,
242 'contact_type' => 1,
243 'contact_sub_type' => 1,
244 'mailing_recipients_id' => 1,
245 ];
246 }
247 return $properties;
248 }
249
250 /**
251 * @param $values
252 * @param $query
253 */
254 public static function whereClauseSingle(&$values, &$query) {
255 list($name, $op, $value, $grouping, $wildcard) = $values;
256
257 switch ($name) {
258 case 'mailing_id':
259 $selectedMailings = array_flip($value);
260 $value = "(" . implode(',', $value) . ")";
261 $op = 'IN';
262 $query->_where[$grouping][] = "civicrm_mailing.id $op $value";
263
264 $mailings = CRM_Mailing_BAO_Mailing::getMailingsList();
265 foreach ($selectedMailings as $id => $dnc) {
266 $selectedMailings[$id] = $mailings[$id];
267 }
268 $selectedMailings = implode(' or ', $selectedMailings);
269
270 $query->_qill[$grouping][] = "Mailing Name $op \"$selectedMailings\"";
271 $query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1;
272 $query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1;
273 return;
274
275 case 'mailing_name':
276 $value = addslashes($value);
277 if ($wildcard) {
278 $value = "%$value%";
279 $op = 'LIKE';
280 }
281
282 $query->_where[$grouping][] = "civicrm_mailing.name $op '$value'";
283 $query->_qill[$grouping][] = "Mailing Namename $op \"$value\"";
284 $query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1;
285 $query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1;
286 return;
287
288 case 'mailing_date':
289 case 'mailing_date_low':
290 case 'mailing_date_high':
291 $query->_tables['civicrm_mailing_job'] = $query->_whereTables['civicrm_mailing_job'] = 1;
292 $query->dateQueryBuilder($values,
293 'civicrm_mailing_job', 'mailing_date', 'start_date', 'Mailing Delivery Date'
294 );
295 return;
296
297 case 'mailing_delivery_status':
298 $options = CRM_Mailing_PseudoConstant::yesNoOptions('delivered');
299
300 list($name, $op, $value, $grouping, $wildcard) = $values;
301 if ($value == 'Y') {
302 self::mailingEventQueryBuilder($query, $values,
303 'civicrm_mailing_event_delivered',
304 'mailing_delivery_status',
305 ts('Mailing Delivery'),
306 $options
307 );
308 }
309 elseif ($value == 'N') {
310 $options['Y'] = $options['N'];
311 $values = [$name, $op, 'Y', $grouping, $wildcard];
312 self::mailingEventQueryBuilder($query, $values,
313 'civicrm_mailing_event_bounce',
314 'mailing_delivery_status',
315 ts('Mailing Delivery'),
316 $options
317 );
318 }
319 return;
320
321 case 'mailing_bounce_types':
322 $op = 'IN';
323 $values = [$name, $op, $value, $grouping, $wildcard];
324 self::mailingEventQueryBuilder($query, $values,
325 'civicrm_mailing_event_bounce',
326 'bounce_type_id',
327 ts('Bounce type(s)'),
328 CRM_Core_PseudoConstant::get('CRM_Mailing_Event_DAO_Bounce', 'bounce_type_id', [
329 'keyColumn' => 'id',
330 'labelColumn' => 'name',
331 ])
332 );
333 return;
334
335 case 'mailing_open_status':
336 self::mailingEventQueryBuilder($query, $values,
337 'civicrm_mailing_event_opened', 'mailing_open_status', ts('Mailing: Trackable Opens'), CRM_Mailing_PseudoConstant::yesNoOptions('open')
338 );
339 return;
340
341 case 'mailing_click_status':
342 self::mailingEventQueryBuilder($query, $values,
343 'civicrm_mailing_event_trackable_url_open', 'mailing_click_status', ts('Mailing: Trackable URL Clicks'), CRM_Mailing_PseudoConstant::yesNoOptions('click')
344 );
345 return;
346
347 case 'mailing_reply_status':
348 self::mailingEventQueryBuilder($query, $values,
349 'civicrm_mailing_event_reply', 'mailing_reply_status', ts('Mailing: Trackable Replies'), CRM_Mailing_PseudoConstant::yesNoOptions('reply')
350 );
351 return;
352
353 case 'mailing_optout':
354 $valueTitle = [1 => ts('Opt-out Requests')];
355 // include opt-out events only
356 $query->_where[$grouping][] = "civicrm_mailing_event_unsubscribe.org_unsubscribe = 1";
357 self::mailingEventQueryBuilder($query, $values,
358 'civicrm_mailing_event_unsubscribe', 'mailing_unsubscribe',
359 ts('Mailing: '), $valueTitle
360 );
361 return;
362
363 case 'mailing_unsubscribe':
364 $valueTitle = [1 => ts('Unsubscribe Requests')];
365 // exclude opt-out events
366 $query->_where[$grouping][] = "civicrm_mailing_event_unsubscribe.org_unsubscribe = 0";
367 self::mailingEventQueryBuilder($query, $values,
368 'civicrm_mailing_event_unsubscribe', 'mailing_unsubscribe',
369 ts('Mailing: '), $valueTitle
370 );
371 return;
372
373 case 'mailing_forward':
374 $valueTitle = ['Y' => ts('Forwards')];
375 // since its a checkbox
376 $values[2] = 'Y';
377 self::mailingEventQueryBuilder($query, $values,
378 'civicrm_mailing_event_forward', 'mailing_forward',
379 ts('Mailing: '), $valueTitle
380 );
381 return;
382
383 case 'mailing_job_status':
384 if (!empty($value)) {
385 if ($value != 'Scheduled' && $value != 'Canceled') {
386 $query->_tables['civicrm_mailing_event_queue'] = $query->_whereTables['civicrm_mailing_event_queue'] = 1;
387 }
388 $query->_tables['civicrm_mailing_job'] = $query->_whereTables['civicrm_mailing_job'] = 1;
389 $query->_where[$grouping][] = " civicrm_mailing_job.status = '{$value}' ";
390 $query->_qill[$grouping][] = "Mailing Job Status IS \"$value\"";
391 }
392 return;
393
394 case 'mailing_campaign_id':
395 $name = 'campaign_id';
396 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_mailing.$name", $op, $value, 'Integer');
397 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Mailing_DAO_Mailing', $name, $value, $op);
398 $query->_qill[$grouping][] = ts('Campaign %1 %2', [1 => $op, 2 => $value]);
399 $query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1;
400 $query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1;
401 return;
402 }
403 }
404
405 /**
406 * Add all the elements shared between Mailing search and advnaced search.
407 *
408 * @param \CRM_Mailing_Form_Search $form
409 *
410 * @throws \CiviCRM_API3_Exception
411 */
412 public static function buildSearchForm(&$form) {
413 $form->addSearchFieldMetadata(['Mailing' => self::getSearchFieldMetadata()]);
414 $form->addFormFieldsFromMetadata();
415
416 // mailing selectors
417 $mailings = CRM_Mailing_BAO_Mailing::getMailingsList();
418
419 if (!empty($mailings)) {
420 $form->add('select', 'mailing_id', ts('Mailing Name(s)'), $mailings, FALSE,
421 ['id' => 'mailing_id', 'multiple' => 'multiple', 'class' => 'crm-select2']
422 );
423 }
424
425 $mailingJobStatuses = [
426 '' => ts('- select -'),
427 'Complete' => 'Complete',
428 'Scheduled' => 'Scheduled',
429 'Running' => 'Running',
430 'Canceled' => 'Canceled',
431 ];
432 $form->addElement('select', 'mailing_job_status', ts('Mailing Job Status'), $mailingJobStatuses, FALSE);
433
434 $mailingBounceTypes = CRM_Core_PseudoConstant::get(
435 'CRM_Mailing_Event_DAO_Bounce', 'bounce_type_id',
436 ['keyColumn' => 'id', 'labelColumn' => 'name']
437 );
438 $form->add('select', 'mailing_bounce_types', ts('Bounce Types'), $mailingBounceTypes, FALSE,
439 ['id' => 'mailing_bounce_types', 'multiple' => 'multiple', 'class' => 'crm-select2']
440 );
441
442 // event filters
443 $form->addRadio('mailing_delivery_status', ts('Delivery Status'), CRM_Mailing_PseudoConstant::yesNoOptions('delivered'), ['allowClear' => TRUE]);
444 $form->addRadio('mailing_open_status', ts('Trackable Opens'), CRM_Mailing_PseudoConstant::yesNoOptions('open'), ['allowClear' => TRUE]);
445 $form->addRadio('mailing_click_status', ts('Trackable URLs'), CRM_Mailing_PseudoConstant::yesNoOptions('click'), ['allowClear' => TRUE]);
446 $form->addRadio('mailing_reply_status', ts('Trackable Replies'), CRM_Mailing_PseudoConstant::yesNoOptions('reply'), ['allowClear' => TRUE]);
447
448 $form->add('checkbox', 'mailing_unsubscribe', ts('Unsubscribe Requests'));
449 $form->add('checkbox', 'mailing_optout', ts('Opt-out Requests'));
450 $form->add('checkbox', 'mailing_forward', ts('Forwards'));
451 // Campaign select field
452 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'mailing_campaign_id');
453
454 $form->assign('validCiviMailing', TRUE);
455 }
456
457 /**
458 * @param $row
459 * @param int $id
460 */
461 public static function searchAction(&$row, $id) {
462 }
463
464 /**
465 * @param $tables
466 */
467 public static function tableNames(&$tables) {
468 if (isset($tables['civicrm_mailing_job'])) {
469 $tables['civicrm_mailing'] = $tables['civicrm_mailing'] ?? 1;
470 $tables['civicrm_mailing_recipients'] = $tables['civicrm_mailing_recipients'] ?? 1;
471 }
472 }
473
474 /**
475 * Filter query results based on which contacts do (not) have a particular mailing event in their history.
476 *
477 * @param $query
478 * @param $values
479 * @param string $tableName
480 * @param string $fieldName
481 * @param $fieldTitle
482 *
483 * @param $valueTitles
484 */
485 public static function mailingEventQueryBuilder(&$query, &$values, $tableName, $fieldName, $fieldTitle, &$valueTitles) {
486 list($name, $op, $value, $grouping, $wildcard) = $values;
487
488 if (empty($value) || $value == 'A') {
489 // don't do any filtering
490 return;
491 }
492
493 if ($value == 'Y') {
494 $query->_where[$grouping][] = $tableName . ".id is not null ";
495 }
496 elseif ($value == 'N') {
497 $query->_where[$grouping][] = $tableName . ".id is null ";
498 }
499
500 if (is_array($value)) {
501 $query->_where[$grouping][] = "$tableName.$fieldName $op (" . implode(',', $value) . ")";
502 $query->_qill[$grouping][] = "$fieldTitle $op " . implode(', ', array_intersect_key($valueTitles, array_flip($value)));
503 }
504 else {
505 $query->_qill[$grouping][] = $fieldTitle . ' - ' . $valueTitles[$value];
506 }
507
508 $query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1;
509 $query->_tables['civicrm_mailing_job'] = $query->_whereTables['civicrm_mailing_job'] = 1;
510 $query->_tables['civicrm_mailing_event_queue'] = $query->_whereTables['civicrm_mailing_event_queue'] = 1;
511 $query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1;
512 $query->_tables[$tableName] = $query->_whereTables[$tableName] = 1;
513 }
514
515 }