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