Commit | Line | Data |
---|---|---|
0baed70b | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
0baed70b | 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 | | |
0baed70b | 9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
0baed70b | 16 | */ |
17 | ||
18 | /** | |
07f8d162 | 19 | * This class build form elements for select existing or create new soft block. |
0baed70b | 20 | */ |
21 | class CRM_Contribute_Form_SoftCredit { | |
22 | ||
23 | /** | |
fe482240 | 24 | * Function used to build form element for soft credit block. |
0baed70b | 25 | * |
ccec9d6b | 26 | * @param CRM_Core_Form $form |
0baed70b | 27 | * |
95cdcc0f | 28 | * @return \CRM_Core_Form |
0baed70b | 29 | */ |
00be9182 | 30 | public static function buildQuickForm(&$form) { |
8d4230cd | 31 | if (!empty($form->_honor_block_is_active)) { |
133e2c99 | 32 | $ufJoinDAO = new CRM_Core_DAO_UFJoin(); |
33 | $ufJoinDAO->module = 'soft_credit'; | |
34 | $ufJoinDAO->entity_id = $form->_id; | |
35 | if ($ufJoinDAO->find(TRUE)) { | |
e63910c5 | 36 | $jsonData = CRM_Contribute_BAO_ContributionPage::formatModuleData($ufJoinDAO->module_data, TRUE, 'soft_credit'); |
133e2c99 | 37 | if ($jsonData) { |
be2fb01f | 38 | foreach (['honor_block_title', 'honor_block_text'] as $name) { |
ba60f73e | 39 | $form->assign($name, $jsonData[$name]); |
40 | } | |
133e2c99 | 41 | |
42 | $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE); | |
133e2c99 | 43 | |
44 | // radio button for Honor Type | |
ba60f73e | 45 | foreach ($jsonData['soft_credit_types'] as $value) { |
ca5603d5 | 46 | $honorTypes[$value] = $softCreditTypes[$value]; |
133e2c99 | 47 | } |
ca5603d5 | 48 | $form->addRadio('soft_credit_type_id', NULL, $honorTypes, ['allowClear' => TRUE]); |
133e2c99 | 49 | } |
50 | } | |
874c9be7 | 51 | return $form; |
133e2c99 | 52 | } |
53 | ||
fff86967 | 54 | // by default generate 10 blocks |
e56e813e | 55 | $item_count = $form->_softCreditItemCount; |
17db9f82 | 56 | |
35729234 | 57 | $showSoftCreditRow = 2; |
1eb70457 | 58 | if ($form->getAction() & CRM_Core_Action::UPDATE) { |
7ccf8829 | 59 | $form->_softCreditInfo = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($form->_id, TRUE); |
133e2c99 | 60 | } |
cce065f9 | 61 | elseif (!empty($form->_pledgeID)) { |
133e2c99 | 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; | |
70 | } | |
71 | } | |
72 | } | |
73 | } | |
74 | ||
75 | if (property_exists($form, "_softCreditInfo")) { | |
35729234 KJ |
76 | if (!empty($form->_softCreditInfo['soft_credit'])) { |
77 | $showSoftCreditRow = count($form->_softCreditInfo['soft_credit']); | |
78 | $showSoftCreditRow++; | |
35729234 | 79 | } |
17db9f82 KJ |
80 | } |
81 | ||
82 | for ($rowNumber = 1; $rowNumber <= $item_count; $rowNumber++) { | |
be2fb01f | 83 | $form->addEntityRef("soft_credit_contact_id[{$rowNumber}]", ts('Contact'), ['create' => TRUE]); |
d4a9a18a | 84 | |
ccec9d6b | 85 | $form->addMoney("soft_credit_amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE); |
51fa20cb | 86 | |
be2fb01f | 87 | $form->addSelect("soft_credit_type[{$rowNumber}]", [ |
1330f57a SL |
88 | 'entity' => 'contribution_soft', |
89 | 'field' => 'soft_credit_type_id', | |
90 | 'label' => ts('Type'), | |
91 | ]); | |
d4a9a18a | 92 | if (!empty($form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id'])) { |
ccec9d6b | 93 | $form->add('hidden', "soft_credit_id[{$rowNumber}]", |
d4a9a18a KJ |
94 | $form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id']); |
95 | } | |
0baed70b | 96 | } |
0baed70b | 97 | |
03ad81ae AH |
98 | self::addPCPFields($form); |
99 | ||
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"), | |
be2fb01f | 104 | ['id' => 'sct_default_id'] |
03ad81ae AH |
105 | ); |
106 | } | |
107 | ||
108 | /** | |
109 | * Add PCP fields for the new contribution form and others. | |
110 | * | |
111 | * @param CRM_Core_Form &$form | |
112 | * The form being built. | |
113 | * @param string $suffix | |
114 | * A suffix to add to field names. | |
115 | */ | |
116 | public static function addPCPFields(&$form, $suffix = '') { | |
e41cebea | 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); | |
d4abe802 | 121 | // Fixme: Not a true entityRef field. Relies on PCP.js.tpl |
be2fb01f | 122 | $form->add('text', "pcp_made_through_id$suffix", ts('Credit to a Personal Campaign Page'), ['class' => 'twenty', 'placeholder' => ts('- select -')]); |
d4abe802 | 123 | // stores the label |
03ad81ae AH |
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)')); | |
e41cebea | 128 | } |
0baed70b | 129 | } |
dfbad3f7 KJ |
130 | |
131 | /** | |
fe482240 | 132 | * Function used to set defaults for soft credit block. |
03110609 EM |
133 | * |
134 | * @param $defaults | |
135 | * @param $form | |
dfbad3f7 | 136 | */ |
00be9182 | 137 | public static function setDefaultValues(&$defaults, &$form) { |
00a1afe2 | 138 | //Used to hide/unhide PCP and/or Soft-credit Panes |
139 | $noPCP = $noSoftCredit = TRUE; | |
35729234 | 140 | if (!empty($form->_softCreditInfo['soft_credit'])) { |
00a1afe2 | 141 | $noSoftCredit = FALSE; |
c7e376bf | 142 | foreach ($form->_softCreditInfo['soft_credit'] as $key => $value) { |
d90d1031 | 143 | $defaults["soft_credit_amount[$key]"] = CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($value['amount']); |
ccec9d6b | 144 | $defaults["soft_credit_contact_id[$key]"] = $value['contact_id']; |
51fa20cb | 145 | $defaults["soft_credit_type[$key]"] = $value['soft_credit_type']; |
35729234 KJ |
146 | } |
147 | } | |
b6545333 | 148 | if (!empty($form->_softCreditInfo['pcp_id'])) { |
00a1afe2 | 149 | $noPCP = FALSE; |
76ca3345 | 150 | $pcpInfo = $form->_softCreditInfo; |
9c1bc317 | 151 | $pcpId = $pcpInfo['pcp_id'] ?? NULL; |
dfbad3f7 KJ |
152 | $pcpTitle = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title'); |
153 | $contributionPageTitle = CRM_PCP_BAO_PCP::getPcpPageTitle($pcpId, 'contribute'); | |
d4a9a18a | 154 | $defaults['pcp_made_through'] = CRM_Utils_Array::value('sort_name', $pcpInfo) . " :: " . $pcpTitle . " :: " . $contributionPageTitle; |
9c1bc317 CW |
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; | |
dfbad3f7 | 159 | } |
00a1afe2 | 160 | |
161 | $form->assign('noSoftCredit', $noSoftCredit); | |
162 | $form->assign('noPCP', $noPCP); | |
dfbad3f7 | 163 | } |
ac0c89ed | 164 | |
165 | /** | |
fe482240 | 166 | * Global form rule. |
ac0c89ed | 167 | * |
014c4014 TO |
168 | * @param array $fields |
169 | * The input form values. | |
da6b46f4 EM |
170 | * |
171 | * @param $errors | |
172 | * @param $self | |
ac0c89ed | 173 | * |
a6c01b45 | 174 | * @return array |
16b10e64 | 175 | * Array of errors |
ac0c89ed | 176 | */ |
00be9182 | 177 | public static function formRule($fields, $errors, $self) { |
be2fb01f | 178 | $errors = []; |
ac0c89ed | 179 | |
180 | // if honor roll fields are populated but no PCP is selected | |
a7488080 | 181 | if (empty($fields['pcp_made_through_id'])) { |
8cc574cf | 182 | if (!empty($fields['pcp_display_in_roll']) || !empty($fields['pcp_roll_nickname']) || |
b99f3e96 | 183 | !empty($fields['pcp_personal_note']) |
ac0c89ed | 184 | ) { |
6bb107ce | 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.'); |
ac0c89ed | 186 | } |
187 | } | |
188 | ||
189 | if (!empty($fields['soft_credit_amount'])) { | |
ccec9d6b | 190 | $repeat = array_count_values($fields['soft_credit_contact_id']); |
ac0c89ed | 191 | foreach ($fields['soft_credit_amount'] as $key => $val) { |
ccec9d6b CW |
192 | if (!empty($fields['soft_credit_contact_id'][$key])) { |
193 | if ($repeat[$fields['soft_credit_contact_id'][$key]] > 1) { | |
f9d15320 | 194 | $errors["soft_credit_contact_id[$key]"] = ts('You cannot enter multiple soft credits for the same contact.'); |
ac0c89ed | 195 | } |
b326919b | 196 | if ($self->_action == CRM_Core_Action::ADD && $fields['soft_credit_amount'][$key] |
353ffa53 TO |
197 | && (CRM_Utils_Rule::cleanMoney($fields['soft_credit_amount'][$key]) > CRM_Utils_Rule::cleanMoney($fields['total_amount'])) |
198 | ) { | |
ac0c89ed | 199 | $errors["soft_credit_amount[$key]"] = ts('Soft credit amount cannot be more than the total amount.'); |
200 | } | |
201 | if (empty($fields['soft_credit_amount'][$key])) { | |
202 | $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.'); | |
203 | } | |
204 | } | |
205 | } | |
206 | } | |
1421174e | 207 | |
ac0c89ed | 208 | return $errors; |
209 | } | |
96025800 | 210 | |
0baed70b | 211 | } |