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 | * | |
dd244018 EM |
44 | * @param $form |
45 | * | |
1421174e | 46 | * @return void |
47 | * @access static | |
48 | */ | |
49 | static function preProcess(&$form) { | |
50 | $contriDAO = new CRM_Contribute_DAO_Contribution(); | |
51 | $contriDAO->id = $form->_id; | |
52 | $contriDAO->find(TRUE); | |
53 | if ($contriDAO->contribution_page_id) { | |
54 | $ufJoinParams = array( | |
55 | 'module' => 'soft_credit', | |
56 | 'entity_table' => 'civicrm_contribution_page', | |
57 | 'entity_id' => $contriDAO->contribution_page_id, | |
58 | ); | |
59 | $profileId = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); | |
60 | ||
61 | //check if any honree profile is enabled if yes then assign its profile type to $_honoreeProfileType | |
62 | // which will be used to constraint soft-credit contact type in formRule, CRM-13981 | |
63 | if ($profileId[0]) { | |
64 | $form->_honoreeProfileType = CRM_Core_BAO_UFGroup::getContactType($profileId[0]); | |
65 | } | |
66 | } | |
67 | } | |
68 | ||
69 | ||
0baed70b | 70 | /** |
71 | * Function used to build form element for soft credit block | |
72 | * | |
ccec9d6b | 73 | * @param CRM_Core_Form $form |
0baed70b | 74 | * @access public |
75 | * | |
76 | * @return void | |
77 | */ | |
78 | static function buildQuickForm(&$form) { | |
377fa510 | 79 | if ($form->_mode == 'live' && !empty($form->_honor_block_is_active)) { |
133e2c99 | 80 | $ufJoinDAO = new CRM_Core_DAO_UFJoin(); |
81 | $ufJoinDAO->module = 'soft_credit'; | |
82 | $ufJoinDAO->entity_id = $form->_id; | |
83 | if ($ufJoinDAO->find(TRUE)) { | |
ba60f73e | 84 | $jsonData = CRM_Contribute_BAO_ContributionPage::formatMultilingualHonorParams($ufJoinDAO->module_data, TRUE); |
133e2c99 | 85 | if ($jsonData) { |
ba60f73e | 86 | foreach (array('honor_block_title', 'honor_block_text') as $name) { |
87 | $form->assign($name, $jsonData[$name]); | |
88 | } | |
133e2c99 | 89 | |
90 | $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE); | |
133e2c99 | 91 | |
92 | // radio button for Honor Type | |
ba60f73e | 93 | foreach ($jsonData['soft_credit_types'] as $value) { |
4cda11c9 | 94 | $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value); |
133e2c99 | 95 | } |
4cda11c9 | 96 | $form->addGroup($honorTypes, 'soft_credit_type_id', NULL)->setAttribute('allowClear', TRUE); |
133e2c99 | 97 | } |
98 | } | |
99 | return $form; | |
100 | } | |
101 | ||
65377546 | 102 | // by default generate 5 blocks |
17db9f82 KJ |
103 | $item_count = 6; |
104 | ||
35729234 | 105 | $showSoftCreditRow = 2; |
1eb70457 | 106 | if ($form->getAction() & 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++; | |
35729234 | 127 | } |
17db9f82 KJ |
128 | } |
129 | ||
130 | for ($rowNumber = 1; $rowNumber <= $item_count; $rowNumber++) { | |
ccec9d6b | 131 | $form->addEntityRef("soft_credit_contact_id[{$rowNumber}]", ts('Contact'), array('create' => TRUE)); |
d4a9a18a | 132 | |
ccec9d6b | 133 | $form->addMoney("soft_credit_amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE); |
51fa20cb | 134 | |
ccec9d6b | 135 | $form->addSelect("soft_credit_type[{$rowNumber}]", array('entity' => 'contribution_soft', 'field' => 'soft_credit_type_id', 'label' => ts('Type'))); |
d4a9a18a | 136 | if (!empty($form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id'])) { |
ccec9d6b | 137 | $form->add('hidden', "soft_credit_id[{$rowNumber}]", |
d4a9a18a KJ |
138 | $form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id']); |
139 | } | |
0baed70b | 140 | } |
0baed70b | 141 | |
e41cebea | 142 | // CRM-7368 allow user to set or edit PCP link for contributions |
143 | $siteHasPCPs = CRM_Contribute_PseudoConstant::pcPage(); | |
144 | if (!CRM_Utils_Array::crmIsEmptyArray($siteHasPCPs)) { | |
145 | $form->assign('siteHasPCPs', 1); | |
6bb107ce CW |
146 | $form->add('hidden', 'pcp_made_through'); // stores the label |
147 | $form->add('text', 'pcp_made_through_id', ts('Credit to a Personal Campaign Page'), array('class' => 'twenty')); | |
e41cebea | 148 | $form->addElement('checkbox', 'pcp_display_in_roll', ts('Display in Honor Roll?'), NULL); |
149 | $form->addElement('text', 'pcp_roll_nickname', ts('Name (for Honor Roll)')); | |
150 | $form->addElement('textarea', 'pcp_personal_note', ts('Personal Note (for Honor Roll)')); | |
151 | } | |
17db9f82 KJ |
152 | $form->assign('showSoftCreditRow', $showSoftCreditRow); |
153 | $form->assign('rowCount', $item_count); | |
51fa20cb | 154 | $form->addElement('hidden', 'sct_default_id', |
ccec9d6b | 155 | CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), |
51fa20cb | 156 | array('id' => 'sct_default_id') |
157 | ); | |
0baed70b | 158 | } |
dfbad3f7 KJ |
159 | |
160 | /** | |
161 | * Function used to set defaults for soft credit block | |
162 | */ | |
17db9f82 | 163 | static function setDefaultValues(&$defaults, &$form) { |
00a1afe2 | 164 | //Used to hide/unhide PCP and/or Soft-credit Panes |
165 | $noPCP = $noSoftCredit = TRUE; | |
35729234 | 166 | if (!empty($form->_softCreditInfo['soft_credit'])) { |
00a1afe2 | 167 | $noSoftCredit = FALSE; |
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 | } | |
b6545333 | 174 | if (!empty($form->_softCreditInfo['pcp_id'])) { |
00a1afe2 | 175 | $noPCP = FALSE; |
76ca3345 | 176 | $pcpInfo = $form->_softCreditInfo; |
17db9f82 | 177 | $pcpId = CRM_Utils_Array::value('pcp_id', $pcpInfo); |
dfbad3f7 KJ |
178 | $pcpTitle = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title'); |
179 | $contributionPageTitle = CRM_PCP_BAO_PCP::getPcpPageTitle($pcpId, 'contribute'); | |
d4a9a18a KJ |
180 | $defaults['pcp_made_through'] = CRM_Utils_Array::value('sort_name', $pcpInfo) . " :: " . $pcpTitle . " :: " . $contributionPageTitle; |
181 | $defaults['pcp_made_through_id'] = CRM_Utils_Array::value('pcp_id', $pcpInfo); | |
182 | $defaults['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $pcpInfo); | |
183 | $defaults['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcpInfo); | |
184 | $defaults['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcpInfo); | |
dfbad3f7 | 185 | } |
00a1afe2 | 186 | |
187 | $form->assign('noSoftCredit', $noSoftCredit); | |
188 | $form->assign('noPCP', $noPCP); | |
dfbad3f7 | 189 | } |
ac0c89ed | 190 | |
191 | /** | |
192 | * global form rule | |
193 | * | |
da6b46f4 EM |
194 | * @param array $fields the input form values |
195 | * | |
196 | * @param $errors | |
197 | * @param $self | |
ac0c89ed | 198 | * |
d77a0a58 | 199 | * @return array of errors |
ac0c89ed | 200 | * @access public |
201 | * @static | |
202 | */ | |
b326919b | 203 | static function formRule($fields, $errors, $self) { |
ac0c89ed | 204 | $errors = array(); |
205 | ||
206 | // if honor roll fields are populated but no PCP is selected | |
a7488080 | 207 | if (empty($fields['pcp_made_through_id'])) { |
8cc574cf | 208 | if (!empty($fields['pcp_display_in_roll']) || !empty($fields['pcp_roll_nickname']) || |
ac0c89ed | 209 | CRM_Utils_Array::value('pcp_personal_note', $fields) |
210 | ) { | |
6bb107ce | 211 | $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 | 212 | } |
213 | } | |
214 | ||
215 | if (!empty($fields['soft_credit_amount'])) { | |
ccec9d6b | 216 | $repeat = array_count_values($fields['soft_credit_contact_id']); |
ac0c89ed | 217 | foreach ($fields['soft_credit_amount'] as $key => $val) { |
ccec9d6b CW |
218 | if (!empty($fields['soft_credit_contact_id'][$key])) { |
219 | if ($repeat[$fields['soft_credit_contact_id'][$key]] > 1) { | |
1421174e | 220 | $errors["soft_credit_contact[$key]"] = ts('You cannot enter multiple soft credits for the same contact.'); |
ac0c89ed | 221 | } |
b326919b | 222 | if ($self->_action == CRM_Core_Action::ADD && $fields['soft_credit_amount'][$key] |
0689c15c | 223 | && (CRM_Utils_Rule::cleanMoney($fields['soft_credit_amount'][$key]) > CRM_Utils_Rule::cleanMoney($fields['total_amount']))) { |
ac0c89ed | 224 | $errors["soft_credit_amount[$key]"] = ts('Soft credit amount cannot be more than the total amount.'); |
225 | } | |
226 | if (empty($fields['soft_credit_amount'][$key])) { | |
227 | $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.'); | |
228 | } | |
ccec9d6b | 229 | $contactType = CRM_Contact_BAO_Contact::getContactType($fields['soft_credit_contact_id'][$key]); |
1421174e | 230 | if ($self->_honoreeProfileType && $self->_honoreeProfileType != $contactType) { |
231 | $errors["soft_credit_contact[$key]"] = ts('Please choose a contact of type %1', array(1 => $self->_honoreeProfileType)); | |
232 | } | |
ac0c89ed | 233 | } |
234 | } | |
235 | } | |
1421174e | 236 | |
ac0c89ed | 237 | return $errors; |
238 | } | |
0baed70b | 239 | } |
240 |