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