copyright and version fixes
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Custom.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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 */
39 class 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
49 // Register 'contact_1' model
50 $entities = array();
51 $entities[] = array('entity_name' => 'contact_1', 'entity_type' => 'IndividualModel');
52 $allowCoreTypes = array_merge(array('Contact', 'Individual'), CRM_Contact_BAO_ContactType::subTypes('Individual'));
53 $allowSubTypes = array();
54
55 // Register 'contribution_1'
56 $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'financial_type_id');
57 $allowCoreTypes[] = 'Contribution';
58 // $allowSubTypes['ContributionType'] = array($financialTypeId);
59 $entities[] = array('entity_name' => 'contribution_1', 'entity_type' => 'ContributionModel', 'entity_sub_type' => $financialTypeId);
60
61 // If applicable, register 'membership_1'
62 $member = CRM_Member_BAO_Membership::getMembershipBlock($this->_id);
63 if ($member && $member['is_active']) {
64 $entities[] = array('entity_name' => 'membership_1', 'entity_type' => 'MembershipModel', 'entity_sub_type' => $member['membership_type_default']);
65 $allowCoreTypes[] = 'Membership';
66 $allowSubTypes['MembershipType'] = explode(',', $member['membership_types']);
67 }
68
69 $this->addProfileSelector('custom_pre_id', ts('Include Profile') . '<br />' . ts('(top of page)'), $allowCoreTypes, $allowSubTypes, $entities);
70 $this->addProfileSelector('custom_post_id', ts('Include Profile') . '<br />' . ts('(bottom of page)'), $allowCoreTypes, $allowSubTypes, $entities);
71
72 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Custom', 'formRule'), $this->_id);
73
74 parent::buildQuickForm();
75 }
76
77 /**
78 * This function sets the default values for the form. Note that in edit/view mode
79 * the default values are retrieved from the database
80 *
81 * @access public
82 *
83 * @return void
84 */
85 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 (%1)', array(1 => $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 * @access public
112 */
113 public function postProcess() {
114 // get the submitted form values.
115 $params = $this->controller->exportValues($this->_name);
116
117 if ($this->_action & CRM_Core_Action::UPDATE) {
118 $params['id'] = $this->_id;
119 }
120
121 $transaction = new CRM_Core_Transaction();
122
123 // also update uf join table
124 $ufJoinParams = array(
125 'is_active' => 1,
126 'module' => 'CiviContribute',
127 'entity_table' => 'civicrm_contribution_page',
128 'entity_id' => $this->_id,
129 );
130
131 // first delete all past entries
132 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
133
134 if (!empty($params['custom_pre_id'])) {
135 $ufJoinParams['weight'] = 1;
136 $ufJoinParams['uf_group_id'] = $params['custom_pre_id'];
137 CRM_Core_BAO_UFJoin::create($ufJoinParams);
138 }
139
140 unset($ufJoinParams['id']);
141
142 if (!empty($params['custom_post_id'])) {
143 $ufJoinParams['weight'] = 2;
144 $ufJoinParams['uf_group_id'] = $params['custom_post_id'];
145 CRM_Core_BAO_UFJoin::create($ufJoinParams);
146 }
147
148 $transaction->commit();
149 parent::endPostProcess();
150 }
151
152 /**
153 * Return a descriptive name for the page, used in wizard header
154 *
155 * @return string
156 * @access public
157 */
158 public function getTitle() {
159 return ts('Include Profiles');
160 }
161
162 /**
163 * global form rule
164 *
165 * @param array $fields the input form values
166 *
167 * @return true if no errors, else array of errors
168 * @access public
169 * @static
170 */
171 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 }
221