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