Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
f299f7db | 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
6a488035 TO |
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 | | |
c73475ea | 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
6a488035 TO |
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 | | |
c73475ea WA |
20 | | License and the CiviCRM Licensing Exception along | |
21 | | with this program; if not, contact CiviCRM LLC | | |
6a488035 TO |
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 | |
f299f7db | 31 | * @copyright CiviCRM LLC (c) 2004-2020 |
6a488035 TO |
32 | */ |
33 | class CRM_Mailing_BAO_Query { | |
34 | ||
e0ef6999 | 35 | /** |
0a71a7f7 | 36 | * Get fields for the mailing & mailing job entity. |
37 | * | |
38 | * @return array | |
e0ef6999 | 39 | */ |
00be9182 | 40 | public static function &getFields() { |
0a71a7f7 | 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; | |
6a488035 TO |
52 | } |
53 | ||
54 | /** | |
100fef9d | 55 | * If mailings are involved, add the specific Mailing fields |
6a488035 | 56 | * |
77b97be7 | 57 | * @param $query |
6a488035 | 58 | */ |
00be9182 | 59 | public static function select(&$query) { |
6a488035 TO |
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; | |
2cc569f2 | 64 | |
335038eb | 65 | // base table is contact, so join recipients to it |
6c552737 TO |
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 "; | |
2cc569f2 | 68 | |
335038eb | 69 | $query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1; |
2cc569f2 PJ |
70 | |
71 | // get mailing name | |
a7488080 | 72 | if (!empty($query->_returnProperties['mailing_name'])) { |
353ffa53 | 73 | $query->_select['mailing_name'] = "civicrm_mailing.name as mailing_name"; |
2cc569f2 PJ |
74 | $query->_element['mailing_name'] = 1; |
75 | } | |
76 | ||
77 | // get mailing subject | |
a7488080 | 78 | if (!empty($query->_returnProperties['mailing_subject'])) { |
353ffa53 | 79 | $query->_select['mailing_subject'] = "civicrm_mailing.subject as mailing_subject"; |
2cc569f2 PJ |
80 | $query->_element['mailing_subject'] = 1; |
81 | } | |
82 | ||
83 | // get mailing status | |
a7488080 | 84 | if (!empty($query->_returnProperties['mailing_job_status'])) { |
6c552737 TO |
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 "; | |
353ffa53 | 87 | $query->_select['mailing_job_status'] = "civicrm_mailing_job.status as mailing_job_status"; |
2cc569f2 PJ |
88 | $query->_element['mailing_job_status'] = 1; |
89 | } | |
90 | ||
91 | // get email on hold | |
a7488080 | 92 | if (!empty($query->_returnProperties['email_on_hold'])) { |
2cc569f2 PJ |
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 | |
a7488080 | 99 | if (!empty($query->_returnProperties['email'])) { |
2cc569f2 PJ |
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 | |
a7488080 | 106 | if (!empty($query->_returnProperties['contact_opt_out'])) { |
2cc569f2 PJ |
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 | |
a7488080 | 112 | if (!empty($query->_returnProperties['mailing_job_end_date'])) { |
6c552737 TO |
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 "; | |
353ffa53 | 115 | $query->_select['mailing_job_end_date'] = "civicrm_mailing_job.end_date as mailing_job_end_date"; |
2cc569f2 PJ |
116 | $query->_element['mailing_job_end_date'] = 1; |
117 | } | |
47546fcf | 118 | |
a7488080 | 119 | if (!empty($query->_returnProperties['mailing_recipients_id'])) { |
335038eb PJ |
120 | $query->_select['mailing_recipients_id'] = " civicrm_mailing_recipients.id as mailing_recipients_id"; |
121 | $query->_element['mailing_recipients_id'] = 1; | |
47546fcf | 122 | } |
6a488035 | 123 | } |
04cac635 | 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 | } | |
6a488035 TO |
130 | } |
131 | ||
2c3ccaa9 | 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() { | |
0058ef3f | 141 | $fields = ['mailing_job_start_date']; |
2c3ccaa9 | 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 | ||
e0ef6999 EM |
147 | /** |
148 | * @param $query | |
149 | */ | |
00be9182 | 150 | public static function where(&$query) { |
6a488035 TO |
151 | $grouping = NULL; |
152 | foreach (array_keys($query->_params) as $id) { | |
a7488080 | 153 | if (empty($query->_params[$id][0])) { |
6a488035 TO |
154 | continue; |
155 | } | |
156 | if (substr($query->_params[$id][0], 0, 8) == 'mailing_') { | |
2da8c6c4 | 157 | if ($query->_mode == CRM_Contact_BAO_Query::MODE_CONTACTS) { |
6a488035 TO |
158 | $query->_useDistinct = TRUE; |
159 | } | |
160 | $grouping = $query->_params[$id][3]; | |
161 | self::whereClauseSingle($query->_params[$id], $query); | |
162 | } | |
163 | } | |
164 | } | |
165 | ||
e0ef6999 | 166 | /** |
100fef9d | 167 | * @param string $name |
e0ef6999 EM |
168 | * @param $mode |
169 | * @param $side | |
170 | * | |
171 | * @return null|string | |
172 | */ | |
00be9182 | 173 | public static function from($name, $mode, $side) { |
6a488035 | 174 | $from = NULL; |
335038eb | 175 | |
6a488035 | 176 | switch ($name) { |
335038eb PJ |
177 | case 'civicrm_mailing_recipients': |
178 | $from = " $side JOIN civicrm_mailing_recipients ON civicrm_mailing_recipients.contact_id = contact_a.id"; | |
6a488035 TO |
179 | break; |
180 | ||
335038eb PJ |
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'"; | |
6a488035 TO |
186 | break; |
187 | ||
188 | case 'civicrm_mailing': | |
335038eb PJ |
189 | $from = " $side JOIN civicrm_mailing ON civicrm_mailing.id = civicrm_mailing_recipients.mailing_id "; |
190 | break; | |
191 | ||
192 | case 'civicrm_mailing_job': | |
eb6a4756 | 193 | $from = " $side JOIN civicrm_mailing_job ON civicrm_mailing_job.mailing_id = civicrm_mailing.id AND civicrm_mailing_job.is_test != 1 "; |
6a488035 TO |
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; | |
2cc569f2 PJ |
205 | |
206 | case 'recipient_email': | |
335038eb PJ |
207 | $from = " $side JOIN civicrm_email recipient_email ON recipient_email.id = civicrm_mailing_recipients.email_id"; |
208 | break; | |
7851dc81 JP |
209 | |
210 | case 'civicrm_campaign': | |
211 | $from = " $side JOIN civicrm_campaign ON civicrm_campaign.id = civicrm_mailing.campaign_id"; | |
212 | break; | |
6a488035 TO |
213 | } |
214 | ||
215 | return $from; | |
216 | } | |
217 | ||
e0ef6999 EM |
218 | /** |
219 | * @param $mode | |
220 | * @param bool $includeCustomFields | |
221 | * | |
222 | * @return array|null | |
223 | */ | |
2da40d21 | 224 | public static function defaultReturnProperties( |
a3d7e8ee | 225 | $mode, |
6a488035 TO |
226 | $includeCustomFields = TRUE |
227 | ) { | |
228 | ||
229 | $properties = NULL; | |
230 | if ($mode & CRM_Contact_BAO_Query::MODE_MAILING) { | |
be2fb01f | 231 | $properties = [ |
2cc569f2 | 232 | 'mailing_id' => 1, |
04cac635 | 233 | 'mailing_campaign_id' => 1, |
2cc569f2 PJ |
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, | |
47546fcf | 243 | 'contact_sub_type' => 1, |
21dfd5f5 | 244 | 'mailing_recipients_id' => 1, |
be2fb01f | 245 | ]; |
6a488035 TO |
246 | } |
247 | return $properties; | |
248 | } | |
249 | ||
e0ef6999 EM |
250 | /** |
251 | * @param $values | |
252 | * @param $query | |
253 | */ | |
00be9182 | 254 | public static function whereClauseSingle(&$values, &$query) { |
6a488035 TO |
255 | list($name, $op, $value, $grouping, $wildcard) = $values; |
256 | ||
6a488035 TO |
257 | switch ($name) { |
258 | case 'mailing_id': | |
259 | $selectedMailings = array_flip($value); | |
6a488035 TO |
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\""; | |
6a488035 | 271 | $query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1; |
335038eb | 272 | $query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1; |
6a488035 TO |
273 | return; |
274 | ||
275 | case 'mailing_name': | |
b0f72207 | 276 | $value = addslashes($value); |
481a74f4 | 277 | if ($wildcard) { |
6a488035 | 278 | $value = "%$value%"; |
353ffa53 | 279 | $op = 'LIKE'; |
6a488035 | 280 | } |
b27d1855 | 281 | |
b0f72207 | 282 | $query->_where[$grouping][] = "civicrm_mailing.name $op '$value'"; |
353ffa53 | 283 | $query->_qill[$grouping][] = "Mailing Namename $op \"$value\""; |
6a488035 | 284 | $query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1; |
335038eb | 285 | $query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1; |
6a488035 TO |
286 | return; |
287 | ||
288 | case 'mailing_date': | |
289 | case 'mailing_date_low': | |
290 | case 'mailing_date_high': | |
6a488035 TO |
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']; | |
be2fb01f | 311 | $values = [$name, $op, 'Y', $grouping, $wildcard]; |
6a488035 TO |
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 | ||
111b2847 | 321 | case 'mailing_bounce_types': |
322 | $op = 'IN'; | |
be2fb01f | 323 | $values = [$name, $op, $value, $grouping, $wildcard]; |
111b2847 | 324 | self::mailingEventQueryBuilder($query, $values, |
325 | 'civicrm_mailing_event_bounce', | |
326 | 'bounce_type_id', | |
327 | ts('Bounce type(s)'), | |
be2fb01f | 328 | CRM_Core_PseudoConstant::get('CRM_Mailing_Event_DAO_Bounce', 'bounce_type_id', [ |
7e8c8317 SL |
329 | 'keyColumn' => 'id', |
330 | 'labelColumn' => 'name', | |
331 | ]) | |
111b2847 | 332 | ); |
333 | return; | |
334 | ||
6a488035 TO |
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': | |
be2fb01f | 354 | $valueTitle = [1 => ts('Opt-out Requests')]; |
6a488035 TO |
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': | |
be2fb01f | 364 | $valueTitle = [1 => ts('Unsubscribe Requests')]; |
6a488035 TO |
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': | |
be2fb01f | 374 | $valueTitle = ['Y' => ts('Forwards')]; |
6a488035 TO |
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; | |
335038eb PJ |
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 | } | |
335038eb | 388 | $query->_tables['civicrm_mailing_job'] = $query->_whereTables['civicrm_mailing_job'] = 1; |
335038eb | 389 | $query->_where[$grouping][] = " civicrm_mailing_job.status = '{$value}' "; |
353ffa53 | 390 | $query->_qill[$grouping][] = "Mailing Job Status IS \"$value\""; |
335038eb PJ |
391 | } |
392 | return; | |
04cac635 | 393 | |
394 | case 'mailing_campaign_id': | |
6f56dc34 | 395 | $name = 'campaign_id'; |
c432c016 | 396 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_mailing.$name", $op, $value, 'Integer'); |
6f56dc34 | 397 | list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Mailing_DAO_Mailing', $name, $value, $op); |
be2fb01f | 398 | $query->_qill[$grouping][] = ts('Campaign %1 %2', [1 => $op, 2 => $value]); |
6f56dc34 | 399 | $query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1; |
04cac635 | 400 | $query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1; |
401 | return; | |
6a488035 TO |
402 | } |
403 | } | |
404 | ||
405 | /** | |
fe482240 | 406 | * Add all the elements shared between Mailing search and advnaced search. |
6a488035 | 407 | * |
2c3ccaa9 | 408 | * @param \CRM_Mailing_Form_Search $form |
6a488035 | 409 | * |
2c3ccaa9 | 410 | * @throws \CiviCRM_API3_Exception |
6a488035 | 411 | */ |
00be9182 | 412 | public static function buildSearchForm(&$form) { |
2c3ccaa9 | 413 | $form->addSearchFieldMetadata(['Mailing' => self::getSearchFieldMetadata()]); |
414 | $form->addFormFieldsFromMetadata(); | |
415 | ||
6a488035 TO |
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, | |
be2fb01f | 421 | ['id' => 'mailing_id', 'multiple' => 'multiple', 'class' => 'crm-select2'] |
6a488035 TO |
422 | ); |
423 | } | |
424 | ||
be2fb01f | 425 | $mailingJobStatuses = [ |
335038eb PJ |
426 | '' => ts('- select -'), |
427 | 'Complete' => 'Complete', | |
428 | 'Scheduled' => 'Scheduled', | |
429 | 'Running' => 'Running', | |
21dfd5f5 | 430 | 'Canceled' => 'Canceled', |
be2fb01f | 431 | ]; |
335038eb PJ |
432 | $form->addElement('select', 'mailing_job_status', ts('Mailing Job Status'), $mailingJobStatuses, FALSE); |
433 | ||
111b2847 | 434 | $mailingBounceTypes = CRM_Core_PseudoConstant::get( |
435 | 'CRM_Mailing_Event_DAO_Bounce', 'bounce_type_id', | |
be2fb01f | 436 | ['keyColumn' => 'id', 'labelColumn' => 'name'] |
111b2847 | 437 | ); |
c9fd7217 | 438 | $form->add('select', 'mailing_bounce_types', ts('Bounce Types'), $mailingBounceTypes, FALSE, |
be2fb01f | 439 | ['id' => 'mailing_bounce_types', 'multiple' => 'multiple', 'class' => 'crm-select2'] |
c9fd7217 | 440 | ); |
111b2847 | 441 | |
6a488035 | 442 | // event filters |
be2fb01f CW |
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]); | |
6a488035 TO |
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')); | |
04cac635 | 451 | // Campaign select field |
452 | CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'mailing_campaign_id'); | |
6a488035 TO |
453 | |
454 | $form->assign('validCiviMailing', TRUE); | |
6a488035 TO |
455 | } |
456 | ||
e0ef6999 EM |
457 | /** |
458 | * @param $row | |
100fef9d | 459 | * @param int $id |
e0ef6999 | 460 | */ |
35f7561f TO |
461 | public static function searchAction(&$row, $id) { |
462 | } | |
6a488035 | 463 | |
e0ef6999 EM |
464 | /** |
465 | * @param $tables | |
466 | */ | |
00be9182 | 467 | public static function tableNames(&$tables) { |
0a71a7f7 | 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 | } | |
337750c9 | 472 | } |
6a488035 TO |
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 | |
100fef9d CW |
479 | * @param string $tableName |
480 | * @param string $fieldName | |
6a488035 TO |
481 | * @param $fieldTitle |
482 | * | |
77b97be7 | 483 | * @param $valueTitles |
6a488035 | 484 | */ |
00be9182 | 485 | public static function mailingEventQueryBuilder(&$query, &$values, $tableName, $fieldName, $fieldTitle, &$valueTitles) { |
6a488035 TO |
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 | ||
111b2847 | 500 | if (is_array($value)) { |
501 | $query->_where[$grouping][] = "$tableName.$fieldName $op (" . implode(',', $value) . ")"; | |
86bfa4f6 | 502 | $query->_qill[$grouping][] = "$fieldTitle $op " . implode(', ', array_intersect_key($valueTitles, array_flip($value))); |
111b2847 | 503 | } |
504 | else { | |
505 | $query->_qill[$grouping][] = $fieldTitle . ' - ' . $valueTitles[$value]; | |
506 | } | |
507 | ||
dd879954 | 508 | $query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1; |
335038eb | 509 | $query->_tables['civicrm_mailing_job'] = $query->_whereTables['civicrm_mailing_job'] = 1; |
6a488035 | 510 | $query->_tables['civicrm_mailing_event_queue'] = $query->_whereTables['civicrm_mailing_event_queue'] = 1; |
335038eb | 511 | $query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1; |
6a488035 TO |
512 | $query->_tables[$tableName] = $query->_whereTables[$tableName] = 1; |
513 | } | |
96025800 | 514 | |
6a488035 | 515 | } |