Fix CRM-20488: remove restrictions on soft credit contact type.
[civicrm-core.git] / CRM / Contribute / Form / SoftCredit.php
CommitLineData
0baed70b 1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
0baed70b 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
0baed70b 32 */
33
34/**
07f8d162 35 * This class build form elements for select existing or create new soft block.
0baed70b 36 */
37class CRM_Contribute_Form_SoftCredit {
38
1421174e 39 /**
fe482240 40 * Set variables up before form is built.
1421174e 41 *
c490a46a 42 * @param CRM_Core_Form $form
1421174e 43 */
00be9182 44 public static function preProcess(&$form) {
1421174e 45 $contriDAO = new CRM_Contribute_DAO_Contribution();
46 $contriDAO->id = $form->_id;
47 $contriDAO->find(TRUE);
48 if ($contriDAO->contribution_page_id) {
49 $ufJoinParams = array(
50 'module' => 'soft_credit',
51 'entity_table' => 'civicrm_contribution_page',
52 'entity_id' => $contriDAO->contribution_page_id,
53 );
54 $profileId = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
1421174e 55 }
56 }
57
0baed70b 58 /**
fe482240 59 * Function used to build form element for soft credit block.
0baed70b 60 *
ccec9d6b 61 * @param CRM_Core_Form $form
0baed70b 62 *
95cdcc0f 63 * @return \CRM_Core_Form
0baed70b 64 */
00be9182 65 public static function buildQuickForm(&$form) {
8d4230cd 66 if (!empty($form->_honor_block_is_active)) {
133e2c99 67 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
68 $ufJoinDAO->module = 'soft_credit';
69 $ufJoinDAO->entity_id = $form->_id;
70 if ($ufJoinDAO->find(TRUE)) {
e63910c5 71 $jsonData = CRM_Contribute_BAO_ContributionPage::formatModuleData($ufJoinDAO->module_data, TRUE, 'soft_credit');
133e2c99 72 if ($jsonData) {
ba60f73e 73 foreach (array('honor_block_title', 'honor_block_text') as $name) {
74 $form->assign($name, $jsonData[$name]);
75 }
133e2c99 76
77 $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE);
133e2c99 78
79 // radio button for Honor Type
ba60f73e 80 foreach ($jsonData['soft_credit_types'] as $value) {
4cda11c9 81 $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value);
133e2c99 82 }
4cda11c9 83 $form->addGroup($honorTypes, 'soft_credit_type_id', NULL)->setAttribute('allowClear', TRUE);
133e2c99 84 }
85 }
874c9be7 86 return $form;
133e2c99 87 }
88
fff86967
CW
89 // by default generate 10 blocks
90 $item_count = 11;
17db9f82 91
35729234 92 $showSoftCreditRow = 2;
1eb70457 93 if ($form->getAction() & CRM_Core_Action::UPDATE) {
7ccf8829 94 $form->_softCreditInfo = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($form->_id, TRUE);
133e2c99 95 }
cce065f9 96 elseif (!empty($form->_pledgeID)) {
133e2c99 97 //Check and select most recent completed contrubtion and use it to retrieve
98 //soft-credit information to use as default for current pledge payment, CRM-13981
99 $pledgePayments = CRM_Pledge_BAO_PledgePayment::getPledgePayments($form->_pledgeID);
100 foreach ($pledgePayments as $id => $record) {
101 if ($record['contribution_id']) {
102 $softCredits = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($record['contribution_id'], TRUE);
103 if ($record['status'] == 'Completed' && count($softCredits) > 0) {
104 $form->_softCreditInfo = $softCredits;
105 }
106 }
107 }
108 }
109
110 if (property_exists($form, "_softCreditInfo")) {
35729234
KJ
111 if (!empty($form->_softCreditInfo['soft_credit'])) {
112 $showSoftCreditRow = count($form->_softCreditInfo['soft_credit']);
113 $showSoftCreditRow++;
35729234 114 }
17db9f82
KJ
115 }
116
117 for ($rowNumber = 1; $rowNumber <= $item_count; $rowNumber++) {
ccec9d6b 118 $form->addEntityRef("soft_credit_contact_id[{$rowNumber}]", ts('Contact'), array('create' => TRUE));
d4a9a18a 119
ccec9d6b 120 $form->addMoney("soft_credit_amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE);
51fa20cb 121
353ffa53
TO
122 $form->addSelect("soft_credit_type[{$rowNumber}]", array(
123 'entity' => 'contribution_soft',
124 'field' => 'soft_credit_type_id',
a130e045 125 'label' => ts('Type'),
353ffa53 126 ));
d4a9a18a 127 if (!empty($form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id'])) {
ccec9d6b 128 $form->add('hidden', "soft_credit_id[{$rowNumber}]",
d4a9a18a
KJ
129 $form->_softCreditInfo['soft_credit'][$rowNumber]['soft_credit_id']);
130 }
0baed70b 131 }
0baed70b 132
03ad81ae
AH
133 self::addPCPFields($form);
134
135 $form->assign('showSoftCreditRow', $showSoftCreditRow);
136 $form->assign('rowCount', $item_count);
137 $form->addElement('hidden', 'sct_default_id',
138 CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"),
139 array('id' => 'sct_default_id')
140 );
141 }
142
143 /**
144 * Add PCP fields for the new contribution form and others.
145 *
146 * @param CRM_Core_Form &$form
147 * The form being built.
148 * @param string $suffix
149 * A suffix to add to field names.
150 */
151 public static function addPCPFields(&$form, $suffix = '') {
e41cebea 152 // CRM-7368 allow user to set or edit PCP link for contributions
153 $siteHasPCPs = CRM_Contribute_PseudoConstant::pcPage();
154 if (!CRM_Utils_Array::crmIsEmptyArray($siteHasPCPs)) {
155 $form->assign('siteHasPCPs', 1);
d4abe802 156 // Fixme: Not a true entityRef field. Relies on PCP.js.tpl
03ad81ae 157 $form->add('text', "pcp_made_through_id$suffix", ts('Credit to a Personal Campaign Page'), array('class' => 'twenty', 'placeholder' => ts('- select -')));
d4abe802 158 // stores the label
03ad81ae
AH
159 $form->add('hidden', "pcp_made_through$suffix");
160 $form->addElement('checkbox', "pcp_display_in_roll$suffix", ts('Display in Honor Roll?'), NULL);
161 $form->addElement('text', "pcp_roll_nickname$suffix", ts('Name (for Honor Roll)'));
162 $form->addElement('textarea', "pcp_personal_note$suffix", ts('Personal Note (for Honor Roll)'));
e41cebea 163 }
0baed70b 164 }
dfbad3f7
KJ
165
166 /**
fe482240 167 * Function used to set defaults for soft credit block.
03110609
EM
168 *
169 * @param $defaults
170 * @param $form
dfbad3f7 171 */
00be9182 172 public static function setDefaultValues(&$defaults, &$form) {
00a1afe2 173 //Used to hide/unhide PCP and/or Soft-credit Panes
174 $noPCP = $noSoftCredit = TRUE;
35729234 175 if (!empty($form->_softCreditInfo['soft_credit'])) {
00a1afe2 176 $noSoftCredit = FALSE;
c7e376bf 177 foreach ($form->_softCreditInfo['soft_credit'] as $key => $value) {
0689c15c 178 $defaults["soft_credit_amount[$key]"] = CRM_Utils_Money::format($value['amount'], NULL, '%a');
ccec9d6b 179 $defaults["soft_credit_contact_id[$key]"] = $value['contact_id'];
51fa20cb 180 $defaults["soft_credit_type[$key]"] = $value['soft_credit_type'];
35729234
KJ
181 }
182 }
b6545333 183 if (!empty($form->_softCreditInfo['pcp_id'])) {
00a1afe2 184 $noPCP = FALSE;
76ca3345 185 $pcpInfo = $form->_softCreditInfo;
17db9f82 186 $pcpId = CRM_Utils_Array::value('pcp_id', $pcpInfo);
dfbad3f7
KJ
187 $pcpTitle = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title');
188 $contributionPageTitle = CRM_PCP_BAO_PCP::getPcpPageTitle($pcpId, 'contribute');
d4a9a18a
KJ
189 $defaults['pcp_made_through'] = CRM_Utils_Array::value('sort_name', $pcpInfo) . " :: " . $pcpTitle . " :: " . $contributionPageTitle;
190 $defaults['pcp_made_through_id'] = CRM_Utils_Array::value('pcp_id', $pcpInfo);
191 $defaults['pcp_display_in_roll'] = CRM_Utils_Array::value('pcp_display_in_roll', $pcpInfo);
192 $defaults['pcp_roll_nickname'] = CRM_Utils_Array::value('pcp_roll_nickname', $pcpInfo);
193 $defaults['pcp_personal_note'] = CRM_Utils_Array::value('pcp_personal_note', $pcpInfo);
dfbad3f7 194 }
00a1afe2 195
196 $form->assign('noSoftCredit', $noSoftCredit);
197 $form->assign('noPCP', $noPCP);
dfbad3f7 198 }
ac0c89ed 199
200 /**
fe482240 201 * Global form rule.
ac0c89ed 202 *
014c4014
TO
203 * @param array $fields
204 * The input form values.
da6b46f4
EM
205 *
206 * @param $errors
207 * @param $self
ac0c89ed 208 *
a6c01b45 209 * @return array
16b10e64 210 * Array of errors
ac0c89ed 211 */
00be9182 212 public static function formRule($fields, $errors, $self) {
ac0c89ed 213 $errors = array();
214
215 // if honor roll fields are populated but no PCP is selected
a7488080 216 if (empty($fields['pcp_made_through_id'])) {
8cc574cf 217 if (!empty($fields['pcp_display_in_roll']) || !empty($fields['pcp_roll_nickname']) ||
ac0c89ed 218 CRM_Utils_Array::value('pcp_personal_note', $fields)
219 ) {
6bb107ce 220 $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 221 }
222 }
223
224 if (!empty($fields['soft_credit_amount'])) {
ccec9d6b 225 $repeat = array_count_values($fields['soft_credit_contact_id']);
ac0c89ed 226 foreach ($fields['soft_credit_amount'] as $key => $val) {
ccec9d6b
CW
227 if (!empty($fields['soft_credit_contact_id'][$key])) {
228 if ($repeat[$fields['soft_credit_contact_id'][$key]] > 1) {
f9d15320 229 $errors["soft_credit_contact_id[$key]"] = ts('You cannot enter multiple soft credits for the same contact.');
ac0c89ed 230 }
b326919b 231 if ($self->_action == CRM_Core_Action::ADD && $fields['soft_credit_amount'][$key]
353ffa53
TO
232 && (CRM_Utils_Rule::cleanMoney($fields['soft_credit_amount'][$key]) > CRM_Utils_Rule::cleanMoney($fields['total_amount']))
233 ) {
ac0c89ed 234 $errors["soft_credit_amount[$key]"] = ts('Soft credit amount cannot be more than the total amount.');
235 }
236 if (empty($fields['soft_credit_amount'][$key])) {
237 $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.');
238 }
239 }
240 }
241 }
1421174e 242
ac0c89ed 243 return $errors;
244 }
96025800 245
0baed70b 246}