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