3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Contribution Page form.
21 class CRM_Contribute_Form_ContributionPage
extends CRM_Core_Form
{
24 * The page id saved to the session for an update.
31 * The pledgeBlock id saved to the session for an update.
35 protected $_pledgeBlockID;
38 * Are we in single form mode or wizard mode?
45 * Is this the first page?
49 protected $_first = FALSE;
52 * Is this the last page?
56 protected $_last = FALSE;
63 protected $_priceSetID = NULL;
68 * Explicitly declare the entity api name.
70 public function getDefaultEntity() {
71 return 'Contribution';
75 * Explicitly declare the form context.
77 public function getDefaultContext() {
82 * Set variables up before form is built.
84 public function preProcess() {
85 // current contribution page id
86 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive',
87 $this, FALSE, NULL, 'REQUEST'
89 $this->assign('contributionPageID', $this->_id
);
91 // get the requested action
92 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String',
93 // default to 'browse'
94 $this, FALSE, 'browse'
97 // setting title and 3rd level breadcrumb for html page if contrib page exists
99 $title = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id
, 'title');
101 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
102 $this->_single
= TRUE;
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);
111 CRM_Contribute_Form_ContributionPage_TabHeader
::build($this);
113 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
114 $this->setTitle(ts('Configure Page - %1', [1 => $title]));
116 elseif ($this->_action
== CRM_Core_Action
::VIEW
) {
117 $this->setTitle(ts('Preview Page - %1', [1 => $title]));
119 elseif ($this->_action
== CRM_Core_Action
::DELETE
) {
120 $this->setTitle(ts('Delete Page - %1', [1 => $title]));
124 $this->_values
= $this->get('values');
125 if (!is_array($this->_values
)) {
127 if (isset($this->_id
) && $this->_id
) {
128 $params = ['id' => $this->_id
];
129 CRM_Core_DAO
::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $this->_values
);
130 CRM_Contribute_BAO_ContributionPage
::setValues($this->_id
, $this->_values
);
132 $this->set('values', $this->_values
);
135 // Check permission to edit contribution page
136 if (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus() && $this->_action
& CRM_Core_Action
::UPDATE
) {
137 $financialTypeID = CRM_Contribute_PseudoConstant
::financialType($this->_values
['financial_type_id']);
138 if (!CRM_Core_Permission
::check('edit contributions of type ' . $financialTypeID)) {
139 CRM_Core_Error
::statusBounce(ts('You do not have permission to access this page.'));
143 // Preload libraries required by the "Profiles" tab
144 $schemas = ['IndividualModel', 'OrganizationModel', 'ContributionModel'];
145 if (in_array('CiviMember', CRM_Core_Config
::singleton()->enableComponents
)) {
146 $schemas[] = 'MembershipModel';
148 CRM_UF_Page_ProfileEditor
::registerProfileScripts();
149 CRM_UF_Page_ProfileEditor
::registerSchemas($schemas);
153 * Build the form object.
155 public function buildQuickForm() {
156 $this->applyFilter('__ALL__', 'trim');
158 $session = CRM_Core_Session
::singleton();
159 $this->_cancelURL
= $_POST['cancelURL'] ??
NULL;
161 if (!$this->_cancelURL
) {
162 $this->_cancelURL
= CRM_Utils_System
::url('civicrm/admin/contribute', 'reset=1');
165 if ($this->_cancelURL
) {
166 $this->addElement('hidden', 'cancelURL', $this->_cancelURL
);
169 if ($this->_single
) {
173 'name' => ts('Save'),
174 'spacing' => ' ',
179 'name' => ts('Save and Done'),
180 'spacing' => ' ',
187 'name' => ts('Save and Next'),
188 'spacing' => ' ',
189 'subName' => 'savenext',
194 'name' => ts('Cancel'),
196 $this->addButtons($buttons);
200 if (!$this->_first
) {
203 'name' => ts('Previous'),
204 'spacing' => ' ',
209 'name' => ts('Continue'),
210 'spacing' => ' ',
215 'name' => ts('Cancel'),
218 $this->addButtons($buttons);
221 $session->replaceUserContext($this->_cancelURL
);
222 // views are implemented as frozen form
223 if ($this->_action
& CRM_Core_Action
::VIEW
) {
225 $this->addElement('xbutton', 'done', ts('Done'), [
227 'onclick' => "location.href='civicrm/admin/custom/group?reset=1&action=browse'",
231 // don't show option for contribution amounts section if membership price set
232 // this flag is sent to template
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;
239 if ($membershipBlock->find(TRUE) &&
240 ($setID = CRM_Price_BAO_PriceSet
::getFor('civicrm_contribution_page', $this->_id
, NULL, 1))
242 $extends = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'extends');
243 if ($extends && $extends == CRM_Core_Component
::getComponentID('CiviMember')) {
244 $hasMembershipBlk = TRUE;
247 // set value in DOM that membership price set exists
248 CRM_Core_Resources
::singleton()->addSetting(['memberPriceset' => $hasMembershipBlk]);
252 * Set default values for the form. Note that in edit/view mode
253 * the default values are retrieved from the database
259 public function setDefaultValues() {
260 //some child classes calling setdefaults directly w/o preprocess.
261 $this->_values
= $this->get('values');
262 if (!is_array($this->_values
)) {
264 if (isset($this->_id
) && $this->_id
) {
265 $params = ['id' => $this->_id
];
266 CRM_Core_DAO
::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $this->_values
);
268 $this->set('values', $this->_values
);
270 $defaults = $this->_values
;
272 $config = CRM_Core_Config
::singleton();
273 if (isset($this->_id
)) {
275 //set defaults for pledgeBlock values.
276 $pledgeBlockParams = [
277 'entity_id' => $this->_id
,
278 'entity_table' => ts('civicrm_contribution_page'),
280 $pledgeBlockDefaults = [];
281 CRM_Pledge_BAO_PledgeBlock
::retrieve($pledgeBlockParams, $pledgeBlockDefaults);
282 if ($this->_pledgeBlockID
= CRM_Utils_Array
::value('id', $pledgeBlockDefaults)) {
283 $defaults['is_pledge_active'] = TRUE;
286 'is_pledge_interval',
288 'initial_reminder_day',
289 'additional_reminder_day',
291 'is_pledge_start_date_visible',
292 'is_pledge_start_date_editable',
294 foreach ($pledgeBlock as $key) {
295 $defaults[$key] = $pledgeBlockDefaults[$key] ??
NULL;
296 if ($key == 'pledge_start_date' && !empty($pledgeBlockDefaults[$key])) {
297 $defaultPledgeDate = (array) json_decode($pledgeBlockDefaults['pledge_start_date']);
298 $pledgeDateFields = [
299 'pledge_calendar_date' => 'calendar_date',
300 'pledge_calendar_month' => 'calendar_month',
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));
311 if (!empty($pledgeBlockDefaults['pledge_frequency_unit'])) {
312 $defaults['pledge_frequency_unit'] = array_fill_keys(explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
313 $pledgeBlockDefaults['pledge_frequency_unit']
317 // fix the display of the monetary value, CRM-4038
318 if (isset($defaults['goal_amount'])) {
319 $defaults['goal_amount'] = CRM_Utils_Money
::format($defaults['goal_amount'], NULL, '%a');
322 // get price set of type contributions
323 //this is the value for stored in db if price set extends contribution
325 $this->_priceSetID
= CRM_Price_BAO_PriceSet
::getFor('civicrm_contribution_page', $this->_id
, $usedFor, 1);
326 if ($this->_priceSetID
) {
327 $defaults['price_set_id'] = $this->_priceSetID
;
331 $defaults['is_active'] = 1;
332 // set current date as start date
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
336 $defaults['start_date'] = date('Y-m-d H:i:s');
339 if (!empty($defaults['recur_frequency_unit'])) {
340 $defaults['recur_frequency_unit'] = array_fill_keys(explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
341 $defaults['recur_frequency_unit']
346 $defaults['recur_frequency_unit'] = ['month' => 1];
349 // confirm page starts out enabled
350 if (!isset($defaults['is_confirm_enabled'])) {
351 $defaults['is_confirm_enabled'] = 1;
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'));
369 public function endPostProcess() {
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
);
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);
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] : '';
383 //unfortunately, some classes don't map to subpage names, so we alter the exceptions
385 switch ($className) {
387 $attributes = $this->getVar('_attributes');
388 $subPage = CRM_Utils_Request
::retrieveComponent($attributes);
389 if ($subPage == 'friend') {
390 $nextPage = 'custom';
393 $nextPage = 'settings';
397 case 'MembershipBlock':
398 $subPage = 'membership';
399 $nextPage = 'thankyou';
408 $subPage = strtolower($className);
409 $nextPage = strtolower($nextPage);
411 if ($subPage == 'amount') {
412 $nextPage = 'membership';
414 elseif ($subPage == 'thankyou') {
415 $nextPage = 'friend';
420 CRM_Core_Session
::setStatus(ts("'%1' information has been saved.",
421 [1 => CRM_Utils_Array
::value('title', CRM_Utils_Array
::value($subPage, $this->get('tabHeader')), $className)]
422 ), $this->getTitle(), 'success');
424 $this->postProcessHook();
426 if ($this->controller
->getButtonName('submit') == "_qf_{$className}_next") {
427 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/admin/contribute/{$subPage}",
428 "action=update&reset=1&id={$this->_id}"
431 elseif ($this->controller
->getButtonName('submit') == "_qf_{$className}_submit_savenext") {
433 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/admin/contribute/{$nextPage}",
434 "action=update&reset=1&id={$this->_id}"
438 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/admin/contribute",
444 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/admin/contribute", 'reset=1'));
450 * Use the form name to create the tpl file name.
458 public function getTemplateFileName() {
459 if ($this->controller
->getPrint() ||
$this->getVar('_id') <= 0 ||
460 ($this->_action
& CRM_Core_Action
::DELETE
) ||
461 (CRM_Utils_String
::getClassName($this->_name
) == 'AddProduct')
463 return parent
::getTemplateFileName();
466 // hack lets suppress the form rendering for now
467 self
::$_template->assign('isForm', FALSE);
468 return 'CRM/Contribute/Form/ContributionPage/Tab.tpl';