3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * Class CRM_Mailing_BAO_MailingAB
39 class CRM_Mailing_BAO_MailingAB
extends CRM_Mailing_DAO_MailingAB
{
44 function __construct() {
45 parent
::__construct();
49 * Construct a new mailingab object
51 * @params array $params Form values
56 * @return object $mailingab The new mailingab object
60 public static function create(&$params, $ids = array()) {
61 $transaction = new CRM_Core_Transaction();
63 $mailingab = self
::add($params, $ids);
65 if (is_a($mailingab, 'CRM_Core_Error')) {
66 $transaction->rollback();
69 $transaction->commit();
74 * function to add the mailings
76 * @param array $params reference array contains the values submitted by the form
77 * @param array $ids reference array contains the id
84 static function add(&$params, $ids = array()) {
85 $id = CRM_Utils_Array
::value('mailingab_id', $ids, CRM_Utils_Array
::value('id', $params));
88 CRM_Utils_Hook
::pre('edit', 'MailingAB', $id, $params);
91 CRM_Utils_Hook
::pre('create', 'MailingAB', NULL, $params);
94 $mailingab = new CRM_Mailing_DAO_MailingAB();
97 $mailingab->domain_id
= CRM_Utils_Array
::value('domain_id', $params, CRM_Core_Config
::domainID());
100 $mailingab->copyValues($params);
102 $result = $mailingab->save();
105 CRM_Utils_Hook
::post('edit', 'MailingAB', $mailingab->id
, $mailingab);
108 CRM_Utils_Hook
::post('create', 'MailingAB', $mailingab->id
, $mailingab);
116 * Delete MailingAB and all its associated records
118 * @param int $id id of the mail to delete
124 public static function del($id) {
126 CRM_Core_Error
::fatal();
128 CRM_Core_Transaction
::create()->run(function() use ($id) {
129 CRM_Utils_Hook
::pre('delete', 'MailingAB', $id, CRM_Core_DAO
::$_nullArray);
131 $dao = new CRM_Mailing_DAO_MailingAB();
133 if ($dao->find(TRUE)) {
134 $mailing_ids = array($dao->mailing_id_a
, $dao->mailing_id_b
, $dao->mailing_id_c
);
136 foreach ($mailing_ids as $mailing_id) {
138 CRM_Mailing_BAO_Mailing
::del($mailing_id);
143 CRM_Core_Session
::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success');
145 CRM_Utils_Hook
::post('delete', 'MailingAB', $id, $dao);
150 * Transfer recipients from the canonical mailing A to the other mailings.
152 * @param CRM_Mailing_DAO_MailingAB $dao
154 public static function distributeRecipients(CRM_Mailing_DAO_MailingAB
$dao) {
155 CRM_Mailing_BAO_Mailing
::getRecipients($dao->mailing_id_a
, $dao->mailing_id_a
, NULL, NULL, TRUE);
157 //calculate total number of random recipients for mail C from group_percentage selected
158 $totalCount = CRM_Mailing_BAO_Recipients
::mailingSize($dao->mailing_id_a
);
159 $totalSelected = max(1, round(($totalCount * $dao->group_percentage
) / 100));
161 CRM_Mailing_BAO_Recipients
::reassign($dao->mailing_id_a
, array(
162 $dao->mailing_id_b
=> (2 * $totalSelected <= $totalCount) ?
$totalSelected : $totalCount - $totalSelected,
163 $dao->mailing_id_c
=> max(0, $totalCount - $totalSelected - $totalSelected),