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