First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,104 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
class CRM_Mailing_BAO_BouncePattern extends CRM_Mailing_DAO_BouncePattern {
/**
* Pseudo-constant pattern array.
*/
static $_patterns = NULL;
/**
* Class constructor.
*/
public function __construct() {
parent::__construct();
}
/**
* Build the static pattern array.
*/
public static function buildPatterns() {
self::$_patterns = array();
$bp = new CRM_Mailing_BAO_BouncePattern();
$bp->find();
while ($bp->fetch()) {
self::$_patterns[$bp->bounce_type_id][] = $bp->pattern;
}
foreach (self::$_patterns as $type => $patterns) {
if (count($patterns) == 1) {
self::$_patterns[$type] = '{(' . $patterns[0] . ')}im';
}
else {
self::$_patterns[$type] = '{(' . implode(')|(', $patterns) . ')}im';
}
}
}
/**
* Try to match the string to a bounce type.
*
* @param string $message
* The message to be matched.
*
* @return array
* Tuple (bounce_type, bounce_reason)
*/
public static function &match(&$message) {
// clean up $message and replace all white space by a single space, CRM-4767
$message = preg_replace('/\s+/', ' ', $message);
if (self::$_patterns == NULL) {
self::buildPatterns();
}
foreach (self::$_patterns as $type => $re) {
if (preg_match($re, $message, $matches)) {
$bounce = array(
'bounce_type_id' => $type,
'bounce_reason' => $message,
);
return $bounce;
}
}
$bounce = array(
'bounce_type_id' => NULL,
'bounce_reason' => $message,
);
return $bounce;
}
}

View file

