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