| 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 | $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 | |
| 91 | $sql = " |
| 92 | SELECT r.contact_id, r.email_id, r.phone_id |
| 93 | FROM civicrm_mailing_recipients r |
| 94 | {$additionalJoin} |
| 95 | WHERE r.mailing_id = %1 |
| 96 | $limitString |
| 97 | "; |
| 98 | $params = [1 => [$mailingID, 'Integer']]; |
| 99 | |
| 100 | return CRM_Core_DAO::executeQuery($sql, $params); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Moves a number of randomly-chosen recipients of one Mailing to another Mailing. |
| 105 | * |
| 106 | * @param int $sourceMailingId |
| 107 | * Source mailing ID |
| 108 | * @param int $newMailingID |
| 109 | * Destination mailing ID |
| 110 | * @param int $totalLimit |
| 111 | * Number of recipients to move |
| 112 | */ |
| 113 | public static function updateRandomRecipients($sourceMailingId, $newMailingID, $totalLimit = NULL) { |
| 114 | $limitString = NULL; |
| 115 | if ($totalLimit) { |
| 116 | $limitString = "LIMIT 0, $totalLimit"; |
| 117 | } |
| 118 | CRM_Core_DAO::executeQuery("DROP TEMPORARY TABLE IF EXISTS srcMailing_$sourceMailingId"); |
| 119 | $sql = " |
| 120 | CREATE TEMPORARY TABLE srcMailing_$sourceMailingId |
| 121 | (mailing_recipient_id int unsigned, id int PRIMARY KEY AUTO_INCREMENT, INDEX(mailing_recipient_id)) |
| 122 | ENGINE=HEAP"; |
| 123 | CRM_Core_DAO::executeQuery($sql); |
| 124 | $sql = " |
| 125 | INSERT INTO srcMailing_$sourceMailingId (mailing_recipient_id) |
| 126 | SELECT mr.id |
| 127 | FROM civicrm_mailing_recipients mr |
| 128 | WHERE mr.mailing_id = $sourceMailingId |
| 129 | ORDER BY RAND() |
| 130 | $limitString |
| 131 | "; |
| 132 | CRM_Core_DAO::executeQuery($sql); |
| 133 | $sql = " |
| 134 | UPDATE civicrm_mailing_recipients mr |
| 135 | INNER JOIN srcMailing_$sourceMailingId temp_mr ON temp_mr.mailing_recipient_id = mr.id |
| 136 | SET mr.mailing_id = $newMailingID |
| 137 | "; |
| 138 | CRM_Core_DAO::executeQuery($sql); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Redistribute recipients from $sourceMailingId to a series of other mailings. |
| 143 | * |
| 144 | * @param int $sourceMailingId |
| 145 | * @param array $to |
| 146 | * (int $targetMailingId => int $count). |
| 147 | */ |
| 148 | public static function reassign($sourceMailingId, $to) { |
| 149 | foreach ($to as $targetMailingId => $count) { |
| 150 | if ($count > 0) { |
| 151 | CRM_Mailing_BAO_Recipients::updateRandomRecipients($sourceMailingId, $targetMailingId, $count); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | } |