3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class build form elements for select existing or create new soft block.
21 class CRM_Contribute_Form_SoftCredit
{
24 * Function used to build form element for soft credit block.
26 * @param CRM_Core_Form $form
28 * @return \CRM_Core_Form
30 public static function buildQuickForm(&$form) {
31 if (!empty($form->_honor_block_is_active
)) {
32 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
33 $ufJoinDAO->module
= 'soft_credit';
34 $ufJoinDAO->entity_id
= $form->_id
;
35 if ($ufJoinDAO->find(TRUE)) {
36 $jsonData = CRM_Contribute_BAO_ContributionPage
::formatModuleData($ufJoinDAO->module_data
, TRUE, 'soft_credit');
38 foreach (['honor_block_title', 'honor_block_text'] as $name) {
39 $form->assign($name, $jsonData[$name]);
42 $softCreditTypes = CRM_Core_OptionGroup
::values("soft_credit_type", FALSE);
44 // radio button for Honor Type
45 foreach ($jsonData['soft_credit_types'] as $value) {
46 $honorTypes[$value] = $softCreditTypes[$value];
48 $form->addRadio('soft_credit_type_id', NULL, $honorTypes, ['allowClear' => TRUE]);
54 // by default generate 10 blocks
55 $item_count = $form->_softCreditItemCount
;
57 $showSoftCreditRow = 2;
58 if ($form->getAction() & CRM_Core_Action
::UPDATE
) {
59 $form->_softCreditInfo
= CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($form->_id
, TRUE);
61 elseif (!empty($form->_pledgeID
)) {
62 //Check and select most recent completed contrubtion and use it to retrieve
63 //soft-credit information to use as default for current pledge payment, CRM-13981
64 $pledgePayments = CRM_Pledge_BAO_PledgePayment
::getPledgePayments($form->_pledgeID
);
65 foreach ($pledgePayments as $id => $record) {
66 if ($record['contribution_id']) {
67 $softCredits = CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($record['contribution_id'], TRUE);
68 if ($record['status'] == 'Completed' && count($softCredits) > 0) {
69 $form->_softCreditInfo
= $softCredits;
75 if (property_exists($form, "_softCreditInfo")) {
76 if (!empty($form->_softCreditInfo
['soft_credit'])) {
77 $showSoftCreditRow = count($form->_softCreditInfo
['soft_credit']);
82 for ($rowNumber = 1; $rowNumber <= $item_count; $rowNumber++
) {
83 $form->addEntityRef("soft_credit_contact_id[{$rowNumber}]", ts('Contact'), ['create' => TRUE]);
85 $form->addMoney("soft_credit_amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE);
87 $form->addSelect("soft_credit_type[{$rowNumber}]", [
88 'entity' => 'contribution_soft',
89 'field' => 'soft_credit_type_id',
90 'label' => ts('Type'),
92 if (!empty($form->_softCreditInfo
['soft_credit'][$rowNumber]['soft_credit_id'])) {
93 $form->add('hidden', "soft_credit_id[{$rowNumber}]",
94 $form->_softCreditInfo
['soft_credit'][$rowNumber]['soft_credit_id']);
98 self
::addPCPFields($form);
100 $form->assign('showSoftCreditRow', $showSoftCreditRow);
101 $form->assign('rowCount', $item_count);
102 $form->addElement('hidden', 'sct_default_id',
103 CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"),
104 ['id' => 'sct_default_id']
109 * Add PCP fields for the new contribution form and others.
111 * @param CRM_Core_Form &$form
112 * The form being built.
113 * @param string $suffix
114 * A suffix to add to field names.
116 public static function addPCPFields(&$form, $suffix = '') {
117 // CRM-7368 allow user to set or edit PCP link for contributions
118 $siteHasPCPs = CRM_Contribute_PseudoConstant
::pcPage();
119 if (!CRM_Utils_Array
::crmIsEmptyArray($siteHasPCPs)) {
120 $form->assign('siteHasPCPs', 1);
121 // Fixme: Not a true entityRef field. Relies on PCP.js.tpl
122 $form->add('text', "pcp_made_through_id$suffix", ts('Credit to a Personal Campaign Page'), ['class' => 'twenty', 'placeholder' => ts('- select -')]);
124 $form->add('hidden', "pcp_made_through$suffix");
125 $form->addElement('checkbox', "pcp_display_in_roll$suffix", ts('Display in Honor Roll?'), NULL);
126 $form->addElement('text', "pcp_roll_nickname$suffix", ts('Name (for Honor Roll)'));
127 $form->addElement('textarea', "pcp_personal_note$suffix", ts('Personal Note (for Honor Roll)'));
132 * Function used to set defaults for soft credit block.
137 public static function setDefaultValues(&$defaults, &$form) {
138 //Used to hide/unhide PCP and/or Soft-credit Panes
139 $noPCP = $noSoftCredit = TRUE;
140 if (!empty($form->_softCreditInfo
['soft_credit'])) {
141 $noSoftCredit = FALSE;
142 foreach ($form->_softCreditInfo
['soft_credit'] as $key => $value) {
143 $defaults["soft_credit_amount[$key]"] = CRM_Utils_Money
::formatLocaleNumericRoundedForDefaultCurrency($value['amount']);
144 $defaults["soft_credit_contact_id[$key]"] = $value['contact_id'];
145 $defaults["soft_credit_type[$key]"] = $value['soft_credit_type'];
148 if (!empty($form->_softCreditInfo
['pcp_id'])) {
150 $pcpInfo = $form->_softCreditInfo
;
151 $pcpId = $pcpInfo['pcp_id'] ??
NULL;
152 $pcpTitle = CRM_Core_DAO
::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title');
153 $contributionPageTitle = CRM_PCP_BAO_PCP
::getPcpPageTitle($pcpId, 'contribute');
154 $defaults['pcp_made_through'] = CRM_Utils_Array
::value('sort_name', $pcpInfo) . " :: " . $pcpTitle . " :: " . $contributionPageTitle;
155 $defaults['pcp_made_through_id'] = $pcpInfo['pcp_id'] ??
NULL;
156 $defaults['pcp_display_in_roll'] = $pcpInfo['pcp_display_in_roll'] ??
NULL;
157 $defaults['pcp_roll_nickname'] = $pcpInfo['pcp_roll_nickname'] ??
NULL;
158 $defaults['pcp_personal_note'] = $pcpInfo['pcp_personal_note'] ??
NULL;
161 $form->assign('noSoftCredit', $noSoftCredit);
162 $form->assign('noPCP', $noPCP);
168 * @param array $fields
169 * The input form values.
177 public static function formRule($fields, $errors, $self) {
180 // if honor roll fields are populated but no PCP is selected
181 if (empty($fields['pcp_made_through_id'])) {
182 if (!empty($fields['pcp_display_in_roll']) ||
!empty($fields['pcp_roll_nickname']) ||
183 !empty($fields['pcp_personal_note'])
185 $errors['pcp_made_through_id'] = ts('Please select a Personal Campaign Page, OR uncheck Display in Honor Roll and clear both the Honor Roll Name and the Personal Note field.');
189 if (!empty($fields['soft_credit_amount'])) {
190 $repeat = array_count_values($fields['soft_credit_contact_id']);
191 foreach ($fields['soft_credit_amount'] as $key => $val) {
192 if (!empty($fields['soft_credit_contact_id'][$key])) {
193 if ($repeat[$fields['soft_credit_contact_id'][$key]] > 1) {
194 $errors["soft_credit_contact_id[$key]"] = ts('You cannot enter multiple soft credits for the same contact.');
196 if ($self->_action
== CRM_Core_Action
::ADD
&& $fields['soft_credit_amount'][$key]
197 && (CRM_Utils_Rule
::cleanMoney($fields['soft_credit_amount'][$key]) > CRM_Utils_Rule
::cleanMoney($fields['total_amount']))
199 $errors["soft_credit_amount[$key]"] = ts('Soft credit amount cannot be more than the total amount.');
201 if (empty($fields['soft_credit_amount'][$key])) {
202 $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.');