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