Fix opening specific contribution page tab from Manage Contribution Pages
[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
be2fb01f 160 $options = [];
6a488035
TO
161 $options[] = $this->createElement('radio', NULL, NULL, ts('Optional'), 1);
162 $options[] = $this->createElement('radio', NULL, NULL, ts('Required'), 2);
ba8f6a69 163 $this->addGroup($options, 'is_for_organization', '');
be2fb01f 164 $this->add('textarea', 'for_organization', ts('On behalf of Label'), ['rows' => 2, 'cols' => 50]);
6a488035
TO
165
166 // collect goal amount
be2fb01f
CW
167 $this->add('text', 'goal_amount', ts('Goal Amount'), ['size' => 8, 'maxlength' => 12]);
168 $this->addRule('goal_amount', ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money::format('99.99', ' ')]), 'money');
6a488035
TO
169
170 // is confirmation page enabled?
171 $this->addElement('checkbox', 'is_confirm_enabled', ts('Use a confirmation page?'));
172
173 // is this page shareable through social media ?
174 $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?'));
175
176 // is this page active ?
177 $this->addElement('checkbox', 'is_active', ts('Is this Online Contribution Page Active?'));
178
179 // should the honor be enabled
be2fb01f 180 $this->addElement('checkbox', 'honor_block_is_active', ts('Honoree Section Enabled'), NULL, ['onclick' => "showHonor()"]);
6a488035 181
be2fb01f 182 $this->add('text', 'honor_block_title', ts('Honoree Section Title'), ['maxlength' => 255, 'size' => 45]);
6a488035 183
be2fb01f 184 $this->add('textarea', 'honor_block_text', ts('Honoree Introductory Message'), ['rows' => 2, 'cols' => 50]);
133e2c99 185
be2fb01f 186 $this->addSelect('soft_credit_types', [
955b4327
CW
187 'label' => ts('Honor Types'),
188 'entity' => 'ContributionSoft',
189 'field' => 'soft_credit_type_id',
190 'multiple' => TRUE,
21dfd5f5 191 'class' => 'huge',
be2fb01f 192 ]);
133e2c99 193
be2fb01f
CW
194 $entities = [
195 [
41fd8e26 196 'entity_name' => 'contact_1',
197 'entity_type' => 'IndividualModel',
be2fb01f
CW
198 ],
199 ];
41fd8e26 200
be2fb01f 201 $allowCoreTypes = array_merge([
1330f57a
SL
202 'Contact',
203 'Individual',
204 'Organization',
205 'Household',
206 ], CRM_Contact_BAO_ContactType::subTypes('Individual'));
be2fb01f 207 $allowSubTypes = [];
133e2c99 208
209 $this->addProfileSelector('honoree_profile', ts('Honoree Profile'), $allowCoreTypes, $allowSubTypes, $entities);
41fd8e26 210
a7488080 211 if (!empty($this->_submitValues['honor_block_is_active'])) {
133e2c99 212 $this->addRule('soft_credit_types', ts('At least one value must be selected if Honor Section is active'), 'required');
213 $this->addRule('honoree_profile', ts('Please select a profile used for honoree'), 'required');
214 }
6a488035
TO
215
216 // add optional start and end dates
09fbb309 217 $this->add('datepicker', 'start_date', ts('Start Date'));
218 $this->add('datepicker', 'end_date', ts('End Date'));
6a488035 219
be2fb01f 220 $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Settings', 'formRule'], $this);
6a488035
TO
221
222 parent::buildQuickForm();
223 }
224
225 /**
fe482240 226 * Global validation rules for the form.
6a488035 227 *
014c4014
TO
228 * @param array $values
229 * Posted values of the form.
6a488035 230 *
dd244018
EM
231 * @param $files
232 * @param $self
233 *
a6c01b45
CW
234 * @return array
235 * list of errors to be posted back to the form
6a488035 236 */
00be9182 237 public static function formRule($values, $files, $self) {
be2fb01f 238 $errors = [];
3b67ab13 239 $contributionPageId = $self->_id;
6a488035
TO
240 //CRM-4286
241 if (strstr($values['title'], '/')) {
242 $errors['title'] = ts("Please do not use '/' in Title");
243 }
244
612cbad2 245 // ensure on-behalf-of profile meets minimum requirements
246 if (!empty($values['is_organization'])) {
481a74f4 247 if (empty($values['onbehalf_profile_id'])) {
612cbad2 248 $errors['onbehalf_profile_id'] = ts('Please select a profile to collect organization information on this contribution page.');
50c8cca4 249 }
250 else {
be2fb01f 251 $requiredProfileFields = ['organization_name', 'email'];
50c8cca4 252 if (!CRM_Core_BAO_UFGroup::checkValidProfile($values['onbehalf_profile_id'], $requiredProfileFields)) {
612cbad2 253 $errors['onbehalf_profile_id'] = ts('Profile does not contain the minimum required fields for an On Behalf Of Organization');
dd244018 254 }
612cbad2 255 }
6a488035 256 }
cde484fd 257
6a488035
TO
258 //CRM-11494
259 $start = CRM_Utils_Date::processDate($values['start_date']);
260 $end = CRM_Utils_Date::processDate($values['end_date']);
261 if (($end < $start) && ($end != 0)) {
262 $errors['end_date'] = ts('End date should be after Start date.');
263 }
133e2c99 264
a7488080 265 if (!empty($self->_values['payment_processor']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($values['financial_type_id'])) {
133e2c99 266 $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
3b67ab13 267 }
133e2c99 268
6a488035 269 //dont allow on behalf of save when
cde484fd 270 //pre or post profile consists of membership fields
8cc574cf 271 if ($contributionPageId && !empty($values['is_organization'])) {
be2fb01f 272 $ufJoinParams = [
6a488035
TO
273 'module' => 'CiviContribute',
274 'entity_table' => 'civicrm_contribution_page',
275 'entity_id' => $contributionPageId,
be2fb01f 276 ];
cde484fd 277
6a488035
TO
278 list($contributionProfiles['custom_pre_id'],
279 $contributionProfiles['custom_post_id']
353ffa53 280 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
6a488035
TO
281
282 $conProfileType = NULL;
283 if ($contributionProfiles['custom_pre_id']) {
284 $preProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_pre_id']);
285 if ($preProfileType == 'Membership') {
286 $conProfileType = "'Includes Profile (top of page)'";
287 }
288 }
133e2c99 289
6a488035
TO
290 if ($contributionProfiles['custom_post_id']) {
291 $postProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_post_id']);
292 if ($postProfileType == 'Membership') {
353ffa53 293 $conProfileType = empty($conProfileType) ? "'Includes Profile (bottom of page)'" : "{$conProfileType} and 'Includes Profile (bottom of page)'";
6a488035
TO
294 }
295 }
296 if (!empty($conProfileType)) {
be2fb01f 297 $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
298 }
299 }
300 return $errors;
301 }
302
303 /**
fe482240 304 * Process the form.
6a488035
TO
305 */
306 public function postProcess() {
307 // get the submitted form values.
308 $params = $this->controller->exportValues($this->_name);
309
310 // we do this in case the user has hit the forward/back button
311 if ($this->_id) {
312 $params['id'] = $this->_id;
313 }
314 else {
315 $session = CRM_Core_Session::singleton();
316 $params['created_id'] = $session->get('userID');
317 $params['created_date'] = date('YmdHis');
318 $config = CRM_Core_Config::singleton();
319 $params['currency'] = $config->defaultCurrency;
320 }
321
322 $params['is_confirm_enabled'] = CRM_Utils_Array::value('is_confirm_enabled', $params, FALSE);
323 $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE);
324 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
325 $params['is_credit_card_only'] = CRM_Utils_Array::value('is_credit_card_only', $params, FALSE);
326 $params['honor_block_is_active'] = CRM_Utils_Array::value('honor_block_is_active', $params, FALSE);
0d8afee2 327 $params['is_for_organization'] = !empty($params['is_organization']) ? CRM_Utils_Array::value('is_for_organization', $params, FALSE) : 0;
6a488035
TO
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 }
d1f8e278 334
6a488035
TO
335 $dao = CRM_Contribute_BAO_ContributionPage::create($params);
336
be2fb01f
CW
337 $ufJoinParams = [
338 'is_organization' => [
d1f8e278 339 'module' => 'on_behalf',
c301f76e 340 'entity_table' => 'civicrm_contribution_page',
341 'entity_id' => $dao->id,
be2fb01f
CW
342 ],
343 'honor_block_is_active' => [
c301f76e 344 'module' => 'soft_credit',
345 'entity_table' => 'civicrm_contribution_page',
346 'entity_id' => $dao->id,
be2fb01f
CW
347 ],
348 ];
6a488035 349
133e2c99 350 foreach ($ufJoinParams as $index => $ufJoinParam) {
a7488080 351 if (!empty($params[$index])) {
d1f8e278 352 // first delete all past entries
353 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParam);
354 $ufJoinParam['uf_group_id'] = $params[$index];
874c9be7 355 $ufJoinParam['weight'] = 1;
d1f8e278 356 $ufJoinParam['is_active'] = 1;
874c9be7 357 if ($index == 'honor_block_is_active') {
874c9be7 358 $ufJoinParam['uf_group_id'] = $params['honoree_profile'];
e63910c5 359 $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage::formatModuleData($params, FALSE, 'soft_credit');
874c9be7
TO
360 }
361 else {
d1f8e278 362 $ufJoinParam['uf_group_id'] = $params['onbehalf_profile_id'];
e63910c5 363 $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage::formatModuleData($params, FALSE, 'on_behalf');
133e2c99 364 }
874c9be7
TO
365 CRM_Core_BAO_UFJoin::create($ufJoinParam);
366 }
d1f8e278 367 else {
368 if ($index == 'honor_block_is_active') {
369 $params['honor_block_title'] = NULL;
370 $params['honor_block_text'] = NULL;
371 }
372 else {
373 $params['for_organization'] = NULL;
374 }
375
133e2c99 376 //On subsequent honor_block_is_active uncheck, disable(don't delete)
377 //that particular honoree profile entry in UFjoin table, CRM-13981
378 $ufId = CRM_Core_BAO_UFJoin::findJoinEntryId($ufJoinParam);
379 if ($ufId) {
380 $ufJoinParam['uf_group_id'] = CRM_Core_BAO_UFJoin::findUFGroupId($ufJoinParam);
381 $ufJoinParam['is_active'] = 0;
382 CRM_Core_BAO_UFJoin::create($ufJoinParam);
383 }
384 }
6a488035
TO
385 }
386
387 $this->set('id', $dao->id);
388 if ($this->_action & CRM_Core_Action::ADD) {
389 $url = 'civicrm/admin/contribute/amount';
390 $urlParams = "action=update&reset=1&id={$dao->id}";
391 // special case for 'Save and Done' consistency.
392 if ($this->controller->getButtonName('submit') == '_qf_Amount_upload_done') {
393 $url = 'civicrm/admin/contribute';
394 $urlParams = 'reset=1';
395 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
be2fb01f 396 [1 => $this->getTitle()]
353ffa53 397 ), ts('Saved'), 'success');
6a488035
TO
398 }
399
400 CRM_Utils_System::redirect(CRM_Utils_System::url($url, $urlParams));
401 }
402 parent::endPostProcess();
403 }
404
405 /**
406 * Return a descriptive name for the page, used in wizard header
407 *
408 * @return string
6a488035
TO
409 */
410 public function getTitle() {
411 return ts('Title and Settings');
412 }
96025800 413
6a488035 414}