bulk comment fix
[civicrm-core.git] / CRM / Contribute / Form / SoftCredit.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 to set variables up before form is built
43 *
44 * @param $form
45 *
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
70 /**
71 * Function used to build form element for soft credit block
72 *
73 * @param CRM_Core_Form $form
74 * @access public
75 *
76 * @return void
77 */
78 static function buildQuickForm(&$form) {
79 if ($form->_mode == 'live' && !empty($form->_honor_block_is_active)) {
80 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
81 $ufJoinDAO->module = 'soft_credit';
82 $ufJoinDAO->entity_id = $form->_id;
83 if ($ufJoinDAO->find(TRUE)) {
84 $jsonData = CRM_Contribute_BAO_ContributionPage::formatMultilingualHonorParams($ufJoinDAO->module_data, TRUE);
85 if ($jsonData) {
86 foreach (array('honor_block_title', 'honor_block_text') as $name) {
87 $form->assign($name, $jsonData[$name]);
88 }
89
90 $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE);
91 $extraOption = array('onclick' => "enableHonorType();");
92
93 // radio button for Honor Type
94 foreach ($jsonData['soft_credit_types'] as $value) {
95 $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value, $extraOption);
96 }
97 $form->addGroup($honorTypes, 'soft_credit_type_id', NULL);
98 }
99 }
100 return $form;
101 }
102
103 // by default generate 5 blocks
104 $item_count = 6;
105
106 $showSoftCreditRow = 2;
107 if ($form->getAction() & CRM_Core_Action::UPDATE) {
108 $form->_softCreditInfo = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($form->_id, TRUE);
109 }
110 elseif ($form->_pledgeID) {
111 //Check and select most recent completed contrubtion and use it to retrieve
112 //soft-credit information to use as default for current pledge payment, CRM-13981
113 $pledgePayments = CRM_Pledge_BAO_PledgePayment::getPledgePayments($form->_pledgeID);
114 foreach ($pledgePayments as $id => $record) {
115 if ($record['contribution_id']) {
116 $softCredits = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($record['contribution_id'], TRUE);
117 if ($record['status'] == 'Completed' && count($softCredits) > 0) {
118 $form->_softCreditInfo = $softCredits;
119 }
120 }
121 }
122 }
123
124 if (property_exists($form, "_softCreditInfo")) {
125 if (!empty($form->_softCreditInfo['soft_credit'])) {
126 $showSoftCreditRow = count($form->_softCreditInfo['soft_credit']);
127 $showSoftCreditRow++;
128 }
129 }
130
131 for ($rowNumber = 1; $rowNumber <= $item_count; $rowNumber++) {
132 $form->addEntityRef("soft_credit_contact_id[{$rowNumber}]", ts('Contact'), array('create' => TRUE));
133
134 $form->addMoney("soft_credit_amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE);
135
136 $form->addSelect("soft_credit_type[{$rowNumber}]", array('entity' => 'contribution_soft', 'field' => 'soft_credit_type_id', 'label' => ts('Type')));
137 if (!empty($form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id'])) {
138 $form->add('hidden', "soft_credit_id[{$rowNumber}]",
139 $form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id']);
140 }
141 }
142
143 // CRM-7368 allow user to set or edit PCP link for contributions
144 $siteHasPCPs = CRM_Contribute_PseudoConstant::pcPage();
145 if (!CRM_Utils_Array::crmIsEmptyArray($siteHasPCPs)) {
146 $form->assign('siteHasPCPs', 1);
147 $form->add('hidden', 'pcp_made_through'); // stores the label
148 $form->add('text', 'pcp_made_through_id', ts('Credit to a Personal Campaign Page'), array('class' => 'twenty'));
149 $form->addElement('checkbox', 'pcp_display_in_roll', ts('Display in Honor Roll?'), NULL);
150 $form->addElement('text', 'pcp_roll_nickname', ts('Name (for Honor Roll)'));
151 $form->addElement('textarea', 'pcp_personal_note', ts('Personal Note (for Honor Roll)'));
152 }
153 $form->assign('showSoftCreditRow', $showSoftCreditRow);
154 $form->assign('rowCount', $item_count);
155 $form->addElement('hidden', 'sct_default_id',
156 CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"),
157 array('id' => 'sct_default_id')
158 );
159 }
160
161 /**
162 * Function used to set defaults for soft credit block
163 */
164 static function setDefaultValues(&$defaults, &$form) {
165 //Used to hide/unhide PCP and/or Soft-credit Panes
166 $noPCP = $noSoftCredit = TRUE;
167 if (!empty($form->_softCreditInfo['soft_credit'])) {
168 $noSoftCredit = FALSE;
169 foreach ($form->_softCreditInfo['soft_credit'] as $key => $value) {
170 $defaults["soft_credit_amount[$key]"] = CRM_Utils_Money::format($value['amount'], NULL, '%a');
171 $defaults["soft_credit_contact_id[$key]"] = $value['contact_id'];
172 $defaults["soft_credit_type[$key]"] = $value['soft_credit_type'];
173 }
174 }
175 if (!empty($form->_softCreditInfo['pcp_id'])) {
176 $noPCP = FALSE;
177 $pcpInfo = $form->_softCreditInfo;
178 $pcpId = CRM_Utils_Array::value('pcp_id', $pcpInfo);
179 $pcpTitle = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title');
180 $contributionPageTitle = CRM_PCP_BAO_PCP::getPcpPageTitle($pcpId, 'contribute');
181 $defaults['pcp_made_through'] = CRM_Utils_Array::value('sort_name', $pcpInfo) . " :: " . $pcpTitle . " :: " . $contributionPageTitle;
182 $defaults['pcp_made_through_id'] = CRM_Utils_Array::value('pcp_id', $pcpInfo);
183 $defaults['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $pcpInfo);
184 $defaults['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcpInfo);
185 $defaults['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcpInfo);
186 }
187
188 $form->assign('noSoftCredit', $noSoftCredit);
189 $form->assign('noPCP', $noPCP);
190 }
191
192 /**
193 * global form rule
194 *
195 * @param array $fields the input form values
196 *
197 * @param $errors
198 * @param $self
199 *
200 * @return true if no errors, else array of errors
201 * @access public
202 * @static
203 */
204 static function formRule($fields, $errors, $self) {
205 $errors = array();
206
207 // if honor roll fields are populated but no PCP is selected
208 if (empty($fields['pcp_made_through_id'])) {
209 if (!empty($fields['pcp_display_in_roll']) || !empty($fields['pcp_roll_nickname']) ||
210 CRM_Utils_Array::value('pcp_personal_note', $fields)
211 ) {
212 $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.');
213 }
214 }
215
216 if (!empty($fields['soft_credit_amount'])) {
217 $repeat = array_count_values($fields['soft_credit_contact_id']);
218 foreach ($fields['soft_credit_amount'] as $key => $val) {
219 if (!empty($fields['soft_credit_contact_id'][$key])) {
220 if ($repeat[$fields['soft_credit_contact_id'][$key]] > 1) {
221 $errors["soft_credit_contact[$key]"] = ts('You cannot enter multiple soft credits for the same contact.');
222 }
223 if ($self->_action == CRM_Core_Action::ADD && $fields['soft_credit_amount'][$key]
224 && (CRM_Utils_Rule::cleanMoney($fields['soft_credit_amount'][$key]) > CRM_Utils_Rule::cleanMoney($fields['total_amount']))) {
225 $errors["soft_credit_amount[$key]"] = ts('Soft credit amount cannot be more than the total amount.');
226 }
227 if (empty($fields['soft_credit_amount'][$key])) {
228 $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.');
229 }
230 $contactType = CRM_Contact_BAO_Contact::getContactType($fields['soft_credit_contact_id'][$key]);
231 if ($self->_honoreeProfileType && $self->_honoreeProfileType != $contactType) {
232 $errors["soft_credit_contact[$key]"] = ts('Please choose a contact of type %1', array(1 => $self->_honoreeProfileType));
233 }
234 }
235 }
236 }
237
238 return $errors;
239 }
240 }
241