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