Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | */ |
33 | class CRM_Mailing_BAO_Query { | |
34 | ||
35 | static $_mailingFields = NULL; | |
36 | ||
e0ef6999 EM |
37 | /** |
38 | * @return array|null | |
39 | */ | |
00be9182 | 40 | public static function &getFields() { |
6a488035 TO |
41 | if (!self::$_mailingFields) { |
42 | self::$_mailingFields = array(); | |
43 | $_mailingFields['mailing_id'] = array( | |
44 | 'name' => 'mailing_id', | |
e300cf31 | 45 | 'title' => ts('Mailing ID'), |
6a488035 TO |
46 | 'where' => 'civicrm_mailing.id', |
47 | ); | |
48 | } | |
49 | return self::$_mailingFields; | |
50 | } | |
51 | ||
52 | /** | |
100fef9d | 53 | * If mailings are involved, add the specific Mailing fields |
6a488035 | 54 | * |
77b97be7 | 55 | * @param $query |
6a488035 | 56 | */ |
00be9182 | 57 | public static function select(&$query) { |
6a488035 TO |
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; | |
2cc569f2 | 62 | |
335038eb | 63 | // base table is contact, so join recipients to it |
6c552737 TO |
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 "; | |
2cc569f2 | 66 | |
335038eb | 67 | $query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1; |
2cc569f2 PJ |
68 | |
69 | // get mailing name | |
a7488080 | 70 | if (!empty($query->_returnProperties['mailing_name'])) { |
353ffa53 | 71 | $query->_select['mailing_name'] = "civicrm_mailing.name as mailing_name"; |
2cc569f2 PJ |
72 | $query->_element['mailing_name'] = 1; |
73 | } | |
74 | ||
75 | // get mailing subject | |
a7488080 | 76 | if (!empty($query->_returnProperties['mailing_subject'])) { |
353ffa53 | 77 | $query->_select['mailing_subject'] = "civicrm_mailing.subject as mailing_subject"; |
2cc569f2 PJ |
78 | $query->_element['mailing_subject'] = 1; |
79 | } | |
80 | ||
81 | // get mailing status | |
a7488080 | 82 | if (!empty($query->_returnProperties['mailing_job_status'])) { |
6c552737 TO |
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 "; | |
353ffa53 | 85 | $query->_select['mailing_job_status'] = "civicrm_mailing_job.status as mailing_job_status"; |
2cc569f2 PJ |
86 | $query->_element['mailing_job_status'] = 1; |
87 | } | |
88 | ||
89 | // get email on hold | |
a7488080 | 90 | if (!empty($query->_returnProperties['email_on_hold'])) { |
2cc569f2 PJ |
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; | |
94 | } | |
95 | ||
96 | // get recipient email | |
a7488080 | 97 | if (!empty($query->_returnProperties['email'])) { |
2cc569f2 PJ |
98 | $query->_select['email'] = "recipient_email.email as email"; |
99 | $query->_element['email'] = 1; | |
100 | $query->_tables['recipient_email'] = $query->_whereTables['recipient_email'] = 1; | |
101 | } | |
102 | ||
103 | // get user opt out | |
a7488080 | 104 | if (!empty($query->_returnProperties['contact_opt_out'])) { |
2cc569f2 PJ |
105 | $query->_select['contact_opt_out'] = "contact_a.is_opt_out as contact_opt_out"; |
106 | $query->_element['contact_opt_out'] = 1; | |
107 | } | |
108 | ||
109 | // mailing job end date / completed date | |
a7488080 | 110 | if (!empty($query->_returnProperties['mailing_job_end_date'])) { |
6c552737 TO |
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 "; | |
353ffa53 | 113 | $query->_select['mailing_job_end_date'] = "civicrm_mailing_job.end_date as mailing_job_end_date"; |
2cc569f2 PJ |
114 | $query->_element['mailing_job_end_date'] = 1; |
115 | } | |
47546fcf | 116 | |
a7488080 | 117 | if (!empty($query->_returnProperties['mailing_recipients_id'])) { |
335038eb PJ |
118 | $query->_select['mailing_recipients_id'] = " civicrm_mailing_recipients.id as mailing_recipients_id"; |
119 | $query->_element['mailing_recipients_id'] = 1; | |
47546fcf | 120 | } |
6a488035 | 121 | } |
04cac635 | 122 | |
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; | |
127 | } | |
6a488035 TO |
128 | } |
129 | ||
e0ef6999 EM |
130 | /** |
131 | * @param $query | |
132 | */ | |
00be9182 | 133 | public static function where(&$query) { |
6a488035 TO |
134 | $grouping = NULL; |
135 | foreach (array_keys($query->_params) as $id) { | |
a7488080 | 136 | if (empty($query->_params[$id][0])) { |
6a488035 TO |
137 | continue; |
138 | } | |
139 | if (substr($query->_params[$id][0], 0, 8) == 'mailing_') { | |
140 | if ($query->_mode == CRM_Contact_BAO_QUERY::MODE_CONTACTS) { | |
141 | $query->_useDistinct = TRUE; | |
142 | } | |
143 | $grouping = $query->_params[$id][3]; | |
144 | self::whereClauseSingle($query->_params[$id], $query); | |
145 | } | |
146 | } | |
147 | } | |
148 | ||
e0ef6999 | 149 | /** |
100fef9d | 150 | * @param string $name |
e0ef6999 EM |
151 | * @param $mode |
152 | * @param $side | |
153 | * | |
154 | * @return null|string | |
155 | */ | |
00be9182 | 156 | public static function from($name, $mode, $side) { |
6a488035 | 157 | $from = NULL; |
335038eb | 158 | |
6a488035 | 159 | switch ($name) { |
335038eb PJ |
160 | case 'civicrm_mailing_recipients': |
161 | $from = " $side JOIN civicrm_mailing_recipients ON civicrm_mailing_recipients.contact_id = contact_a.id"; | |
6a488035 TO |
162 | break; |
163 | ||
335038eb PJ |
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'"; | |
6a488035 TO |
169 | break; |
170 | ||
171 | case 'civicrm_mailing': | |
335038eb PJ |
172 | $from = " $side JOIN civicrm_mailing ON civicrm_mailing.id = civicrm_mailing_recipients.mailing_id "; |
173 | break; | |
174 | ||
175 | case 'civicrm_mailing_job': | |
eb6a4756 | 176 | $from = " $side JOIN civicrm_mailing_job ON civicrm_mailing_job.mailing_id = civicrm_mailing.id AND civicrm_mailing_job.is_test != 1 "; |
6a488035 TO |
177 | break; |
178 | ||
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"; | |
187 | break; | |
2cc569f2 PJ |
188 | |
189 | case 'recipient_email': | |
335038eb PJ |
190 | $from = " $side JOIN civicrm_email recipient_email ON recipient_email.id = civicrm_mailing_recipients.email_id"; |
191 | break; | |
7851dc81 JP |
192 | |
193 | case 'civicrm_campaign': | |
194 | $from = " $side JOIN civicrm_campaign ON civicrm_campaign.id = civicrm_mailing.campaign_id"; | |
195 | break; | |
6a488035 TO |
196 | } |
197 | ||
198 | return $from; | |
199 | } | |
200 | ||
e0ef6999 EM |
201 | /** |
202 | * @param $mode | |
203 | * @param bool $includeCustomFields | |
204 | * | |
205 | * @return array|null | |
206 | */ | |
2da40d21 | 207 | public static function defaultReturnProperties( |
a3d7e8ee | 208 | $mode, |
6a488035 TO |
209 | $includeCustomFields = TRUE |
210 | ) { | |
211 | ||
212 | $properties = NULL; | |
213 | if ($mode & CRM_Contact_BAO_Query::MODE_MAILING) { | |
2cc569f2 PJ |
214 | $properties = array( |
215 | 'mailing_id' => 1, | |
04cac635 | 216 | 'mailing_campaign_id' => 1, |
2cc569f2 PJ |
217 | 'mailing_name' => 1, |
218 | 'sort_name' => 1, | |
219 | 'email' => 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, | |
225 | 'contact_type' => 1, | |
47546fcf | 226 | 'contact_sub_type' => 1, |
21dfd5f5 | 227 | 'mailing_recipients_id' => 1, |
2cc569f2 | 228 | ); |
6a488035 TO |
229 | } |
230 | return $properties; | |
231 | } | |
232 | ||
e0ef6999 EM |
233 | /** |
234 | * @param $values | |
235 | * @param $query | |
236 | */ | |
00be9182 | 237 | public static function whereClauseSingle(&$values, &$query) { |
6a488035 TO |
238 | list($name, $op, $value, $grouping, $wildcard) = $values; |
239 | ||
6a488035 TO |
240 | switch ($name) { |
241 | case 'mailing_id': | |
242 | $selectedMailings = array_flip($value); | |
6a488035 TO |
243 | $value = "(" . implode(',', $value) . ")"; |
244 | $op = 'IN'; | |
245 | $query->_where[$grouping][] = "civicrm_mailing.id $op $value"; | |
246 | ||
247 | $mailings = CRM_Mailing_BAO_Mailing::getMailingsList(); | |
248 | foreach ($selectedMailings as $id => $dnc) { | |
249 | $selectedMailings[$id] = $mailings[$id]; | |
250 | } | |
251 | $selectedMailings = implode(' or ', $selectedMailings); | |
252 | ||
253 | $query->_qill[$grouping][] = "Mailing Name $op \"$selectedMailings\""; | |
6a488035 | 254 | $query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1; |
335038eb | 255 | $query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1; |
6a488035 TO |
256 | return; |
257 | ||
258 | case 'mailing_name': | |
b0f72207 | 259 | $value = addslashes($value); |
481a74f4 | 260 | if ($wildcard) { |
6a488035 | 261 | $value = "%$value%"; |
353ffa53 | 262 | $op = 'LIKE'; |
6a488035 | 263 | } |
b27d1855 | 264 | |
b0f72207 | 265 | $query->_where[$grouping][] = "civicrm_mailing.name $op '$value'"; |
353ffa53 | 266 | $query->_qill[$grouping][] = "Mailing Namename $op \"$value\""; |
6a488035 | 267 | $query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1; |
335038eb | 268 | $query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1; |
6a488035 TO |
269 | return; |
270 | ||
271 | case 'mailing_date': | |
272 | case 'mailing_date_low': | |
273 | case 'mailing_date_high': | |
274 | // process to / from date | |
335038eb | 275 | $query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1; |
6a488035 TO |
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; | |
335038eb | 278 | $query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1; |
6a488035 TO |
279 | $query->dateQueryBuilder($values, |
280 | 'civicrm_mailing_job', 'mailing_date', 'start_date', 'Mailing Delivery Date' | |
281 | ); | |
282 | return; | |
283 | ||
284 | case 'mailing_delivery_status': | |
285 | $options = CRM_Mailing_PseudoConstant::yesNoOptions('delivered'); | |
286 | ||
287 | list($name, $op, $value, $grouping, $wildcard) = $values; | |
288 | if ($value == 'Y') { | |
289 | self::mailingEventQueryBuilder($query, $values, | |
290 | 'civicrm_mailing_event_delivered', | |
291 | 'mailing_delivery_status', | |
292 | ts('Mailing Delivery'), | |
293 | $options | |
294 | ); | |
295 | } | |
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'), | |
303 | $options | |
304 | ); | |
305 | } | |
306 | return; | |
307 | ||
111b2847 | 308 | case 'mailing_bounce_types': |
309 | $op = 'IN'; | |
310 | $values = array($name, $op, $value, $grouping, $wildcard); | |
311 | self::mailingEventQueryBuilder($query, $values, | |
312 | 'civicrm_mailing_event_bounce', | |
313 | 'bounce_type_id', | |
314 | ts('Bounce type(s)'), | |
353ffa53 TO |
315 | CRM_Core_PseudoConstant::get('CRM_Mailing_Event_DAO_Bounce', 'bounce_type_id', array( |
316 | 'keyColumn' => 'id', | |
af9b09df | 317 | 'labelColumn' => 'name', |
353ffa53 | 318 | )) |
111b2847 | 319 | ); |
320 | return; | |
321 | ||
6a488035 TO |
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') | |
325 | ); | |
326 | return; | |
327 | ||
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') | |
331 | ); | |
332 | return; | |
333 | ||
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') | |
337 | ); | |
338 | return; | |
339 | ||
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 | |
347 | ); | |
348 | return; | |
349 | ||
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 | |
357 | ); | |
358 | return; | |
359 | ||
360 | case 'mailing_forward': | |
361 | $valueTitle = array('Y' => ts('Forwards')); | |
362 | // since its a checkbox | |
363 | $values[2] = 'Y'; | |
364 | self::mailingEventQueryBuilder($query, $values, | |
365 | 'civicrm_mailing_event_forward', 'mailing_forward', | |
366 | ts('Mailing: '), $valueTitle | |
367 | ); | |
368 | return; | |
335038eb PJ |
369 | |
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; | |
374 | } | |
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; | |
378 | ||
379 | $query->_where[$grouping][] = " civicrm_mailing_job.status = '{$value}' "; | |
353ffa53 | 380 | $query->_qill[$grouping][] = "Mailing Job Status IS \"$value\""; |
335038eb PJ |
381 | } |
382 | return; | |
04cac635 | 383 | |
384 | case 'mailing_campaign_id': | |
6f56dc34 | 385 | $name = 'campaign_id'; |
c432c016 | 386 | $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_mailing.$name", $op, $value, 'Integer'); |
6f56dc34 | 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; | |
04cac635 | 390 | $query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1; |
391 | return; | |
6a488035 TO |
392 | } |
393 | } | |
394 | ||
395 | /** | |
fe482240 | 396 | * Add all the elements shared between Mailing search and advnaced search. |
6a488035 | 397 | * |
6a488035 | 398 | * |
100fef9d | 399 | * @param CRM_Core_Form $form |
6a488035 | 400 | */ |
00be9182 | 401 | public static function buildSearchForm(&$form) { |
6a488035 TO |
402 | // mailing selectors |
403 | $mailings = CRM_Mailing_BAO_Mailing::getMailingsList(); | |
404 | ||
405 | if (!empty($mailings)) { | |
406 | $form->add('select', 'mailing_id', ts('Mailing Name(s)'), $mailings, FALSE, | |
ab345ca5 | 407 | array('id' => 'mailing_id', 'multiple' => 'multiple', 'class' => 'crm-select2') |
6a488035 TO |
408 | ); |
409 | } | |
410 | ||
bc3f7f04 | 411 | CRM_Core_Form_Date::buildDateRange($form, 'mailing_date', 1, '_low', '_high', ts('From'), FALSE); |
e512e4b5 SB |
412 | $form->addElement('hidden', 'mailing_date_range_error'); |
413 | $form->addFormRule(array('CRM_Mailing_BAO_Query', 'formRule'), $form); | |
6a488035 | 414 | |
335038eb PJ |
415 | $mailingJobStatuses = array( |
416 | '' => ts('- select -'), | |
417 | 'Complete' => 'Complete', | |
418 | 'Scheduled' => 'Scheduled', | |
419 | 'Running' => 'Running', | |
21dfd5f5 | 420 | 'Canceled' => 'Canceled', |
335038eb PJ |
421 | ); |
422 | $form->addElement('select', 'mailing_job_status', ts('Mailing Job Status'), $mailingJobStatuses, FALSE); | |
423 | ||
111b2847 | 424 | $mailingBounceTypes = CRM_Core_PseudoConstant::get( |
425 | 'CRM_Mailing_Event_DAO_Bounce', 'bounce_type_id', | |
426 | array('keyColumn' => 'id', 'labelColumn' => 'name') | |
427 | ); | |
c9fd7217 | 428 | $form->add('select', 'mailing_bounce_types', ts('Bounce Types'), $mailingBounceTypes, FALSE, |
ab345ca5 | 429 | array('id' => 'mailing_bounce_types', 'multiple' => 'multiple', 'class' => 'crm-select2') |
c9fd7217 | 430 | ); |
111b2847 | 431 | |
6a488035 | 432 | // event filters |
b847e6e7 CW |
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)); | |
6a488035 TO |
437 | |
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')); | |
04cac635 | 441 | // Campaign select field |
442 | CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'mailing_campaign_id'); | |
6a488035 TO |
443 | |
444 | $form->assign('validCiviMailing', TRUE); | |
6a488035 TO |
445 | } |
446 | ||
e0ef6999 EM |
447 | /** |
448 | * @param $row | |
100fef9d | 449 | * @param int $id |
e0ef6999 | 450 | */ |
35f7561f TO |
451 | public static function searchAction(&$row, $id) { |
452 | } | |
6a488035 | 453 | |
e0ef6999 EM |
454 | /** |
455 | * @param $tables | |
456 | */ | |
00be9182 | 457 | public static function tableNames(&$tables) { |
337750c9 | 458 | } |
6a488035 TO |
459 | |
460 | /** | |
461 | * Filter query results based on which contacts do (not) have a particular mailing event in their history. | |
462 | * | |
463 | * @param $query | |
464 | * @param $values | |
100fef9d CW |
465 | * @param string $tableName |
466 | * @param string $fieldName | |
6a488035 TO |
467 | * @param $fieldTitle |
468 | * | |
77b97be7 | 469 | * @param $valueTitles |
6a488035 | 470 | */ |
00be9182 | 471 | public static function mailingEventQueryBuilder(&$query, &$values, $tableName, $fieldName, $fieldTitle, &$valueTitles) { |
6a488035 TO |
472 | list($name, $op, $value, $grouping, $wildcard) = $values; |
473 | ||
474 | if (empty($value) || $value == 'A') { | |
475 | // don't do any filtering | |
476 | return; | |
477 | } | |
478 | ||
479 | if ($value == 'Y') { | |
480 | $query->_where[$grouping][] = $tableName . ".id is not null "; | |
481 | } | |
482 | elseif ($value == 'N') { | |
483 | $query->_where[$grouping][] = $tableName . ".id is null "; | |
484 | } | |
485 | ||
111b2847 | 486 | if (is_array($value)) { |
487 | $query->_where[$grouping][] = "$tableName.$fieldName $op (" . implode(',', $value) . ")"; | |
86bfa4f6 | 488 | $query->_qill[$grouping][] = "$fieldTitle $op " . implode(', ', array_intersect_key($valueTitles, array_flip($value))); |
111b2847 | 489 | } |
490 | else { | |
491 | $query->_qill[$grouping][] = $fieldTitle . ' - ' . $valueTitles[$value]; | |
492 | } | |
493 | ||
dd879954 | 494 | $query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1; |
335038eb | 495 | $query->_tables['civicrm_mailing_job'] = $query->_whereTables['civicrm_mailing_job'] = 1; |
6a488035 | 496 | $query->_tables['civicrm_mailing_event_queue'] = $query->_whereTables['civicrm_mailing_event_queue'] = 1; |
335038eb | 497 | $query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1; |
6a488035 TO |
498 | $query->_tables[$tableName] = $query->_whereTables[$tableName] = 1; |
499 | } | |
96025800 | 500 | |
e512e4b5 SB |
501 | /** |
502 | * Check if the values in the date range are in correct chronological order. | |
503 | * | |
504 | * @param array $fields | |
505 | * @param array $files | |
506 | * @param CRM_Core_Form $form | |
507 | * | |
508 | * @return bool|array | |
509 | */ | |
510 | public static function formRule($fields, $files, $form) { | |
511 | $errors = array(); | |
512 | ||
513 | if (empty($fields['mailing_date_high']) || empty($fields['mailing_date_low'])) { | |
514 | return TRUE; | |
515 | } | |
e512e4b5 | 516 | |
79326ee2 SB |
517 | CRM_Utils_Rule::validDateRange($fields, 'mailing_date', $errors, ts('Mailing Date')); |
518 | ||
e512e4b5 SB |
519 | return empty($errors) ? TRUE : $errors; |
520 | } | |
521 | ||
6a488035 | 522 | } |