Commit | Line | Data |
---|---|---|
0baed70b | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
0baed70b | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
0baed70b | 32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class build form elements for select exitsing or create new soft block | |
38 | */ | |
39 | class CRM_Contribute_Form_SoftCredit { | |
40 | ||
1421174e | 41 | /** |
42 | * Function to set variables up before form is built | |
43 | * | |
44 | * @return void | |
45 | * @access static | |
46 | */ | |
47 | static function preProcess(&$form) { | |
48 | $contriDAO = new CRM_Contribute_DAO_Contribution(); | |
49 | $contriDAO->id = $form->_id; | |
50 | $contriDAO->find(TRUE); | |
51 | if ($contriDAO->contribution_page_id) { | |
52 | $ufJoinParams = array( | |
53 | 'module' => 'soft_credit', | |
54 | 'entity_table' => 'civicrm_contribution_page', | |
55 | 'entity_id' => $contriDAO->contribution_page_id, | |
56 | ); | |
57 | $profileId = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); | |
58 | ||
59 | //check if any honree profile is enabled if yes then assign its profile type to $_honoreeProfileType | |
60 | // which will be used to constraint soft-credit contact type in formRule, CRM-13981 | |
61 | if ($profileId[0]) { | |
62 | $form->_honoreeProfileType = CRM_Core_BAO_UFGroup::getContactType($profileId[0]); | |
63 | } | |
64 | } | |
65 | } | |
66 | ||
67 | ||
0baed70b | 68 | /** |
69 | * Function used to build form element for soft credit block | |
70 | * | |
ccec9d6b | 71 | * @param CRM_Core_Form $form |
0baed70b | 72 | * @access public |
73 | * | |
74 | * @return void | |
75 | */ | |
76 | static function buildQuickForm(&$form) { | |
8af73472 | 77 | if ($form->_mode == 'live' && $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)) { | |
82 | $jsonData = json_decode($ufJoinDAO->module_data); | |
83 | if ($jsonData) { | |
84 | $form->assign('honor_block_title', $jsonData->soft_credit->honor_block_title); | |
85 | $form->assign('honor_block_text', $jsonData->soft_credit->honor_block_text); | |
86 | ||
87 | $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE); | |
88 | $extraOption = array('onclick' => "enableHonorType();"); | |
89 | ||
90 | // radio button for Honor Type | |
91 | foreach ($jsonData->soft_credit->soft_credit_types as $value) { | |
92 | $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value, $extraOption); | |
93 | } | |
94 | $form->addGroup($honorTypes, 'soft_credit_type_id', NULL); | |
95 | } | |
96 | } | |
97 | return $form; | |
98 | } | |
99 | ||
65377546 | 100 | // by default generate 5 blocks |
17db9f82 KJ |
101 | $item_count = 6; |
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 | } |
107 | elseif ($form->_pledgeID) { | |
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 | |
ccec9d6b | 133 | $form->addSelect("soft_credit_type[{$rowNumber}]", array('entity' => 'contribution_soft', 'field' => 'soft_credit_type_id', 'label' => ts('Type'))); |
d4a9a18a | 134 | if (!empty($form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id'])) { |
ccec9d6b | 135 | $form->add('hidden', "soft_credit_id[{$rowNumber}]", |
d4a9a18a KJ |
136 | $form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id']); |
137 | } | |
0baed70b | 138 | } |
0baed70b | 139 | |
e41cebea | 140 | // CRM-7368 allow user to set or edit PCP link for contributions |
141 | $siteHasPCPs = CRM_Contribute_PseudoConstant::pcPage(); | |
142 | if (!CRM_Utils_Array::crmIsEmptyArray($siteHasPCPs)) { | |
143 | $form->assign('siteHasPCPs', 1); | |
6bb107ce CW |
144 | $form->add('hidden', 'pcp_made_through'); // stores the label |
145 | $form->add('text', 'pcp_made_through_id', ts('Credit to a Personal Campaign Page'), array('class' => 'twenty')); | |
e41cebea | 146 | $form->addElement('checkbox', 'pcp_display_in_roll', ts('Display in Honor Roll?'), NULL); |
147 | $form->addElement('text', 'pcp_roll_nickname', ts('Name (for Honor Roll)')); | |
148 | $form->addElement('textarea', 'pcp_personal_note', ts('Personal Note (for Honor Roll)')); | |
149 | } | |
17db9f82 KJ |
150 | $form->assign('showSoftCreditRow', $showSoftCreditRow); |
151 | $form->assign('rowCount', $item_count); | |
51fa20cb | 152 | $form->addElement('hidden', 'sct_default_id', |
ccec9d6b | 153 | CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), |
51fa20cb | 154 | array('id' => 'sct_default_id') |
155 | ); | |
81bc499e | 156 | |
17db9f82 | 157 | // Tell tpl to hide soft credit field if contribution is linked directly to a PCP Page |
a7488080 | 158 | if (!empty($form->_values['pcp_made_through_id'])) { |
0baed70b | 159 | $form->assign('pcpLinked', 1); |
160 | } | |
0baed70b | 161 | } |
dfbad3f7 KJ |
162 | |
163 | /** | |
164 | * Function used to set defaults for soft credit block | |
165 | */ | |
17db9f82 | 166 | static function setDefaultValues(&$defaults, &$form) { |
35729234 | 167 | if (!empty($form->_softCreditInfo['soft_credit'])) { |
c7e376bf | 168 | foreach ($form->_softCreditInfo['soft_credit'] as $key => $value) { |
0689c15c | 169 | $defaults["soft_credit_amount[$key]"] = CRM_Utils_Money::format($value['amount'], NULL, '%a'); |
ccec9d6b | 170 | $defaults["soft_credit_contact_id[$key]"] = $value['contact_id']; |
51fa20cb | 171 | $defaults["soft_credit_type[$key]"] = $value['soft_credit_type']; |
35729234 KJ |
172 | } |
173 | } | |
a7488080 | 174 | elseif (!empty($form->_softCreditInfo['pcp_id'])) { |
76ca3345 | 175 | $pcpInfo = $form->_softCreditInfo; |
17db9f82 | 176 | $pcpId = CRM_Utils_Array::value('pcp_id', $pcpInfo); |
dfbad3f7 KJ |
177 | $pcpTitle = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title'); |
178 | $contributionPageTitle = CRM_PCP_BAO_PCP::getPcpPageTitle($pcpId, 'contribute'); | |
d4a9a18a KJ |
179 | $defaults['pcp_made_through'] = CRM_Utils_Array::value('sort_name', $pcpInfo) . " :: " . $pcpTitle . " :: " . $contributionPageTitle; |
180 | $defaults['pcp_made_through_id'] = CRM_Utils_Array::value('pcp_id', $pcpInfo); | |
181 | $defaults['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $pcpInfo); | |
182 | $defaults['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcpInfo); | |
183 | $defaults['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcpInfo); | |
dfbad3f7 | 184 | } |
dfbad3f7 | 185 | } |
ac0c89ed | 186 | |
187 | /** | |
188 | * global form rule | |
189 | * | |
190 | * @param array $fields the input form values | |
191 | * | |
192 | * @return true if no errors, else array of errors | |
193 | * @access public | |
194 | * @static | |
195 | */ | |
b326919b | 196 | static function formRule($fields, $errors, $self) { |
ac0c89ed | 197 | $errors = array(); |
198 | ||
199 | // if honor roll fields are populated but no PCP is selected | |
a7488080 | 200 | if (empty($fields['pcp_made_through_id'])) { |
8cc574cf | 201 | if (!empty($fields['pcp_display_in_roll']) || !empty($fields['pcp_roll_nickname']) || |
ac0c89ed | 202 | CRM_Utils_Array::value('pcp_personal_note', $fields) |
203 | ) { | |
6bb107ce | 204 | $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 | 205 | } |
206 | } | |
207 | ||
208 | if (!empty($fields['soft_credit_amount'])) { | |
ccec9d6b | 209 | $repeat = array_count_values($fields['soft_credit_contact_id']); |
ac0c89ed | 210 | foreach ($fields['soft_credit_amount'] as $key => $val) { |
ccec9d6b CW |
211 | if (!empty($fields['soft_credit_contact_id'][$key])) { |
212 | if ($repeat[$fields['soft_credit_contact_id'][$key]] > 1) { | |
1421174e | 213 | $errors["soft_credit_contact[$key]"] = ts('You cannot enter multiple soft credits for the same contact.'); |
ac0c89ed | 214 | } |
b326919b | 215 | if ($self->_action == CRM_Core_Action::ADD && $fields['soft_credit_amount'][$key] |
0689c15c | 216 | && (CRM_Utils_Rule::cleanMoney($fields['soft_credit_amount'][$key]) > CRM_Utils_Rule::cleanMoney($fields['total_amount']))) { |
ac0c89ed | 217 | $errors["soft_credit_amount[$key]"] = ts('Soft credit amount cannot be more than the total amount.'); |
218 | } | |
219 | if (empty($fields['soft_credit_amount'][$key])) { | |
220 | $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.'); | |
221 | } | |
ccec9d6b | 222 | $contactType = CRM_Contact_BAO_Contact::getContactType($fields['soft_credit_contact_id'][$key]); |
1421174e | 223 | if ($self->_honoreeProfileType && $self->_honoreeProfileType != $contactType) { |
224 | $errors["soft_credit_contact[$key]"] = ts('Please choose a contact of type %1', array(1 => $self->_honoreeProfileType)); | |
225 | } | |
ac0c89ed | 226 | } |
227 | } | |
228 | } | |
1421174e | 229 | |
ac0c89ed | 230 | return $errors; |
231 | } | |
0baed70b | 232 | } |
233 |