@ -0,0 +1,121 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
class CRM_Mailing_BAO_Component extends CRM_Mailing_DAO_Component {
/**
* Class constructor.
*/
public function __construct() {
parent::__construct();
}
/**
* Fetch object based on array of properties.
*
* @param array $params
* (reference ) an assoc array of name/value pairs.
* @param array $defaults
* (reference ) an assoc array to hold the flattened values.
*
* @return CRM_Core_BAO_LocationType.
*/
public static function retrieve(&$params, &$defaults) {
$component = new CRM_Mailing_DAO_Component();
$component->copyValues($params);
if ($component->find(TRUE)) {
CRM_Core_DAO::storeValues($component, $defaults);
return $component;
}
return NULL;
}
/**
* Update the is_active flag in the db.
*
* @param int $id
* Id of the database record.
* @param bool $is_active
* Value we want to set the is_active field.
*
* @return Object
* DAO object on success, null otherwise
*/
public static function setIsActive($id, $is_active) {
return CRM_Core_DAO::setFieldValue('CRM_Mailing_DAO_Component', $id, 'is_active', $is_active);
}
/**
* Create and Update mailing component.
*
* @param array $params
* (reference ) an assoc array of name/value pairs.
* @param array $ids
* (deprecated) the array that holds all the db ids.
*
* @return CRM_Mailing_BAO_Component
*/
public static function add(&$params, $ids = array()) {
$id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('id', $ids));
$component = new CRM_Mailing_DAO_Component();
if ($id) {
$component->id = $id;
$component->find(TRUE);
}
$component->copyValues($params);
if (empty($id) && empty($params['body_text'])) {
$component->body_text = CRM_Utils_String::htmlToText(CRM_Utils_Array::value('body_html', $params));
}
if ($component->is_default) {
if (!empty($id)) {
$sql = 'UPDATE civicrm_mailing_component SET is_default = 0 WHERE component_type = %1 AND id <> %2';
$sqlParams = array(
1 => array($component->component_type, 'String'),
2 => array($id, 'Positive'),
);
}
else {
$sql = 'UPDATE civicrm_mailing_component SET is_default = 0 WHERE component_type = %1';
$sqlParams = array(
1 => array($component->component_type, 'String'),
);
}
CRM_Core_DAO::executeQuery($sql, $sqlParams);
}
$component->save();
return $component;
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,183 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
/**
* Class CRM_Mailing_BAO_MailingAB
*/
class CRM_Mailing_BAO_MailingAB extends CRM_Mailing_DAO_MailingAB {
/**
* class constructor.
*/
public function __construct() {
parent::__construct();
}
/**
* Construct a new mailingab object.
*
* @params array $params
* Form values.
*
* @param array $params
* @param array $ids
*
* @return object
* $mailingab The new mailingab object
*/
public static function create(&$params, $ids = array()) {
$transaction = new CRM_Core_Transaction();
$mailingab = self::add($params, $ids);
if (is_a($mailingab, 'CRM_Core_Error')) {
$transaction->rollback();
return $mailingab;
}
$transaction->commit();
return $mailingab;
}
/**
* function to add the mailings.
*
* @param array $params
* Reference array contains the values submitted by the form.
* @param array $ids
* Reference array contains the id.
*
*
* @return object
*/
public static function add(&$params, $ids = array()) {
$id = CRM_Utils_Array::value('mailingab_id', $ids, CRM_Utils_Array::value('id', $params));
if ($id) {
CRM_Utils_Hook::pre('edit', 'MailingAB', $id, $params);
}
else {
CRM_Utils_Hook::pre('create', 'MailingAB', NULL, $params);
}
$mailingab = new CRM_Mailing_DAO_MailingAB();
$mailingab->id = $id;
if (!$id) {
$mailingab->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
}
$mailingab->copyValues($params);
$result = $mailingab->save();
if ($id) {
CRM_Utils_Hook::post('edit', 'MailingAB', $mailingab->id, $mailingab);
}
else {
CRM_Utils_Hook::post('create', 'MailingAB', $mailingab->id, $mailingab);
}
return $result;
}
/**
* Delete MailingAB and all its associated records.
*
* @param int $id
* Id of the mail to delete.
*/
public static function del($id) {
if (empty($id)) {
CRM_Core_Error::fatal();
}
CRM_Core_Transaction::create()->run(function () use ($id) {
CRM_Utils_Hook::pre('delete', 'MailingAB', $id, CRM_Core_DAO::$_nullArray);
$dao = new CRM_Mailing_DAO_MailingAB();
$dao->id = $id;
if ($dao->find(TRUE)) {
$mailing_ids = array($dao->mailing_id_a, $dao->mailing_id_b, $dao->mailing_id_c);
$dao->delete();
foreach ($mailing_ids as $mailing_id) {
if ($mailing_id) {
CRM_Mailing_BAO_Mailing::del($mailing_id);
}
}
}
CRM_Core_Session::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success');
CRM_Utils_Hook::post('delete', 'MailingAB', $id, $dao);
});
}
/**
* Transfer recipients from the canonical mailing A to the other mailings.
*
* @param CRM_Mailing_DAO_MailingAB $dao
*/
public static function distributeRecipients(CRM_Mailing_DAO_MailingAB $dao) {
CRM_Mailing_BAO_Mailing::getRecipients($dao->mailing_id_a, $dao->mailing_id_a, TRUE);
//calculate total number of random recipients for mail C from group_percentage selected
$totalCount = CRM_Mailing_BAO_Recipients::mailingSize($dao->mailing_id_a);
$totalSelected = max(1, round(($totalCount * $dao->group_percentage) / 100));
CRM_Mailing_BAO_Recipients::reassign($dao->mailing_id_a, array(
$dao->mailing_id_b => (2 * $totalSelected <= $totalCount) ? $totalSelected : $totalCount - $totalSelected,
$dao->mailing_id_c => max(0, $totalCount - $totalSelected - $totalSelected),
));
}
/**
* get abtest based on Mailing ID
*
* @param int $mailingID
* Mailing ID.
*
* @return object
*/
public static function getABTest($mailingID) {
$query = "SELECT * FROM `civicrm_mailing_abtest` ab
where (ab.mailing_id_a = %1
OR ab.mailing_id_b = %1
OR ab.mailing_id_c = %1)
GROUP BY ab.id";
$params = array(1 => array($mailingID, 'Integer'));
$abTest = CRM_Core_DAO::executeQuery($query, $params);
$abTest->fetch();
return $abTest;
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,526 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
class CRM_Mailing_BAO_Query {
static $_mailingFields = NULL;
/**
* @return array|null
*/
public static function &getFields() {
if (!self::$_mailingFields) {
self::$_mailingFields = array();
$_mailingFields['mailing_id'] = array(
'name' => 'mailing_id',
'title' => ts('Mailing ID'),
'where' => 'civicrm_mailing.id',
);
}
return self::$_mailingFields;
}
/**
* If mailings are involved, add the specific Mailing fields
*
* @param $query
*/
public static function select(&$query) {
// if Mailing mode add mailing id
if ($query->_mode & CRM_Contact_BAO_Query::MODE_MAILING) {
$query->_select['mailing_id'] = "civicrm_mailing.id as mailing_id";
$query->_element['mailing_id'] = 1;
// base table is contact, so join recipients to it
$query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients']
= " INNER JOIN civicrm_mailing_recipients ON civicrm_mailing_recipients.contact_id = contact_a.id ";
$query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1;
// get mailing name
if (!empty($query->_returnProperties['mailing_name'])) {
$query->_select['mailing_name'] = "civicrm_mailing.name as mailing_name";
$query->_element['mailing_name'] = 1;
}
// get mailing subject
if (!empty($query->_returnProperties['mailing_subject'])) {
$query->_select['mailing_subject'] = "civicrm_mailing.subject as mailing_subject";
$query->_element['mailing_subject'] = 1;
}
// get mailing status
if (!empty($query->_returnProperties['mailing_job_status'])) {
$query->_tables['civicrm_mailing_job'] = $query->_whereTables['civicrm_mailing_job']
= " 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 ";
$query->_select['mailing_job_status'] = "civicrm_mailing_job.status as mailing_job_status";
$query->_element['mailing_job_status'] = 1;
}
// get email on hold
if (!empty($query->_returnProperties['email_on_hold'])) {
$query->_select['email_on_hold'] = "recipient_email.on_hold as email_on_hold";
$query->_element['email_on_hold'] = 1;
$query->_tables['recipient_email'] = $query->_whereTables['recipient_email'] = 1;
}
// get recipient email
if (!empty($query->_returnProperties['email'])) {
$query->_select['email'] = "recipient_email.email as email";
$query->_element['email'] = 1;
$query->_tables['recipient_email'] = $query->_whereTables['recipient_email'] = 1;
}
// get user opt out
if (!empty($query->_returnProperties['contact_opt_out'])) {
$query->_select['contact_opt_out'] = "contact_a.is_opt_out as contact_opt_out";
$query->_element['contact_opt_out'] = 1;
}
// mailing job end date / completed date
if (!empty($query->_returnProperties['mailing_job_end_date'])) {
$query->_tables['civicrm_mailing_job'] = $query->_whereTables['civicrm_mailing_job']
= " 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 ";
$query->_select['mailing_job_end_date'] = "civicrm_mailing_job.end_date as mailing_job_end_date";
$query->_element['mailing_job_end_date'] = 1;
}
if (!empty($query->_returnProperties['mailing_recipients_id'])) {
$query->_select['mailing_recipients_id'] = " civicrm_mailing_recipients.id as mailing_recipients_id";
$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;
}
}
/**
* @param $query
*/
public static function where(&$query) {
$grouping = NULL;
foreach (array_keys($query->_params) as $id) {
if (empty($query->_params[$id][0])) {
continue;
}
if (substr($query->_params[$id][0], 0, 8) == 'mailing_') {
if ($query->_mode == CRM_Contact_BAO_QUERY::MODE_CONTACTS) {
$query->_useDistinct = TRUE;
}
$grouping = $query->_params[$id][3];
self::whereClauseSingle($query->_params[$id], $query);
}
}
}
/**
* @param string $name
* @param $mode
* @param $side
*
* @return null|string
*/
public static function from($name, $mode, $side) {
$from = NULL;
switch ($name) {
case 'civicrm_mailing_recipients':
$from = " $side JOIN civicrm_mailing_recipients ON civicrm_mailing_recipients.contact_id = contact_a.id";
break;
case 'civicrm_mailing_event_queue':
// this is tightly binded so as to do a check WRT actual job recipients ('child' type jobs)
$from = " INNER JOIN civicrm_mailing_event_queue ON
civicrm_mailing_event_queue.contact_id = civicrm_mailing_recipients.contact_id
AND civicrm_mailing_event_queue.job_id = civicrm_mailing_job.id AND civicrm_mailing_job.job_type = 'child'";
break;
case 'civicrm_mailing':
$from = " $side JOIN civicrm_mailing ON civicrm_mailing.id = civicrm_mailing_recipients.mailing_id ";
break;
case 'civicrm_mailing_job':
$from = " $side JOIN civicrm_mailing_job ON civicrm_mailing_job.mailing_id = civicrm_mailing.id AND civicrm_mailing_job.is_test != 1 ";
break;
case 'civicrm_mailing_event_bounce':
case 'civicrm_mailing_event_delivered':
case 'civicrm_mailing_event_opened':
case 'civicrm_mailing_event_reply':
case 'civicrm_mailing_event_unsubscribe':
case 'civicrm_mailing_event_forward':
case 'civicrm_mailing_event_trackable_url_open':
$from = " $side JOIN $name ON $name.event_queue_id = civicrm_mailing_event_queue.id";
break;
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;
}
/**
* @param $mode
* @param bool $includeCustomFields
*
* @return array|null
*/
public static function defaultReturnProperties(
$mode,
$includeCustomFields = TRUE
) {
$properties = NULL;
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,
'mailing_subject' => 1,
'email_on_hold' => 1,
'contact_opt_out' => 1,
'mailing_job_status' => 1,
'mailing_job_end_date' => 1,
'contact_type' => 1,
'contact_sub_type' => 1,
'mailing_recipients_id' => 1,
);
}
return $properties;
}
/**
* @param $values
* @param $query
*/
public static function whereClauseSingle(&$values, &$query) {
list($name, $op, $value, $grouping, $wildcard) = $values;
$fields = array();
$fields = self::getFields();
switch ($name) {
case 'mailing_id':
$selectedMailings = array_flip($value);
$value = "(" . implode(',', $value) . ")";
$op = 'IN';
$query->_where[$grouping][] = "civicrm_mailing.id $op $value";
$mailings = CRM_Mailing_BAO_Mailing::getMailingsList();
foreach ($selectedMailings as $id => $dnc) {
$selectedMailings[$id] = $mailings[$id];
}
$selectedMailings = implode(' or ', $selectedMailings);
$query->_qill[$grouping][] = "Mailing Name $op \"$selectedMailings\"";
$query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1;
$query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1;
return;
case 'mailing_name':
$value = strtolower(addslashes($value));
if ($wildcard) {
$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;
$query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1;
return;
case 'mailing_date':
case 'mailing_date_low':
case 'mailing_date_high':
// process to / from date
$query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1;
$query->_tables['civicrm_mailing_event_queue'] = $query->_whereTables['civicrm_mailing_event_queue'] = 1;
$query->_tables['civicrm_mailing_job'] = $query->_whereTables['civicrm_mailing_job'] = 1;
$query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1;
$query->dateQueryBuilder($values,
'civicrm_mailing_job', 'mailing_date', 'start_date', 'Mailing Delivery Date'
);
return;
case 'mailing_delivery_status':
$options = CRM_Mailing_PseudoConstant::yesNoOptions('delivered');
list($name, $op, $value, $grouping, $wildcard) = $values;
if ($value == 'Y') {
self::mailingEventQueryBuilder($query, $values,
'civicrm_mailing_event_delivered',
'mailing_delivery_status',
ts('Mailing Delivery'),
$options
);
}
elseif ($value == 'N') {
$options['Y'] = $options['N'];
$values = array($name, $op, 'Y', $grouping, $wildcard);
self::mailingEventQueryBuilder($query, $values,
'civicrm_mailing_event_bounce',
'mailing_delivery_status',
ts('Mailing Delivery'),
$options
);
}
return;
case 'mailing_bounce_types':
$op = 'IN';
$values = array($name, $op, $value, $grouping, $wildcard);
self::mailingEventQueryBuilder($query, $values,
'civicrm_mailing_event_bounce',
'bounce_type_id',
ts('Bounce type(s)'),
CRM_Core_PseudoConstant::get('CRM_Mailing_Event_DAO_Bounce', 'bounce_type_id', array(
'keyColumn' => 'id',
'labelColumn' => 'name',
))
);
return;
case 'mailing_open_status':
self::mailingEventQueryBuilder($query, $values,
'civicrm_mailing_event_opened', 'mailing_open_status', ts('Mailing: Trackable Opens'), CRM_Mailing_PseudoConstant::yesNoOptions('open')
);
return;
case 'mailing_click_status':
self::mailingEventQueryBuilder($query, $values,
'civicrm_mailing_event_trackable_url_open', 'mailing_click_status', ts('Mailing: Trackable URL Clicks'), CRM_Mailing_PseudoConstant::yesNoOptions('click')
);
return;
case 'mailing_reply_status':
self::mailingEventQueryBuilder($query, $values,
'civicrm_mailing_event_reply', 'mailing_reply_status', ts('Mailing: Trackable Replies'), CRM_Mailing_PseudoConstant::yesNoOptions('reply')
);
return;
case 'mailing_optout':
$valueTitle = array(1 => ts('Opt-out Requests'));
// include opt-out events only
$query->_where[$grouping][] = "civicrm_mailing_event_unsubscribe.org_unsubscribe = 1";
self::mailingEventQueryBuilder($query, $values,
'civicrm_mailing_event_unsubscribe', 'mailing_unsubscribe',
ts('Mailing: '), $valueTitle
);
return;
case 'mailing_unsubscribe':
$valueTitle = array(1 => ts('Unsubscribe Requests'));
// exclude opt-out events
$query->_where[$grouping][] = "civicrm_mailing_event_unsubscribe.org_unsubscribe = 0";
self::mailingEventQueryBuilder($query, $values,
'civicrm_mailing_event_unsubscribe', 'mailing_unsubscribe',
ts('Mailing: '), $valueTitle
);
return;
case 'mailing_forward':
$valueTitle = array('Y' => ts('Forwards'));
// since its a checkbox
$values[2] = 'Y';
self::mailingEventQueryBuilder($query, $values,
'civicrm_mailing_event_forward', 'mailing_forward',
ts('Mailing: '), $valueTitle
);
return;
case 'mailing_job_status':
if (!empty($value)) {
if ($value != 'Scheduled' && $value != 'Canceled') {
$query->_tables['civicrm_mailing_event_queue'] = $query->_whereTables['civicrm_mailing_event_queue'] = 1;
}
$query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1;
$query->_tables['civicrm_mailing_job'] = $query->_whereTables['civicrm_mailing_job'] = 1;
$query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1;
$query->_where[$grouping][] = " civicrm_mailing_job.status = '{$value}' ";
$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;
}
}
/**
* Add all the elements shared between Mailing search and advnaced search.
*
*
* @param CRM_Core_Form $form
*/
public static function buildSearchForm(&$form) {
// mailing selectors
$mailings = CRM_Mailing_BAO_Mailing::getMailingsList();
if (!empty($mailings)) {
$form->add('select', 'mailing_id', ts('Mailing Name(s)'), $mailings, FALSE,
array('id' => 'mailing_id', 'multiple' => 'multiple', 'class' => 'crm-select2')
);
}
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 -'),
'Complete' => 'Complete',
'Scheduled' => 'Scheduled',
'Running' => 'Running',
'Canceled' => 'Canceled',
);
$form->addElement('select', 'mailing_job_status', ts('Mailing Job Status'), $mailingJobStatuses, FALSE);
$mailingBounceTypes = CRM_Core_PseudoConstant::get(
'CRM_Mailing_Event_DAO_Bounce', 'bounce_type_id',
array('keyColumn' => 'id', 'labelColumn' => 'name')
);
$form->add('select', 'mailing_bounce_types', ts('Bounce Types'), $mailingBounceTypes, FALSE,
array('id' => 'mailing_bounce_types', 'multiple' => 'multiple', 'class' => 'crm-select2')
);
// event filters
$form->addRadio('mailing_delivery_status', ts('Delivery Status'), CRM_Mailing_PseudoConstant::yesNoOptions('delivered'), array('allowClear' => TRUE));
$form->addRadio('mailing_open_status', ts('Trackable Opens'), CRM_Mailing_PseudoConstant::yesNoOptions('open'), array('allowClear' => TRUE));
$form->addRadio('mailing_click_status', ts('Trackable URLs'), CRM_Mailing_PseudoConstant::yesNoOptions('click'), array('allowClear' => TRUE));
$form->addRadio('mailing_reply_status', ts('Trackable Replies'), CRM_Mailing_PseudoConstant::yesNoOptions('reply'), array('allowClear' => TRUE));
$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->assign('validCiviMailing', TRUE);
}
/**
* @param $row
* @param int $id
*/
public static function searchAction(&$row, $id) {
}
/**
* @param $tables
*/
public static function tableNames(&$tables) {
}
/**
* Filter query results based on which contacts do (not) have a particular mailing event in their history.
*
* @param $query
* @param $values
* @param string $tableName
* @param string $fieldName
* @param $fieldTitle
*
* @param $valueTitles
*/
public static function mailingEventQueryBuilder(&$query, &$values, $tableName, $fieldName, $fieldTitle, &$valueTitles) {
list($name, $op, $value, $grouping, $wildcard) = $values;
if (empty($value) || $value == 'A') {
// don't do any filtering
return;
}
if ($value == 'Y') {
$query->_where[$grouping][] = $tableName . ".id is not null ";
}
elseif ($value == 'N') {
$query->_where[$grouping][] = $tableName . ".id is null ";
}
if (is_array($value)) {
$query->_where[$grouping][] = "$tableName.$fieldName $op (" . implode(',', $value) . ")";
$query->_qill[$grouping][] = "$fieldTitle $op " . implode(', ', array_intersect_key($valueTitles, array_flip($value)));
}
else {
$query->_qill[$grouping][] = $fieldTitle . ' - ' . $valueTitles[$value];
}
$query->_tables['civicrm_mailing'] = $query->_whereTables['civicrm_mailing'] = 1;
$query->_tables['civicrm_mailing_job'] = $query->_whereTables['civicrm_mailing_job'] = 1;
$query->_tables['civicrm_mailing_event_queue'] = $query->_whereTables['civicrm_mailing_event_queue'] = 1;
$query->_tables['civicrm_mailing_recipients'] = $query->_whereTables['civicrm_mailing_recipients'] = 1;
$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;
}
}

View file

@ -0,0 +1,140 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
class CRM_Mailing_BAO_Recipients extends CRM_Mailing_DAO_Recipients {
/**
* Class constructor.
*/
public function __construct() {
parent::__construct();
}
/**
* @param int $mailingID
*
* @return null|string
*/
public static function mailingSize($mailingID) {
$sql = "
SELECT count(*) as count
FROM civicrm_mailing_recipients
WHERE mailing_id = %1
";
$params = array(1 => array($mailingID, 'Integer'));
return CRM_Core_DAO::singleValueQuery($sql, $params);
}
/**
* @param int $mailingID
* @param null $offset
* @param null $limit
*
* @return Object
*/
public static function mailingQuery(
$mailingID,
$offset = NULL, $limit = NULL
) {
$limitString = NULL;
if ($limit && $offset !== NULL) {
$offset = CRM_Utils_Type::escape($offset, 'Int');
$limit = CRM_Utils_Type::escape($limit, 'Int');
$limitString = "LIMIT $offset, $limit";
}
$sql = "
SELECT contact_id, email_id, phone_id
FROM civicrm_mailing_recipients
WHERE mailing_id = %1
$limitString
";
$params = array(1 => array($mailingID, 'Integer'));
return CRM_Core_DAO::executeQuery($sql, $params);
}
/**
* Moves a number of randomly-chosen recipients of one Mailing to another Mailing.
*
* @param int $sourceMailingId
* Source mailing ID
* @param int $newMailingID
* Destination mailing ID
* @param int $totalLimit
* Number of recipients to move
*/
public static function updateRandomRecipients($sourceMailingId, $newMailingID, $totalLimit = NULL) {
$limitString = NULL;
if ($totalLimit) {
$limitString = "LIMIT 0, $totalLimit";
}
CRM_Core_DAO::executeQuery("DROP TEMPORARY TABLE IF EXISTS srcMailing_$sourceMailingId");
$sql = "
CREATE TEMPORARY TABLE srcMailing_$sourceMailingId
(mailing_recipient_id int, id int PRIMARY KEY AUTO_INCREMENT, INDEX(mailing_recipient_id))
ENGINE=HEAP";
CRM_Core_DAO::executeQuery($sql);
$sql = "
INSERT INTO srcMailing_$sourceMailingId (mailing_recipient_id)
SELECT mr.id
FROM civicrm_mailing_recipients mr
WHERE mr.mailing_id = $sourceMailingId
ORDER BY RAND()
$limitString
";
CRM_Core_DAO::executeQuery($sql);
$sql = "
UPDATE civicrm_mailing_recipients mr
INNER JOIN srcMailing_$sourceMailingId temp_mr ON temp_mr.mailing_recipient_id = mr.id
SET mr.mailing_id = $newMailingID
";
CRM_Core_DAO::executeQuery($sql);
}
/**
* Redistribute recipients from $sourceMailingId to a series of other mailings.
*
* @param int $sourceMailingId
* @param array $to
* (int $targetMailingId => int $count).
*/
public static function reassign($sourceMailingId, $to) {
foreach ($to as $targetMailingId => $count) {
if ($count > 0) {
CRM_Mailing_BAO_Recipients::updateRandomRecipients($sourceMailingId, $targetMailingId, $count);
}
}
}
}

View file

@ -0,0 +1,135 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
class CRM_Mailing_BAO_Spool extends CRM_Mailing_DAO_Spool {
/**
* Class constructor.
*/
public function __construct() {
parent::__construct();
}
/**
* Store Mails into Spool table.
*
* @param string|array $recipient
* Either a comma-seperated list of recipients
* (RFC822 compliant), or an array of recipients,
* each RFC822 valid. This may contain recipients not
* specified in the headers, for Bcc:, resending
* messages, etc.
* @param array $headers
* The array of headers to send with the mail.
*
* @param string $body
* The full text of the message body, including any mime parts, etc.
*
* @param int $job_id
*
* @return bool|CRM_Core_Error
* true if successful
*/
public function send($recipient, $headers, $body, $job_id = NULL) {
$headerStr = array();
foreach ($headers as $name => $value) {
$headerStr[] = "$name: $value";
}
$headerStr = implode("\n", $headerStr);
if (is_null($job_id)) {
// This is not a bulk mailing. Create a dummy job for it.
$session = CRM_Core_Session::singleton();
$params = array();
$params['created_id'] = $session->get('userID');
$params['created_date'] = date('YmdHis');
$params['scheduled_id'] = $params['created_id'];
$params['scheduled_date'] = $params['created_date'];
$params['is_completed'] = 1;
$params['is_archived'] = 1;
$params['body_html'] = htmlspecialchars($headerStr) . "\n\n" . $body;
$params['subject'] = $headers['Subject'];
$params['name'] = $headers['Subject'];
$ids = array();
$mailing = CRM_Mailing_BAO_Mailing::create($params, $ids);
if (empty($mailing) || is_a($mailing, 'CRM_Core_Error')) {
return PEAR::raiseError('Unable to create spooled mailing.');
}
$job = new CRM_Mailing_BAO_MailingJob();
$job->is_test = 0; // if set to 1 it doesn't show in the UI
$job->status = 'Complete';
$job->scheduled_date = CRM_Utils_Date::processDate(date('Y-m-d'), date('H:i:s'));
$job->start_date = $job->scheduled_date;
$job->end_date = $job->scheduled_date;
$job->mailing_id = $mailing->id;
$job->save();
$job_id = $job->id; // need this for parent_id below
$job = new CRM_Mailing_BAO_MailingJob();
$job->is_test = 0;
$job->status = 'Complete';
$job->scheduled_date = CRM_Utils_Date::processDate(date('Y-m-d'), date('H:i:s'));
$job->start_date = $job->scheduled_date;
$job->end_date = $job->scheduled_date;
$job->mailing_id = $mailing->id;
$job->parent_id = $job_id;
$job->job_type = 'child';
$job->save();
$job_id = $job->id; // this is the one we want for the spool
if (is_array($recipient)) {
$recipient = implode(';', $recipient);
}
}
$session = CRM_Core_Session::singleton();
$params = array(
'job_id' => $job_id,
'recipient_email' => $recipient,
'headers' => $headerStr,
'body' => $body,
'added_at' => date("YmdHis"),
'removed_at' => NULL,
);
$spoolMail = new CRM_Mailing_DAO_Spool();
$spoolMail->copyValues($params);
$spoolMail->save();
return TRUE;
}
}

View file

@ -0,0 +1,124 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
class CRM_Mailing_BAO_TrackableURL extends CRM_Mailing_DAO_TrackableURL {
/**
* Class constructor.
*/
public function __construct() {
parent::__construct();
}
/**
* Given a url, mailing id and queue event id, find or construct a
* trackable url and redirect url.
*
* @param string $url
* The target url to track.
* @param int $mailing_id
* The id of the mailing.
* @param int $queue_id
* The queue event id (contact clicking through).
*
* @return string
* The redirect/tracking url
*/
public static function getTrackerURL($url, $mailing_id, $queue_id) {
static $urlCache = array();
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:
// let's not replace possible image URLs and CiviMail ones
if (preg_match('/\.(png|jpg|jpeg|gif|css)[\'"]?$/i', $url)
or substr_count($url, 'civicrm/extern/')
or substr_count($url, 'civicrm/mailing/')
) {
// let's not cache these, so they don't get &qid= appended to them
return $url;
}
else {
$hrefExists = FALSE;
$config = CRM_Core_Config::singleton();
$tracker = new CRM_Mailing_BAO_TrackableURL();
if (preg_match('/^href/i', $url)) {
$url = preg_replace('/^href[ ]*=[ ]*[\'"](.*?)[\'"]$/i', '$1', $url);
$hrefExists = TRUE;
}
$tracker->url = $url;
$tracker->mailing_id = $mailing_id;
if (!$tracker->find(TRUE)) {
$tracker->save();
}
$id = $tracker->id;
$tracker->free();
$redirect = $config->userFrameworkResourceURL . "extern/url.php?u=$id";
$urlCache[$mailing_id . $url] = $redirect;
}
$returnUrl = "{$urlCache[$mailing_id . $url]}&qid={$queue_id}";
if ($hrefExists) {
$returnUrl = "href='{$returnUrl}'";
}
return $returnUrl;
}
/**
* @param $url
* @param $mailing_id
*
* @return int
* Url id of the given url and mail
*/
public static function getTrackerURLId($url, $mailing_id) {
$tracker = new CRM_Mailing_BAO_TrackableURL();
$tracker->url = $url;
$tracker->mailing_id = $mailing_id;
if ($tracker->find(TRUE)) {
return $tracker->id;
}
return NULL;
}
}