Merge pull request #16330 from eileenmcnaughton/process_event
[civicrm-core.git] / CRM / Contribute / BAO / ContributionSoft.php
CommitLineData
a45a51e3 1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
a45a51e3 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
a45a51e3 9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
a45a51e3 16 */
17class CRM_Contribute_BAO_ContributionSoft extends CRM_Contribute_DAO_ContributionSoft {
18
8cb2c285 19 /**
fe482240 20 * Construct method.
8cb2c285 21 */
00be9182 22 public function __construct() {
a45a51e3 23 parent::__construct();
24 }
25
26 /**
fe482240 27 * Add contribution soft credit record.
a45a51e3 28 *
014c4014
TO
29 * @param array $params
30 * (reference ) an assoc array of name/value pairs.
a45a51e3 31 *
a6c01b45
CW
32 * @return object
33 * soft contribution of object that is added
a45a51e3 34 */
07a88eec 35 public static function add(&$params) {
2271eee8
JG
36 $hook = empty($params['id']) ? 'create' : 'edit';
37 CRM_Utils_Hook::pre($hook, 'ContributionSoft', CRM_Utils_Array::value('id', $params), $params);
38
a45a51e3 39 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
40 $contributionSoft->copyValues($params);
6dcc4d9d 41
42 // set currency for CRM-1496
43 if (!isset($contributionSoft->currency)) {
44 $config = CRM_Core_Config::singleton();
45 $contributionSoft->currency = $config->defaultCurrency;
46 }
2271eee8
JG
47 $result = $contributionSoft->save();
48 CRM_Utils_Hook::post($hook, 'ContributionSoft', $contributionSoft->id, $contributionSoft);
49 return $result;
a45a51e3 50 }
51
7a13735b 52 /**
53 * Process the soft contribution and/or link to personal campaign page.
54 *
55 * @param array $params
56 * @param object $contribution CRM_Contribute_DAO_Contribution
57 *
58 */
59 public static function processSoftContribution($params, $contribution) {
60 //retrieve existing soft-credit and pcp id(s) if any against $contribution
61 $softIDs = self::getSoftCreditIds($contribution->id);
62 $pcpId = self::getSoftCreditIds($contribution->id, TRUE);
63
64 if ($pcp = CRM_Utils_Array::value('pcp', $params)) {
65 $softParams = array();
66 $softParams['id'] = $pcpId ? $pcpId : NULL;
67 $softParams['contribution_id'] = $contribution->id;
68 $softParams['pcp_id'] = $pcp['pcp_made_through_id'];
69 $softParams['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP',
70 $pcp['pcp_made_through_id'], 'contact_id'
71 );
72 $softParams['currency'] = $contribution->currency;
73 $softParams['amount'] = $contribution->total_amount;
74 $softParams['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $pcp);
75 $softParams['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcp);
76 $softParams['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcp);
0571e734 77 $softParams['soft_credit_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', 'pcp');
7a13735b 78 $contributionSoft = self::add($softParams);
79 //Send notification to owner for PCP
80 if ($contributionSoft->pcp_id && empty($pcpId)) {
81 CRM_Contribute_Form_Contribution_Confirm::pcpNotifyOwner($contribution, $contributionSoft);
82 }
83 }
84 //Delete PCP against this contribution and create new on submitted PCP information
85 elseif (array_key_exists('pcp', $params) && $pcpId) {
fae69387 86 civicrm_api3('ContributionSoft', 'delete', array('id' => $pcpId));
7a13735b 87 }
88 if (isset($params['soft_credit'])) {
89 $softParams = $params['soft_credit'];
90 foreach ($softParams as $softParam) {
91 if (!empty($softIDs)) {
92 $key = key($softIDs);
93 $softParam['id'] = $softIDs[$key];
94 unset($softIDs[$key]);
95 }
96 $softParam['contribution_id'] = $contribution->id;
97 $softParam['currency'] = $contribution->currency;
98 //case during Contribution Import when we assign soft contribution amount as contribution's total_amount by default
99 if (empty($softParam['amount'])) {
100 $softParam['amount'] = $contribution->total_amount;
101 }
102 CRM_Contribute_BAO_ContributionSoft::add($softParam);
103 }
104
105 // delete any extra soft-credit while updating back-office contribution
106 foreach ((array) $softIDs as $softID) {
107 if (!in_array($softID, $params['soft_credit_ids'])) {
fae69387 108 civicrm_api3('ContributionSoft', 'delete', array('id' => $softID));
7a13735b 109 }
110 }
111 }
112 }
113
114 /**
115 * Function used to save pcp / soft credit entry.
116 *
117 * This is used by contribution and also event pcps
118 *
119 * @param array $params
120 * @param object $form
121 * Form object.
122 */
123 public static function formatSoftCreditParams(&$params, &$form) {
124 $pcp = $softParams = $softIDs = array();
125 if (!empty($params['pcp_made_through_id'])) {
126 $fields = array(
127 'pcp_made_through_id',
128 'pcp_display_in_roll',
129 'pcp_roll_nickname',
130 'pcp_personal_note',
131 );
132 foreach ($fields as $f) {
133 $pcp[$f] = CRM_Utils_Array::value($f, $params);
134 }
135 }
136
4779abb3 137 if (!empty($form->_values['honoree_profile_id']) && !empty($params['soft_credit_type_id'])) {
7a13735b 138 $honorId = NULL;
139
7f90383e 140 // @todo fix use of deprecated function.
0571e734 141 $contributionSoftParams['soft_credit_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', 'pcp');
7a13735b 142 //check if there is any duplicate contact
d977e221
AH
143 // honoree should never be the donor
144 $exceptKeys = array(
145 'contactID' => 0,
146 'onbehalf_contact_id' => 0,
147 );
148 $except = array_values(array_intersect_key($params, $exceptKeys));
7f90383e 149 $ids = CRM_Contact_BAO_Contact::getDuplicateContacts(
150 $params['honor'],
151 CRM_Core_BAO_UFGroup::getContactType($form->_values['honoree_profile_id']),
152 'Unsupervised',
153 $except,
154 FALSE
155 );
7a13735b 156 if (count($ids)) {
157 $honorId = CRM_Utils_Array::value(0, $ids);
158 }
159
db62d3a5 160 $null = [];
7a13735b 161 $honorId = CRM_Contact_BAO_Contact::createProfileContact(
db62d3a5 162 $params['honor'], $null,
7a13735b 163 $honorId, NULL,
4779abb3 164 $form->_values['honoree_profile_id']
7a13735b 165 );
166 $softParams[] = array(
167 'contact_id' => $honorId,
168 'soft_credit_type_id' => $params['soft_credit_type_id'],
169 );
170
171 if (CRM_Utils_Array::value('is_email_receipt', $form->_values)) {
172 $form->_values['honor'] = array(
173 'soft_credit_type' => CRM_Utils_Array::value(
174 $params['soft_credit_type_id'],
175 CRM_Core_OptionGroup::values("soft_credit_type")
176 ),
177 'honor_id' => $honorId,
4779abb3 178 'honor_profile_id' => $form->_values['honoree_profile_id'],
7a13735b 179 'honor_profile_values' => $params['honor'],
180 );
181 }
182 }
183 elseif (!empty($params['soft_credit_contact_id'])) {
184 //build soft credit params
185 foreach ($params['soft_credit_contact_id'] as $key => $val) {
186 if ($val && $params['soft_credit_amount'][$key]) {
187 $softParams[$key]['contact_id'] = $val;
188 $softParams[$key]['amount'] = CRM_Utils_Rule::cleanMoney($params['soft_credit_amount'][$key]);
189 $softParams[$key]['soft_credit_type_id'] = $params['soft_credit_type'][$key];
190 if (!empty($params['soft_credit_id'][$key])) {
191 $softIDs[] = $softParams[$key]['id'] = $params['soft_credit_id'][$key];
192 }
193 }
194 }
195 }
196
197 $params['pcp'] = !empty($pcp) ? $pcp : NULL;
198 $params['soft_credit'] = $softParams;
199 $params['soft_credit_ids'] = $softIDs;
200 }
201
a45a51e3 202 /**
fe482240 203 * Fetch object based on array of properties.
a45a51e3 204 *
014c4014
TO
205 * @param array $params
206 * (reference ) an assoc array of name/value pairs.
207 * @param array $defaults
208 * (reference ) an assoc array to hold the flattened values.
a45a51e3 209 *
16b10e64 210 * @return CRM_Contribute_BAO_ContributionSoft
a45a51e3 211 */
00be9182 212 public static function retrieve(&$params, &$defaults) {
a45a51e3 213 $contributionSoft = new CRM_Contribute_DAO_ContributionSoft();
214 $contributionSoft->copyValues($params);
215 if ($contributionSoft->find(TRUE)) {
216 CRM_Core_DAO::storeValues($contributionSoft, $defaults);
217 return $contributionSoft;
218 }
219 return NULL;
220 }
221
186c9c17 222 /**
100fef9d 223 * @param int $contact_id
186c9c17
EM
224 * @param int $isTest
225 *
226 * @return array
227 */
00be9182 228 public static function getSoftContributionTotals($contact_id, $isTest = 0) {
879fda37 229
971cdafb 230 $whereClause = "AND cc.cancel_date IS NULL";
879fda37 231
971cdafb 232 $query = "
8cb2c285
KJ
233 SELECT SUM(amount) as amount, AVG(total_amount) as average, cc.currency
234 FROM civicrm_contribution_soft ccs
235 LEFT JOIN civicrm_contribution cc ON ccs.contribution_id = cc.id
971cdafb 236 WHERE cc.is_test = %2 AND ccs.contact_id = %1 {$whereClause}
237 GROUP BY currency";
6dcc4d9d 238
874c9be7 239 $params = array(
353ffa53 240 1 => array($contact_id, 'Integer'),
389bcebf 241 2 => array($isTest, 'Integer'),
353ffa53 242 );
6dcc4d9d 243
244 $cs = CRM_Core_DAO::executeQuery($query, $params);
245
d9c8c3c8 246 $count = $countCancelled = 0;
971cdafb 247 $amount = $average = $cancelAmount = array();
6dcc4d9d 248
249 while ($cs->fetch()) {
250 if ($cs->amount > 0) {
251 $count++;
d9c8c3c8
PF
252 $amount[] = CRM_Utils_Money::format($cs->amount, $cs->currency);
253 $average[] = CRM_Utils_Money::format($cs->average, $cs->currency);
6dcc4d9d 254 }
255 }
879fda37 256
971cdafb 257 //to get cancel amount
258 $cancelAmountWhereClause = "AND cc.cancel_date IS NOT NULL";
259 $query = str_replace($whereClause, $cancelAmountWhereClause, $query);
260 $cancelAmountSQL = CRM_Core_DAO::executeQuery($query, $params);
261 while ($cancelAmountSQL->fetch()) {
262 if ($cancelAmountSQL->amount > 0) {
d9c8c3c8
PF
263 $countCancelled++;
264 $cancelAmount[] = CRM_Utils_Money::format($cancelAmountSQL->amount, $cancelAmountSQL->currency);
971cdafb 265 }
266 }
879fda37 267
d9c8c3c8 268 if ($count > 0 || $countCancelled > 0) {
91bb24a7 269 return array(
d9c8c3c8
PF
270 $count,
271 $countCancelled,
91bb24a7 272 implode(',&nbsp;', $amount),
6dcc4d9d 273 implode(',&nbsp;', $average),
971cdafb 274 implode(',&nbsp;', $cancelAmount),
6dcc4d9d 275 );
276 }
277 return array(0, 0);
278 }
279
280 /**
100fef9d 281 * Retrieve soft contributions for contribution record.
6dcc4d9d 282 *
100fef9d 283 * @param int $contributionID
014c4014
TO
284 * @param bool $all
285 * Include PCP data.
dd244018 286 *
a6c01b45 287 * @return array
16b10e64 288 * Array of soft contribution ids, amounts, and associated contact ids
6dcc4d9d 289 */
00be9182 290 public static function getSoftContribution($contributionID, $all = FALSE) {
5062a7b3
MWMC
291 $softContributionFields = self::getSoftCreditContributionFields([$contributionID], $all);
292 return isset($softContributionFields[$contributionID]) ? $softContributionFields[$contributionID] : [];
293 }
294
295 /**
296 * Retrieve soft contributions for an array of contribution records.
297 *
298 * @param array $contributionIDs
299 * @param bool $all
300 * Include PCP data.
301 *
302 * @return array
303 * Array of soft contribution ids, amounts, and associated contact ids
304 */
305 public static function getSoftCreditContributionFields($contributionIDs, $all = FALSE) {
306 $pcpFields = [
17db9f82 307 'pcp_id',
b6545333 308 'pcp_title',
17db9f82
KJ
309 'pcp_display_in_roll',
310 'pcp_roll_nickname',
311 'pcp_personal_note',
5062a7b3 312 ];
17db9f82 313
5062a7b3
MWMC
314 $query = "
315 SELECT ccs.id, pcp_id, ccs.contribution_id as contribution_id, cpcp.title as pcp_title, pcp_display_in_roll, pcp_roll_nickname, pcp_personal_note, ccs.currency as currency, amount, ccs.contact_id as contact_id, c.display_name, ccs.soft_credit_type_id
7ccf8829 316 FROM civicrm_contribution_soft ccs INNER JOIN civicrm_contact c on c.id = ccs.contact_id
b6545333 317 LEFT JOIN civicrm_pcp cpcp ON ccs.pcp_id = cpcp.id
5062a7b3
MWMC
318 WHERE contribution_id IN (%1)
319 ";
320 $queryParams = [1 => [implode(',', $contributionIDs), 'CommaSeparatedIntegers']];
321 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
7ccf8829 322
5062a7b3 323 $softContribution = $indexes = [];
7ccf8829 324 while ($dao->fetch()) {
b6545333 325 if ($dao->pcp_id) {
6dcc4d9d 326 if ($all) {
17db9f82 327 foreach ($pcpFields as $val) {
5062a7b3 328 $softContribution[$dao->contribution_id][$val] = $dao->$val;
6dcc4d9d 329 }
5062a7b3
MWMC
330 $softContribution[$dao->contribution_id]['pcp_soft_credit_to_name'] = $dao->display_name;
331 $softContribution[$dao->contribution_id]['pcp_soft_credit_to_id'] = $dao->contact_id;
6dcc4d9d 332 }
b6545333 333 }
334 else {
5062a7b3
MWMC
335 // Use a 1-based array because that's what this function returned before refactoring in https://github.com/civicrm/civicrm-core/pull/14747
336 $indexes[$dao->contribution_id] = isset($indexes[$dao->contribution_id]) ? $indexes[$dao->contribution_id] + 1 : 1;
337 $softContribution[$dao->contribution_id]['soft_credit'][$indexes[$dao->contribution_id]] = [
b6545333 338 'contact_id' => $dao->contact_id,
339 'soft_credit_id' => $dao->id,
340 'currency' => $dao->currency,
341 'amount' => $dao->amount,
342 'contact_name' => $dao->display_name,
343 'soft_credit_type' => $dao->soft_credit_type_id,
b7617307 344 'soft_credit_type_label' => CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $dao->soft_credit_type_id),
5062a7b3 345 ];
6dcc4d9d 346 }
347 }
7ccf8829 348
6dcc4d9d 349 return $softContribution;
350 }
351
186c9c17 352 /**
100fef9d 353 * @param int $contributionID
186c9c17
EM
354 * @param bool $isPCP
355 *
356 * @return array
357 */
874c9be7 358 public static function getSoftCreditIds($contributionID, $isPCP = FALSE) {
91bb24a7 359 $query = "
b6545333 360 SELECT id
2cfc0f58 361 FROM civicrm_contribution_soft
362 WHERE contribution_id = %1
363 ";
b6545333 364
365 if ($isPCP) {
366 $query .= " AND pcp_id IS NOT NULL";
367 }
1221efe9 368 else {
369 $query .= " AND pcp_id IS NULL";
370 }
2cfc0f58 371 $params = array(1 => array($contributionID, 'Integer'));
372
373 $dao = CRM_Core_DAO::executeQuery($query, $params);
374 $id = array();
7f880799 375 $type = '';
2cfc0f58 376 while ($dao->fetch()) {
b6545333 377 if ($isPCP) {
378 return $dao->id;
2cfc0f58 379 }
380 $id[] = $dao->id;
381 }
b6545333 382 return $id;
2cfc0f58 383 }
91bb24a7 384
dfe9afbf 385 /**
386 * Wrapper for ajax soft contribution selector.
387 *
388 * @param array $params
389 * Associated array for params.
390 *
391 * @return array
392 * Associated array of soft contributions
393 */
394 public static function getSoftContributionSelector($params) {
395 $isTest = 0;
396 if (!empty($params['isTest'])) {
397 $isTest = $params['isTest'];
398 }
399 // Format the params.
400 $params['offset'] = ($params['page'] - 1) * $params['rp'];
401 $params['rowCount'] = $params['rp'];
402 $params['sort'] = CRM_Utils_Array::value('sortBy', $params);
403 $contactId = $params['cid'];
404
405 $filter = NULL;
406 if ($params['context'] == 'membership' && !empty($params['entityID']) && $contactId) {
407 $filter = " AND cc.id IN (SELECT contribution_id FROM civicrm_membership_payment WHERE membership_id = {$params['entityID']})";
408 }
409
410 $softCreditList = self::getSoftContributionList($contactId, $filter, $isTest, $params);
411
412 $softCreditListDT = array();
413 $softCreditListDT['data'] = array_values($softCreditList);
414 $softCreditListDT['recordsTotal'] = $params['total'];
415 $softCreditListDT['recordsFiltered'] = $params['total'];
416
417 return $softCreditListDT;
418 }
419
6dcc4d9d 420 /**
91bb24a7 421 * Function to retrieve the list of soft contributions for given contact.
6dcc4d9d 422 *
014c4014
TO
423 * @param int $contact_id
424 * Contact id.
014c4014 425 * @param string $filter
389bcebf 426 * @param int $isTest
014c4014 427 * Additional filter criteria, later used in where clause.
dfe9afbf 428 * @param array $dTParams
dd244018
EM
429 *
430 * @return array
6dcc4d9d 431 */
dfe9afbf 432 public static function getSoftContributionList($contact_id, $filter = NULL, $isTest = 0, &$dTParams = NULL) {
433 $config = CRM_Core_Config::singleton();
434 $links = array(
435 CRM_Core_Action::VIEW => array(
436 'name' => ts('View'),
437 'url' => 'civicrm/contact/view/contribution',
438 'qs' => 'reset=1&id=%%contributionid%%&cid=%%contactId%%&action=view&context=contribution&selectedChild=contribute',
439 'title' => ts('View related contribution'),
440 ),
441 );
442 $orderBy = 'cc.receive_date DESC';
443 if (!empty($dTParams['sort'])) {
444 $orderBy = $dTParams['sort'];
445 }
446 $limit = '';
447 if (!empty($dTParams['rowCount']) && $dTParams['rowCount'] > 0) {
448 $limit = " LIMIT {$dTParams['offset']}, {$dTParams['rowCount']} ";
449 }
450 $softOgId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'soft_credit_type', 'id', 'name');
451 $statusOgId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'contribution_status', 'id', 'name');
452
8cb2c285 453 $query = '
dfe9afbf 454 SELECT SQL_CALC_FOUND_ROWS ccs.id, ccs.amount as amount,
8cb2c285
KJ
455 ccs.contribution_id,
456 ccs.pcp_id,
457 ccs.pcp_display_in_roll,
458 ccs.pcp_roll_nickname,
459 ccs.pcp_personal_note,
51fa20cb 460 ccs.soft_credit_type_id,
dfe9afbf 461 sov.label as sct_label,
8cb2c285
KJ
462 cc.receive_date,
463 cc.contact_id as contributor_id,
464 cc.contribution_status_id as contribution_status_id,
dfe9afbf 465 cov.label as contribution_status,
8cb2c285
KJ
466 cp.title as pcp_title,
467 cc.currency,
dfe9afbf 468 contact.display_name as contributor_name,
469 cct.name as financial_type
8cb2c285
KJ
470 FROM civicrm_contribution_soft ccs
471 LEFT JOIN civicrm_contribution cc
472 ON ccs.contribution_id = cc.id
473 LEFT JOIN civicrm_pcp cp
474 ON ccs.pcp_id = cp.id
475 LEFT JOIN civicrm_contact contact ON
476 ccs.contribution_id = cc.id AND cc.contact_id = contact.id
477 LEFT JOIN civicrm_financial_type cct ON cc.financial_type_id = cct.id
dfe9afbf 478 LEFT JOIN civicrm_option_value sov ON sov.option_group_id = %3 AND ccs.soft_credit_type_id = sov.value
479 LEFT JOIN civicrm_option_value cov ON cov.option_group_id = %4 AND cc.contribution_status_id = cov.value
91ef9be0 480 ';
481
482 $where = "
483 WHERE cc.is_test = %2 AND ccs.contact_id = %1";
484 if ($filter) {
485 $where .= $filter;
486 }
487
dfe9afbf 488 $query .= "{$where} ORDER BY {$orderBy} {$limit}";
8cb2c285
KJ
489
490 $params = array(
491 1 => array($contact_id, 'Integer'),
21dfd5f5 492 2 => array($isTest, 'Integer'),
dfe9afbf 493 3 => array($softOgId, 'Integer'),
494 4 => array($statusOgId, 'Integer'),
8cb2c285
KJ
495 );
496 $cs = CRM_Core_DAO::executeQuery($query, $params);
dfe9afbf 497
498 $dTParams['total'] = CRM_Core_DAO::singleValueQuery('SELECT FOUND_ROWS()');
8cb2c285 499 $result = array();
6dcc4d9d 500 while ($cs->fetch()) {
ff8242a8 501 $result[$cs->id]['amount'] = CRM_Utils_Money::format($cs->amount, $cs->currency);
6dcc4d9d 502 $result[$cs->id]['currency'] = $cs->currency;
503 $result[$cs->id]['contributor_id'] = $cs->contributor_id;
504 $result[$cs->id]['contribution_id'] = $cs->contribution_id;
dfe9afbf 505 $result[$cs->id]['contributor_name'] = CRM_Utils_System::href(
506 $cs->contributor_name,
507 'civicrm/contact/view',
508 "reset=1&cid={$cs->contributor_id}"
509 );
510 $result[$cs->id]['financial_type'] = $cs->financial_type;
511 $result[$cs->id]['receive_date'] = CRM_Utils_Date::customFormat($cs->receive_date, $config->dateformatDatetime);
6dcc4d9d 512 $result[$cs->id]['pcp_id'] = $cs->pcp_id;
dfe9afbf 513 $result[$cs->id]['pcp_title'] = ($cs->pcp_title) ? $cs->pcp_title : 'n/a';
6dcc4d9d 514 $result[$cs->id]['pcp_display_in_roll'] = $cs->pcp_display_in_roll;
515 $result[$cs->id]['pcp_roll_nickname'] = $cs->pcp_roll_nickname;
516 $result[$cs->id]['pcp_personal_note'] = $cs->pcp_personal_note;
dfe9afbf 517 $result[$cs->id]['contribution_status'] = $cs->contribution_status;
518 $result[$cs->id]['sct_label'] = $cs->sct_label;
519 $replace = array(
520 'contributionid' => $cs->contribution_id,
521 'contactId' => $cs->contributor_id,
522 );
523 $result[$cs->id]['links'] = CRM_Core_Action::formLink($links, NULL, $replace);
6dcc4d9d 524
525 if ($isTest) {
cd355351 526 $result[$cs->id]['contribution_status'] = CRM_Core_TestEntity::appendTestText($result[$cs->id]['contribution_status']);
6dcc4d9d 527 }
528 }
529 return $result;
530 }
1421174e 531
186c9c17 532 /**
d424ffde
CW
533 * Function to assign honor profile fields to template/form, if $honorId (as soft-credit's contact_id)
534 * is passed then whole honoreeprofile fields with title/value assoc array assigned or only honoreeName
535 * is assigned
536 *
c490a46a
CW
537 * @param CRM_Core_Form $form
538 * @param array $params
100fef9d 539 * @param int $honorId
186c9c17 540 */
4779abb3 541 public static function formatHonoreeProfileFields($form, $params, $honorId = NULL) {
542 if (empty($form->_values['honoree_profile_id'])) {
543 return;
544 }
545 $profileContactType = CRM_Core_BAO_UFGroup::getContactType($form->_values['honoree_profile_id']);
546 $profileFields = CRM_Core_BAO_UFGroup::getFields($form->_values['honoree_profile_id']);
1421174e 547 $honoreeProfileFields = $values = array();
8af73472 548 $honorName = NULL;
1421174e 549
550 if ($honorId) {
551 CRM_Core_BAO_UFGroup::getValues($honorId, $profileFields, $values, FALSE, $params);
8af73472 552 if (empty($params)) {
553 foreach ($profileFields as $name => $field) {
554 $title = $field['title'];
555 $params[$field['name']] = $values[$title];
556 }
557 }
1421174e 558 }
559
560 //remove name related fields and construct name string with prefix/suffix
561 //which will be later assigned to template
562 switch ($profileContactType) {
563 case 'Individual':
564 if (array_key_exists('prefix_id', $params)) {
565 $honorName = CRM_Utils_Array::value(CRM_Utils_Array::value('prefix_id', $params),
566 CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id')
567 );
568 unset($profileFields['prefix_id']);
569 }
874c9be7
TO
570 $honorName .= ' ' . $params['first_name'] . ' ' . $params['last_name'];
571 unset($profileFields['first_name']);
572 unset($profileFields['last_name']);
573 if (array_key_exists('suffix_id', $params)) {
574 $honorName .= ' ' . CRM_Utils_Array::value(CRM_Utils_Array::value('suffix_id', $params),
1421174e 575 CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id')
576 );
874c9be7
TO
577 unset($profileFields['suffix_id']);
578 }
579 break;
580
581 case 'Organization':
582 $honorName = $params['organization_name'];
583 unset($profileFields['organization_name']);
584 break;
585
586 case 'Household':
587 $honorName = $params['household_name'];
588 unset($profileFields['household_name']);
589 break;
1421174e 590 }
591
592 if ($honorId) {
593 $honoreeProfileFields['Name'] = $honorName;
594 foreach ($profileFields as $name => $field) {
595 $title = $field['title'];
596 $honoreeProfileFields[$title] = $values[$title];
597 }
598 $form->assign('honoreeProfile', $honoreeProfileFields);
599 }
600 else {
601 $form->assign('honorName', $honorName);
602 }
603 }
96025800 604
a45a51e3 605}