| 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 | */ |
| 33 | |
| 34 | /** |
| 35 | * This class contains Contribution Page related functions. |
| 36 | */ |
| 37 | class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_ContributionPage { |
| 38 | |
| 39 | /** |
| 40 | * Creates a contribution page. |
| 41 | * |
| 42 | * @param array $params |
| 43 | * |
| 44 | * @return CRM_Contribute_DAO_ContributionPage |
| 45 | */ |
| 46 | public static function create($params) { |
| 47 | $financialTypeId = NULL; |
| 48 | if (!empty($params['id']) && !CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $params['id'], NULL, 1)) { |
| 49 | $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $params['id'], 'financial_type_id'); |
| 50 | } |
| 51 | |
| 52 | if (isset($params['payment_processor']) && is_array($params['payment_processor'])) { |
| 53 | $params['payment_processor'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $params['payment_processor']); |
| 54 | } |
| 55 | $hook = empty($params['id']) ? 'create' : 'edit'; |
| 56 | CRM_Utils_Hook::pre($hook, 'ContributionPage', CRM_Utils_Array::value('id', $params), $params); |
| 57 | $dao = new CRM_Contribute_DAO_ContributionPage(); |
| 58 | $dao->copyValues($params); |
| 59 | $dao->save(); |
| 60 | if ($financialTypeId && !empty($params['financial_type_id']) && $financialTypeId != $params['financial_type_id']) { |
| 61 | CRM_Price_BAO_PriceFieldValue::updateFinancialType($params['id'], 'civicrm_contribution_page', $params['financial_type_id']); |
| 62 | } |
| 63 | CRM_Utils_Hook::post($hook, 'ContributionPage', $dao->id, $dao); |
| 64 | return $dao; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Update the is_active flag in the db. |
| 69 | * |
| 70 | * @deprecated - this bypasses hooks. |
| 71 | * |
| 72 | * @param int $id |
| 73 | * Id of the database record. |
| 74 | * @param bool $is_active |
| 75 | * Value we want to set the is_active field. |
| 76 | * |
| 77 | * @return Object |
| 78 | * DAO object on success, null otherwise |
| 79 | */ |
| 80 | public static function setIsActive($id, $is_active) { |
| 81 | return CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_ContributionPage', $id, 'is_active', $is_active); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Load values for a contribution page. |
| 86 | * |
| 87 | * @param int $id |
| 88 | * @param array $values |
| 89 | */ |
| 90 | public static function setValues($id, &$values) { |
| 91 | $modules = array('CiviContribute', 'soft_credit', 'on_behalf'); |
| 92 | $values['custom_pre_id'] = $values['custom_post_id'] = NULL; |
| 93 | |
| 94 | $params = array('id' => $id); |
| 95 | CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $values); |
| 96 | |
| 97 | // get the profile ids |
| 98 | $ufJoinParams = array( |
| 99 | 'entity_table' => 'civicrm_contribution_page', |
| 100 | 'entity_id' => $id, |
| 101 | ); |
| 102 | |
| 103 | // retrieve profile id as also unserialize module_data corresponding to each $module |
| 104 | foreach ($modules as $module) { |
| 105 | $ufJoinParams['module'] = $module; |
| 106 | $ufJoin = new CRM_Core_DAO_UFJoin(); |
| 107 | $ufJoin->copyValues($ufJoinParams); |
| 108 | if ($module == 'CiviContribute') { |
| 109 | $ufJoin->orderBy('weight asc'); |
| 110 | $ufJoin->find(); |
| 111 | while ($ufJoin->fetch()) { |
| 112 | if ($ufJoin->weight == 1) { |
| 113 | $values['custom_pre_id'] = $ufJoin->uf_group_id; |
| 114 | } |
| 115 | else { |
| 116 | $values['custom_post_id'] = $ufJoin->uf_group_id; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | else { |
| 121 | $ufJoin->find(TRUE); |
| 122 | if (!$ufJoin->is_active) { |
| 123 | continue; |
| 124 | } |
| 125 | $params = CRM_Contribute_BAO_ContributionPage::formatModuleData($ufJoin->module_data, TRUE, $module); |
| 126 | $values = array_merge($params, $values); |
| 127 | if ($module == 'soft_credit') { |
| 128 | $values['honoree_profile_id'] = $ufJoin->uf_group_id; |
| 129 | $values['honor_block_is_active'] = $ufJoin->is_active; |
| 130 | } |
| 131 | else { |
| 132 | $values['onbehalf_profile_id'] = $ufJoin->uf_group_id; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Send the emails. |
| 140 | * |
| 141 | * @param int $contactID |
| 142 | * Contact id. |
| 143 | * @param array $values |
| 144 | * Associated array of fields. |
| 145 | * @param bool $isTest |
| 146 | * If in test mode. |
| 147 | * @param bool $returnMessageText |
| 148 | * Return the message text instead of sending the mail. |
| 149 | * |
| 150 | * @param array $fieldTypes |
| 151 | */ |
| 152 | public static function sendMail($contactID, $values, $isTest = FALSE, $returnMessageText = FALSE, $fieldTypes = NULL) { |
| 153 | $gIds = $params = array(); |
| 154 | $email = NULL; |
| 155 | if (isset($values['custom_pre_id'])) { |
| 156 | $preProfileType = CRM_Core_BAO_UFField::getProfileType($values['custom_pre_id']); |
| 157 | if ($preProfileType == 'Membership' && !empty($values['membership_id'])) { |
| 158 | $params['custom_pre_id'] = array( |
| 159 | array( |
| 160 | 'member_id', |
| 161 | '=', |
| 162 | $values['membership_id'], |
| 163 | 0, |
| 164 | 0, |
| 165 | ), |
| 166 | ); |
| 167 | } |
| 168 | elseif ($preProfileType == 'Contribution' && !empty($values['contribution_id'])) { |
| 169 | $params['custom_pre_id'] = array( |
| 170 | array( |
| 171 | 'contribution_id', |
| 172 | '=', |
| 173 | $values['contribution_id'], |
| 174 | 0, |
| 175 | 0, |
| 176 | ), |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | $gIds['custom_pre_id'] = $values['custom_pre_id']; |
| 181 | } |
| 182 | |
| 183 | if (isset($values['custom_post_id'])) { |
| 184 | $postProfileType = CRM_Core_BAO_UFField::getProfileType($values['custom_post_id']); |
| 185 | if ($postProfileType == 'Membership' && !empty($values['membership_id'])) { |
| 186 | $params['custom_post_id'] = array( |
| 187 | array( |
| 188 | 'member_id', |
| 189 | '=', |
| 190 | $values['membership_id'], |
| 191 | 0, |
| 192 | 0, |
| 193 | ), |
| 194 | ); |
| 195 | } |
| 196 | elseif ($postProfileType == 'Contribution' && !empty($values['contribution_id'])) { |
| 197 | $params['custom_post_id'] = array( |
| 198 | array( |
| 199 | 'contribution_id', |
| 200 | '=', |
| 201 | $values['contribution_id'], |
| 202 | 0, |
| 203 | 0, |
| 204 | ), |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | $gIds['custom_post_id'] = $values['custom_post_id']; |
| 209 | } |
| 210 | |
| 211 | if (!empty($values['is_for_organization'])) { |
| 212 | if (!empty($values['membership_id'])) { |
| 213 | $params['onbehalf_profile'] = array( |
| 214 | array( |
| 215 | 'member_id', |
| 216 | '=', |
| 217 | $values['membership_id'], |
| 218 | 0, |
| 219 | 0, |
| 220 | ), |
| 221 | ); |
| 222 | } |
| 223 | elseif (!empty($values['contribution_id'])) { |
| 224 | $params['onbehalf_profile'] = array( |
| 225 | array( |
| 226 | 'contribution_id', |
| 227 | '=', |
| 228 | $values['contribution_id'], |
| 229 | 0, |
| 230 | 0, |
| 231 | ), |
| 232 | ); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | //check whether it is a test drive |
| 237 | if ($isTest && !empty($params['custom_pre_id'])) { |
| 238 | $params['custom_pre_id'][] = array( |
| 239 | 'contribution_test', |
| 240 | '=', |
| 241 | 1, |
| 242 | 0, |
| 243 | 0, |
| 244 | ); |
| 245 | } |
| 246 | |
| 247 | if ($isTest && !empty($params['custom_post_id'])) { |
| 248 | $params['custom_post_id'][] = array( |
| 249 | 'contribution_test', |
| 250 | '=', |
| 251 | 1, |
| 252 | 0, |
| 253 | 0, |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | if (!$returnMessageText && !empty($gIds)) { |
| 258 | //send notification email if field values are set (CRM-1941) |
| 259 | foreach ($gIds as $key => $gId) { |
| 260 | if ($gId) { |
| 261 | $email = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gId, 'notify'); |
| 262 | if ($email) { |
| 263 | $val = CRM_Core_BAO_UFGroup::checkFieldsEmptyValues($gId, $contactID, CRM_Utils_Array::value($key, $params), TRUE); |
| 264 | CRM_Core_BAO_UFGroup::commonSendMail($contactID, $val); |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | if (!empty($values['is_email_receipt']) || !empty($values['onbehalf_dupe_alert']) || |
| 271 | $returnMessageText |
| 272 | ) { |
| 273 | $template = CRM_Core_Smarty::singleton(); |
| 274 | |
| 275 | // get the billing location type |
| 276 | if (!array_key_exists('related_contact', $values)) { |
| 277 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate'); |
| 278 | $billingLocationTypeId = array_search('Billing', $locationTypes); |
| 279 | } |
| 280 | else { |
| 281 | // presence of related contact implies onbehalf of org case, |
| 282 | // where location type is set to default. |
| 283 | $locType = CRM_Core_BAO_LocationType::getDefault(); |
| 284 | $billingLocationTypeId = $locType->id; |
| 285 | } |
| 286 | |
| 287 | if (!array_key_exists('related_contact', $values)) { |
| 288 | list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID, FALSE, $billingLocationTypeId); |
| 289 | } |
| 290 | // get primary location email if no email exist( for billing location). |
| 291 | if (!$email) { |
| 292 | list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID); |
| 293 | } |
| 294 | if (empty($displayName)) { |
| 295 | list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID); |
| 296 | } |
| 297 | |
| 298 | //for display profile need to get individual contact id, |
| 299 | //hence get it from related_contact if on behalf of org true CRM-3767 |
| 300 | //CRM-5001 Contribution/Membership:: On Behalf of Organization, |
| 301 | //If profile GROUP contain the Individual type then consider the |
| 302 | //profile is of Individual ( including the custom data of membership/contribution ) |
| 303 | //IF Individual type not present in profile then it is consider as Organization data. |
| 304 | $userID = $contactID; |
| 305 | if ($preID = CRM_Utils_Array::value('custom_pre_id', $values)) { |
| 306 | if (!empty($values['related_contact'])) { |
| 307 | $preProfileTypes = CRM_Core_BAO_UFGroup::profileGroups($preID); |
| 308 | //@todo - following line should not refer to undefined $postProfileTypes? figure out way to test |
| 309 | if (in_array('Individual', $preProfileTypes) || in_array('Contact', $postProfileTypes)) { |
| 310 | //Take Individual contact ID |
| 311 | $userID = CRM_Utils_Array::value('related_contact', $values); |
| 312 | } |
| 313 | } |
| 314 | self::buildCustomDisplay($preID, 'customPre', $userID, $template, $params['custom_pre_id']); |
| 315 | } |
| 316 | $userID = $contactID; |
| 317 | if ($postID = CRM_Utils_Array::value('custom_post_id', $values)) { |
| 318 | if (!empty($values['related_contact'])) { |
| 319 | $postProfileTypes = CRM_Core_BAO_UFGroup::profileGroups($postID); |
| 320 | if (in_array('Individual', $postProfileTypes) || in_array('Contact', $postProfileTypes)) { |
| 321 | //Take Individual contact ID |
| 322 | $userID = CRM_Utils_Array::value('related_contact', $values); |
| 323 | } |
| 324 | } |
| 325 | self::buildCustomDisplay($postID, 'customPost', $userID, $template, $params['custom_post_id']); |
| 326 | } |
| 327 | if (isset($values['honor'])) { |
| 328 | $honorValues = $values['honor']; |
| 329 | $template->_values = array('honoree_profile_id' => $values['honoree_profile_id']); |
| 330 | CRM_Contribute_BAO_ContributionSoft::formatHonoreeProfileFields( |
| 331 | $template, |
| 332 | $honorValues['honor_profile_values'], |
| 333 | $honorValues['honor_id'] |
| 334 | ); |
| 335 | } |
| 336 | |
| 337 | $title = isset($values['title']) ? $values['title'] : CRM_Contribute_PseudoConstant::contributionPage($values['contribution_page_id']); |
| 338 | |
| 339 | // set email in the template here |
| 340 | $tplParams = array( |
| 341 | 'email' => $email, |
| 342 | 'receiptFromEmail' => CRM_Utils_Array::value('receipt_from_email', $values), |
| 343 | 'contactID' => $contactID, |
| 344 | 'displayName' => $displayName, |
| 345 | 'contributionID' => CRM_Utils_Array::value('contribution_id', $values), |
| 346 | 'contributionOtherID' => CRM_Utils_Array::value('contribution_other_id', $values), |
| 347 | // CRM-5095 |
| 348 | 'lineItem' => CRM_Utils_Array::value('lineItem', $values), |
| 349 | // CRM-5095 |
| 350 | 'priceSetID' => CRM_Utils_Array::value('priceSetID', $values), |
| 351 | 'title' => $title, |
| 352 | 'isShare' => CRM_Utils_Array::value('is_share', $values), |
| 353 | 'thankyou_title' => CRM_Utils_Array::value('thankyou_title', $values), |
| 354 | ); |
| 355 | |
| 356 | if ($contributionTypeId = CRM_Utils_Array::value('financial_type_id', $values)) { |
| 357 | $tplParams['contributionTypeId'] = $contributionTypeId; |
| 358 | $tplParams['contributionTypeName'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', |
| 359 | $contributionTypeId); |
| 360 | } |
| 361 | |
| 362 | if ($contributionPageId = CRM_Utils_Array::value('id', $values)) { |
| 363 | $tplParams['contributionPageId'] = $contributionPageId; |
| 364 | } |
| 365 | |
| 366 | // address required during receipt processing (pdf and email receipt) |
| 367 | if ($displayAddress = CRM_Utils_Array::value('address', $values)) { |
| 368 | $tplParams['address'] = $displayAddress; |
| 369 | } |
| 370 | |
| 371 | // CRM-6976 |
| 372 | $originalCCReceipt = CRM_Utils_Array::value('cc_receipt', $values); |
| 373 | |
| 374 | // cc to related contacts of contributor OR the one who |
| 375 | // signs up. Is used for cases like - on behalf of |
| 376 | // contribution / signup ..etc |
| 377 | if (array_key_exists('related_contact', $values)) { |
| 378 | list($ccDisplayName, $ccEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($values['related_contact']); |
| 379 | $ccMailId = "{$ccDisplayName} <{$ccEmail}>"; |
| 380 | |
| 381 | //@todo - this is the only place in this function where $values is altered - but I can't find any evidence it is used |
| 382 | $values['cc_receipt'] = !empty($values['cc_receipt']) ? ($values['cc_receipt'] . ',' . $ccMailId) : $ccMailId; |
| 383 | |
| 384 | // reset primary-email in the template |
| 385 | $tplParams['email'] = $ccEmail; |
| 386 | |
| 387 | $tplParams['onBehalfName'] = $displayName; |
| 388 | $tplParams['onBehalfEmail'] = $email; |
| 389 | |
| 390 | if (!empty($values['onbehalf_profile_id'])) { |
| 391 | self::buildCustomDisplay($values['onbehalf_profile_id'], 'onBehalfProfile', $contactID, $template, $params['onbehalf_profile'], $fieldTypes); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | // use either the contribution or membership receipt, based on whether it’s a membership-related contrib or not |
| 396 | $sendTemplateParams = array( |
| 397 | 'groupName' => !empty($values['isMembership']) ? 'msg_tpl_workflow_membership' : 'msg_tpl_workflow_contribution', |
| 398 | 'valueName' => !empty($values['isMembership']) ? 'membership_online_receipt' : 'contribution_online_receipt', |
| 399 | 'contactId' => $contactID, |
| 400 | 'tplParams' => $tplParams, |
| 401 | 'isTest' => $isTest, |
| 402 | 'PDFFilename' => 'receipt.pdf', |
| 403 | ); |
| 404 | |
| 405 | if ($returnMessageText) { |
| 406 | list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); |
| 407 | return array( |
| 408 | 'subject' => $subject, |
| 409 | 'body' => $message, |
| 410 | 'to' => $displayName, |
| 411 | 'html' => $html, |
| 412 | ); |
| 413 | } |
| 414 | |
| 415 | if ($values['is_email_receipt']) { |
| 416 | $sendTemplateParams['from'] = CRM_Utils_Array::value('receipt_from_name', $values) . ' <' . $values['receipt_from_email'] . '>'; |
| 417 | $sendTemplateParams['toName'] = $displayName; |
| 418 | $sendTemplateParams['toEmail'] = $email; |
| 419 | $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc_receipt', $values); |
| 420 | $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc_receipt', $values); |
| 421 | //send email with pdf invoice |
| 422 | $template = CRM_Core_Smarty::singleton(); |
| 423 | $taxAmt = $template->get_template_vars('dataArray'); |
| 424 | $prefixValue = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings'); |
| 425 | $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue); |
| 426 | if (isset($invoicing) && isset($prefixValue['is_email_pdf'])) { |
| 427 | $sendTemplateParams['isEmailPdf'] = TRUE; |
| 428 | $sendTemplateParams['contributionId'] = $values['contribution_id']; |
| 429 | } |
| 430 | list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); |
| 431 | } |
| 432 | |
| 433 | // send duplicate alert, if dupe match found during on-behalf-of processing. |
| 434 | if (!empty($values['onbehalf_dupe_alert'])) { |
| 435 | $sendTemplateParams['groupName'] = 'msg_tpl_workflow_contribution'; |
| 436 | $sendTemplateParams['valueName'] = 'contribution_dupalert'; |
| 437 | $sendTemplateParams['from'] = ts('Automatically Generated') . " <{$values['receipt_from_email']}>"; |
| 438 | $sendTemplateParams['toName'] = CRM_Utils_Array::value('receipt_from_name', $values); |
| 439 | $sendTemplateParams['toEmail'] = CRM_Utils_Array::value('receipt_from_email', $values); |
| 440 | $sendTemplateParams['tplParams']['onBehalfID'] = $contactID; |
| 441 | $sendTemplateParams['tplParams']['receiptMessage'] = $message; |
| 442 | |
| 443 | // fix cc and reset back to original, CRM-6976 |
| 444 | $sendTemplateParams['cc'] = $originalCCReceipt; |
| 445 | |
| 446 | CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * Send the emails for Recurring Contribution Notification. |
| 453 | * |
| 454 | * @param string $type |
| 455 | * TxnType. |
| 456 | * @param int $contactID |
| 457 | * Contact id for contributor. |
| 458 | * @param int $pageID |
| 459 | * Contribution page id. |
| 460 | * @param object $recur |
| 461 | * Object of recurring contribution table. |
| 462 | * @param bool|object $autoRenewMembership is it a auto renew membership. |
| 463 | */ |
| 464 | public static function recurringNotify($type, $contactID, $pageID, $recur, $autoRenewMembership = FALSE) { |
| 465 | $value = array(); |
| 466 | $isEmailReceipt = FALSE; |
| 467 | if ($pageID) { |
| 468 | CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id', $pageID, $value, array( |
| 469 | 'title', |
| 470 | 'is_email_receipt', |
| 471 | 'receipt_from_name', |
| 472 | 'receipt_from_email', |
| 473 | 'cc_receipt', |
| 474 | 'bcc_receipt', |
| 475 | )); |
| 476 | $isEmailReceipt = CRM_Utils_Array::value('is_email_receipt', $value[$pageID]); |
| 477 | } |
| 478 | elseif ($recur->id) { |
| 479 | // This means we are coming from back-office - ie. no page ID, but recurring. |
| 480 | // Ideally this information would be passed into the function clearly rather than guessing by convention. |
| 481 | $isEmailReceipt = TRUE; |
| 482 | } |
| 483 | |
| 484 | if ($isEmailReceipt) { |
| 485 | if ($pageID) { |
| 486 | $receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$pageID]) . '" <' . $value[$pageID]['receipt_from_email'] . '>'; |
| 487 | |
| 488 | $receiptFromName = $value[$pageID]['receipt_from_name']; |
| 489 | $receiptFromEmail = $value[$pageID]['receipt_from_email']; |
| 490 | } |
| 491 | else { |
| 492 | $domainValues = CRM_Core_BAO_Domain::getNameAndEmail(); |
| 493 | $receiptFrom = "$domainValues[0] <$domainValues[1]>"; |
| 494 | $receiptFromName = $domainValues[0]; |
| 495 | $receiptFromEmail = $domainValues[1]; |
| 496 | } |
| 497 | |
| 498 | list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID, FALSE); |
| 499 | $templatesParams = array( |
| 500 | 'groupName' => 'msg_tpl_workflow_contribution', |
| 501 | 'valueName' => 'contribution_recurring_notify', |
| 502 | 'contactId' => $contactID, |
| 503 | 'tplParams' => array( |
| 504 | 'recur_frequency_interval' => $recur->frequency_interval, |
| 505 | 'recur_frequency_unit' => $recur->frequency_unit, |
| 506 | 'recur_installments' => $recur->installments, |
| 507 | 'recur_start_date' => $recur->start_date, |
| 508 | 'recur_end_date' => $recur->end_date, |
| 509 | 'recur_amount' => $recur->amount, |
| 510 | 'recur_txnType' => $type, |
| 511 | 'displayName' => $displayName, |
| 512 | 'receipt_from_name' => $receiptFromName, |
| 513 | 'receipt_from_email' => $receiptFromEmail, |
| 514 | 'auto_renew_membership' => $autoRenewMembership, |
| 515 | ), |
| 516 | 'from' => $receiptFrom, |
| 517 | 'toName' => $displayName, |
| 518 | 'toEmail' => $email, |
| 519 | ); |
| 520 | //CRM-13811 |
| 521 | if ($pageID) { |
| 522 | $templatesParams['cc'] = CRM_Utils_Array::value('cc_receipt', $value[$pageID]); |
| 523 | $templatesParams['bcc'] = CRM_Utils_Array::value('bcc_receipt', $value[$pageID]); |
| 524 | } |
| 525 | if ($recur->id) { |
| 526 | // in some cases its just recurringNotify() thats called for the first time and these urls don't get set. |
| 527 | // like in PaypalPro, & therefore we set it here additionally. |
| 528 | $template = CRM_Core_Smarty::singleton(); |
| 529 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($recur->id, 'recur', 'obj'); |
| 530 | $url = $paymentProcessor->subscriptionURL($recur->id, 'recur'); |
| 531 | $template->assign('cancelSubscriptionUrl', $url); |
| 532 | |
| 533 | $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'billing'); |
| 534 | $template->assign('updateSubscriptionBillingUrl', $url); |
| 535 | |
| 536 | $url = $paymentProcessor->subscriptionURL($recur->id, 'recur', 'update'); |
| 537 | $template->assign('updateSubscriptionUrl', $url); |
| 538 | } |
| 539 | |
| 540 | list($sent) = CRM_Core_BAO_MessageTemplate::sendTemplate($templatesParams); |
| 541 | |
| 542 | if ($sent) { |
| 543 | CRM_Core_Error::debug_log_message('Success: mail sent for recurring notification.'); |
| 544 | } |
| 545 | else { |
| 546 | CRM_Core_Error::debug_log_message('Failure: mail not sent for recurring notification.'); |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Add the custom fields for contribution page (ie profile) |
| 553 | * |
| 554 | * @param int $gid |
| 555 | * Uf group id. |
| 556 | * @param string $name |
| 557 | * @param int $cid |
| 558 | * Contact id. |
| 559 | * @param $template |
| 560 | * @param array $params |
| 561 | * Params to build component whereclause. |
| 562 | * |
| 563 | * @param array $fieldTypes |
| 564 | */ |
| 565 | public static function buildCustomDisplay($gid, $name, $cid, &$template, &$params, $fieldTypes = array()) { |
| 566 | if ($gid) { |
| 567 | if (CRM_Core_BAO_UFGroup::filterUFGroups($gid, $cid)) { |
| 568 | $values = array(); |
| 569 | $groupTitle = NULL; |
| 570 | $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::VIEW, NULL, NULL, FALSE, NULL, FALSE, NULL, CRM_Core_Permission::CREATE, NULL); |
| 571 | foreach ($fields as $k => $v) { |
| 572 | if (!$groupTitle) { |
| 573 | $groupTitle = $v["groupTitle"]; |
| 574 | } |
| 575 | // suppress all file fields from display and formatting fields |
| 576 | if ( |
| 577 | CRM_Utils_Array::value('data_type', $v, '') == 'File' || |
| 578 | CRM_Utils_Array::value('name', $v, '') == 'image_URL' || |
| 579 | CRM_Utils_Array::value('field_type', $v) == 'Formatting' |
| 580 | ) { |
| 581 | unset($fields[$k]); |
| 582 | } |
| 583 | |
| 584 | if (!empty($fieldTypes) && (!in_array($v['field_type'], $fieldTypes))) { |
| 585 | unset($fields[$k]); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | if ($groupTitle) { |
| 590 | $template->assign($name . "_grouptitle", $groupTitle); |
| 591 | } |
| 592 | |
| 593 | CRM_Core_BAO_UFGroup::getValues($cid, $fields, $values, FALSE, $params); |
| 594 | |
| 595 | if (count($values)) { |
| 596 | $template->assign($name, $values); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Make a copy of a contribution page, including all the blocks in the page. |
| 604 | * |
| 605 | * @param int $id |
| 606 | * The contribution page id to copy. |
| 607 | * |
| 608 | * @return CRM_Contribute_DAO_ContributionPage |
| 609 | */ |
| 610 | public static function copy($id) { |
| 611 | $fieldsFix = array( |
| 612 | 'prefix' => array( |
| 613 | 'title' => ts('Copy of') . ' ', |
| 614 | ), |
| 615 | ); |
| 616 | $copy = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_ContributionPage', array( |
| 617 | 'id' => $id, |
| 618 | ), NULL, $fieldsFix); |
| 619 | |
| 620 | //copying all the blocks pertaining to the contribution page |
| 621 | $copyPledgeBlock = &CRM_Core_DAO::copyGeneric('CRM_Pledge_DAO_PledgeBlock', array( |
| 622 | 'entity_id' => $id, |
| 623 | 'entity_table' => 'civicrm_contribution_page', |
| 624 | ), array( |
| 625 | 'entity_id' => $copy->id, |
| 626 | )); |
| 627 | |
| 628 | $copyMembershipBlock = &CRM_Core_DAO::copyGeneric('CRM_Member_DAO_MembershipBlock', array( |
| 629 | 'entity_id' => $id, |
| 630 | 'entity_table' => 'civicrm_contribution_page', |
| 631 | ), array( |
| 632 | 'entity_id' => $copy->id, |
| 633 | )); |
| 634 | |
| 635 | $copyUFJoin = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_UFJoin', array( |
| 636 | 'entity_id' => $id, |
| 637 | 'entity_table' => 'civicrm_contribution_page', |
| 638 | ), array( |
| 639 | 'entity_id' => $copy->id, |
| 640 | )); |
| 641 | |
| 642 | $copyWidget = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Widget', array( |
| 643 | 'contribution_page_id' => $id, |
| 644 | ), array( |
| 645 | 'contribution_page_id' => $copy->id, |
| 646 | )); |
| 647 | |
| 648 | //copy price sets |
| 649 | CRM_Price_BAO_PriceSet::copyPriceSet('civicrm_contribution_page', $id, $copy->id); |
| 650 | |
| 651 | $copyTellFriend = &CRM_Core_DAO::copyGeneric('CRM_Friend_DAO_Friend', array( |
| 652 | 'entity_id' => $id, |
| 653 | 'entity_table' => 'civicrm_contribution_page', |
| 654 | ), array( |
| 655 | 'entity_id' => $copy->id, |
| 656 | )); |
| 657 | |
| 658 | $copyPersonalCampaignPages = &CRM_Core_DAO::copyGeneric('CRM_PCP_DAO_PCPBlock', array( |
| 659 | 'entity_id' => $id, |
| 660 | 'entity_table' => 'civicrm_contribution_page', |
| 661 | ), array( |
| 662 | 'entity_id' => $copy->id, |
| 663 | 'target_entity_id' => $copy->id, |
| 664 | )); |
| 665 | |
| 666 | $copyPremium = &CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_Premium', array( |
| 667 | 'entity_id' => $id, |
| 668 | 'entity_table' => 'civicrm_contribution_page', |
| 669 | ), array( |
| 670 | 'entity_id' => $copy->id, |
| 671 | )); |
| 672 | $premiumQuery = " |
| 673 | SELECT id |
| 674 | FROM civicrm_premiums |
| 675 | WHERE entity_table = 'civicrm_contribution_page' |
| 676 | AND entity_id ={$id}"; |
| 677 | |
| 678 | $premiumDao = CRM_Core_DAO::executeQuery($premiumQuery, CRM_Core_DAO::$_nullArray); |
| 679 | while ($premiumDao->fetch()) { |
| 680 | if ($premiumDao->id) { |
| 681 | CRM_Core_DAO::copyGeneric('CRM_Contribute_DAO_PremiumsProduct', array( |
| 682 | 'premiums_id' => $premiumDao->id, |
| 683 | ), array( |
| 684 | 'premiums_id' => $copyPremium->id, |
| 685 | )); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | $copy->save(); |
| 690 | |
| 691 | CRM_Utils_Hook::copy('ContributionPage', $copy); |
| 692 | |
| 693 | return $copy; |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * Get info for all sections enable/disable. |
| 698 | * |
| 699 | * @param array $contribPageIds |
| 700 | * @return array |
| 701 | * info regarding all sections. |
| 702 | */ |
| 703 | public static function getSectionInfo($contribPageIds = array()) { |
| 704 | $info = array(); |
| 705 | $whereClause = NULL; |
| 706 | if (is_array($contribPageIds) && !empty($contribPageIds)) { |
| 707 | $whereClause = 'WHERE civicrm_contribution_page.id IN ( ' . implode(', ', $contribPageIds) . ' )'; |
| 708 | } |
| 709 | |
| 710 | $sections = array( |
| 711 | 'settings', |
| 712 | 'amount', |
| 713 | 'membership', |
| 714 | 'custom', |
| 715 | 'thankyou', |
| 716 | 'friend', |
| 717 | 'pcp', |
| 718 | 'widget', |
| 719 | 'premium', |
| 720 | ); |
| 721 | $query = " |
| 722 | SELECT civicrm_contribution_page.id as id, |
| 723 | civicrm_contribution_page.financial_type_id as settings, |
| 724 | amount_block_is_active as amount, |
| 725 | civicrm_membership_block.id as membership, |
| 726 | civicrm_uf_join.id as custom, |
| 727 | civicrm_contribution_page.thankyou_title as thankyou, |
| 728 | civicrm_tell_friend.id as friend, |
| 729 | civicrm_pcp_block.id as pcp, |
| 730 | civicrm_contribution_widget.id as widget, |
| 731 | civicrm_premiums.id as premium |
| 732 | FROM civicrm_contribution_page |
| 733 | LEFT JOIN civicrm_membership_block ON ( civicrm_membership_block.entity_id = civicrm_contribution_page.id |
| 734 | AND civicrm_membership_block.entity_table = 'civicrm_contribution_page' |
| 735 | AND civicrm_membership_block.is_active = 1 ) |
| 736 | LEFT JOIN civicrm_uf_join ON ( civicrm_uf_join.entity_id = civicrm_contribution_page.id |
| 737 | AND civicrm_uf_join.entity_table = 'civicrm_contribution_page' |
| 738 | AND module = 'CiviContribute' |
| 739 | AND civicrm_uf_join.is_active = 1 ) |
| 740 | LEFT JOIN civicrm_tell_friend ON ( civicrm_tell_friend.entity_id = civicrm_contribution_page.id |
| 741 | AND civicrm_tell_friend.entity_table = 'civicrm_contribution_page' |
| 742 | AND civicrm_tell_friend.is_active = 1) |
| 743 | LEFT JOIN civicrm_pcp_block ON ( civicrm_pcp_block.entity_id = civicrm_contribution_page.id |
| 744 | AND civicrm_pcp_block.entity_table = 'civicrm_contribution_page' |
| 745 | AND civicrm_pcp_block.is_active = 1 ) |
| 746 | LEFT JOIN civicrm_contribution_widget ON ( civicrm_contribution_widget.contribution_page_id = civicrm_contribution_page.id |
| 747 | AND civicrm_contribution_widget.is_active = 1 ) |
| 748 | LEFT JOIN civicrm_premiums ON ( civicrm_premiums.entity_id = civicrm_contribution_page.id |
| 749 | AND civicrm_premiums.entity_table = 'civicrm_contribution_page' |
| 750 | AND civicrm_premiums.premiums_active = 1 ) |
| 751 | $whereClause"; |
| 752 | |
| 753 | $contributionPage = CRM_Core_DAO::executeQuery($query); |
| 754 | while ($contributionPage->fetch()) { |
| 755 | if (!isset($info[$contributionPage->id]) || !is_array($info[$contributionPage->id])) { |
| 756 | $info[$contributionPage->id] = array_fill_keys(array_values($sections), FALSE); |
| 757 | } |
| 758 | foreach ($sections as $section) { |
| 759 | if ($contributionPage->$section) { |
| 760 | $info[$contributionPage->id][$section] = TRUE; |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | return $info; |
| 766 | } |
| 767 | |
| 768 | /** |
| 769 | * Get options for a given field. |
| 770 | * @see CRM_Core_DAO::buildOptions |
| 771 | * |
| 772 | * @param string $fieldName |
| 773 | * @param string $context : @see CRM_Core_DAO::buildOptionsContext |
| 774 | * @param array $props : whatever is known about this dao object |
| 775 | * |
| 776 | * @return array|bool |
| 777 | */ |
| 778 | public static function buildOptions($fieldName, $context = NULL, $props = array()) { |
| 779 | $params = array(); |
| 780 | // Special logic for fields whose options depend on context or properties |
| 781 | switch ($fieldName) { |
| 782 | case 'financial_type_id': |
| 783 | // Fixme - this is going to ignore context, better to get conditions, add params, and call PseudoConstant::get |
| 784 | return CRM_Financial_BAO_FinancialType::getIncomeFinancialType(); |
| 785 | |
| 786 | break; |
| 787 | } |
| 788 | return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); |
| 789 | } |
| 790 | |
| 791 | /** |
| 792 | * Get or Set honor/on_behalf params for processing module_data or setting default values. |
| 793 | * |
| 794 | * @param array $params : |
| 795 | * @param bool $setDefault : If yes then returns array to used for setting default value afterward |
| 796 | * @param string $module : processing module_data for which module? e.g. soft_credit, on_behalf |
| 797 | * |
| 798 | * @return array|string |
| 799 | */ |
| 800 | public static function formatModuleData($params, $setDefault = FALSE, $module) { |
| 801 | $config = CRM_Core_Config::singleton(); |
| 802 | $json = $jsonDecode = NULL; |
| 803 | $domain = new CRM_Core_DAO_Domain(); |
| 804 | $domain->find(TRUE); |
| 805 | |
| 806 | $moduleDataFormat = array( |
| 807 | 'soft_credit' => array( |
| 808 | 1 => 'soft_credit_types', |
| 809 | 'multilingual' => array( |
| 810 | 'honor_block_title', |
| 811 | 'honor_block_text', |
| 812 | ), |
| 813 | ), |
| 814 | 'on_behalf' => array( |
| 815 | 1 => 'is_for_organization', |
| 816 | 'multilingual' => array( |
| 817 | 'for_organization', |
| 818 | ), |
| 819 | ), |
| 820 | ); |
| 821 | |
| 822 | //When we are fetching the honor params respecting both multi and mono lingual state |
| 823 | //and setting it to default param of Contribution Page's Main and Setting form |
| 824 | if ($setDefault) { |
| 825 | $jsonDecode = json_decode($params); |
| 826 | $jsonDecode = (array) $jsonDecode->$module; |
| 827 | if (!$domain->locales && !empty($jsonDecode['default'])) { |
| 828 | //monolingual state |
| 829 | $jsonDecode += (array) $jsonDecode['default']; |
| 830 | unset($jsonDecode['default']); |
| 831 | } |
| 832 | elseif (!empty($jsonDecode[$config->lcMessages])) { |
| 833 | //multilingual state |
| 834 | foreach ($jsonDecode[$config->lcMessages] as $column => $value) { |
| 835 | $jsonDecode[$column] = $value; |
| 836 | } |
| 837 | unset($jsonDecode[$config->lcMessages]); |
| 838 | } |
| 839 | return $jsonDecode; |
| 840 | } |
| 841 | |
| 842 | //check and handle multilingual honoree params |
| 843 | if (!$domain->locales) { |
| 844 | //if in singlelingual state simply return the array format |
| 845 | $json = array($module => NULL); |
| 846 | foreach ($moduleDataFormat[$module] as $key => $attribute) { |
| 847 | if ($key === 'multilingual') { |
| 848 | $json[$module]['default'] = array(); |
| 849 | foreach ($attribute as $attr) { |
| 850 | $json[$module]['default'][$attr] = $params[$attr]; |
| 851 | } |
| 852 | } |
| 853 | else { |
| 854 | $json[$module][$attribute] = $params[$attribute]; |
| 855 | } |
| 856 | } |
| 857 | $json = json_encode($json); |
| 858 | } |
| 859 | else { |
| 860 | //if in multilingual state then retrieve the module_data against this contribution and |
| 861 | //merge with earlier module_data json data to current so not to lose earlier multilingual module_data information |
| 862 | $json = array($module => NULL); |
| 863 | foreach ($moduleDataFormat[$module] as $key => $attribute) { |
| 864 | if ($key === 'multilingual') { |
| 865 | $json[$module][$config->lcMessages] = array(); |
| 866 | foreach ($attribute as $attr) { |
| 867 | $json[$module][$config->lcMessages][$attr] = $params[$attr]; |
| 868 | } |
| 869 | } |
| 870 | else { |
| 871 | $json[$module][$attribute] = $params[$attribute]; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | $ufJoinDAO = new CRM_Core_DAO_UFJoin(); |
| 876 | $ufJoinDAO->module = $module; |
| 877 | $ufJoinDAO->entity_id = $params['id']; |
| 878 | $ufJoinDAO->find(TRUE); |
| 879 | $jsonData = json_decode($ufJoinDAO->module_data); |
| 880 | if ($jsonData) { |
| 881 | $json[$module] = array_merge((array) $jsonData->$module, $json[$module]); |
| 882 | } |
| 883 | $json = json_encode($json); |
| 884 | } |
| 885 | return $json; |
| 886 | } |
| 887 | |
| 888 | /** |
| 889 | * Generate html for pdf in confirmation receipt email attachment. |
| 890 | * @param int $contributionId |
| 891 | * Contribution Page Id. |
| 892 | * @param int $userID |
| 893 | * Contact id for contributor. |
| 894 | * @return array |
| 895 | */ |
| 896 | public static function addInvoicePdfToEmail($contributionId, $userID) { |
| 897 | $contributionID = array($contributionId); |
| 898 | $contactId = array($userID); |
| 899 | $pdfParams = array( |
| 900 | 'output' => 'pdf_invoice', |
| 901 | 'forPage' => 'confirmpage', |
| 902 | ); |
| 903 | $pdfHtml = CRM_Contribute_Form_Task_Invoice::printPDF($contributionID, |
| 904 | $pdfParams, $contactId, CRM_Core_DAO::$_nullObject); |
| 905 | return $pdfHtml; |
| 906 | } |
| 907 | |
| 908 | /** |
| 909 | * Helper to determine if the page supports separate membership payments. |
| 910 | * |
| 911 | * @param int $id form id |
| 912 | * |
| 913 | * @return bool |
| 914 | * isSeparateMembershipPayment |
| 915 | */ |
| 916 | public static function getIsMembershipPayment($id) { |
| 917 | $membershipBlocks = civicrm_api3('membership_block', 'get', array( |
| 918 | 'entity_table' => 'civicrm_contribution_page', |
| 919 | 'entity_id' => $id, |
| 920 | 'sequential' => TRUE, |
| 921 | )); |
| 922 | if (!$membershipBlocks['count']) { |
| 923 | return FALSE; |
| 924 | } |
| 925 | return $membershipBlocks['values'][0]['is_separate_payment']; |
| 926 | } |
| 927 | |
| 928 | } |