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