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