Merge pull request #14891 from eileenmcnaughton/saved_search
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * Contribution Page form.
36 */
37 class CRM_Contribute_Form_ContributionPage extends CRM_Core_Form {
38
39 /**
40 * The page id saved to the session for an update.
41 *
42 * @var int
43 */
44 protected $_id;
45
46 /**
47 * The pledgeBlock id saved to the session for an update.
48 *
49 * @var int
50 */
51 protected $_pledgeBlockID;
52
53 /**
54 * Are we in single form mode or wizard mode?
55 *
56 * @var bool
57 */
58 protected $_single;
59
60 /**
61 * Is this the first page?
62 *
63 * @var bool
64 */
65 protected $_first = FALSE;
66
67 /**
68 * Is this the last page?
69 *
70 * @var bool
71 */
72 protected $_last = FALSE;
73
74 /**
75 * Store price set id.
76 *
77 * @var int
78 */
79 protected $_priceSetID = NULL;
80
81 protected $_values;
82
83 /**
84 * Explicitly declare the entity api name.
85 */
86 public function getDefaultEntity() {
87 return 'Contribution';
88 }
89
90 /**
91 * Explicitly declare the form context.
92 */
93 public function getDefaultContext() {
94 return 'create';
95 }
96
97 /**
98 * Set variables up before form is built.
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
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 }
126 // set up tabs
127 CRM_Contribute_Form_ContributionPage_TabHeader::build($this);
128
129 if ($this->_action == CRM_Core_Action::UPDATE) {
130 $this->setTitle(ts('Configure Page - %1', [1 => $title]));
131 }
132 elseif ($this->_action == CRM_Core_Action::VIEW) {
133 $this->setTitle(ts('Preview Page - %1', [1 => $title]));
134 }
135 elseif ($this->_action == CRM_Core_Action::DELETE) {
136 $this->setTitle(ts('Delete Page - %1', [1 => $title]));
137 }
138
139 //cache values.
140 $this->_values = $this->get('values');
141 if (!is_array($this->_values)) {
142 $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);
147 }
148 $this->set('values', $this->_values);
149 }
150
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.'));
156 }
157 }
158
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';
163 }
164 CRM_UF_Page_ProfileEditor::registerProfileScripts();
165 CRM_UF_Page_ProfileEditor::registerSchemas($schemas);
166 }
167
168 /**
169 * Build the form object.
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
185 if ($this->_single) {
186 $buttons = [
187 [
188 'type' => 'next',
189 'name' => ts('Save'),
190 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
191 'isDefault' => TRUE,
192 ],
193 [
194 'type' => 'upload',
195 'name' => ts('Save and Done'),
196 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
197 'subName' => 'done',
198 ],
199 ];
200 if (!$this->_last) {
201 $buttons[] = [
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',
206 ];
207 }
208 $buttons[] = [
209 'type' => 'cancel',
210 'name' => ts('Cancel'),
211 ];
212 $this->addButtons($buttons);
213 }
214 else {
215 $buttons = [];
216 if (!$this->_first) {
217 $buttons[] = [
218 'type' => 'back',
219 'name' => ts('Previous'),
220 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
221 ];
222 }
223 $buttons[] = [
224 'type' => 'next',
225 'name' => ts('Continue'),
226 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
227 'isDefault' => TRUE,
228 ];
229 $buttons[] = [
230 'type' => 'cancel',
231 'name' => ts('Cancel'),
232 ];
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();
241 $this->addElement('button', 'done', ts('Done'), ['onclick' => "location.href='civicrm/admin/custom/group?reset=1&action=browse'"]);
242 }
243
244 // don't show option for contribution amounts section if membership price set
245 // this flag is sent to template
246
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))
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
261 CRM_Core_Resources::singleton()->addSetting(['memberPriceset' => $hasMembershipBlk]);
262 }
263
264 /**
265 * Set default values for the form. Note that in edit/view mode
266 * the default values are retrieved from the database
267 *
268 *
269 * @return array
270 * defaults
271 */
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)) {
276 $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);
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.
289 $pledgeBlockParams = [
290 'entity_id' => $this->_id,
291 'entity_table' => ts('civicrm_contribution_page'),
292 ];
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;
297 }
298 $pledgeBlock = [
299 'is_pledge_interval',
300 'max_reminders',
301 'initial_reminder_day',
302 'additional_reminder_day',
303 'pledge_start_date',
304 'is_pledge_start_date_visible',
305 'is_pledge_start_date_editable',
306 ];
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',
314 ];
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 }
323 }
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']
327 ), '1');
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;
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;
341 }
342 }
343 else {
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');
350 }
351
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']
355 ), '1');
356 }
357 else {
358 // CRM-10860
359 $defaults['recur_frequency_unit'] = ['month' => 1];
360 }
361
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 /**
371 * Process the form.
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
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);
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);
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] : '';
395
396 //unfortunately, some classes don't map to subpage names, so we alter the exceptions
397
398 switch ($className) {
399 case 'Contribute':
400 $attributes = $this->getVar('_attributes');
401 $subPage = CRM_Utils_Request::retrieveComponent($attributes);
402 if ($subPage == 'friend') {
403 $nextPage = 'custom';
404 }
405 else {
406 $nextPage = 'settings';
407 }
408 break;
409
410 case 'MembershipBlock':
411 $subPage = 'membership';
412 $nextPage = 'thankyou';
413 break;
414
415 default:
416 $subPage = strtolower($className);
417 $nextPage = strtolower($nextPage);
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.",
429 [1 => CRM_Utils_Array::value('title', CRM_Utils_Array::value($subPage, $this->get('tabHeader')), $className)]
430 ), $this->getTitle(), 'success');
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}",
436 "action=update&reset=1&id={$this->_id}"
437 ));
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}",
442 "action=update&reset=1&id={$this->_id}"
443 ));
444 }
445 else {
446 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute",
447 "reset=1"
448 ));
449 }
450 }
451 else {
452 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute", 'reset=1'));
453 }
454 }
455 }
456
457 /**
458 * Use the form name to create the tpl file name.
459 *
460 * @return string
461 */
462
463 /**
464 * @return string
465 */
466 public function getTemplateFileName() {
467 if ($this->controller->getPrint() || $this->getVar('_id') <= 0 ||
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 }
479
480 }