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