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