3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 class CRM_Mailing_BAO_Recipients
extends CRM_Mailing_DAO_Recipients
{
38 public function __construct() {
39 parent
::__construct();
43 * @param int $mailingID
47 public static function mailingSize($mailingID) {
49 SELECT count(*) as count
50 FROM civicrm_mailing_recipients
53 $params = [1 => [$mailingID, 'Integer']];
54 return CRM_Core_DAO
::singleValueQuery($sql, $params);
58 * @param int $mailingID
64 public static function mailingQuery(
66 $offset = NULL, $limit = NULL
69 if ($limit && $offset !== NULL) {
70 $offset = CRM_Utils_Type
::escape($offset, 'Int');
71 $limit = CRM_Utils_Type
::escape($limit, 'Int');
73 $limitString = "LIMIT $offset, $limit";
77 SELECT contact_id, email_id, phone_id
78 FROM civicrm_mailing_recipients
82 $params = [1 => [$mailingID, 'Integer']];
84 return CRM_Core_DAO
::executeQuery($sql, $params);
88 * Moves a number of randomly-chosen recipients of one Mailing to another Mailing.
90 * @param int $sourceMailingId
92 * @param int $newMailingID
93 * Destination mailing ID
94 * @param int $totalLimit
95 * Number of recipients to move
97 public static function updateRandomRecipients($sourceMailingId, $newMailingID, $totalLimit = NULL) {
100 $limitString = "LIMIT 0, $totalLimit";
102 CRM_Core_DAO
::executeQuery("DROP TEMPORARY TABLE IF EXISTS srcMailing_$sourceMailingId");
104 CREATE TEMPORARY TABLE srcMailing_$sourceMailingId
105 (mailing_recipient_id int unsigned, id int PRIMARY KEY AUTO_INCREMENT, INDEX(mailing_recipient_id))
107 CRM_Core_DAO
::executeQuery($sql);
109 INSERT INTO srcMailing_$sourceMailingId (mailing_recipient_id)
111 FROM civicrm_mailing_recipients mr
112 WHERE mr.mailing_id = $sourceMailingId
116 CRM_Core_DAO
::executeQuery($sql);
118 UPDATE civicrm_mailing_recipients mr
119 INNER JOIN srcMailing_$sourceMailingId temp_mr ON temp_mr.mailing_recipient_id = mr.id
120 SET mr.mailing_id = $newMailingID
122 CRM_Core_DAO
::executeQuery($sql);
126 * Redistribute recipients from $sourceMailingId to a series of other mailings.
128 * @param int $sourceMailingId
130 * (int $targetMailingId => int $count).
132 public static function reassign($sourceMailingId, $to) {
133 foreach ($to as $targetMailingId => $count) {
135 CRM_Mailing_BAO_Recipients
::updateRandomRecipients($sourceMailingId, $targetMailingId, $count);