| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Class CRM_Mailing_BAO_MailingAB |
| 38 | */ |
| 39 | class CRM_Mailing_BAO_MailingAB extends CRM_Mailing_DAO_MailingAB { |
| 40 | |
| 41 | /** |
| 42 | * class constructor. |
| 43 | */ |
| 44 | public function __construct() { |
| 45 | parent::__construct(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Construct a new mailingab object. |
| 50 | * |
| 51 | * @params array $params |
| 52 | * Form values. |
| 53 | * |
| 54 | * @param array $params |
| 55 | * @param array $ids |
| 56 | * |
| 57 | * @return object |
| 58 | * $mailingab The new mailingab object |
| 59 | */ |
| 60 | public static function create(&$params, $ids = array()) { |
| 61 | $transaction = new CRM_Core_Transaction(); |
| 62 | |
| 63 | $mailingab = self::add($params, $ids); |
| 64 | |
| 65 | if (is_a($mailingab, 'CRM_Core_Error')) { |
| 66 | $transaction->rollback(); |
| 67 | return $mailingab; |
| 68 | } |
| 69 | $transaction->commit(); |
| 70 | return $mailingab; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * function to add the mailings. |
| 75 | * |
| 76 | * @param array $params |
| 77 | * Reference array contains the values submitted by the form. |
| 78 | * @param array $ids |
| 79 | * Reference array contains the id. |
| 80 | * |
| 81 | * |
| 82 | * @return object |
| 83 | */ |
| 84 | public static function add(&$params, $ids = array()) { |
| 85 | $id = CRM_Utils_Array::value('mailingab_id', $ids, CRM_Utils_Array::value('id', $params)); |
| 86 | |
| 87 | if ($id) { |
| 88 | CRM_Utils_Hook::pre('edit', 'MailingAB', $id, $params); |
| 89 | } |
| 90 | else { |
| 91 | CRM_Utils_Hook::pre('create', 'MailingAB', NULL, $params); |
| 92 | } |
| 93 | |
| 94 | $mailingab = new CRM_Mailing_DAO_MailingAB(); |
| 95 | $mailingab->id = $id; |
| 96 | if (!$id) { |
| 97 | $mailingab->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID()); |
| 98 | } |
| 99 | |
| 100 | $mailingab->copyValues($params); |
| 101 | |
| 102 | $result = $mailingab->save(); |
| 103 | |
| 104 | if ($id) { |
| 105 | CRM_Utils_Hook::post('edit', 'MailingAB', $mailingab->id, $mailingab); |
| 106 | } |
| 107 | else { |
| 108 | CRM_Utils_Hook::post('create', 'MailingAB', $mailingab->id, $mailingab); |
| 109 | } |
| 110 | |
| 111 | return $result; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | * Delete MailingAB and all its associated records. |
| 117 | * |
| 118 | * @param int $id |
| 119 | * Id of the mail to delete. |
| 120 | * |
| 121 | * @return void |
| 122 | */ |
| 123 | public static function del($id) { |
| 124 | if (empty($id)) { |
| 125 | CRM_Core_Error::fatal(); |
| 126 | } |
| 127 | CRM_Core_Transaction::create()->run(function () use ($id) { |
| 128 | CRM_Utils_Hook::pre('delete', 'MailingAB', $id, CRM_Core_DAO::$_nullArray); |
| 129 | |
| 130 | $dao = new CRM_Mailing_DAO_MailingAB(); |
| 131 | $dao->id = $id; |
| 132 | if ($dao->find(TRUE)) { |
| 133 | $mailing_ids = array($dao->mailing_id_a, $dao->mailing_id_b, $dao->mailing_id_c); |
| 134 | $dao->delete(); |
| 135 | foreach ($mailing_ids as $mailing_id) { |
| 136 | if ($mailing_id) { |
| 137 | CRM_Mailing_BAO_Mailing::del($mailing_id); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | CRM_Core_Session::setStatus(ts('Selected mailing has been deleted.'), ts('Deleted'), 'success'); |
| 143 | |
| 144 | CRM_Utils_Hook::post('delete', 'MailingAB', $id, $dao); |
| 145 | }); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Transfer recipients from the canonical mailing A to the other mailings. |
| 150 | * |
| 151 | * @param CRM_Mailing_DAO_MailingAB $dao |
| 152 | */ |
| 153 | public static function distributeRecipients(CRM_Mailing_DAO_MailingAB $dao) { |
| 154 | CRM_Mailing_BAO_Mailing::getRecipients($dao->mailing_id_a, $dao->mailing_id_a, TRUE); |
| 155 | |
| 156 | //calculate total number of random recipients for mail C from group_percentage selected |
| 157 | $totalCount = CRM_Mailing_BAO_Recipients::mailingSize($dao->mailing_id_a); |
| 158 | $totalSelected = max(1, round(($totalCount * $dao->group_percentage) / 100)); |
| 159 | |
| 160 | CRM_Mailing_BAO_Recipients::reassign($dao->mailing_id_a, array( |
| 161 | $dao->mailing_id_b => (2 * $totalSelected <= $totalCount) ? $totalSelected : $totalCount - $totalSelected, |
| 162 | $dao->mailing_id_c => max(0, $totalCount - $totalSelected - $totalSelected), |
| 163 | )); |
| 164 | |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * get abtest based on Mailing ID |
| 169 | * |
| 170 | * @param int $mailingID |
| 171 | * Mailing ID. |
| 172 | * |
| 173 | * @return object |
| 174 | */ |
| 175 | public static function getABTest($mailingID) { |
| 176 | $query = "SELECT * FROM `civicrm_mailing_abtest` ab |
| 177 | where (ab.mailing_id_a = %1 |
| 178 | OR ab.mailing_id_b = %1 |
| 179 | OR ab.mailing_id_c = %1) |
| 180 | GROUP BY ab.id"; |
| 181 | $params = array(1 => array($mailingID, 'Integer')); |
| 182 | $abTest = CRM_Core_DAO::executeQuery($query, $params); |
| 183 | $abTest->fetch(); |
| 184 | return $abTest; |
| 185 | } |
| 186 | |
| 187 | } |