Merge pull request #22290 from eileenmcnaughton/cont
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage.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 */
17
18/**
07f8d162 19 * Contribution Page form.
6a488035
TO
20 */
21class CRM_Contribute_Form_ContributionPage extends CRM_Core_Form {
22
23 /**
fe482240 24 * The page id saved to the session for an update.
6a488035
TO
25 *
26 * @var int
6a488035
TO
27 */
28 protected $_id;
29
30 /**
fe482240 31 * The pledgeBlock id saved to the session for an update.
6a488035
TO
32 *
33 * @var int
6a488035
TO
34 */
35 protected $_pledgeBlockID;
36
37 /**
100fef9d 38 * Are we in single form mode or wizard mode?
6a488035 39 *
d51c6add 40 * @var bool
6a488035
TO
41 */
42 protected $_single;
43
44 /**
100fef9d 45 * Is this the first page?
6a488035 46 *
d51c6add 47 * @var bool
6a488035
TO
48 */
49 protected $_first = FALSE;
50
5fe87df6
N
51 /**
52 * Is this the last page?
53 *
d51c6add 54 * @var bool
5fe87df6
N
55 */
56 protected $_last = FALSE;
57
6a488035 58 /**
100fef9d 59 * Store price set id.
6a488035
TO
60 *
61 * @var int
6a488035
TO
62 */
63 protected $_priceSetID = NULL;
64
65 protected $_values;
d5965a37 66
6e62b28c
TM
67 /**
68 * Explicitly declare the entity api name.
69 */
70 public function getDefaultEntity() {
71 return 'Contribution';
72 }
6a488035 73
a0a2c91d
MWMC
74 /**
75 * Explicitly declare the form context.
76 */
77 public function getDefaultContext() {
78 return 'create';
79 }
80
6a488035 81 /**
fe482240 82 * Set variables up before form is built.
6a488035
TO
83 */
84 public function preProcess() {
85 // current contribution page id
86 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive',
87 $this, FALSE, NULL, 'REQUEST'
88 );
89 $this->assign('contributionPageID', $this->_id);
90
91 // get the requested action
92 $this->_action = CRM_Utils_Request::retrieve('action', 'String',
93 // default to 'browse'
94 $this, FALSE, 'browse'
95 );
96
97 // setting title and 3rd level breadcrumb for html page if contrib page exists
98 if ($this->_id) {
99 $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'title');
100
101 if ($this->_action == CRM_Core_Action::UPDATE) {
102 $this->_single = TRUE;
103 }
104 }
105
e4fa553b 106 // CRM-16776 - show edit/copy/create buttons on Profiles Tab if user has required permission.
107 if (CRM_Core_Permission::check('administer CiviCRM')) {
108 $this->assign('perm', TRUE);
109 }
6a488035
TO
110 // set up tabs
111 CRM_Contribute_Form_ContributionPage_TabHeader::build($this);
112
113 if ($this->_action == CRM_Core_Action::UPDATE) {
1ca02e8b 114 $this->setTitle(ts('Configure Page - %1', [1 => $title]));
6a488035
TO
115 }
116 elseif ($this->_action == CRM_Core_Action::VIEW) {
1ca02e8b 117 $this->setTitle(ts('Preview Page - %1', [1 => $title]));
6a488035
TO
118 }
119 elseif ($this->_action == CRM_Core_Action::DELETE) {
1ca02e8b 120 $this->setTitle(ts('Delete Page - %1', [1 => $title]));
6a488035
TO
121 }
122
123 //cache values.
124 $this->_values = $this->get('values');
125 if (!is_array($this->_values)) {
be2fb01f 126 $this->_values = [];
6a488035 127 if (isset($this->_id) && $this->_id) {
be2fb01f 128 $params = ['id' => $this->_id];
6a488035 129 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $this->_values);
c0eb40c1 130 CRM_Contribute_BAO_ContributionPage::setValues($this->_id, $this->_values);
6a488035
TO
131 }
132 $this->set('values', $this->_values);
133 }
c50f55b5 134
bb08e888 135 // Check permission to edit contribution page
66af7c48 136 if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && $this->_action & CRM_Core_Action::UPDATE) {
bb08e888
E
137 $financialTypeID = CRM_Contribute_PseudoConstant::financialType($this->_values['financial_type_id']);
138 if (!CRM_Core_Permission::check('edit contributions of type ' . $financialTypeID)) {
beb414cc 139 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
bb08e888
E
140 }
141 }
142
c50f55b5 143 // Preload libraries required by the "Profiles" tab
be2fb01f 144 $schemas = ['IndividualModel', 'OrganizationModel', 'ContributionModel'];
c50f55b5
CW
145 if (in_array('CiviMember', CRM_Core_Config::singleton()->enableComponents)) {
146 $schemas[] = 'MembershipModel';
147 }
148 CRM_UF_Page_ProfileEditor::registerProfileScripts();
149 CRM_UF_Page_ProfileEditor::registerSchemas($schemas);
6a488035
TO
150 }
151
152 /**
fe482240 153 * Build the form object.
6a488035
TO
154 */
155 public function buildQuickForm() {
156 $this->applyFilter('__ALL__', 'trim');
157
158 $session = CRM_Core_Session::singleton();
9c1bc317 159 $this->_cancelURL = $_POST['cancelURL'] ?? NULL;
6a488035
TO
160
161 if (!$this->_cancelURL) {
162 $this->_cancelURL = CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1');
163 }
164
165 if ($this->_cancelURL) {
166 $this->addElement('hidden', 'cancelURL', $this->_cancelURL);
167 }
168
6a488035 169 if ($this->_single) {
be2fb01f
CW
170 $buttons = [
171 [
5fe87df6
N
172 'type' => 'next',
173 'name' => ts('Save'),
174 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
175 'isDefault' => TRUE,
be2fb01f
CW
176 ],
177 [
5fe87df6
N
178 'type' => 'upload',
179 'name' => ts('Save and Done'),
180 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
181 'subName' => 'done',
be2fb01f
CW
182 ],
183 ];
5fe87df6 184 if (!$this->_last) {
be2fb01f 185 $buttons[] = [
5fe87df6
N
186 'type' => 'submit',
187 'name' => ts('Save and Next'),
188 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
189 'subName' => 'savenext',
be2fb01f 190 ];
5fe87df6 191 }
be2fb01f 192 $buttons[] = [
5fe87df6
N
193 'type' => 'cancel',
194 'name' => ts('Cancel'),
be2fb01f 195 ];
5fe87df6 196 $this->addButtons($buttons);
6a488035
TO
197 }
198 else {
be2fb01f 199 $buttons = [];
6a488035 200 if (!$this->_first) {
be2fb01f 201 $buttons[] = [
6a488035 202 'type' => 'back',
f212d37d 203 'name' => ts('Previous'),
6a488035 204 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
be2fb01f 205 ];
6a488035 206 }
be2fb01f 207 $buttons[] = [
6a488035 208 'type' => 'next',
f212d37d 209 'name' => ts('Continue'),
6a488035
TO
210 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
211 'isDefault' => TRUE,
be2fb01f
CW
212 ];
213 $buttons[] = [
6a488035
TO
214 'type' => 'cancel',
215 'name' => ts('Cancel'),
be2fb01f 216 ];
6a488035
TO
217
218 $this->addButtons($buttons);
219 }
220
221 $session->replaceUserContext($this->_cancelURL);
222 // views are implemented as frozen form
223 if ($this->_action & CRM_Core_Action::VIEW) {
224 $this->freeze();
cbd83dde
AH
225 $this->addElement('xbutton', 'done', ts('Done'), [
226 'type' => 'button',
227 'onclick' => "location.href='civicrm/admin/custom/group?reset=1&action=browse'",
228 ]);
6a488035 229 }
133e2c99 230
1ebebaab
AH
231 // don't show option for contribution amounts section if membership price set
232 // this flag is sent to template
133e2c99 233
1ebebaab
AH
234 $membershipBlock = new CRM_Member_DAO_MembershipBlock();
235 $membershipBlock->entity_table = 'civicrm_contribution_page';
236 $membershipBlock->entity_id = $this->_id;
237 $membershipBlock->is_active = 1;
238 $hasMembershipBlk = FALSE;
133e2c99 239 if ($membershipBlock->find(TRUE) &&
1ebebaab
AH
240 ($setID = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, NULL, 1))
241 ) {
242 $extends = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'extends');
243 if ($extends && $extends == CRM_Core_Component::getComponentID('CiviMember')) {
244 $hasMembershipBlk = TRUE;
245 }
246 }
247 // set value in DOM that membership price set exists
be2fb01f 248 CRM_Core_Resources::singleton()->addSetting(['memberPriceset' => $hasMembershipBlk]);
6a488035
TO
249 }
250
251 /**
c490a46a 252 * Set default values for the form. Note that in edit/view mode
6a488035
TO
253 * the default values are retrieved from the database
254 *
6a488035 255 *
a6c01b45
CW
256 * @return array
257 * defaults
6a488035 258 */
00be9182 259 public function setDefaultValues() {
6a488035
TO
260 //some child classes calling setdefaults directly w/o preprocess.
261 $this->_values = $this->get('values');
262 if (!is_array($this->_values)) {
be2fb01f 263 $this->_values = [];
6a488035 264 if (isset($this->_id) && $this->_id) {
be2fb01f 265 $params = ['id' => $this->_id];
6a488035
TO
266 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $this->_values);
267 }
268 $this->set('values', $this->_values);
269 }
270 $defaults = $this->_values;
271
272 $config = CRM_Core_Config::singleton();
273 if (isset($this->_id)) {
274
275 //set defaults for pledgeBlock values.
be2fb01f 276 $pledgeBlockParams = [
6a488035
TO
277 'entity_id' => $this->_id,
278 'entity_table' => ts('civicrm_contribution_page'),
be2fb01f
CW
279 ];
280 $pledgeBlockDefaults = [];
6a488035
TO
281 CRM_Pledge_BAO_PledgeBlock::retrieve($pledgeBlockParams, $pledgeBlockDefaults);
282 if ($this->_pledgeBlockID = CRM_Utils_Array::value('id', $pledgeBlockDefaults)) {
283 $defaults['is_pledge_active'] = TRUE;
284 }
be2fb01f 285 $pledgeBlock = [
353ffa53
TO
286 'is_pledge_interval',
287 'max_reminders',
288 'initial_reminder_day',
289 'additional_reminder_day',
dccd9f4f
ERL
290 'pledge_start_date',
291 'is_pledge_start_date_visible',
292 'is_pledge_start_date_editable',
be2fb01f 293 ];
6a488035 294 foreach ($pledgeBlock as $key) {
9c1bc317 295 $defaults[$key] = $pledgeBlockDefaults[$key] ?? NULL;
de6c59ca 296 if ($key == 'pledge_start_date' && !empty($pledgeBlockDefaults[$key])) {
dccd9f4f 297 $defaultPledgeDate = (array) json_decode($pledgeBlockDefaults['pledge_start_date']);
be2fb01f 298 $pledgeDateFields = [
dccd9f4f
ERL
299 'pledge_calendar_date' => 'calendar_date',
300 'pledge_calendar_month' => 'calendar_month',
be2fb01f 301 ];
dccd9f4f
ERL
302 $defaults['pledge_default_toggle'] = key($defaultPledgeDate);
303 foreach ($pledgeDateFields as $key => $value) {
304 if (array_key_exists($value, $defaultPledgeDate)) {
305 $defaults[$key] = reset($defaultPledgeDate);
306 $this->assign($key, reset($defaultPledgeDate));
307 }
308 }
309 }
6a488035 310 }
a7488080 311 if (!empty($pledgeBlockDefaults['pledge_frequency_unit'])) {
6a488035 312 $defaults['pledge_frequency_unit'] = array_fill_keys(explode(CRM_Core_DAO::VALUE_SEPARATOR,
353ffa53
TO
313 $pledgeBlockDefaults['pledge_frequency_unit']
314 ), '1');
6a488035
TO
315 }
316
317 // fix the display of the monetary value, CRM-4038
318 if (isset($defaults['goal_amount'])) {
d90d1031 319 $defaults['goal_amount'] = CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($defaults['goal_amount']);
6a488035
TO
320 }
321
322 // get price set of type contributions
323 //this is the value for stored in db if price set extends contribution
324 $usedFor = 2;
9da8dc8c 325 $this->_priceSetID = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, $usedFor, 1);
6a488035
TO
326 if ($this->_priceSetID) {
327 $defaults['price_set_id'] = $this->_priceSetID;
328 }
6a488035
TO
329 }
330 else {
331 $defaults['is_active'] = 1;
332 // set current date as start date
e843748f 333 // @todo look to change to $defaults['start_date'] = date('Ymd His');
334 // main settings form overrides this to implement above but this is left here
335 // 'in case' another extending form uses start_date - for now
b59a905f 336 $defaults['start_date'] = date('Y-m-d H:i:s');
6a488035
TO
337 }
338
a7488080 339 if (!empty($defaults['recur_frequency_unit'])) {
6a488035 340 $defaults['recur_frequency_unit'] = array_fill_keys(explode(CRM_Core_DAO::VALUE_SEPARATOR,
353ffa53
TO
341 $defaults['recur_frequency_unit']
342 ), '1');
6a488035
TO
343 }
344 else {
09fbb309 345 // CRM-10860
be2fb01f 346 $defaults['recur_frequency_unit'] = ['month' => 1];
6a488035
TO
347 }
348
6a488035
TO
349 // confirm page starts out enabled
350 if (!isset($defaults['is_confirm_enabled'])) {
351 $defaults['is_confirm_enabled'] = 1;
352 }
353
354 return $defaults;
355 }
356
357 /**
fe482240 358 * Process the form.
6a488035
TO
359 */
360 public function postProcess() {
361 $pageId = $this->get('id');
362 //page is newly created.
363 if ($pageId && !$this->_id) {
364 $session = CRM_Core_Session::singleton();
365 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1'));
366 }
367 }
368
00be9182 369 public function endPostProcess() {
6a488035
TO
370 // make submit buttons keep the current working tab opened, or save and next tab
371 if ($this->_action & CRM_Core_Action::UPDATE) {
372 $className = CRM_Utils_String::getClassName($this->_name);
373
374 //retrieve list of pages from StateMachine and find next page
375 //this is quite painful because StateMachine is full of protected variables
376 //so we have to retrieve all pages, find current page, and then retrieve next
377 $stateMachine = new CRM_Contribute_StateMachine_ContributionPage($this);
353ffa53
TO
378 $states = $stateMachine->getStates();
379 $statesList = array_keys($states);
380 $currKey = array_search($className, $statesList);
381 $nextPage = (array_key_exists($currKey + 1, $statesList)) ? $statesList[$currKey + 1] : '';
6a488035
TO
382
383 //unfortunately, some classes don't map to subpage names, so we alter the exceptions
384
385 switch ($className) {
386 case 'Contribute':
353ffa53 387 $attributes = $this->getVar('_attributes');
b90552b7 388 $subPage = CRM_Utils_Request::retrieveComponent($attributes);
6a488035
TO
389 if ($subPage == 'friend') {
390 $nextPage = 'custom';
391 }
392 else {
393 $nextPage = 'settings';
394 }
395 break;
396
397 case 'MembershipBlock':
353ffa53 398 $subPage = 'membership';
353ffa53 399 $nextPage = 'thankyou';
6a488035
TO
400 break;
401
d2539131
EL
402 case 'Widget':
403 $subPage = 'widget';
404 $nextPage = 'pcp';
405 break;
406
6a488035 407 default:
353ffa53 408 $subPage = strtolower($className);
353ffa53 409 $nextPage = strtolower($nextPage);
6a488035
TO
410
411 if ($subPage == 'amount') {
412 $nextPage = 'membership';
413 }
414 elseif ($subPage == 'thankyou') {
415 $nextPage = 'friend';
416 }
417 break;
418 }
419
420 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
be2fb01f 421 [1 => CRM_Utils_Array::value('title', CRM_Utils_Array::value($subPage, $this->get('tabHeader')), $className)]
1ca02e8b 422 ), $this->getTitle(), 'success');
6a488035
TO
423
424 $this->postProcessHook();
425
426 if ($this->controller->getButtonName('submit') == "_qf_{$className}_next") {
427 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute/{$subPage}",
353ffa53
TO
428 "action=update&reset=1&id={$this->_id}"
429 ));
6a488035
TO
430 }
431 elseif ($this->controller->getButtonName('submit') == "_qf_{$className}_submit_savenext") {
432 if ($nextPage) {
433 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute/{$nextPage}",
353ffa53
TO
434 "action=update&reset=1&id={$this->_id}"
435 ));
6a488035
TO
436 }
437 else {
438 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute",
353ffa53
TO
439 "reset=1"
440 ));
6a488035
TO
441 }
442 }
443 else {
444 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute", 'reset=1'));
445 }
446 }
447 }
448
186c9c17 449 /**
fe482240 450 * Use the form name to create the tpl file name.
186c9c17
EM
451 *
452 * @return string
186c9c17 453 */
1330f57a 454
186c9c17
EM
455 /**
456 * @return string
457 */
00be9182 458 public function getTemplateFileName() {
cc4f2812 459 if ($this->controller->getPrint() || $this->getVar('_id') <= 0 ||
6a488035
TO
460 ($this->_action & CRM_Core_Action::DELETE) ||
461 (CRM_Utils_String::getClassName($this->_name) == 'AddProduct')
462 ) {
463 return parent::getTemplateFileName();
464 }
465 else {
466 // hack lets suppress the form rendering for now
467 self::$_template->assign('isForm', FALSE);
468 return 'CRM/Contribute/Form/ContributionPage/Tab.tpl';
469 }
470 }
96025800 471
6a488035 472}