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 to set variables up before form is built
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
,
57 $profileId = CRM_Core_BAO_UFJoin
::getUFGroupIds($ufJoinParams);
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
62 $form->_honoreeProfileType
= CRM_Core_BAO_UFGroup
::getContactType($profileId[0]);
69 * Function used to build form element for soft credit block
71 * @param object $form form object
76 static function buildQuickForm(&$form) {
77 if ($form->_mode
== 'live' && !empty($form->_values
['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 = json_decode($ufJoinDAO->module_data
);
84 $form->assign('honor_block_title', $jsonData->soft_credit
->honor_block_title
);
85 $form->assign('honor_block_text', $jsonData->soft_credit
->honor_block_text
);
87 $softCreditTypes = CRM_Core_OptionGroup
::values("soft_credit_type", FALSE);
88 $extraOption = array('onclick' => "enableHonorType();");
90 // radio button for Honor Type
91 foreach ($jsonData->soft_credit
->soft_credit_types
as $value) {
92 $honorTypes[$value] = $form->createElement('radio', NULL, NULL, $softCreditTypes[$value], $value, $extraOption);
94 $form->addGroup($honorTypes, 'soft_credit_type_id', NULL);
100 $prefix = 'soft_credit_';
101 // by default generate 5 blocks
104 $showSoftCreditRow = 2;
105 $showCreateNew = TRUE;
106 if ($form->_action
& CRM_Core_Action
::UPDATE
) {
107 $form->_softCreditInfo
= CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($form->_id
, TRUE);
109 elseif ($form->_pledgeID
) {
110 //Check and select most recent completed contrubtion and use it to retrieve
111 //soft-credit information to use as default for current pledge payment, CRM-13981
112 $pledgePayments = CRM_Pledge_BAO_PledgePayment
::getPledgePayments($form->_pledgeID
);
113 foreach ($pledgePayments as $id => $record) {
114 if ($record['contribution_id']) {
115 $softCredits = CRM_Contribute_BAO_ContributionSoft
::getSoftContribution($record['contribution_id'], TRUE);
116 if ($record['status'] == 'Completed' && count($softCredits) > 0) {
117 $form->_softCreditInfo
= $softCredits;
123 if (property_exists($form, "_softCreditInfo")) {
124 if (!empty($form->_softCreditInfo
['soft_credit'])) {
125 $showSoftCreditRow = count($form->_softCreditInfo
['soft_credit']);
126 $showSoftCreditRow++
;
127 $showCreateNew = FALSE;
131 for ($rowNumber = 1; $rowNumber <= $item_count; $rowNumber++
) {
132 CRM_Contact_Form_NewContact
::buildQuickForm($form, $rowNumber, NULL, FALSE, $prefix);
134 $form->addMoney("{$prefix}amount[{$rowNumber}]", ts('Amount'), FALSE, NULL, FALSE);
136 $form->add('select', "{$prefix}type[{$rowNumber}]",
137 ts( 'Soft Credit Type' ),
139 '' => ts('- select -')) +
140 CRM_Core_OptionGroup
::values("{$prefix}type", FALSE)
142 if (!empty($form->_softCreditInfo
['soft_credit'][$rowNumber]['soft_credit_id'])) {
143 $form->add('hidden', "{$prefix}id[{$rowNumber}]",
144 $form->_softCreditInfo
['soft_credit'][$rowNumber]['soft_credit_id']);
148 // CRM-7368 allow user to set or edit PCP link for contributions
149 $siteHasPCPs = CRM_Contribute_PseudoConstant
::pcPage();
150 if (!CRM_Utils_Array
::crmIsEmptyArray($siteHasPCPs)) {
151 $form->assign('siteHasPCPs', 1);
152 $pcpDataUrl = CRM_Utils_System
::url('civicrm/ajax/rest',
153 'className=CRM_Contact_Page_AJAX&fnName=getPCPList&json=1&context=contact&reset=1',
156 $form->assign('pcpDataUrl', $pcpDataUrl);
157 $form->addElement('text', 'pcp_made_through', ts('Credit to a Personal Campaign Page'));
158 $form->addElement('hidden', 'pcp_made_through_id', '', array('id' => 'pcp_made_through_id'));
159 $form->addElement('checkbox', 'pcp_display_in_roll', ts('Display in Honor Roll?'), NULL);
160 $form->addElement('text', 'pcp_roll_nickname', ts('Name (for Honor Roll)'));
161 $form->addElement('textarea', 'pcp_personal_note', ts('Personal Note (for Honor Roll)'));
163 $form->assign('showSoftCreditRow', $showSoftCreditRow);
164 $form->assign('rowCount', $item_count);
165 $form->assign('showCreateNew', $showCreateNew);
166 $form->addElement('hidden', 'sct_default_id',
167 CRM_Core_OptionGroup
::getDefaultValue("{$prefix}type"),
168 array('id' => 'sct_default_id')
171 // Tell tpl to hide soft credit field if contribution is linked directly to a PCP Page
172 if (!empty($form->_values
['pcp_made_through_id'])) {
173 $form->assign('pcpLinked', 1);
178 * Function used to set defaults for soft credit block
180 static function setDefaultValues(&$defaults, &$form) {
181 if (!empty($form->_softCreditInfo
['soft_credit'])) {
182 foreach ($form->_softCreditInfo
['soft_credit'] as $key => $value) {
183 $defaults["soft_credit_amount[$key]"] = CRM_Utils_Money
::format($value['amount'], NULL, '%a');
184 $defaults["soft_credit_contact_select_id[$key]"] = $value['contact_id'];
185 $defaults["soft_credit_type[$key]"] = $value['soft_credit_type'];
188 elseif (!empty($form->_softCreditInfo
['pcp_id'])) {
189 $pcpInfo = $form->_softCreditInfo
;
190 $pcpId = CRM_Utils_Array
::value('pcp_id', $pcpInfo);
191 $pcpTitle = CRM_Core_DAO
::getFieldValue('CRM_PCP_DAO_PCP', $pcpId, 'title');
192 $contributionPageTitle = CRM_PCP_BAO_PCP
::getPcpPageTitle($pcpId, 'contribute');
193 $defaults['pcp_made_through'] = CRM_Utils_Array
::value('sort_name', $pcpInfo) . " :: " . $pcpTitle . " :: " . $contributionPageTitle;
194 $defaults['pcp_made_through_id'] = CRM_Utils_Array
::value('pcp_id', $pcpInfo);
195 $defaults['pcp_display_in_roll'] = CRM_Utils_Array
::value('pcp_display_in_roll', $pcpInfo);
196 $defaults['pcp_roll_nickname'] = CRM_Utils_Array
::value('pcp_roll_nickname', $pcpInfo);
197 $defaults['pcp_personal_note'] = CRM_Utils_Array
::value('pcp_personal_note', $pcpInfo);
204 * @param array $fields the input form values
206 * @return true if no errors, else array of errors
210 static function formRule($fields, $errors, $self) {
213 // if honor roll fields are populated but no PCP is selected
214 if (empty($fields['pcp_made_through_id'])) {
215 if (!empty($fields['pcp_display_in_roll']) ||
!empty($fields['pcp_roll_nickname']) ||
216 CRM_Utils_Array
::value('pcp_personal_note', $fields)
218 $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.');
222 if (!empty($fields['soft_credit_amount'])) {
223 $repeat = array_count_values($fields['soft_credit_contact_select_id']);
224 foreach ($fields['soft_credit_amount'] as $key => $val) {
225 if (!empty($fields['soft_credit_contact_select_id'][$key])) {
226 if ($repeat[$fields['soft_credit_contact_select_id'][$key]] > 1) {
227 $errors["soft_credit_contact[$key]"] = ts('You cannot enter multiple soft credits for the same contact.');
229 if ($self->_action
== CRM_Core_Action
::ADD
&& $fields['soft_credit_amount'][$key]
230 && (CRM_Utils_Rule
::cleanMoney($fields['soft_credit_amount'][$key]) > CRM_Utils_Rule
::cleanMoney($fields['total_amount']))) {
231 $errors["soft_credit_amount[$key]"] = ts('Soft credit amount cannot be more than the total amount.');
233 if (empty($fields['soft_credit_amount'][$key])) {
234 $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount.');
236 $contactType = CRM_Contact_BAO_Contact
::getContactType($fields['soft_credit_contact_select_id'][$key]);
237 if ($self->_honoreeProfileType
&& $self->_honoreeProfileType
!= $contactType) {
238 $errors["soft_credit_contact[$key]"] = ts('Please choose a contact of type %1', array(1 => $self->_honoreeProfileType
));