Commit | Line | Data |
---|---|---|
0baed70b | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
0baed70b | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
0baed70b | 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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
0baed70b | 32 | */ |
33 | ||
34 | /** | |
07f8d162 | 35 | * This class build form elements for select existing or create new soft block. |
0baed70b | 36 | */ |
37 | class CRM_Contribute_Form_SoftCredit { | |
38 | ||
39 | /** | |
fe482240 | 40 | * Function used to build form element for soft credit block. |
0baed70b | 41 | * |
ccec9d6b | 42 | * @param CRM_Core_Form $form |
0baed70b | 43 | * |
95cdcc0f | 44 | * @return \CRM_Core_Form |
0baed70b | 45 | */ |
00be9182 | 46 | public static function buildQuickForm(&$form) { |
8d4230cd | 47 | if (!empty($form->_honor_block_is_active)) { |
133e2c99 | 48 | $ufJoinDAO = new CRM_Core_DAO_UFJoin(); |
49 | $ufJoinDAO->module = 'soft_credit'; | |
50 | $ufJoinDAO->entity_id = $form->_id; | |
51 | if ($ufJoinDAO->find(TRUE)) { | |
e63910c5 | 52 | $jsonData = CRM_Contribute_BAO_ContributionPage::formatModuleData($ufJoinDAO->module_data, TRUE, 'soft_credit'); |
133e2c99 | 53 | if ($jsonData) { |
be2fb01f | 54 | foreach (['honor_block_title', 'honor_block_text'] as $name) { |
ba60f73e | 55 | $form->assign($name, $jsonData[$name]); |
56 | } | |
133e2c99 | 57 | |
58 | $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE); | |
133e2c99 | 59 | |
60 | // radio button for Honor Type | |
ba60f73e | 61 | foreach ($jsonData['soft_credit_types'] as $value) { |
4cda11c9 | 62 | $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value); |
133e2c99 | 63 | } |
4cda11c9 | 64 | $form->addGroup($honorTypes, 'soft_credit_type_id', NULL)->setAttribute('allowClear', TRUE); |
133e2c99 | 65 | } |
66 | } | |
874c9be7 | 67 | return $form; |
133e2c99 | 68 | } |
69 | ||
fff86967 | 70 | // by default generate 10 blocks |
e56e813e | 71 | $item_count = $form->_softCreditItemCount; |
17db9f82 | 72 | |
35729234 | 73 | $showSoftCreditRow = 2; |
1eb70457 | 74 | if ($form->getAction() & CRM_Core_Action::UPDATE) { |
7ccf8829 | 75 | $form->_softCreditInfo = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($form->_id, TRUE); |
133e2c99 | 76 | } |
cce065f9 | 77 | elseif (!empty($form->_pledgeID)) { |
133e2c99 | 78 | //Check and select most recent completed contrubtion and use it to retrieve |
79 | //soft-credit information to use as default for current pledge payment, CRM-13981 | |
80 | $pledgePayments = CRM_Pledge_BAO_PledgePayment::getPledgePayments($form->_pledgeID); | |
81 | foreach ($pledgePayments as $id => $record) { | |
82 | if ($record['contribution_id']) { | |
83 | $softCredits = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($record['contribution_id'], TRUE); | |
84 | if ($record['status'] == 'Completed' && count($softCredits) > 0) { | |
85 | $form->_softCreditInfo = $softCredits; | |
86 | } | |
87 | } | |
88 | } | |
89 | } | |
90 | ||
91 | if (property_exists($form, "_softCreditInfo")) { | |
35729234 KJ |
92 | if (!empty($form->_softCreditInfo['soft_credit'])) { |
93 | $showSoftCreditRow = count($form->_softCreditInfo['soft_credit']); | |
94 | $showSoftCreditRow++; | |
35729234 | 95 | } |
17db9f82 KJ |
96 | } |
97 | ||
98 | for ($rowNumber = 1; $rowNumber <= $item_count; $rowNumber++) { | |
be2fb01f | 99 | $form->addEntityRef("soft_credit_contact_id[{$rowNumber}]", ts('Contact'), ['create' => TRUE]); |
d4a9a18a | 100 | |
ccec9d6b | 101 | $form->addMoney("soft_credit_amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE); |
51fa20cb | 102 | |
be2fb01f | 103 | $form->addSelect("soft_credit_type[{$rowNumber}]", [ |
1330f57a SL |
104 | 'entity' => 'contribution_soft', |
105 | 'field' => 'soft_credit_type_id', | |
106 | 'label' => ts('Type'), | |
107 | ]); | |
d4a9a18a | 108 | if (!empty($form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id'])) { |
ccec9d6b | 109 | $form->add('hidden', "soft_credit_id[{$rowNumber}]", |
d4a9a18a KJ |
110 | $form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id']); |
111 | } | |
0baed70b | 112 | } |
0baed70b | 113 | |
03ad81ae AH |
114 | self::addPCPFields($form); |
115 | ||
116 | $form->assign('showSoftCreditRow', $showSoftCreditRow); | |
117 | $form->assign('rowCount', $item_count); | |
118 | $form->addElement('hidden', 'sct_default_id', | |
119 | CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), | |
be2fb01f | 120 | ['id' => 'sct_default_id'] |
03ad81ae AH |
121 | ); |
122 | } | |
123 | ||
124 | /** | |
125 | * Add PCP fields for the new contribution form and others. | |
126 | * | |
127 | * @param CRM_Core_Form &$form | |
128 | * The form being built. | |
129 | * @param string $suffix | |
130 | * A suffix to add to field names. | |
131 | */ | |
132 | public static function addPCPFields(&$form, $suffix = '') { | |
e41cebea | 133 | // CRM-7368 allow user to set or edit PCP link for contributions |
134 | $siteHasPCPs = CRM_Contribute_PseudoConstant::pcPage(); | |
135 | if (!CRM_Utils_Array::crmIsEmptyArray($siteHasPCPs)) { | |
136 | $form->assign('siteHasPCPs', 1); | |
d4abe802 | 137 | // Fixme: Not a true entityRef field. Relies on PCP.js.tpl |
be2fb01f | 138 | $form->add('text', "pcp_made_through_id$suffix", ts('Credit to a Personal Campaign Page'), ['class' => 'twenty', 'placeholder' => ts('- select -')]); |
d4abe802 | 139 | // stores the label |
03ad81ae AH |
140 | $form->add('hidden', "pcp_made_through$suffix"); |
141 | $form->addElement('checkbox', "pcp_display_in_roll$suffix", ts('Display in Honor Roll?'), NULL); | |
142 | $form->addElement('text', "pcp_roll_nickname$suffix", ts('Name (for Honor Roll)')); | |
143 | $form->addElement('textarea', "pcp_personal_note$suffix", ts('Personal Note (for Honor Roll)')); | |
e41cebea | 144 | } |
0baed70b | 145 | } |
dfbad3f7 KJ |
146 | |
147 | /** | |
fe482240 | 148 | * Function used to set defaults for soft credit block. |
03110609 EM |
149 | * |
150 | * @param $defaults | |
151 | * @param $form | |
dfbad3f7 | 152 | */ |
00be9182 | 153 | public static function setDefaultValues(&$defaults, &$form) { |
00a1afe2 | 154 | //Used to hide/unhide PCP and/or Soft-credit Panes |
155 | $noPCP = $noSoftCredit = TRUE; | |
35729234 | 156 | if (!empty($form->_softCreditInfo['soft_credit'])) { |
00a1afe2 | 157 | $noSoftCredit = FALSE; |
c7e376bf | 158 | foreach ($form->_softCreditInfo['soft_credit'] as $key => $value) { |
0689c15c | 159 | $defaults["soft_credit_amount[$key]"] = CRM_Utils_Money::format($value['amount'], NULL, '%a'); |
ccec9d6b | 160 | $defaults["soft_credit_contact_id[$key]"] = $value['contact_id']; |
51fa20cb | 161 | $defaults["soft_credit_type[$key]"] = $value['soft_credit_type']; |
35729234 KJ |
162 | } |
163 | } | |
b6545333 | 164 | if (!empty($form->_softCreditInfo['pcp_id'])) { |
00a1afe2 | 165 | $noPCP = FALSE; |
76ca3345 | 166 | $pcpInfo = $form->_softCreditInfo; |
17db9f82 | 167 | $pcpId = CRM_Utils_Array::value('pcp_id', $pcpInfo); |
dfbad3f7 KJ |
168 | $pcpTitle = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title'); |
169 | $contributionPageTitle = CRM_PCP_BAO_PCP::getPcpPageTitle($pcpId, 'contribute'); | |
d4a9a18a KJ |
170 | $defaults['pcp_made_through'] = CRM_Utils_Array::value('sort_name', $pcpInfo) . " :: " . $pcpTitle . " :: " . $contributionPageTitle; |
171 | $defaults['pcp_made_through_id'] = CRM_Utils_Array::value('pcp_id', $pcpInfo); | |
172 | $defaults['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $pcpInfo); | |
173 | $defaults['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcpInfo); | |
174 | $defaults['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcpInfo); | |
dfbad3f7 | 175 | } |
00a1afe2 | 176 | |
177 | $form->assign('noSoftCredit', $noSoftCredit); | |
178 | $form->assign('noPCP', $noPCP); | |
dfbad3f7 | 179 | } |
ac0c89ed | 180 | |
181 | /** | |
fe482240 | 182 | * Global form rule. |
ac0c89ed | 183 | * |
014c4014 TO |
184 | * @param array $fields |
185 | * The input form values. | |
da6b46f4 EM |
186 | * |
187 | * @param $errors | |
188 | * @param $self | |
ac0c89ed | 189 | * |
a6c01b45 | 190 | * @return array |
16b10e64 | 191 | * Array of errors |
ac0c89ed | 192 | */ |
00be9182 | 193 | public static function formRule($fields, $errors, $self) { |
be2fb01f | 194 | $errors = []; |
ac0c89ed | 195 | |
196 | // if honor roll fields are populated but no PCP is selected | |
a7488080 | 197 | if (empty($fields['pcp_made_through_id'])) { |
8cc574cf | 198 | if (!empty($fields['pcp_display_in_roll']) || !empty($fields['pcp_roll_nickname']) || |
ac0c89ed | 199 | CRM_Utils_Array::value('pcp_personal_note', $fields) |
200 | ) { | |
6bb107ce | 201 | $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 | 202 | } |
203 | } | |
204 | ||
205 | if (!empty($fields['soft_credit_amount'])) { | |
ccec9d6b | 206 | $repeat = array_count_values($fields['soft_credit_contact_id']); |
ac0c89ed | 207 | foreach ($fields['soft_credit_amount'] as $key => $val) { |
ccec9d6b CW |
208 | if (!empty($fields['soft_credit_contact_id'][$key])) { |
209 | if ($repeat[$fields['soft_credit_contact_id'][$key]] > 1) { | |
f9d15320 | 210 | $errors["soft_credit_contact_id[$key]"] = ts('You cannot enter multiple soft credits for the same contact.'); |
ac0c89ed | 211 | } |
b326919b | 212 | if ($self->_action == CRM_Core_Action::ADD && $fields['soft_credit_amount'][$key] |
353ffa53 TO |
213 | && (CRM_Utils_Rule::cleanMoney($fields['soft_credit_amount'][$key]) > CRM_Utils_Rule::cleanMoney($fields['total_amount'])) |
214 | ) { | |
ac0c89ed | 215 | $errors["soft_credit_amount[$key]"] = ts('Soft credit amount cannot be more than the total amount.'); |
216 | } | |
217 | if (empty($fields['soft_credit_amount'][$key])) { | |
218 | $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.'); | |
219 | } | |
220 | } | |
221 | } | |
222 | } | |
1421174e | 223 | |
ac0c89ed | 224 | return $errors; |
225 | } | |
96025800 | 226 | |
0baed70b | 227 | } |