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