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