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 | ||
41 | /** | |
42 | * Function used to build form element for soft credit block | |
43 | * | |
44 | * @param object $form form object | |
45 | * @access public | |
46 | * | |
47 | * @return void | |
48 | */ | |
49 | static function buildQuickForm(&$form) { | |
8cc574cf | 50 | if ($form->_mode == 'live' && !empty($form->_values['honor_block_is_active'])) { |
133e2c99 | 51 | $ufJoinDAO = new CRM_Core_DAO_UFJoin(); |
52 | $ufJoinDAO->module = 'soft_credit'; | |
53 | $ufJoinDAO->entity_id = $form->_id; | |
54 | if ($ufJoinDAO->find(TRUE)) { | |
55 | $jsonData = json_decode($ufJoinDAO->module_data); | |
56 | if ($jsonData) { | |
57 | $form->assign('honor_block_title', $jsonData->soft_credit->honor_block_title); | |
58 | $form->assign('honor_block_text', $jsonData->soft_credit->honor_block_text); | |
59 | ||
60 | $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE); | |
61 | $extraOption = array('onclick' => "enableHonorType();"); | |
62 | ||
63 | // radio button for Honor Type | |
64 | foreach ($jsonData->soft_credit->soft_credit_types as $value) { | |
65 | $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value, $extraOption); | |
66 | } | |
67 | $form->addGroup($honorTypes, 'soft_credit_type_id', NULL); | |
68 | } | |
69 | } | |
70 | return $form; | |
71 | } | |
72 | ||
50046260 | 73 | $prefix = 'soft_credit_'; |
65377546 | 74 | // by default generate 5 blocks |
17db9f82 KJ |
75 | $item_count = 6; |
76 | ||
35729234 | 77 | $showSoftCreditRow = 2; |
c7e376bf | 78 | $showCreateNew = TRUE; |
17db9f82 | 79 | if ($form->_action & CRM_Core_Action::UPDATE) { |
7ccf8829 | 80 | $form->_softCreditInfo = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($form->_id, TRUE); |
133e2c99 | 81 | } |
82 | elseif ($form->_pledgeID) { | |
83 | //Check and select most recent completed contrubtion and use it to retrieve | |
84 | //soft-credit information to use as default for current pledge payment, CRM-13981 | |
85 | $pledgePayments = CRM_Pledge_BAO_PledgePayment::getPledgePayments($form->_pledgeID); | |
86 | foreach ($pledgePayments as $id => $record) { | |
87 | if ($record['contribution_id']) { | |
88 | $softCredits = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($record['contribution_id'], TRUE); | |
89 | if ($record['status'] == 'Completed' && count($softCredits) > 0) { | |
90 | $form->_softCreditInfo = $softCredits; | |
91 | } | |
92 | } | |
93 | } | |
94 | } | |
95 | ||
96 | if (property_exists($form, "_softCreditInfo")) { | |
35729234 KJ |
97 | if (!empty($form->_softCreditInfo['soft_credit'])) { |
98 | $showSoftCreditRow = count($form->_softCreditInfo['soft_credit']); | |
99 | $showSoftCreditRow++; | |
c7e376bf | 100 | $showCreateNew = FALSE; |
35729234 | 101 | } |
17db9f82 KJ |
102 | } |
103 | ||
104 | for ($rowNumber = 1; $rowNumber <= $item_count; $rowNumber++) { | |
50046260 | 105 | CRM_Contact_Form_NewContact::buildQuickForm($form, $rowNumber, NULL, FALSE, $prefix); |
d4a9a18a | 106 | |
5ee60152 | 107 | $form->addMoney("{$prefix}amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE); |
51fa20cb | 108 | |
109 | $form->add('select', "{$prefix}type[{$rowNumber}]", | |
110 | ts( 'Soft Credit Type' ), | |
111 | array( | |
112 | '' => ts('- select -')) + | |
113 | CRM_Core_OptionGroup::values("{$prefix}type", FALSE) | |
114 | ); | |
d4a9a18a KJ |
115 | if (!empty($form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id'])) { |
116 | $form->add('hidden', "{$prefix}id[{$rowNumber}]", | |
117 | $form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id']); | |
118 | } | |
0baed70b | 119 | } |
0baed70b | 120 | |
e41cebea | 121 | // CRM-7368 allow user to set or edit PCP link for contributions |
122 | $siteHasPCPs = CRM_Contribute_PseudoConstant::pcPage(); | |
123 | if (!CRM_Utils_Array::crmIsEmptyArray($siteHasPCPs)) { | |
124 | $form->assign('siteHasPCPs', 1); | |
125 | $pcpDataUrl = CRM_Utils_System::url('civicrm/ajax/rest', | |
126 | 'className=CRM_Contact_Page_AJAX&fnName=getPCPList&json=1&context=contact&reset=1', | |
127 | FALSE, NULL, FALSE | |
128 | ); | |
129 | $form->assign('pcpDataUrl', $pcpDataUrl); | |
130 | $form->addElement('text', 'pcp_made_through', ts('Credit to a Personal Campaign Page')); | |
131 | $form->addElement('hidden', 'pcp_made_through_id', '', array('id' => 'pcp_made_through_id')); | |
132 | $form->addElement('checkbox', 'pcp_display_in_roll', ts('Display in Honor Roll?'), NULL); | |
133 | $form->addElement('text', 'pcp_roll_nickname', ts('Name (for Honor Roll)')); | |
134 | $form->addElement('textarea', 'pcp_personal_note', ts('Personal Note (for Honor Roll)')); | |
135 | } | |
17db9f82 KJ |
136 | $form->assign('showSoftCreditRow', $showSoftCreditRow); |
137 | $form->assign('rowCount', $item_count); | |
35729234 | 138 | $form->assign('showCreateNew', $showCreateNew); |
51fa20cb | 139 | $form->addElement('hidden', 'sct_default_id', |
140 | CRM_Core_OptionGroup::getDefaultValue("{$prefix}type"), | |
141 | array('id' => 'sct_default_id') | |
142 | ); | |
81bc499e | 143 | |
17db9f82 | 144 | // Tell tpl to hide soft credit field if contribution is linked directly to a PCP Page |
a7488080 | 145 | if (!empty($form->_values['pcp_made_through_id'])) { |
0baed70b | 146 | $form->assign('pcpLinked', 1); |
147 | } | |
0baed70b | 148 | } |
dfbad3f7 KJ |
149 | |
150 | /** | |
151 | * Function used to set defaults for soft credit block | |
152 | */ | |
17db9f82 | 153 | static function setDefaultValues(&$defaults, &$form) { |
35729234 | 154 | if (!empty($form->_softCreditInfo['soft_credit'])) { |
c7e376bf | 155 | foreach ($form->_softCreditInfo['soft_credit'] as $key => $value) { |
0689c15c | 156 | $defaults["soft_credit_amount[$key]"] = CRM_Utils_Money::format($value['amount'], NULL, '%a'); |
d4a9a18a | 157 | $defaults["soft_credit_contact_select_id[$key]"] = $value['contact_id']; |
51fa20cb | 158 | $defaults["soft_credit_type[$key]"] = $value['soft_credit_type']; |
35729234 KJ |
159 | } |
160 | } | |
a7488080 | 161 | elseif (!empty($form->_softCreditInfo['pcp_id'])) { |
76ca3345 | 162 | $pcpInfo = $form->_softCreditInfo; |
17db9f82 | 163 | $pcpId = CRM_Utils_Array::value('pcp_id', $pcpInfo); |
dfbad3f7 KJ |
164 | $pcpTitle = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title'); |
165 | $contributionPageTitle = CRM_PCP_BAO_PCP::getPcpPageTitle($pcpId, 'contribute'); | |
d4a9a18a KJ |
166 | $defaults['pcp_made_through'] = CRM_Utils_Array::value('sort_name', $pcpInfo) . " :: " . $pcpTitle . " :: " . $contributionPageTitle; |
167 | $defaults['pcp_made_through_id'] = CRM_Utils_Array::value('pcp_id', $pcpInfo); | |
168 | $defaults['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $pcpInfo); | |
169 | $defaults['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcpInfo); | |
170 | $defaults['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcpInfo); | |
dfbad3f7 | 171 | } |
dfbad3f7 | 172 | } |
ac0c89ed | 173 | |
174 | /** | |
175 | * global form rule | |
176 | * | |
177 | * @param array $fields the input form values | |
178 | * | |
179 | * @return true if no errors, else array of errors | |
180 | * @access public | |
181 | * @static | |
182 | */ | |
b326919b | 183 | static function formRule($fields, $errors, $self) { |
ac0c89ed | 184 | $errors = array(); |
185 | ||
186 | // if honor roll fields are populated but no PCP is selected | |
a7488080 | 187 | if (empty($fields['pcp_made_through_id'])) { |
8cc574cf | 188 | if (!empty($fields['pcp_display_in_roll']) || !empty($fields['pcp_roll_nickname']) || |
ac0c89ed | 189 | CRM_Utils_Array::value('pcp_personal_note', $fields) |
190 | ) { | |
191 | $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.'); | |
192 | } | |
193 | } | |
194 | ||
195 | if (!empty($fields['soft_credit_amount'])) { | |
196 | $repeat = array_count_values($fields['soft_credit_contact_select_id']); | |
197 | foreach ($fields['soft_credit_amount'] as $key => $val) { | |
198 | if (!empty($fields['soft_credit_contact_select_id'][$key])) { | |
199 | if ($repeat[$fields['soft_credit_contact_select_id'][$key]] > 1) { | |
200 | $errors["soft_credit_contact_select_id[$key]"] = ts('You cannot enter multiple soft credits for the same contact.'); | |
201 | } | |
b326919b | 202 | if ($self->_action == CRM_Core_Action::ADD && $fields['soft_credit_amount'][$key] |
0689c15c | 203 | && (CRM_Utils_Rule::cleanMoney($fields['soft_credit_amount'][$key]) > CRM_Utils_Rule::cleanMoney($fields['total_amount']))) { |
ac0c89ed | 204 | $errors["soft_credit_amount[$key]"] = ts('Soft credit amount cannot be more than the total amount.'); |
205 | } | |
206 | if (empty($fields['soft_credit_amount'][$key])) { | |
207 | $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.'); | |
208 | } | |
209 | } | |
210 | } | |
211 | } | |
212 | return $errors; | |
213 | } | |
0baed70b | 214 | } |
215 |