Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sendgrid extension V3.2 for Civi 5.6.1 #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
320 changes: 163 additions & 157 deletions CRM/Contact/Form/Search.php

Large diffs are not rendered by default.

1,244 changes: 594 additions & 650 deletions CRM/Mailing/BAO/Mailing.php

Large diffs are not rendered by default.

67 changes: 54 additions & 13 deletions CRM/Mailing/BAO/Query.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.6 |
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2015 |
| Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
Expand All @@ -28,9 +28,7 @@
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2015
* $Id$
*
* @copyright CiviCRM LLC (c) 2004-2018
*/
class CRM_Mailing_BAO_Query {

Expand All @@ -44,7 +42,7 @@ public static function &getFields() {
self::$_mailingFields = array();
$_mailingFields['mailing_id'] = array(
'name' => 'mailing_id',
'title' => 'Mailing ID',
'title' => ts('Mailing ID'),
'where' => 'civicrm_mailing.id',
);
}
Expand All @@ -55,8 +53,6 @@ public static function &getFields() {
* If mailings are involved, add the specific Mailing fields
*
* @param $query
*
* @return void
*/
public static function select(&$query) {
// if Mailing mode add mailing id
Expand Down Expand Up @@ -123,6 +119,12 @@ public static function select(&$query) {
$query->_element['mailing_recipients_id'] = 1;
}
}

if (CRM_Utils_Array::value('mailing_campaign_id', $query->_returnProperties)) {
$query->_select['mailing_campaign_id'] = 'civicrm_mailing.campaign_id as mailing_campaign_id';
$query->_element['mailing_campaign_id'] = 1;
$query->_tables['civicrm_campaign'] = 1;
}
}

/**
Expand Down Expand Up @@ -188,6 +190,10 @@ public static function from($name, $mode, $side) {
case 'recipient_email':
$from = " $side JOIN civicrm_email recipient_email ON recipient_email.id = civicrm_mailing_recipients.email_id";
break;

case 'civicrm_campaign':
$from = " $side JOIN civicrm_campaign ON civicrm_campaign.id = civicrm_mailing.campaign_id";
break;
}

return $from;
Expand All @@ -208,6 +214,7 @@ public static function defaultReturnProperties(
if ($mode & CRM_Contact_BAO_Query::MODE_MAILING) {
$properties = array(
'mailing_id' => 1,
'mailing_campaign_id' => 1,
'mailing_name' => 1,
'sort_name' => 1,
'email' => 1,
Expand Down Expand Up @@ -258,6 +265,8 @@ public static function whereClauseSingle(&$values, &$query) {
$value = "%$value%";
$op = 'LIKE';
}

// LOWER in query below roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
$query->_where[$grouping][] = "LOWER(civicrm_mailing.name) $op '$value'";
$query->_qill[$grouping][] = "Mailing Namename $op \"$value\"";
$query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1;
Expand Down Expand Up @@ -376,15 +385,24 @@ public static function whereClauseSingle(&$values, &$query) {
$query->_qill[$grouping][] = "Mailing Job Status IS \"$value\"";
}
return;

case 'mailing_campaign_id':
$name = 'campaign_id';
$query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_mailing.$name", $op, $value, 'Integer');
list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Mailing_DAO_Mailing', $name, $value, $op);
$query->_qill[$grouping][] = ts('Campaign %1 %2', array(1 => $op, 2 => $value));
$query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1;
$query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1;
return;

case 'mailing_spam_status':
case 'mailing_spam_status':
$valueTitle = array('Y' => ts('Spam Reports'));
$values[2] = 'Y';
self::mailingEventQueryBuilder($query, $values,
'civicrm_mailing_event_spam_report', 'mailing_spam_status',
ts('Mailing: '), $valueTitle
);
return;
return;
}
}

Expand All @@ -393,7 +411,6 @@ public static function whereClauseSingle(&$values, &$query) {
*
*
* @param CRM_Core_Form $form
* @return void
*/
public static function buildSearchForm(&$form) {
// mailing selectors
Expand All @@ -406,6 +423,8 @@ public static function buildSearchForm(&$form) {
}

CRM_Core_Form_Date::buildDateRange($form, 'mailing_date', 1, '_low', '_high', ts('From'), FALSE);
$form->addElement('hidden', 'mailing_date_range_error');
$form->addFormRule(array('CRM_Mailing_BAO_Query', 'formRule'), $form);

$mailingJobStatuses = array(
'' => ts('- select -'),
Expand Down Expand Up @@ -433,6 +452,9 @@ public static function buildSearchForm(&$form) {
$form->add('checkbox', 'mailing_unsubscribe', ts('Unsubscribe Requests'));
$form->add('checkbox', 'mailing_optout', ts('Opt-out Requests'));
$form->add('checkbox', 'mailing_forward', ts('Forwards'));

// Campaign select field
CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'mailing_campaign_id');
$form->add('checkbox', 'mailing_spam_status', ts('Spam Reports'));

$form->assign('validCiviMailing', TRUE);
Expand Down Expand Up @@ -461,8 +483,6 @@ public static function tableNames(&$tables) {
* @param $fieldTitle
*
* @param $valueTitles
*
* @return void
*/
public static function mailingEventQueryBuilder(&$query, &$values, $tableName, $fieldName, $fieldTitle, &$valueTitles) {
list($name, $op, $value, $grouping, $wildcard) = $values;
Expand Down Expand Up @@ -494,4 +514,25 @@ public static function mailingEventQueryBuilder(&$query, &$values, $tableName, $
$query->_tables[$tableName] = $query->_whereTables[$tableName] = 1;
}

/**
* Check if the values in the date range are in correct chronological order.
*
* @param array $fields
* @param array $files
* @param CRM_Core_Form $form
*
* @return bool|array
*/
public static function formRule($fields, $files, $form) {
$errors = array();

if (empty($fields['mailing_date_high']) || empty($fields['mailing_date_low'])) {
return TRUE;
}

CRM_Utils_Rule::validDateRange($fields, 'mailing_date', $errors, ts('Mailing Date'));

return empty($errors) ? TRUE : $errors;
}

}
49 changes: 8 additions & 41 deletions CRM/Mailing/BAO/TrackableURL.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.6 |
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2015 |
| Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
Expand All @@ -28,9 +28,7 @@
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2015
* $Id$
*
* @copyright CiviCRM LLC (c) 2004-2018
*/
class CRM_Mailing_BAO_TrackableURL extends CRM_Mailing_DAO_TrackableURL {

Expand Down Expand Up @@ -59,8 +57,8 @@ public static function getTrackerURL($url, $mailing_id, $queue_id) {

static $urlCache = array();

if (array_key_exists($url, $urlCache)) {
return $urlCache[$url] . "&qid=$queue_id";
if (array_key_exists($mailing_id . $url, $urlCache)) {
return $urlCache[$mailing_id . $url] . "&qid=$queue_id";
}

// hack for basic CRM-1014 and CRM-1151 and CRM-3492 compliance:
Expand All @@ -85,17 +83,15 @@ public static function getTrackerURL($url, $mailing_id, $queue_id) {

$tracker->url = $url;
$tracker->mailing_id = $mailing_id;
if (strlen($tracker->url) > 254) {
return $url;
}

if (!$tracker->find(TRUE)) {
$tracker->save();
}
$id = $tracker->id;
$tracker->free();

$redirect = $config->userFrameworkResourceURL . "extern/url.php?u=$id";
$urlCache[$url] = $redirect;
$urlCache[$mailing_id . $url] = $redirect;
}

// com.imba.sendgrid
Expand All @@ -111,7 +107,7 @@ public static function getTrackerURL($url, $mailing_id, $queue_id) {
// com.imba.sendgrid

if ($hrefExists) {
$returnUrl = "href='{$returnUrl}'";
$returnUrl = "href='{$returnUrl}' rel='nofollow'";
}

return $returnUrl;
Expand All @@ -135,33 +131,4 @@ public static function getTrackerURLId($url, $mailing_id) {
return NULL;
}

/**
* @param $msg
* @param int $mailing_id
* @param int $queue_id
* @param bool $onlyHrefs
*/
public static function scan_and_replace(&$msg, $mailing_id, $queue_id, $onlyHrefs = FALSE) {
if (!$mailing_id) {
return;
}

$protos = '(https?|ftp)';
$letters = '\w';
$gunk = '/#~:.?+=&%@!\-';
$punc = '.:?\-';
$any = "{$letters}{$gunk}{$punc}";
if ($onlyHrefs) {
$pattern = "{\\b(href=([\"'])?($protos:[$any]+?(?=[$punc]*[^$any]|$))([\"'])?)}im";
}
else {
$pattern = "{\\b($protos:[$any]+?(?=[$punc]*[^$any]|$))}eim";
}

$trackURL = CRM_Mailing_BAO_TrackableURL::getTrackerURL('\\1', $mailing_id, $queue_id);
$replacement = $onlyHrefs ? ("href=\"{$trackURL}\"") : ("\"{$trackURL}\"");

$msg = preg_replace($pattern, $replacement, $msg);
}

}
25 changes: 16 additions & 9 deletions CRM/Mailing/Selector/Event.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.6 |
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2015 |
| Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
Expand All @@ -28,21 +28,18 @@
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2015
* $Id$
*
* @copyright CiviCRM LLC (c) 2004-2018
*/

/**
* This class is used to retrieve and display a range of
* contacts that match the given criteria (specifically for
* results of advanced search options.
*
*/
class CRM_Mailing_Selector_Event extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {

/**
* Array of supported links, currenly null
* Array of supported links, currently null
*
* @var array
*/
Expand Down Expand Up @@ -266,7 +263,7 @@ public function &getColumnHeaders($action = NULL, $output = NULL) {
/**
* Returns total number of rows for the query.
*
* @param
* @param string $action
*
* @return int
* Total number of rows
Expand Down Expand Up @@ -467,6 +464,11 @@ public function getExportFileName($output = 'csv') {
return NULL;
}

/**
* Get the title for the mailing event type.
*
* @return string
*/
public function eventToTitle() {
static $events = NULL;

Expand All @@ -480,13 +482,18 @@ public function eventToTitle() {
'unsubscribe' => ts('Unsubscribe Requests'),
'optout' => ts('Opt-out Requests'),
'click' => $this->_is_distinct ? ts('Unique Click-throughs') : ts('Click-throughs'),
'opened' => $this->_is_distinct ? ts('Unique Tracked Opens') : ts('Tracked Opens'),
'opened' => $this->_is_distinct ? ts('Unique Tracked Opens') : ts('Total Tracked Opens'),
'spam' => $this->_is_distinct ? ts('Unique Spam Reports') : ts('Spam Reports'),
);
}
return $events[$this->_event_type];
}

/**
* Get the title of the event.
*
* @return string
*/
public function getTitle() {
return $this->eventToTitle();
}
Expand Down
Loading