Merge pull request #15144 from JKingsnorth/copying-events-contribution-pages
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Custom.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2019
31 */
32
33 /**
34 * Form to process actions on the group aspect of Custom Data.
35 */
36 class CRM_Contribute_Form_ContributionPage_Custom extends CRM_Contribute_Form_ContributionPage {
37
38 /**
39 * Build the form object.
40 */
41 public function buildQuickForm() {
42
43 // Register 'contact_1' model
44 $entities = [];
45 $entities[] = ['entity_name' => 'contact_1', 'entity_type' => 'IndividualModel'];
46 $allowCoreTypes = array_merge(['Contact', 'Individual'], CRM_Contact_BAO_ContactType::subTypes('Individual'));
47 $allowSubTypes = [];
48
49 // Register 'contribution_1'
50 $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'financial_type_id');
51 $allowCoreTypes[] = 'Contribution';
52 //CRM-15427
53 $allowSubTypes['ContributionType'] = [$financialTypeId];
54 $entities[] = [
55 'entity_name' => 'contribution_1',
56 'entity_type' => 'ContributionModel',
57 'entity_sub_type' => '*',
58 ];
59
60 // If applicable, register 'membership_1'
61 $member = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
62 if ($member && $member['is_active']) {
63 //CRM-15427
64 $entities[] = [
65 'entity_name' => 'membership_1',
66 'entity_type' => 'MembershipModel',
67 'entity_sub_type' => '*',
68 ];
69 $allowCoreTypes[] = 'Membership';
70 $allowSubTypes['MembershipType'] = explode(',', $member['membership_types']);
71 }
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);
75
76 $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Custom', 'formRule'], $this);
77
78 parent::buildQuickForm();
79 }
80
81 /**
82 * Set default values for the form.
83 *
84 * Note that in edit/view mode the default values are retrieved from the database.
85 */
86 public function setDefaultValues() {
87 $defaults = parent::setDefaultValues();
88
89 $defaults['custom_pre_id'] = $this->_values['custom_pre_id'];
90 $defaults['custom_post_id'] = $this->_values['custom_post_id'];
91
92 return $defaults;
93 }
94
95 /**
96 * Process the form.
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 = [
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
141 */
142 public function getTitle() {
143 return ts('Include Profiles');
144 }
145
146 /**
147 * Global form rule.
148 *
149 * @param array $fields
150 * The input form values.
151 *
152 * @param $files
153 * @param object $form
154 *
155 * @return bool|array
156 * true if no errors, else array of errors
157 */
158 public static function formRule($fields, $files, $form) {
159 $errors = [];
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;
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 }
190
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;
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 }
207
208 }