Merge pull request #13258 from eileenmcnaughton/isam
[civicrm-core.git] / CRM / Mailing / BAO / Recipients.php
CommitLineData
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 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33class CRM_Mailing_BAO_Recipients extends CRM_Mailing_DAO_Recipients {
34
35 /**
fe482240 36 * Class constructor.
6a488035 37 */
00be9182 38 public function __construct() {
6a488035
TO
39 parent::__construct();
40 }
41
e0ef6999 42 /**
100fef9d 43 * @param int $mailingID
e0ef6999
EM
44 *
45 * @return null|string
46 */
00be9182 47 public static function mailingSize($mailingID) {
6a488035
TO
48 $sql = "
49SELECT count(*) as count
50FROM civicrm_mailing_recipients
51WHERE mailing_id = %1
52";
53 $params = array(1 => array($mailingID, 'Integer'));
54 return CRM_Core_DAO::singleValueQuery($sql, $params);
55 }
56
e0ef6999 57 /**
100fef9d 58 * @param int $mailingID
e0ef6999
EM
59 * @param null $offset
60 * @param null $limit
61 *
62 * @return Object
63 */
d5cc0fc2 64 public static function mailingQuery(
a3d7e8ee 65 $mailingID,
6a488035
TO
66 $offset = NULL, $limit = NULL
67 ) {
68 $limitString = NULL;
69 if ($limit && $offset !== NULL) {
bf00d1b6 70 $offset = CRM_Utils_Type::escape($offset, 'Int');
dd3a4117 71 $limit = CRM_Utils_Type::escape($limit, 'Int');
bf00d1b6 72
6a488035
TO
73 $limitString = "LIMIT $offset, $limit";
74 }
75
76 $sql = "
77SELECT contact_id, email_id, phone_id
78FROM civicrm_mailing_recipients
79WHERE mailing_id = %1
80 $limitString
81";
82 $params = array(1 => array($mailingID, 'Integer'));
83
84 return CRM_Core_DAO::executeQuery($sql, $params);
85 }
ef643544 86
0ae3d02b
DK
87 /**
88 * Moves a number of randomly-chosen recipients of one Mailing to another Mailing.
89 *
768c558c 90 * @param int $sourceMailingId
0ae3d02b
DK
91 * Source mailing ID
92 * @param int $newMailingID
93 * Destination mailing ID
94 * @param int $totalLimit
95 * Number of recipients to move
96 */
00be9182 97 public static function updateRandomRecipients($sourceMailingId, $newMailingID, $totalLimit = NULL) {
ef643544 98 $limitString = NULL;
99 if ($totalLimit) {
100 $limitString = "LIMIT 0, $totalLimit";
101 }
768c558c 102 CRM_Core_DAO::executeQuery("DROP TEMPORARY TABLE IF EXISTS srcMailing_$sourceMailingId");
ef643544 103 $sql = "
768c558c 104CREATE TEMPORARY TABLE srcMailing_$sourceMailingId
1c6eee9d 105 (mailing_recipient_id int unsigned, id int PRIMARY KEY AUTO_INCREMENT, INDEX(mailing_recipient_id))
ef643544 106 ENGINE=HEAP";
107 CRM_Core_DAO::executeQuery($sql);
108 $sql = "
768c558c 109INSERT INTO srcMailing_$sourceMailingId (mailing_recipient_id)
ef643544 110SELECT mr.id
111FROM civicrm_mailing_recipients mr
768c558c 112WHERE mr.mailing_id = $sourceMailingId
ef643544 113ORDER BY RAND()
114$limitString
115 ";
116 CRM_Core_DAO::executeQuery($sql);
117 $sql = "
118UPDATE civicrm_mailing_recipients mr
768c558c 119INNER JOIN srcMailing_$sourceMailingId temp_mr ON temp_mr.mailing_recipient_id = mr.id
ef643544 120SET mr.mailing_id = $newMailingID
121 ";
122 CRM_Core_DAO::executeQuery($sql);
123 }
124
768c558c
TO
125 /**
126 * Redistribute recipients from $sourceMailingId to a series of other mailings.
127 *
128 * @param int $sourceMailingId
90c8230e
TO
129 * @param array $to
130 * (int $targetMailingId => int $count).
768c558c 131 */
00be9182 132 public static function reassign($sourceMailingId, $to) {
768c558c
TO
133 foreach ($to as $targetMailingId => $count) {
134 if ($count > 0) {
135 CRM_Mailing_BAO_Recipients::updateRandomRecipients($sourceMailingId, $targetMailingId, $count);
136 }
137 }
138 }
139
6a488035 140}