3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 * This class build form elements for select existing or create new soft block.
37 class CRM_Contribute_Form_SoftCredit
{
40 * Set variables up before form is built.
42 * @param CRM_Core_Form $form
44 public static function preProcess(&$form) {
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
,
54 $profileId = CRM_Core_BAO_UFJoin
::getUFGroupIds($ufJoinParams);
56 //check if any honree profile is enabled if yes then assign its profile type to $_honoreeProfileType
57 // which will be used to constraint soft-credit contact type in formRule, CRM-13981
59 $form->_honoreeProfileType
= CRM_Core_BAO_UFGroup
::getContactType($profileId[0]);
65 * Function used to build form element for soft credit block.
67 * @param CRM_Core_Form $form
69 * @return \CRM_Core_Form
71 public static function buildQuickForm(&$form) {
72 if (!empty($form->_honor_block_is_active
)) {
73 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
74 $ufJoinDAO->module
= 'soft_credit';
75 $ufJoinDAO->entity_id
= $form->_id
;
76 if ($ufJoinDAO->find(TRUE)) {
77 $jsonData = CRM_Contribute_BAO_ContributionPage
::formatModuleData($ufJoinDAO->module_data
, TRUE, 'soft_credit');
79 foreach (array('honor_block_title', 'honor_block_text') as $name) {
80 $form->assign($name, $jsonData[$name]);
83 $softCreditTypes = CRM_Core_OptionGroup
::values("soft_credit_type", FALSE);
85 // radio button for Honor Type
86 foreach ($jsonData['soft_credit_types'] as $value) {
87 $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value);
89 $form->addGroup($honorTypes, 'soft_credit_type_id', NULL)->setAttribute('allowClear', TRUE);
95 // by default generate 10 blocks
98 $showSoftCreditRow = 2;
99 if ($form->getAction() & CRM_Core_Action
::UPDATE
) {
100 $form->_softCreditInfo
= CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($form->_id
, TRUE);
102 elseif (!empty($form->_pledgeID
)) {
103 //Check and select most recent completed contrubtion and use it to retrieve
104 //soft-credit information to use as default for current pledge payment, CRM-13981
105 $pledgePayments = CRM_Pledge_BAO_PledgePayment
::getPledgePayments($form->_pledgeID
);
106 foreach ($pledgePayments as $id => $record) {
107 if ($record['contribution_id']) {
108 $softCredits = CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($record['contribution_id'], TRUE);
109 if ($record['status'] == 'Completed' && count($softCredits) > 0) {
110 $form->_softCreditInfo
= $softCredits;
116 if (property_exists($form, "_softCreditInfo")) {
117 if (!empty($form->_softCreditInfo
['soft_credit'])) {
118 $showSoftCreditRow = count($form->_softCreditInfo
['soft_credit']);
119 $showSoftCreditRow++
;
123 for ($rowNumber = 1; $rowNumber <= $item_count; $rowNumber++
) {
124 $form->addEntityRef("soft_credit_contact_id[{$rowNumber}]", ts('Contact'), array('create' => TRUE));
126 $form->addMoney("soft_credit_amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE);
128 $form->addSelect("soft_credit_type[{$rowNumber}]", array(
129 'entity' => 'contribution_soft',
130 'field' => 'soft_credit_type_id',
131 'label' => ts('Type'),
133 if (!empty($form->_softCreditInfo
['soft_credit'][$rowNumber]['soft_credit_id'])) {
134 $form->add('hidden', "soft_credit_id[{$rowNumber}]",
135 $form->_softCreditInfo
['soft_credit'][$rowNumber]['soft_credit_id']);
139 self
::addPCPFields($form);
141 $form->assign('showSoftCreditRow', $showSoftCreditRow);
142 $form->assign('rowCount', $item_count);
143 $form->addElement('hidden', 'sct_default_id',
144 CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"),
145 array('id' => 'sct_default_id')
150 * Add PCP fields for the new contribution form and others.
152 * @param CRM_Core_Form &$form
153 * The form being built.
154 * @param string $suffix
155 * A suffix to add to field names.
157 public static function addPCPFields(&$form, $suffix = '') {
158 // CRM-7368 allow user to set or edit PCP link for contributions
159 $siteHasPCPs = CRM_Contribute_PseudoConstant
::pcPage();
160 if (!CRM_Utils_Array
::crmIsEmptyArray($siteHasPCPs)) {
161 $form->assign('siteHasPCPs', 1);
162 // Fixme: Not a true entityRef field. Relies on PCP.js.tpl
163 $form->add('text', "pcp_made_through_id$suffix", ts('Credit to a Personal Campaign Page'), array('class' => 'twenty', 'placeholder' => ts('- select -')));
165 $form->add('hidden', "pcp_made_through$suffix");
166 $form->addElement('checkbox', "pcp_display_in_roll$suffix", ts('Display in Honor Roll?'), NULL);
167 $form->addElement('text', "pcp_roll_nickname$suffix", ts('Name (for Honor Roll)'));
168 $form->addElement('textarea', "pcp_personal_note$suffix", ts('Personal Note (for Honor Roll)'));
173 * Function used to set defaults for soft credit block.
178 public static function setDefaultValues(&$defaults, &$form) {
179 //Used to hide/unhide PCP and/or Soft-credit Panes
180 $noPCP = $noSoftCredit = TRUE;
181 if (!empty($form->_softCreditInfo
['soft_credit'])) {
182 $noSoftCredit = FALSE;
183 foreach ($form->_softCreditInfo
['soft_credit'] as $key => $value) {
184 $defaults["soft_credit_amount[$key]"] = CRM_Utils_Money
::format($value['amount'], NULL, '%a');
185 $defaults["soft_credit_contact_id[$key]"] = $value['contact_id'];
186 $defaults["soft_credit_type[$key]"] = $value['soft_credit_type'];
189 if (!empty($form->_softCreditInfo
['pcp_id'])) {
191 $pcpInfo = $form->_softCreditInfo
;
192 $pcpId = CRM_Utils_Array
::value('pcp_id', $pcpInfo);
193 $pcpTitle = CRM_Core_DAO
::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title');
194 $contributionPageTitle = CRM_PCP_BAO_PCP
::getPcpPageTitle($pcpId, 'contribute');
195 $defaults['pcp_made_through'] = CRM_Utils_Array
::value('sort_name', $pcpInfo) . " :: " . $pcpTitle . " :: " . $contributionPageTitle;
196 $defaults['pcp_made_through_id'] = CRM_Utils_Array
::value('pcp_id', $pcpInfo);
197 $defaults['pcp_display_in_roll'] = CRM_Utils_Array
::value('pcp_display_in_roll', $pcpInfo);
198 $defaults['pcp_roll_nickname'] = CRM_Utils_Array
::value('pcp_roll_nickname', $pcpInfo);
199 $defaults['pcp_personal_note'] = CRM_Utils_Array
::value('pcp_personal_note', $pcpInfo);
202 $form->assign('noSoftCredit', $noSoftCredit);
203 $form->assign('noPCP', $noPCP);
209 * @param array $fields
210 * The input form values.
218 public static function formRule($fields, $errors, $self) {
221 // if honor roll fields are populated but no PCP is selected
222 if (empty($fields['pcp_made_through_id'])) {
223 if (!empty($fields['pcp_display_in_roll']) ||
!empty($fields['pcp_roll_nickname']) ||
224 CRM_Utils_Array
::value('pcp_personal_note', $fields)
226 $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.');
230 if (!empty($fields['soft_credit_amount'])) {
231 $repeat = array_count_values($fields['soft_credit_contact_id']);
232 foreach ($fields['soft_credit_amount'] as $key => $val) {
233 if (!empty($fields['soft_credit_contact_id'][$key])) {
234 if ($repeat[$fields['soft_credit_contact_id'][$key]] > 1) {
235 $errors["soft_credit_contact[$key]"] = ts('You cannot enter multiple soft credits for the same contact.');
237 if ($self->_action
== CRM_Core_Action
::ADD
&& $fields['soft_credit_amount'][$key]
238 && (CRM_Utils_Rule
::cleanMoney($fields['soft_credit_amount'][$key]) > CRM_Utils_Rule
::cleanMoney($fields['total_amount']))
240 $errors["soft_credit_amount[$key]"] = ts('Soft credit amount cannot be more than the total amount.');
242 if (empty($fields['soft_credit_amount'][$key])) {
243 $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.');
245 $contactType = CRM_Contact_BAO_Contact
::getContactType($fields['soft_credit_contact_id'][$key]);
246 if ($self->_honoreeProfileType
&& $self->_honoreeProfileType
!= $contactType) {
247 $errors["soft_credit_contact[$key]"] = ts('Please choose a contact of type %1', array(1 => $self->_honoreeProfileType
));