Merge pull request #9471 from adixon/CRM-19697
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Custom.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
6a488035 29 * @package CRM
0f03f337 30 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
31 */
32
33/**
37b11065 34 * Form to process actions on the group aspect of Custom Data.
6a488035
TO
35 */
36class CRM_Contribute_Form_ContributionPage_Custom extends CRM_Contribute_Form_ContributionPage {
37
38 /**
fe482240 39 * Build the form object.
6a488035
TO
40 */
41 public function buildQuickForm() {
6a488035 42
ede14745 43 // Register 'contact_1' model
c50f55b5 44 $entities = array();
41fd8e26 45 $entities[] = array('entity_name' => 'contact_1', 'entity_type' => 'IndividualModel');
c50f55b5
CW
46 $allowCoreTypes = array_merge(array('Contact', 'Individual'), CRM_Contact_BAO_ContactType::subTypes('Individual'));
47 $allowSubTypes = array();
48
49 // Register 'contribution_1'
50 $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'financial_type_id');
51 $allowCoreTypes[] = 'Contribution';
99e239bc 52 //CRM-15427
53 $allowSubTypes['ContributionType'] = array($financialTypeId);
353ffa53
TO
54 $entities[] = array(
55 'entity_name' => 'contribution_1',
56 'entity_type' => 'ContributionModel',
389bcebf 57 'entity_sub_type' => '*',
353ffa53 58 );
c50f55b5
CW
59
60 // If applicable, register 'membership_1'
61 $member = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
62 if ($member && $member['is_active']) {
99e239bc 63 //CRM-15427
353ffa53
TO
64 $entities[] = array(
65 'entity_name' => 'membership_1',
66 'entity_type' => 'MembershipModel',
389bcebf 67 'entity_sub_type' => '*',
353ffa53 68 );
c50f55b5
CW
69 $allowCoreTypes[] = 'Membership';
70 $allowSubTypes['MembershipType'] = explode(',', $member['membership_types']);
6a488035 71 }
99e239bc 72 //CRM-15427
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);
6a488035 75
c0eb40c1 76 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Custom', 'formRule'), $this);
6a488035
TO
77
78 parent::buildQuickForm();
79 }
80
81 /**
95cdcc0f 82 * Set default values for the form.
6a488035 83 *
95cdcc0f 84 * Note that in edit/view mode the default values are retrieved from the database.
6a488035 85 */
00be9182 86 public function setDefaultValues() {
6a488035
TO
87 $defaults = parent::setDefaultValues();
88
c0eb40c1 89 $defaults['custom_pre_id'] = $this->_values['custom_pre_id'];
90 $defaults['custom_post_id'] = $this->_values['custom_post_id'];
c50f55b5 91
6a488035
TO
92 return $defaults;
93 }
94
95 /**
fe482240 96 * Process the form.
6a488035
TO
97 */
98 public function postProcess() {
99 // get the submitted form values.
100 $params = $this->controller->exportValues($this->_name);
101
102 if ($this->_action & CRM_Core_Action::UPDATE) {
103 $params['id'] = $this->_id;
104 }
105
106 $transaction = new CRM_Core_Transaction();
107
108 // also update uf join table
109 $ufJoinParams = array(
110 'is_active' => 1,
111 'module' => 'CiviContribute',
112 'entity_table' => 'civicrm_contribution_page',
113 'entity_id' => $this->_id,
114 );
115
116 // first delete all past entries
117 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
118
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);
123 }
124
125 unset($ufJoinParams['id']);
126
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);
131 }
132
133 $transaction->commit();
134 parent::endPostProcess();
135 }
136
137 /**
138 * Return a descriptive name for the page, used in wizard header
139 *
140 * @return string
6a488035
TO
141 */
142 public function getTitle() {
143 return ts('Include Profiles');
144 }
145
146 /**
fe482240 147 * Global form rule.
6a488035 148 *
014c4014
TO
149 * @param array $fields
150 * The input form values.
da6b46f4
EM
151 *
152 * @param $files
c0eb40c1 153 * @param object $form
6a488035 154 *
72b3a70c
CW
155 * @return bool|array
156 * true if no errors, else array of errors
6a488035 157 */
c0eb40c1 158 public static function formRule($fields, $files, $form) {
6a488035
TO
159 $errors = array();
160 $preProfileType = $postProfileType = NULL;
161 // for membership profile make sure Membership section is enabled
162 // get membership section for this contribution page
353ffa53 163 $dao = new CRM_Member_DAO_MembershipBlock();
6a488035 164 $dao->entity_table = 'civicrm_contribution_page';
c0eb40c1 165 $dao->entity_id = $form->_id;
6a488035
TO
166
167 $membershipEnable = FALSE;
168
169 if ($dao->find(TRUE) && $dao->is_active) {
170 $membershipEnable = TRUE;
171 }
172
173 if ($fields['custom_pre_id']) {
174 $preProfileType = CRM_Core_BAO_UFField::getProfileType($fields['custom_pre_id']);
175 }
176
177 if ($fields['custom_post_id']) {
178 $postProfileType = CRM_Core_BAO_UFField::getProfileType($fields['custom_post_id']);
179 }
180
181 $errorMsg = ts('You must enable the Membership Block for this Contribution Page if you want to include a Profile with Membership fields.');
182
183 if (($preProfileType == 'Membership') && !$membershipEnable) {
184 $errors['custom_pre_id'] = $errorMsg;
185 }
186
187 if (($postProfileType == 'Membership') && !$membershipEnable) {
188 $errors['custom_post_id'] = $errorMsg;
189 }
665e5ec7 190
c0eb40c1 191 $behalf = (!empty($form->_values['onbehalf_profile_id'])) ? $form->_values['onbehalf_profile_id'] : NULL;
6a488035
TO
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;
196 }
197 }
198
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;
203 }
204 }
205 return empty($errors) ? TRUE : $errors;
206 }
96025800 207
6a488035 208}