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