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