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