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