Commit | Line | Data |
---|---|---|
0baed70b | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
0baed70b | 5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
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 | * | |
71 | * @param object $form form object | |
72 | * @access public | |
73 | * | |
74 | * @return void | |
75 | */ | |
76 | static function buildQuickForm(&$form) { | |
8cc574cf | 77 | if ($form->_mode == 'live' && !empty($form->_values['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 | ||
50046260 | 100 | $prefix = 'soft_credit_'; |
65377546 | 101 | // by default generate 5 blocks |
17db9f82 KJ |
102 | $item_count = 6; |
103 | ||
35729234 | 104 | $showSoftCreditRow = 2; |
c7e376bf | 105 | $showCreateNew = TRUE; |
17db9f82 | 106 | if ($form->_action & CRM_Core_Action::UPDATE) { |
7ccf8829 | 107 | $form->_softCreditInfo = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($form->_id, TRUE); |
133e2c99 | 108 | } |
109 | elseif ($form->_pledgeID) { | |
110 | //Check and select most recent completed contrubtion and use it to retrieve | |
111 | //soft-credit information to use as default for current pledge payment, CRM-13981 | |
112 | $pledgePayments = CRM_Pledge_BAO_PledgePayment::getPledgePayments($form->_pledgeID); | |
113 | foreach ($pledgePayments as $id => $record) { | |
114 | if ($record['contribution_id']) { | |
115 | $softCredits = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($record['contribution_id'], TRUE); | |
116 | if ($record['status'] == 'Completed' && count($softCredits) > 0) { | |
117 | $form->_softCreditInfo = $softCredits; | |
118 | } | |
119 | } | |
120 | } | |
121 | } | |
122 | ||
123 | if (property_exists($form, "_softCreditInfo")) { | |
35729234 KJ |
124 | if (!empty($form->_softCreditInfo['soft_credit'])) { |
125 | $showSoftCreditRow = count($form->_softCreditInfo['soft_credit']); | |
126 | $showSoftCreditRow++; | |
c7e376bf | 127 | $showCreateNew = FALSE; |
35729234 | 128 | } |
17db9f82 KJ |
129 | } |
130 | ||
131 | for ($rowNumber = 1; $rowNumber <= $item_count; $rowNumber++) { | |
50046260 | 132 | CRM_Contact_Form_NewContact::buildQuickForm($form, $rowNumber, NULL, FALSE, $prefix); |
d4a9a18a | 133 | |
5ee60152 | 134 | $form->addMoney("{$prefix}amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE); |
51fa20cb | 135 | |
136 | $form->add('select', "{$prefix}type[{$rowNumber}]", | |
137 | ts( 'Soft Credit Type' ), | |
138 | array( | |
139 | '' => ts('- select -')) + | |
140 | CRM_Core_OptionGroup::values("{$prefix}type", FALSE) | |
141 | ); | |
d4a9a18a KJ |
142 | if (!empty($form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id'])) { |
143 | $form->add('hidden', "{$prefix}id[{$rowNumber}]", | |
144 | $form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id']); | |
145 | } | |
0baed70b | 146 | } |
0baed70b | 147 | |
e41cebea | 148 | // CRM-7368 allow user to set or edit PCP link for contributions |
149 | $siteHasPCPs = CRM_Contribute_PseudoConstant::pcPage(); | |
150 | if (!CRM_Utils_Array::crmIsEmptyArray($siteHasPCPs)) { | |
151 | $form->assign('siteHasPCPs', 1); | |
152 | $pcpDataUrl = CRM_Utils_System::url('civicrm/ajax/rest', | |
153 | 'className=CRM_Contact_Page_AJAX&fnName=getPCPList&json=1&context=contact&reset=1', | |
154 | FALSE, NULL, FALSE | |
155 | ); | |
156 | $form->assign('pcpDataUrl', $pcpDataUrl); | |
157 | $form->addElement('text', 'pcp_made_through', ts('Credit to a Personal Campaign Page')); | |
158 | $form->addElement('hidden', 'pcp_made_through_id', '', array('id' => 'pcp_made_through_id')); | |
159 | $form->addElement('checkbox', 'pcp_display_in_roll', ts('Display in Honor Roll?'), NULL); | |
160 | $form->addElement('text', 'pcp_roll_nickname', ts('Name (for Honor Roll)')); | |
161 | $form->addElement('textarea', 'pcp_personal_note', ts('Personal Note (for Honor Roll)')); | |
162 | } | |
17db9f82 KJ |
163 | $form->assign('showSoftCreditRow', $showSoftCreditRow); |
164 | $form->assign('rowCount', $item_count); | |
35729234 | 165 | $form->assign('showCreateNew', $showCreateNew); |
51fa20cb | 166 | $form->addElement('hidden', 'sct_default_id', |
167 | CRM_Core_OptionGroup::getDefaultValue("{$prefix}type"), | |
168 | array('id' => 'sct_default_id') | |
169 | ); | |
81bc499e | 170 | |
17db9f82 | 171 | // Tell tpl to hide soft credit field if contribution is linked directly to a PCP Page |
a7488080 | 172 | if (!empty($form->_values['pcp_made_through_id'])) { |
0baed70b | 173 | $form->assign('pcpLinked', 1); |
174 | } | |
0baed70b | 175 | } |
dfbad3f7 KJ |
176 | |
177 | /** | |
178 | * Function used to set defaults for soft credit block | |
179 | */ | |
17db9f82 | 180 | static function setDefaultValues(&$defaults, &$form) { |
35729234 | 181 | if (!empty($form->_softCreditInfo['soft_credit'])) { |
c7e376bf | 182 | foreach ($form->_softCreditInfo['soft_credit'] as $key => $value) { |
0689c15c | 183 | $defaults["soft_credit_amount[$key]"] = CRM_Utils_Money::format($value['amount'], NULL, '%a'); |
d4a9a18a | 184 | $defaults["soft_credit_contact_select_id[$key]"] = $value['contact_id']; |
51fa20cb | 185 | $defaults["soft_credit_type[$key]"] = $value['soft_credit_type']; |
35729234 KJ |
186 | } |
187 | } | |
a7488080 | 188 | elseif (!empty($form->_softCreditInfo['pcp_id'])) { |
76ca3345 | 189 | $pcpInfo = $form->_softCreditInfo; |
17db9f82 | 190 | $pcpId = CRM_Utils_Array::value('pcp_id', $pcpInfo); |
dfbad3f7 KJ |
191 | $pcpTitle = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title'); |
192 | $contributionPageTitle = CRM_PCP_BAO_PCP::getPcpPageTitle($pcpId, 'contribute'); | |
d4a9a18a KJ |
193 | $defaults['pcp_made_through'] = CRM_Utils_Array::value('sort_name', $pcpInfo) . " :: " . $pcpTitle . " :: " . $contributionPageTitle; |
194 | $defaults['pcp_made_through_id'] = CRM_Utils_Array::value('pcp_id', $pcpInfo); | |
195 | $defaults['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $pcpInfo); | |
196 | $defaults['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcpInfo); | |
197 | $defaults['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcpInfo); | |
dfbad3f7 | 198 | } |
dfbad3f7 | 199 | } |
ac0c89ed | 200 | |
201 | /** | |
202 | * global form rule | |
203 | * | |
204 | * @param array $fields the input form values | |
205 | * | |
206 | * @return true if no errors, else array of errors | |
207 | * @access public | |
208 | * @static | |
209 | */ | |
b326919b | 210 | 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 | ) { | |
218 | $errors['pcp_made_through'] = 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.'); | |
219 | } | |
220 | } | |
221 | ||
222 | if (!empty($fields['soft_credit_amount'])) { | |
223 | $repeat = array_count_values($fields['soft_credit_contact_select_id']); | |
224 | foreach ($fields['soft_credit_amount'] as $key => $val) { | |
225 | if (!empty($fields['soft_credit_contact_select_id'][$key])) { | |
226 | if ($repeat[$fields['soft_credit_contact_select_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] |
0689c15c | 230 | && (CRM_Utils_Rule::cleanMoney($fields['soft_credit_amount'][$key]) > CRM_Utils_Rule::cleanMoney($fields['total_amount']))) { |
ac0c89ed | 231 | $errors["soft_credit_amount[$key]"] = ts('Soft credit amount cannot be more than the total amount.'); |
232 | } | |
233 | if (empty($fields['soft_credit_amount'][$key])) { | |
234 | $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.'); | |
235 | } | |
1421174e | 236 | $contactType = CRM_Contact_BAO_Contact::getContactType($fields['soft_credit_contact_select_id'][$key]); |
237 | if ($self->_honoreeProfileType && $self->_honoreeProfileType != $contactType) { | |
238 | $errors["soft_credit_contact[$key]"] = ts('Please choose a contact of type %1', array(1 => $self->_honoreeProfileType)); | |
239 | } | |
ac0c89ed | 240 | } |
241 | } | |
242 | } | |
1421174e | 243 | |
ac0c89ed | 244 | return $errors; |
245 | } | |
0baed70b | 246 | } |
247 |