Merge pull request #2842 from totten/master-api-rollback-soft-errors
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Settings.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 class 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 $soft_credit_types = CRM_Core_OptionGroup::values('soft_credit_type', TRUE, FALSE, FALSE, NULL, 'name');
58
59 if ($this->_id) {
60 $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
61 $this->_id,
62 'title'
63 );
64 CRM_Utils_System::setTitle(ts('Title and Settings (%1)',
65 array(1 => $title)
66 ));
67
68 $ufJoinParams = array(
69 'module' => 'OnBehalf',
70 'entity_table' => 'civicrm_contribution_page',
71 'entity_id' => $this->_id,
72 );
73 $onBehalfIDs = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
74 if ($onBehalfIDs) {
75 // get the first one only
76 $defaults['onbehalf_profile_id'] = $onBehalfIDs[0];
77 }
78
79 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
80 $ufJoinDAO->module = 'soft_credit';
81 $ufJoinDAO->entity_id = $this->_id;
82 if ($ufJoinDAO->find(TRUE)) {
83 $defaults['honoree_profile'] = $ufJoinDAO->uf_group_id;
84 $jsonData = CRM_Contribute_BAO_ContributionPage::formatMultilingualHonorParams($ufJoinDAO->module_data, TRUE);
85 $defaults = array_merge($defaults, $jsonData);
86 $defaults['honor_block_is_active'] = $ufJoinDAO->is_active;
87 }
88 else {
89 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
90 $ufGroupDAO->name = 'honoree_individual';
91 if ($ufGroupDAO->find(TRUE)) {
92 $defaults['honoree_profile'] = $ufGroupDAO->id;
93 }
94 $defaults['soft_credit_types'] = array(
95 CRM_Utils_Array::value('in_honor_of', $soft_credit_types),
96 CRM_Utils_Array::value('in_memory_of', $soft_credit_types)
97 );
98 }
99 }
100 else {
101 CRM_Utils_System::setTitle(ts('Title and Settings'));
102
103 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
104 $ufGroupDAO->name = 'honoree_individual';
105 if ($ufGroupDAO->find(TRUE)) {
106 $defaults['honoree_profile'] = $ufGroupDAO->id;
107 }
108 $defaults['soft_credit_types'] = array(
109 CRM_Utils_Array::value('in_honor_of', $soft_credit_types),
110 CRM_Utils_Array::value('in_memory_of', $soft_credit_types)
111 );
112 }
113
114 return $defaults;
115 }
116
117 /**
118 * Function to actually build the form
119 *
120 * @return void
121 * @access public
122 */
123 public function buildQuickForm() {
124
125 $this->_first = TRUE;
126 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_ContributionPage');
127
128 // financial Type
129 $this->addSelect('financial_type_id', array(), TRUE);
130
131 // name
132 $this->add('text', 'title', ts('Title'), $attributes['title'], TRUE);
133
134 //CRM-7362 --add campaigns.
135 CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values));
136
137 $this->addWysiwyg('intro_text', ts('Introductory Message'), $attributes['intro_text']);
138
139 $this->addWysiwyg('footer_text', ts('Footer Message'), $attributes['footer_text']);
140
141 //Register schema which will be used for OnBehalOf and HonorOf profile Selector
142 CRM_UF_Page_ProfileEditor::registerSchemas(array('OrganizationModel', 'HouseholdModel'));
143
144 // is on behalf of an organization ?
145 $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);"));
146
147 $allowCoreTypes = array_merge(array('Contact', 'Organization'), CRM_Contact_BAO_ContactType::subTypes('Organization'));
148 $allowSubTypes = array();
149 $entities = array(
150 array(
151 'entity_name' => 'contact_1',
152 'entity_type' => 'OrganizationModel',
153 ),
154 );
155
156 $this->addProfileSelector('onbehalf_profile_id', ts('Organization Profile'), $allowCoreTypes, $allowSubTypes, $entities);
157
158 $options = array();
159 $options[] = $this->createElement('radio', NULL, NULL, ts('Optional'), 1);
160 $options[] = $this->createElement('radio', NULL, NULL, ts('Required'), 2);
161 $this->addGroup($options, 'is_for_organization', ts(''));
162 $this->add('textarea', 'for_organization', ts('On behalf of Label'), $attributes['for_organization']);
163
164 // collect goal amount
165 $this->add('text', 'goal_amount', ts('Goal Amount'), array('size' => 8, 'maxlength' => 12));
166 $this->addRule('goal_amount', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
167
168 // is confirmation page enabled?
169 $this->addElement('checkbox', 'is_confirm_enabled', ts('Use a confirmation page?'));
170
171 // is this page shareable through social media ?
172 $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?'));
173
174 // is this page active ?
175 $this->addElement('checkbox', 'is_active', ts('Is this Online Contribution Page Active?'));
176
177 // should the honor be enabled
178 $this->addElement('checkbox', 'honor_block_is_active', ts('Honoree Section Enabled'), NULL, array('onclick' => "showHonor()"));
179
180 $this->add('text', 'honor_block_title', ts('Honoree Section Title'), array('maxlength' => 255, 'size' => 45));
181
182 $this->add('textarea', 'honor_block_text', ts('Honoree Introductory Message'), array('rows' => 2, 'cols' => 50));
183
184 $this->addSelect('soft_credit_types', array(
185 'label' => ts('Honor Types'),
186 'entity' => 'ContributionSoft',
187 'field' => 'soft_credit_type_id',
188 'multiple' => TRUE,
189 'class' => 'huge'
190 ));
191
192 $entities = array(
193 array(
194 'entity_name' => 'contact_1',
195 'entity_type' => 'IndividualModel',
196 ),
197 );
198
199 $allowCoreTypes = array_merge(array('Contact', 'Individual', 'Organization', 'Household'), CRM_Contact_BAO_ContactType::subTypes('Individual'));
200 $allowSubTypes = array();
201
202 $this->addProfileSelector('honoree_profile', ts('Honoree Profile'), $allowCoreTypes, $allowSubTypes, $entities);
203
204 if (!empty($this->_submitValues['honor_block_is_active'])) {
205 $this->addRule('soft_credit_types', ts('At least one value must be selected if Honor Section is active'), 'required');
206 $this->addRule('honoree_profile', ts('Please select a profile used for honoree'), 'required');
207 }
208
209 // add optional start and end dates
210 $this->addDateTime('start_date', ts('Start Date'));
211 $this->addDateTime('end_date', ts('End Date'));
212
213 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Settings', 'formRule'), $this);
214
215 parent::buildQuickForm();
216 }
217
218 /**
219 * global validation rules for the form
220 *
221 * @param array $values posted values of the form
222 *
223 * @param $files
224 * @param $self
225 *
226 * @return array list of errors to be posted back to the form
227 * @static
228 * @access public
229 */
230 static function formRule($values, $files, $self) {
231 $errors = array();
232 $contributionPageId = $self->_id;
233 //CRM-4286
234 if (strstr($values['title'], '/')) {
235 $errors['title'] = ts("Please do not use '/' in Title");
236 }
237
238 // ensure on-behalf-of profile meets minimum requirements
239 if (!empty($values['is_organization'])) {
240 if (empty($values['onbehalf_profile_id']) ) {
241 $errors['onbehalf_profile_id'] = ts('Please select a profile to collect organization information on this contribution page.');
242 }
243 else {
244 $requiredProfileFields = array('organization_name', 'email');
245 if (!CRM_Core_BAO_UFGroup::checkValidProfile($values['onbehalf_profile_id'], $requiredProfileFields)) {
246 $errors['onbehalf_profile_id'] = ts('Profile does not contain the minimum required fields for an On Behalf Of Organization');
247 }
248 }
249 }
250
251 //CRM-11494
252 $start = CRM_Utils_Date::processDate($values['start_date']);
253 $end = CRM_Utils_Date::processDate($values['end_date']);
254 if (($end < $start) && ($end != 0)) {
255 $errors['end_date'] = ts('End date should be after Start date.');
256 }
257
258 if (!empty($self->_values['payment_processor']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($values['financial_type_id'])) {
259 $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
260 }
261
262 //dont allow on behalf of save when
263 //pre or post profile consists of membership fields
264 if ($contributionPageId && !empty($values['is_organization'])) {
265 $ufJoinParams = array(
266 'module' => 'CiviContribute',
267 'entity_table' => 'civicrm_contribution_page',
268 'entity_id' => $contributionPageId,
269 );
270
271 list($contributionProfiles['custom_pre_id'],
272 $contributionProfiles['custom_post_id']
273 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
274
275 $conProfileType = NULL;
276 if ($contributionProfiles['custom_pre_id']) {
277 $preProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_pre_id']);
278 if ($preProfileType == 'Membership') {
279 $conProfileType = "'Includes Profile (top of page)'";
280 }
281 }
282
283 if ($contributionProfiles['custom_post_id']) {
284 $postProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_post_id']);
285 if ($postProfileType == 'Membership') {
286 $conProfileType = empty($conProfileType) ? "'Includes Profile (bottom of page)'" : "{$conProfileType} and 'Includes Profile (bottom of page)'";
287 }
288 }
289 if (!empty($conProfileType)) {
290 $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));
291 }
292 }
293 return $errors;
294 }
295
296 /**
297 * Process the form
298 *
299 * @return void
300 * @access public
301 */
302 public function postProcess() {
303 // get the submitted form values.
304 $params = $this->controller->exportValues($this->_name);
305
306 // we do this in case the user has hit the forward/back button
307 if ($this->_id) {
308 $params['id'] = $this->_id;
309 }
310 else {
311 $session = CRM_Core_Session::singleton();
312 $params['created_id'] = $session->get('userID');
313 $params['created_date'] = date('YmdHis');
314 $config = CRM_Core_Config::singleton();
315 $params['currency'] = $config->defaultCurrency;
316 }
317
318 $params['is_confirm_enabled'] = CRM_Utils_Array::value('is_confirm_enabled', $params, FALSE);
319 $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE);
320 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
321 $params['is_credit_card_only'] = CRM_Utils_Array::value('is_credit_card_only', $params, FALSE);
322 $params['honor_block_is_active'] = CRM_Utils_Array::value('honor_block_is_active', $params, FALSE);
323 $params['is_for_organization'] = !empty($params['is_organization']) ? CRM_Utils_Array::value('is_for_organization', $params, FALSE) : 0;
324
325 $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], $params['start_date_time'], TRUE);
326 $params['end_date'] = CRM_Utils_Date::processDate($params['end_date'], $params['end_date_time'], TRUE);
327
328 $params['goal_amount'] = CRM_Utils_Rule::cleanMoney($params['goal_amount']);
329
330 if (!$params['honor_block_is_active']) {
331 $params['honor_block_title'] = NULL;
332 $params['honor_block_text'] = NULL;
333 }
334 else {
335 $sctJSON = CRM_Contribute_BAO_ContributionPage::formatMultilingualHonorParams($params);
336 }
337
338 $dao = CRM_Contribute_BAO_ContributionPage::create($params);
339
340 $ufJoinParams = array(
341 'onbehalf_profile_id' =>
342 array(
343 'is_active' => 1,
344 'module' => 'OnBehalf',
345 'entity_table' => 'civicrm_contribution_page',
346 'entity_id' => $dao->id,
347 ),
348 'honor_block_is_active' =>
349 array(
350 'module' => 'soft_credit',
351 'entity_table' => 'civicrm_contribution_page',
352 'entity_id' => $dao->id,
353 )
354 );
355
356
357 foreach ($ufJoinParams as $index => $ufJoinParam) {
358 if (!empty($params[$index])) {
359 $ufJoinParam['weight'] = 1;
360 if ($index == 'honor_block_is_active') {
361 $ufJoinParam['is_active'] = 1;
362 $ufJoinParam['module'] = 'soft_credit';
363 $ufJoinParam['uf_group_id'] = $params['honoree_profile'];
364 $ufJoinParam['module_data'] = $sctJSON;
365 }
366 else {
367 // first delete all past entries
368 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParam);
369 $ufJoinParam['uf_group_id'] = $params[$index];
370 }
371 CRM_Core_BAO_UFJoin::create($ufJoinParam);
372 }
373 elseif ($index == 'honor_block_is_active') {
374 //On subsequent honor_block_is_active uncheck, disable(don't delete)
375 //that particular honoree profile entry in UFjoin table, CRM-13981
376 $ufId = CRM_Core_BAO_UFJoin::findJoinEntryId($ufJoinParam);
377 if ($ufId) {
378 $ufJoinParam['uf_group_id'] = CRM_Core_BAO_UFJoin::findUFGroupId($ufJoinParam);
379 $ufJoinParam['is_active'] = 0;
380 CRM_Core_BAO_UFJoin::create($ufJoinParam);
381 }
382 }
383 }
384
385 $this->set('id', $dao->id);
386 if ($this->_action & CRM_Core_Action::ADD) {
387 $url = 'civicrm/admin/contribute/amount';
388 $urlParams = "action=update&reset=1&id={$dao->id}";
389 // special case for 'Save and Done' consistency.
390 if ($this->controller->getButtonName('submit') == '_qf_Amount_upload_done') {
391 $url = 'civicrm/admin/contribute';
392 $urlParams = 'reset=1';
393 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
394 array(1 => $this->getTitle())
395 ), ts('Saved'), 'success');
396 }
397
398 CRM_Utils_System::redirect(CRM_Utils_System::url($url, $urlParams));
399 }
400 parent::endPostProcess();
401 }
402
403 /**
404 * Return a descriptive name for the page, used in wizard header
405 *
406 * @return string
407 * @access public
408 */
409 public function getTitle() {
410 return ts('Title and Settings');
411 }
412 }
413