Merge pull request #1652 from vivekarora/CRM-13368
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Custom.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
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 /**
42 * Function to actually build the form
43 *
44 * @return void
45 * @access public
46 */
47 public function buildQuickForm() {
48 $types = array_merge(array('Contact', 'Individual', 'Contribution', 'Membership'),
49 CRM_Contact_BAO_ContactType::subTypes('Individual')
50 );
51
52 $profiles = CRM_Core_BAO_UFGroup::getProfiles($types);
53 $excludeTypes = array('Organization', 'Household', 'Participant', 'Activity');
54
55 $excludeProfiles = CRM_Core_BAO_UFGroup::getProfiles($excludeTypes);
56 foreach ($excludeProfiles as $key => $value) {
57 if (array_key_exists( $key, $profiles)) {
58 unset($profiles[$key]);
59 }
60 }
61
62 if (empty($profiles)) {
63 $this->assign('noProfile', TRUE);
64 }
65
66 $this->add('select', 'custom_pre_id', ts('Include Profile') . '<br />' . ts('(top of page)'), array('' => ts('- select -')) + $profiles);
67 $this->add('select', 'custom_post_id', ts('Include Profile') . '<br />' . ts('(bottom of page)'), array('' => ts('- select -')) + $profiles);
68
69 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Custom', 'formRule'), $this->_id);
70
71 parent::buildQuickForm();
72 }
73
74 /**
75 * This function sets the default values for the form. Note that in edit/view mode
76 * the default values are retrieved from the database
77 *
78 * @access public
79 *
80 * @return void
81 */
82 function setDefaultValues() {
83 $defaults = parent::setDefaultValues();
84
85 if ($this->_id) {
86 $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'title');
87 CRM_Utils_System::setTitle(ts('Include Profiles (%1)', array(1 => $title)));
88 }
89
90
91 $ufJoinParams = array(
92 'module' => 'CiviContribute',
93 'entity_table' => 'civicrm_contribution_page',
94 'entity_id' => $this->_id,
95 );
96 list($defaults['custom_pre_id'],
97 $defaults['custom_post_id']
98 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
99
100 return $defaults;
101 }
102
103 /**
104 * Process the form
105 *
106 * @return void
107 * @access public
108 */
109 public function postProcess() {
110 // get the submitted form values.
111 $params = $this->controller->exportValues($this->_name);
112
113 if ($this->_action & CRM_Core_Action::UPDATE) {
114 $params['id'] = $this->_id;
115 }
116
117 $transaction = new CRM_Core_Transaction();
118
119 // also update uf join table
120 $ufJoinParams = array(
121 'is_active' => 1,
122 'module' => 'CiviContribute',
123 'entity_table' => 'civicrm_contribution_page',
124 'entity_id' => $this->_id,
125 );
126
127 // first delete all past entries
128 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
129
130 if (!empty($params['custom_pre_id'])) {
131 $ufJoinParams['weight'] = 1;
132 $ufJoinParams['uf_group_id'] = $params['custom_pre_id'];
133 CRM_Core_BAO_UFJoin::create($ufJoinParams);
134 }
135
136 unset($ufJoinParams['id']);
137
138 if (!empty($params['custom_post_id'])) {
139 $ufJoinParams['weight'] = 2;
140 $ufJoinParams['uf_group_id'] = $params['custom_post_id'];
141 CRM_Core_BAO_UFJoin::create($ufJoinParams);
142 }
143
144 $transaction->commit();
145 parent::endPostProcess();
146 }
147
148 /**
149 * Return a descriptive name for the page, used in wizard header
150 *
151 * @return string
152 * @access public
153 */
154 public function getTitle() {
155 return ts('Include Profiles');
156 }
157
158 /**
159 * global form rule
160 *
161 * @param array $fields the input form values
162 *
163 * @return true if no errors, else array of errors
164 * @access public
165 * @static
166 */
167 static function formRule($fields, $files, $contributionPageId) {
168 $errors = array();
169 $preProfileType = $postProfileType = NULL;
170 // for membership profile make sure Membership section is enabled
171 // get membership section for this contribution page
172 $dao = new CRM_Member_DAO_MembershipBlock();
173 $dao->entity_table = 'civicrm_contribution_page';
174 $dao->entity_id = $contributionPageId;
175
176 $membershipEnable = FALSE;
177
178 if ($dao->find(TRUE) && $dao->is_active) {
179 $membershipEnable = TRUE;
180 }
181
182 if ($fields['custom_pre_id']) {
183 $preProfileType = CRM_Core_BAO_UFField::getProfileType($fields['custom_pre_id']);
184 }
185
186 if ($fields['custom_post_id']) {
187 $postProfileType = CRM_Core_BAO_UFField::getProfileType($fields['custom_post_id']);
188 }
189
190 $errorMsg = ts('You must enable the Membership Block for this Contribution Page if you want to include a Profile with Membership fields.');
191
192 if (($preProfileType == 'Membership') && !$membershipEnable) {
193 $errors['custom_pre_id'] = $errorMsg;
194 }
195
196 if (($postProfileType == 'Membership') && !$membershipEnable) {
197 $errors['custom_post_id'] = $errorMsg;
198 }
665e5ec7 199
6a488035
TO
200 $behalf = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $contributionPageId, 'is_for_organization');
201 if ($fields['custom_pre_id']) {
202 $errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
203 if ($preProfileType == 'Membership' && $behalf) {
204 $errors['custom_pre_id'] = isset($errors['custom_pre_id']) ? $errors['custom_pre_id'] . $errorMsg : $errorMsg;
205 }
206 }
207
208 if ($fields['custom_post_id']) {
209 $errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
210 if ($postProfileType == 'Membership' && $behalf) {
211 $errors['custom_post_id'] = isset($errors['custom_post_id']) ? $errors['custom_post_id'] . $errorMsg : $errorMsg;
212 }
213 }
214 return empty($errors) ? TRUE : $errors;
215 }
216}
217