3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
30 * @copyright CiviCRM LLC (c) 2004-2019
34 * Form to process actions on the group aspect of Custom Data.
36 class CRM_Contribute_Form_ContributionPage_Custom
extends CRM_Contribute_Form_ContributionPage
{
39 * Build the form object.
41 public function buildQuickForm() {
43 // Register 'contact_1' model
45 $entities[] = ['entity_name' => 'contact_1', 'entity_type' => 'IndividualModel'];
46 $allowCoreTypes = array_merge(['Contact', 'Individual'], CRM_Contact_BAO_ContactType
::subTypes('Individual'));
49 // Register 'contribution_1'
50 $financialTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id
, 'financial_type_id');
51 $allowCoreTypes[] = 'Contribution';
53 $allowSubTypes['ContributionType'] = [$financialTypeId];
55 'entity_name' => 'contribution_1',
56 'entity_type' => 'ContributionModel',
57 'entity_sub_type' => '*',
60 // If applicable, register 'membership_1'
61 $member = CRM_Member_BAO_Membership
::getMembershipBlock($this->_id
);
62 if ($member && $member['is_active']) {
65 'entity_name' => 'membership_1',
66 'entity_type' => 'MembershipModel',
67 'entity_sub_type' => '*',
69 $allowCoreTypes[] = 'Membership';
70 $allowSubTypes['MembershipType'] = explode(',', $member['membership_types']);
73 $this->addProfileSelector('custom_pre_id', ts('Include Profile') . '<br />' . ts('(top of page)'), $allowCoreTypes, $allowSubTypes, $entities, TRUE);
74 $this->addProfileSelector('custom_post_id', ts('Include Profile') . '<br />' . ts('(bottom of page)'), $allowCoreTypes, $allowSubTypes, $entities, TRUE);
76 $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Custom', 'formRule'], $this);
78 parent
::buildQuickForm();
82 * Set default values for the form.
84 * Note that in edit/view mode the default values are retrieved from the database.
86 public function setDefaultValues() {
87 $defaults = parent
::setDefaultValues();
89 $defaults['custom_pre_id'] = $this->_values
['custom_pre_id'];
90 $defaults['custom_post_id'] = $this->_values
['custom_post_id'];
98 public function postProcess() {
99 // get the submitted form values.
100 $params = $this->controller
->exportValues($this->_name
);
102 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
103 $params['id'] = $this->_id
;
106 $transaction = new CRM_Core_Transaction();
108 // also update uf join table
111 'module' => 'CiviContribute',
112 'entity_table' => 'civicrm_contribution_page',
113 'entity_id' => $this->_id
,
116 // first delete all past entries
117 CRM_Core_BAO_UFJoin
::deleteAll($ufJoinParams);
119 if (!empty($params['custom_pre_id'])) {
120 $ufJoinParams['weight'] = 1;
121 $ufJoinParams['uf_group_id'] = $params['custom_pre_id'];
122 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
125 unset($ufJoinParams['id']);
127 if (!empty($params['custom_post_id'])) {
128 $ufJoinParams['weight'] = 2;
129 $ufJoinParams['uf_group_id'] = $params['custom_post_id'];
130 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
133 $transaction->commit();
134 parent
::endPostProcess();
138 * Return a descriptive name for the page, used in wizard header
142 public function getTitle() {
143 return ts('Include Profiles');
149 * @param array $fields
150 * The input form values.
153 * @param object $form
156 * true if no errors, else array of errors
158 public static function formRule($fields, $files, $form) {
160 $preProfileType = $postProfileType = NULL;
161 // for membership profile make sure Membership section is enabled
162 // get membership section for this contribution page
163 $dao = new CRM_Member_DAO_MembershipBlock();
164 $dao->entity_table
= 'civicrm_contribution_page';
165 $dao->entity_id
= $form->_id
;
167 $membershipEnable = FALSE;
169 if ($dao->find(TRUE) && $dao->is_active
) {
170 $membershipEnable = TRUE;
173 if ($fields['custom_pre_id']) {
174 $preProfileType = CRM_Core_BAO_UFField
::getProfileType($fields['custom_pre_id']);
177 if ($fields['custom_post_id']) {
178 $postProfileType = CRM_Core_BAO_UFField
::getProfileType($fields['custom_post_id']);
181 $errorMsg = ts('You must enable the Membership Block for this Contribution Page if you want to include a Profile with Membership fields.');
183 if (($preProfileType == 'Membership') && !$membershipEnable) {
184 $errors['custom_pre_id'] = $errorMsg;
187 if (($postProfileType == 'Membership') && !$membershipEnable) {
188 $errors['custom_post_id'] = $errorMsg;
191 $behalf = (!empty($form->_values
['onbehalf_profile_id'])) ?
$form->_values
['onbehalf_profile_id'] : NULL;
192 if ($fields['custom_pre_id']) {
193 $errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
194 if ($preProfileType == 'Membership' && $behalf) {
195 $errors['custom_pre_id'] = isset($errors['custom_pre_id']) ?
$errors['custom_pre_id'] . $errorMsg : $errorMsg;
199 if ($fields['custom_post_id']) {
200 $errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
201 if ($postProfileType == 'Membership' && $behalf) {
202 $errors['custom_post_id'] = isset($errors['custom_post_id']) ?
$errors['custom_post_id'] . $errorMsg : $errorMsg;
205 return empty($errors) ?
TRUE : $errors;