3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * This class build form elements for select exitsing or create new soft block
39 class CRM_Contribute_Form_SoftCredit
{
42 * Function used to build form element for soft credit block
44 * @param object $form form object
49 static function buildQuickForm(&$form) {
50 if ($form->_mode
== 'live' && CRM_Utils_Array
::value('honor_block_is_active', $form->_values
)) {
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
);
57 $form->assign('honor_block_title', $jsonData->soft_credit
->honor_block_title
);
58 $form->assign('honor_block_text', $jsonData->soft_credit
->honor_block_text
);
60 $softCreditTypes = CRM_Core_OptionGroup
::values("soft_credit_type", FALSE);
61 $extraOption = array('onclick' => "enableHonorType();");
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);
67 $form->addGroup($honorTypes, 'soft_credit_type_id', NULL);
73 $prefix = 'soft_credit_';
74 // by default generate 5 blocks
77 $showSoftCreditRow = 2;
78 $showCreateNew = TRUE;
79 if ($form->_action
& CRM_Core_Action
::UPDATE
) {
80 $form->_softCreditInfo
= CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($form->_id
, TRUE);
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;
96 if (property_exists($form, "_softCreditInfo")) {
97 if (!empty($form->_softCreditInfo
['soft_credit'])) {
98 $showSoftCreditRow = count($form->_softCreditInfo
['soft_credit']);
100 $showCreateNew = FALSE;
104 for ($rowNumber = 1; $rowNumber <= $item_count; $rowNumber++
) {
105 CRM_Contact_Form_NewContact
::buildQuickForm($form, $rowNumber, NULL, FALSE, $prefix);
107 $form->addMoney("{$prefix}amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE);
109 $form->add('select', "{$prefix}type[{$rowNumber}]",
110 ts( 'Soft Credit Type' ),
112 '' => ts('- select -')) +
113 CRM_Core_OptionGroup
::values("{$prefix}type", FALSE)
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']);
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',
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)'));
136 $form->assign('showSoftCreditRow', $showSoftCreditRow);
137 $form->assign('rowCount', $item_count);
138 $form->assign('showCreateNew', $showCreateNew);
139 $form->addElement('hidden', 'sct_default_id',
140 CRM_Core_OptionGroup
::getDefaultValue("{$prefix}type"),
141 array('id' => 'sct_default_id')
144 // Tell tpl to hide soft credit field if contribution is linked directly to a PCP Page
145 if (CRM_Utils_Array
::value('pcp_made_through_id', $form->_values
)) {
146 $form->assign('pcpLinked', 1);
151 * Function used to set defaults for soft credit block
153 static function setDefaultValues(&$defaults, &$form) {
154 if (!empty($form->_softCreditInfo
['soft_credit'])) {
155 foreach ($form->_softCreditInfo
['soft_credit'] as $key => $value) {
156 $defaults["soft_credit_amount[$key]"] = CRM_Utils_Money
::format($value['amount'], NULL, '%a');
157 $defaults["soft_credit_contact_select_id[$key]"] = $value['contact_id'];
158 $defaults["soft_credit_type[$key]"] = $value['soft_credit_type'];
161 elseif (CRM_Utils_Array
::value('pcp_id', $form->_softCreditInfo
)) {
162 $pcpInfo = $form->_softCreditInfo
;
163 $pcpId = CRM_Utils_Array
::value('pcp_id', $pcpInfo);
164 $pcpTitle = CRM_Core_DAO
::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title');
165 $contributionPageTitle = CRM_PCP_BAO_PCP
::getPcpPageTitle($pcpId, 'contribute');
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);
177 * @param array $fields the input form values
179 * @return true if no errors, else array of errors
183 static function formRule($fields, $errors, $self) {
186 // if honor roll fields are populated but no PCP is selected
187 if (!CRM_Utils_Array
::value('pcp_made_through_id', $fields)) {
188 if (CRM_Utils_Array
::value('pcp_display_in_roll', $fields) ||
189 CRM_Utils_Array
::value('pcp_roll_nickname', $fields) ||
190 CRM_Utils_Array
::value('pcp_personal_note', $fields)
192 $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.');
196 if (!empty($fields['soft_credit_amount'])) {
197 $repeat = array_count_values($fields['soft_credit_contact_select_id']);
198 foreach ($fields['soft_credit_amount'] as $key => $val) {
199 if (!empty($fields['soft_credit_contact_select_id'][$key])) {
200 if ($repeat[$fields['soft_credit_contact_select_id'][$key]] > 1) {
201 $errors["soft_credit_contact_select_id[$key]"] = ts('You cannot enter multiple soft credits for the same contact.');
203 if ($self->_action
== CRM_Core_Action
::ADD
&& $fields['soft_credit_amount'][$key]
204 && (CRM_Utils_Rule
::cleanMoney($fields['soft_credit_amount'][$key]) > CRM_Utils_Rule
::cleanMoney($fields['total_amount']))) {
205 $errors["soft_credit_amount[$key]"] = ts('Soft credit amount cannot be more than the total amount.');
207 if (empty($fields['soft_credit_amount'][$key])) {
208 $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.');