Merge pull request #2371 from mepps/add-softcredit-js-fix
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Settings.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
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 */
35class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_ContributionPage {
36
37 /**
38 * Function to set variables up before form is built
39 *
40 * @return void
41 * @access public
42 */
43 public function preProcess() {
44 parent::preProcess();
45 }
46
47 /**
48 * This function sets the default values for the form. Note that in edit/view mode
49 * the default values are retrieved from the database
50 *
51 * @access public
52 *
53 * @return void
54 */
55 function setDefaultValues() {
56 $defaults = parent::setDefaultValues();
57
58 if ($this->_id) {
59 $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
60 $this->_id,
61 'title'
62 );
63 CRM_Utils_System::setTitle(ts('Title and Settings (%1)',
64 array(1 => $title)
65 ));
66
67 $ufJoinParams = array(
68 'module' => 'OnBehalf',
69 'entity_table' => 'civicrm_contribution_page',
70 'entity_id' => $this->_id,
71 );
cde484fd
DL
72 $onBehalfIDs = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
73 if ($onBehalfIDs) {
74 // get the first one only
75 $defaults['onbehalf_profile_id'] = $onBehalfIDs[0];
76 }
6a488035
TO
77 }
78 else {
79 CRM_Utils_System::setTitle(ts('Title and Settings'));
80 }
81
82 return $defaults;
83 }
84
85 /**
86 * Function to actually build the form
87 *
88 * @return void
89 * @access public
90 */
91 public function buildQuickForm() {
92
93 $this->_first = TRUE;
94 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionPage');
95
96 // financial Type
97 $financialType = CRM_Financial_BAO_FinancialType::getIncomeFinancialType();
98 $this->add('select', 'financial_type_id',
99 ts('Financial Type'),
100 $financialType,
101 TRUE
102 );
103
104 // name
105 $this->add('text', 'title', ts('Title'), $attributes['title'], TRUE);
106
107 //CRM-7362 --add campaigns.
108 CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values));
109
110 $this->addWysiwyg('intro_text', ts('Introductory Message'), $attributes['intro_text']);
111
112 $this->addWysiwyg('footer_text', ts('Footer Message'), $attributes['footer_text']);
113
114 // is on behalf of an organization ?
115 $this->addElement('checkbox', 'is_organization', ts('Allow individuals to contribute and / or signup for membership on behalf of an organization?'), NULL, array('onclick' => "showHideByValue('is_organization',true,'for_org_text','table-row','radio',false);showHideByValue('is_organization',true,'for_org_option','table-row','radio',false);"));
116
117 $required = array('Contact', 'Organization');
118 $optional = array('Contribution', 'Membership');
119
120 $profiles = CRM_Core_BAO_UFGroup::getValidProfiles($required, $optional);
121 //Check profiles for Organization subtypes
122 $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization');
123 foreach ($contactSubType as $type) {
124 $required = array('Contact', $type);
125 $subTypeProfiles = CRM_Core_BAO_UFGroup::getValidProfiles($required, $optional);
126 foreach ($subTypeProfiles as $profileId => $profileName) {
127 $profiles[$profileId] = $profileName;
128 }
129 }
130
131 $requiredProfileFields = array('organization_name', 'email');
132
133 if (!empty($profiles)) {
134 foreach ($profiles as $id => $dontCare) {
135 $validProfile = CRM_Core_BAO_UFGroup::checkValidProfile($id, $requiredProfileFields);
136 if (!$validProfile) {
137 unset($profiles[$id]);
138 }
139 }
140 }
141
142 if (empty($profiles)) {
143 $invalidProfiles = TRUE;
144 $this->assign('invalidProfiles', $invalidProfiles);
145 }
146
147 $this->add('select', 'onbehalf_profile_id', ts('Organization Profile'),
148 array(
149 '' => ts('- select -')) + $profiles
150 );
151
152 $options = array();
153 $options[] = $this->createElement('radio', NULL, NULL, ts('Optional'), 1);
154 $options[] = $this->createElement('radio', NULL, NULL, ts('Required'), 2);
155 $this->addGroup($options, 'is_for_organization', ts(''));
156 $this->add('textarea', 'for_organization', ts('On behalf of Label'), $attributes['for_organization']);
157
158 // collect goal amount
159 $this->add('text', 'goal_amount', ts('Goal Amount'), array('size' => 8, 'maxlength' => 12));
160 $this->addRule('goal_amount', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
161
162 // is confirmation page enabled?
163 $this->addElement('checkbox', 'is_confirm_enabled', ts('Use a confirmation page?'));
164
165 // is this page shareable through social media ?
166 $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?'));
167
168 // is this page active ?
169 $this->addElement('checkbox', 'is_active', ts('Is this Online Contribution Page Active?'));
170
171 // should the honor be enabled
172 $this->addElement('checkbox', 'honor_block_is_active', ts('Honoree Section Enabled'), NULL, array('onclick' => "showHonor()"));
173
174 $this->add('text', 'honor_block_title', ts('Honoree Section Title'), $attributes['honor_block_title']);
175
176 $this->add('textarea', 'honor_block_text', ts('Honoree Introductory Message'), $attributes['honor_block_text']);
177
178 // add optional start and end dates
179 $this->addDateTime('start_date', ts('Start Date'));
180 $this->addDateTime('end_date', ts('End Date'));
181
3b67ab13 182 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Settings', 'formRule'), $this);
6a488035
TO
183
184 parent::buildQuickForm();
185 }
186
187 /**
188 * global validation rules for the form
189 *
190 * @param array $values posted values of the form
191 *
192 * @return array list of errors to be posted back to the form
193 * @static
194 * @access public
195 */
3b67ab13 196 static function formRule($values, $files, $self) {
6a488035 197 $errors = array();
3b67ab13 198 $contributionPageId = $self->_id;
6a488035
TO
199 //CRM-4286
200 if (strstr($values['title'], '/')) {
201 $errors['title'] = ts("Please do not use '/' in Title");
202 }
203
204 if (CRM_Utils_Array::value('is_organization', $values) &&
205 !CRM_Utils_Array::value('onbehalf_profile_id', $values)
206 ) {
207 $errors['onbehalf_profile_id'] = ts('Please select a profile to collect organization information on this contribution page.');
208 }
cde484fd 209
6a488035
TO
210 //CRM-11494
211 $start = CRM_Utils_Date::processDate($values['start_date']);
212 $end = CRM_Utils_Date::processDate($values['end_date']);
213 if (($end < $start) && ($end != 0)) {
214 $errors['end_date'] = ts('End date should be after Start date.');
215 }
3b67ab13
PN
216
217 if (CRM_Utils_Array::value('payment_processor', $self->_values)
218 && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($values['financial_type_id'])) {
219 $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
220 }
221
6a488035 222 //dont allow on behalf of save when
cde484fd 223 //pre or post profile consists of membership fields
6a488035
TO
224 if ($contributionPageId && CRM_Utils_Array::value('is_organization', $values)) {
225 $ufJoinParams = array(
226 'module' => 'CiviContribute',
227 'entity_table' => 'civicrm_contribution_page',
228 'entity_id' => $contributionPageId,
229 );
cde484fd 230
6a488035
TO
231 list($contributionProfiles['custom_pre_id'],
232 $contributionProfiles['custom_post_id']
233 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
234
235 $conProfileType = NULL;
236 if ($contributionProfiles['custom_pre_id']) {
237 $preProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_pre_id']);
238 if ($preProfileType == 'Membership') {
239 $conProfileType = "'Includes Profile (top of page)'";
240 }
241 }
3b67ab13 242
6a488035
TO
243 if ($contributionProfiles['custom_post_id']) {
244 $postProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_post_id']);
245 if ($postProfileType == 'Membership') {
cde484fd 246 $conProfileType = empty($conProfileType) ? "'Includes Profile (bottom of page)'" : "{$conProfileType} and 'Includes Profile (bottom of page)'";
6a488035
TO
247 }
248 }
249 if (!empty($conProfileType)) {
250 $errors['is_organization'] = ts("You should move the membership related fields configured in %1 to the 'On Behalf' profile for this Contribution Page", array(1 => $conProfileType));
251 }
252 }
253 return $errors;
254 }
255
256 /**
257 * Process the form
258 *
259 * @return void
260 * @access public
261 */
262 public function postProcess() {
263 // get the submitted form values.
264 $params = $this->controller->exportValues($this->_name);
265
266 // we do this in case the user has hit the forward/back button
267 if ($this->_id) {
268 $params['id'] = $this->_id;
269 }
270 else {
271 $session = CRM_Core_Session::singleton();
272 $params['created_id'] = $session->get('userID');
273 $params['created_date'] = date('YmdHis');
274 $config = CRM_Core_Config::singleton();
275 $params['currency'] = $config->defaultCurrency;
276 }
277
278 $params['is_confirm_enabled'] = CRM_Utils_Array::value('is_confirm_enabled', $params, FALSE);
279 $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE);
280 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
281 $params['is_credit_card_only'] = CRM_Utils_Array::value('is_credit_card_only', $params, FALSE);
282 $params['honor_block_is_active'] = CRM_Utils_Array::value('honor_block_is_active', $params, FALSE);
283 $params['is_for_organization'] = CRM_Utils_Array::value('is_organization', $params) ? CRM_Utils_Array::value('is_for_organization', $params, FALSE) : 0;
284
285 $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], $params['start_date_time'], TRUE);
286 $params['end_date'] = CRM_Utils_Date::processDate($params['end_date'], $params['end_date_time'], TRUE);
287
288 $params['goal_amount'] = CRM_Utils_Rule::cleanMoney($params['goal_amount']);
289
290 if (!$params['honor_block_is_active']) {
291 $params['honor_block_title'] = NULL;
292 $params['honor_block_text'] = NULL;
293 }
294
295 $dao = CRM_Contribute_BAO_ContributionPage::create($params);
296
297 // make entry in UF join table for onbehalf of org profile
298 $ufJoinParams = array(
299 'is_active' => 1,
300 'module' => 'OnBehalf',
301 'entity_table' => 'civicrm_contribution_page',
302 'entity_id' => $dao->id,
303 );
304
305 // first delete all past entries
306 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
307
308 if (CRM_Utils_Array::value('onbehalf_profile_id', $params)) {
309 $ufJoinParams['weight'] = 1;
310 $ufJoinParams['uf_group_id'] = $params['onbehalf_profile_id'];
311 CRM_Core_BAO_UFJoin::create($ufJoinParams);
312 }
313
314 $this->set('id', $dao->id);
315 if ($this->_action & CRM_Core_Action::ADD) {
316 $url = 'civicrm/admin/contribute/amount';
317 $urlParams = "action=update&reset=1&id={$dao->id}";
318 // special case for 'Save and Done' consistency.
319 if ($this->controller->getButtonName('submit') == '_qf_Amount_upload_done') {
320 $url = 'civicrm/admin/contribute';
321 $urlParams = 'reset=1';
322 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
323 array(1 => $this->getTitle())
324 ), ts('Saved'), 'success');
325 }
326
327 CRM_Utils_System::redirect(CRM_Utils_System::url($url, $urlParams));
328 }
329 parent::endPostProcess();
330 }
331
332 /**
333 * Return a descriptive name for the page, used in wizard header
334 *
335 * @return string
336 * @access public
337 */
338 public function getTitle() {
339 return ts('Title and Settings');
340 }
341}
342