3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * Contribution Page form.
37 class CRM_Contribute_Form_ContributionPage
extends CRM_Core_Form
{
40 * The page id saved to the session for an update.
47 * The pledgeBlock id saved to the session for an update.
51 protected $_pledgeBlockID;
54 * Are we in single form mode or wizard mode?
61 * Is this the first page?
65 protected $_first = FALSE;
68 * Is this the last page?
72 protected $_last = FALSE;
79 protected $_priceSetID = NULL;
84 * Explicitly declare the entity api name.
86 public function getDefaultEntity() {
87 return 'Contribution';
91 * Explicitly declare the form context.
93 public function getDefaultContext() {
98 * Set variables up before form is built.
100 public function preProcess() {
101 // current contribution page id
102 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive',
103 $this, FALSE, NULL, 'REQUEST'
105 $this->assign('contributionPageID', $this->_id
);
107 // get the requested action
108 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String',
109 // default to 'browse'
110 $this, FALSE, 'browse'
113 // setting title and 3rd level breadcrumb for html page if contrib page exists
115 $title = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id
, 'title');
117 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
118 $this->_single
= TRUE;
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);
127 CRM_Contribute_Form_ContributionPage_TabHeader
::build($this);
129 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
130 $this->setTitle(ts('Configure Page - %1', [1 => $title]));
132 elseif ($this->_action
== CRM_Core_Action
::VIEW
) {
133 $this->setTitle(ts('Preview Page - %1', [1 => $title]));
135 elseif ($this->_action
== CRM_Core_Action
::DELETE
) {
136 $this->setTitle(ts('Delete Page - %1', [1 => $title]));
140 $this->_values
= $this->get('values');
141 if (!is_array($this->_values
)) {
143 if (isset($this->_id
) && $this->_id
) {
144 $params = ['id' => $this->_id
];
145 CRM_Core_DAO
::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $this->_values
);
146 CRM_Contribute_BAO_ContributionPage
::setValues($this->_id
, $this->_values
);
148 $this->set('values', $this->_values
);
151 // Check permission to edit contribution page
152 if (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus() && $this->_action
& CRM_Core_Action
::UPDATE
) {
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.'));
159 // Preload libraries required by the "Profiles" tab
160 $schemas = ['IndividualModel', 'OrganizationModel', 'ContributionModel'];
161 if (in_array('CiviMember', CRM_Core_Config
::singleton()->enableComponents
)) {
162 $schemas[] = 'MembershipModel';
164 CRM_UF_Page_ProfileEditor
::registerProfileScripts();
165 CRM_UF_Page_ProfileEditor
::registerSchemas($schemas);
169 * Build the form object.
171 public function buildQuickForm() {
172 $this->applyFilter('__ALL__', 'trim');
174 $session = CRM_Core_Session
::singleton();
175 $this->_cancelURL
= CRM_Utils_Array
::value('cancelURL', $_POST);
177 if (!$this->_cancelURL
) {
178 $this->_cancelURL
= CRM_Utils_System
::url('civicrm/admin/contribute', 'reset=1');
181 if ($this->_cancelURL
) {
182 $this->addElement('hidden', 'cancelURL', $this->_cancelURL
);
185 if ($this->_single
) {
189 'name' => ts('Save'),
190 'spacing' => ' ',
195 'name' => ts('Save and Done'),
196 'spacing' => ' ',
203 'name' => ts('Save and Next'),
204 'spacing' => ' ',
205 'subName' => 'savenext',
210 'name' => ts('Cancel'),
212 $this->addButtons($buttons);
216 if (!$this->_first
) {
219 'name' => ts('Previous'),
220 'spacing' => ' ',
225 'name' => ts('Continue'),
226 'spacing' => ' ',
231 'name' => ts('Cancel'),
234 $this->addButtons($buttons);
237 $session->replaceUserContext($this->_cancelURL
);
238 // views are implemented as frozen form
239 if ($this->_action
& CRM_Core_Action
::VIEW
) {
241 $this->addElement('button', 'done', ts('Done'), ['onclick' => "location.href='civicrm/admin/custom/group?reset=1&action=browse'"]);
244 // don't show option for contribution amounts section if membership price set
245 // this flag is sent to template
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;
252 if ($membershipBlock->find(TRUE) &&
253 ($setID = CRM_Price_BAO_PriceSet
::getFor('civicrm_contribution_page', $this->_id
, NULL, 1))
255 $extends = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'extends');
256 if ($extends && $extends == CRM_Core_Component
::getComponentID('CiviMember')) {
257 $hasMembershipBlk = TRUE;
260 // set value in DOM that membership price set exists
261 CRM_Core_Resources
::singleton()->addSetting(['memberPriceset' => $hasMembershipBlk]);
265 * Set default values for the form. Note that in edit/view mode
266 * the default values are retrieved from the database
272 public function setDefaultValues() {
273 //some child classes calling setdefaults directly w/o preprocess.
274 $this->_values
= $this->get('values');
275 if (!is_array($this->_values
)) {
277 if (isset($this->_id
) && $this->_id
) {
278 $params = ['id' => $this->_id
];
279 CRM_Core_DAO
::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $this->_values
);
281 $this->set('values', $this->_values
);
283 $defaults = $this->_values
;
285 $config = CRM_Core_Config
::singleton();
286 if (isset($this->_id
)) {
288 //set defaults for pledgeBlock values.
289 $pledgeBlockParams = [
290 'entity_id' => $this->_id
,
291 'entity_table' => ts('civicrm_contribution_page'),
293 $pledgeBlockDefaults = [];
294 CRM_Pledge_BAO_PledgeBlock
::retrieve($pledgeBlockParams, $pledgeBlockDefaults);
295 if ($this->_pledgeBlockID
= CRM_Utils_Array
::value('id', $pledgeBlockDefaults)) {
296 $defaults['is_pledge_active'] = TRUE;
299 'is_pledge_interval',
301 'initial_reminder_day',
302 'additional_reminder_day',
304 'is_pledge_start_date_visible',
305 'is_pledge_start_date_editable',
307 foreach ($pledgeBlock as $key) {
308 $defaults[$key] = CRM_Utils_Array
::value($key, $pledgeBlockDefaults);
309 if ($key == 'pledge_start_date' && !empty($pledgeBlockDefaults[$key])) {
310 $defaultPledgeDate = (array) json_decode($pledgeBlockDefaults['pledge_start_date']);
311 $pledgeDateFields = [
312 'pledge_calendar_date' => 'calendar_date',
313 'pledge_calendar_month' => 'calendar_month',
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));
324 if (!empty($pledgeBlockDefaults['pledge_frequency_unit'])) {
325 $defaults['pledge_frequency_unit'] = array_fill_keys(explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
326 $pledgeBlockDefaults['pledge_frequency_unit']
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');
335 // get price set of type contributions
336 //this is the value for stored in db if price set extends contribution
338 $this->_priceSetID
= CRM_Price_BAO_PriceSet
::getFor('civicrm_contribution_page', $this->_id
, $usedFor, 1);
339 if ($this->_priceSetID
) {
340 $defaults['price_set_id'] = $this->_priceSetID
;
344 $defaults['is_active'] = 1;
345 // set current date as start date
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
349 $defaults['start_date'] = date('Y-m-d H:i:s');
352 if (!empty($defaults['recur_frequency_unit'])) {
353 $defaults['recur_frequency_unit'] = array_fill_keys(explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
354 $defaults['recur_frequency_unit']
359 $defaults['recur_frequency_unit'] = ['month' => 1];
362 // confirm page starts out enabled
363 if (!isset($defaults['is_confirm_enabled'])) {
364 $defaults['is_confirm_enabled'] = 1;
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'));
382 public function endPostProcess() {
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
);
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);
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] : '';
396 //unfortunately, some classes don't map to subpage names, so we alter the exceptions
398 switch ($className) {
400 $attributes = $this->getVar('_attributes');
401 $subPage = CRM_Utils_Request
::retrieveComponent($attributes);
402 if ($subPage == 'friend') {
403 $nextPage = 'custom';
406 $nextPage = 'settings';
410 case 'MembershipBlock':
411 $subPage = 'membership';
412 $nextPage = 'thankyou';
421 $subPage = strtolower($className);
422 $nextPage = strtolower($nextPage);
424 if ($subPage == 'amount') {
425 $nextPage = 'membership';
427 elseif ($subPage == 'thankyou') {
428 $nextPage = 'friend';
433 CRM_Core_Session
::setStatus(ts("'%1' information has been saved.",
434 [1 => CRM_Utils_Array
::value('title', CRM_Utils_Array
::value($subPage, $this->get('tabHeader')), $className)]
435 ), $this->getTitle(), 'success');
437 $this->postProcessHook();
439 if ($this->controller
->getButtonName('submit') == "_qf_{$className}_next") {
440 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/admin/contribute/{$subPage}",
441 "action=update&reset=1&id={$this->_id}"
444 elseif ($this->controller
->getButtonName('submit') == "_qf_{$className}_submit_savenext") {
446 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/admin/contribute/{$nextPage}",
447 "action=update&reset=1&id={$this->_id}"
451 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/admin/contribute",
457 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/admin/contribute", 'reset=1'));
463 * Use the form name to create the tpl file name.
471 public function getTemplateFileName() {
472 if ($this->controller
->getPrint() ||
$this->getVar('_id') <= 0 ||
473 ($this->_action
& CRM_Core_Action
::DELETE
) ||
474 (CRM_Utils_String
::getClassName($this->_name
) == 'AddProduct')
476 return parent
::getTemplateFileName();
479 // hack lets suppress the form rendering for now
480 self
::$_template->assign('isForm', FALSE);
481 return 'CRM/Contribute/Form/ContributionPage/Tab.tpl';