Merge pull request #15321 from yashodha/dev_1065
[civicrm-core.git] / CRM / Mailing / BAO / Recipients.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
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";
be2fb01f 53 $params = [1 => [$mailingID, 'Integer']];
6a488035
TO
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
acaec976
SL
76 $isSMSmode = CRM_Core_DAO::getFieldValue('CRM_Mailing_BAO_Mailing', $mailingID, 'sms_provider_id', 'id');
77 $additionalJoin = '';
78 if (!$isSMSmode) {
79 // mailing_recipients added when mailing is submitted in UI by user.
80 // if any email is marked on_hold =1 or contact is deceased after mailing is submitted
81 // then it should be get skipped while preparing event_queue
82 // event_queue list is prepared when mailing job gets started.
83 $additionalJoin = " INNER JOIN civicrm_email e ON (r.email_id = e.id AND e.on_hold = 0 AND e.is_primary = 1)
84 INNER JOIN civicrm_contact c on (c.id = r.contact_id AND c.is_deceased <> 1 AND c.do_not_email = 0 AND c.is_opt_out = 0)
85";
86 }
87 else {
88 $additionalJoin = "INNER JOIN civicrm_contact c on (c.id = r.contact_id AND c.is_deceased <> 1 AND c.do_not_sms = 0 AND c.is_opt_out = 0)";
89 }
90
6a488035 91 $sql = "
acaec976
SL
92SELECT r.contact_id, r.email_id, r.phone_id
93FROM civicrm_mailing_recipients r
94{$additionalJoin}
95WHERE r.mailing_id = %1
6a488035
TO
96 $limitString
97";
be2fb01f 98 $params = [1 => [$mailingID, 'Integer']];
6a488035
TO
99
100 return CRM_Core_DAO::executeQuery($sql, $params);
101 }
ef643544 102
0ae3d02b
DK
103 /**
104 * Moves a number of randomly-chosen recipients of one Mailing to another Mailing.
105 *
768c558c 106 * @param int $sourceMailingId
0ae3d02b
DK
107 * Source mailing ID
108 * @param int $newMailingID
109 * Destination mailing ID
110 * @param int $totalLimit
111 * Number of recipients to move
112 */
00be9182 113 public static function updateRandomRecipients($sourceMailingId, $newMailingID, $totalLimit = NULL) {
ef643544 114 $limitString = NULL;
115 if ($totalLimit) {
116 $limitString = "LIMIT 0, $totalLimit";
117 }
768c558c 118 CRM_Core_DAO::executeQuery("DROP TEMPORARY TABLE IF EXISTS srcMailing_$sourceMailingId");
ef643544 119 $sql = "
768c558c 120CREATE TEMPORARY TABLE srcMailing_$sourceMailingId
1c6eee9d 121 (mailing_recipient_id int unsigned, id int PRIMARY KEY AUTO_INCREMENT, INDEX(mailing_recipient_id))
ef643544 122 ENGINE=HEAP";
123 CRM_Core_DAO::executeQuery($sql);
124 $sql = "
768c558c 125INSERT INTO srcMailing_$sourceMailingId (mailing_recipient_id)
ef643544 126SELECT mr.id
127FROM civicrm_mailing_recipients mr
768c558c 128WHERE mr.mailing_id = $sourceMailingId
ef643544 129ORDER BY RAND()
130$limitString
131 ";
132 CRM_Core_DAO::executeQuery($sql);
133 $sql = "
134UPDATE civicrm_mailing_recipients mr
768c558c 135INNER JOIN srcMailing_$sourceMailingId temp_mr ON temp_mr.mailing_recipient_id = mr.id
ef643544 136SET mr.mailing_id = $newMailingID
137 ";
138 CRM_Core_DAO::executeQuery($sql);
139 }
140
768c558c
TO
141 /**
142 * Redistribute recipients from $sourceMailingId to a series of other mailings.
143 *
144 * @param int $sourceMailingId
90c8230e
TO
145 * @param array $to
146 * (int $targetMailingId => int $count).
768c558c 147 */
00be9182 148 public static function reassign($sourceMailingId, $to) {
768c558c
TO
149 foreach ($to as $targetMailingId => $count) {
150 if ($count > 0) {
151 CRM_Mailing_BAO_Recipients::updateRandomRecipients($sourceMailingId, $targetMailingId, $count);
152 }
153 }
154 }
155
6a488035 156}