Comment fix
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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
TO
55 *
56 * @var boolean
6a488035
TO
57 */
58 protected $_single;
59
60 /**
100fef9d 61 * Is this the first page?
6a488035
TO
62 *
63 * @var boolean
6a488035
TO
64 */
65 protected $_first = FALSE;
66
5fe87df6
N
67 /**
68 * Is this the last page?
69 *
70 * @var boolean
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) {
123 CRM_Utils_System::setTitle(ts('Configure Page - %1', array(1 => $title)));
124 }
125 elseif ($this->_action == CRM_Core_Action::VIEW) {
126 CRM_Utils_System::setTitle(ts('Preview Page - %1', array(1 => $title)));
127 }
128 elseif ($this->_action == CRM_Core_Action::DELETE) {
129 CRM_Utils_System::setTitle(ts('Delete Page - %1', array(1 => $title)));
130 }
131
132 //cache values.
133 $this->_values = $this->get('values');
134 if (!is_array($this->_values)) {
135 $this->_values = array();
136 if (isset($this->_id) && $this->_id) {
137 $params = array('id' => $this->_id);
138 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $this->_values);
139 }
140 $this->set('values', $this->_values);
141 }
c50f55b5
CW
142
143 // Preload libraries required by the "Profiles" tab
133e2c99 144 $schemas = array('IndividualModel', 'OrganizationModel', 'ContributionModel');
c50f55b5
CW
145 if (in_array('CiviMember', CRM_Core_Config::singleton()->enableComponents)) {
146 $schemas[] = 'MembershipModel';
147 }
148 CRM_UF_Page_ProfileEditor::registerProfileScripts();
149 CRM_UF_Page_ProfileEditor::registerSchemas($schemas);
6a488035
TO
150 }
151
152 /**
fe482240 153 * Build the form object.
6a488035
TO
154 */
155 public function buildQuickForm() {
156 $this->applyFilter('__ALL__', 'trim');
157
158 $session = CRM_Core_Session::singleton();
159 $this->_cancelURL = CRM_Utils_Array::value('cancelURL', $_POST);
160
161 if (!$this->_cancelURL) {
162 $this->_cancelURL = CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1');
163 }
164
165 if ($this->_cancelURL) {
166 $this->addElement('hidden', 'cancelURL', $this->_cancelURL);
167 }
168
6a488035 169 if ($this->_single) {
5fe87df6
N
170 $buttons = array(
171 array(
172 'type' => 'next',
173 'name' => ts('Save'),
174 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
175 'isDefault' => TRUE,
176 ),
177 array(
178 'type' => 'upload',
179 'name' => ts('Save and Done'),
180 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
181 'subName' => 'done',
182 ),
6a488035 183 );
5fe87df6
N
184 if (!$this->_last) {
185 $buttons[] = array(
186 'type' => 'submit',
187 'name' => ts('Save and Next'),
188 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
189 'subName' => 'savenext',
190 );
191 }
192 $buttons[] = array(
193 'type' => 'cancel',
194 'name' => ts('Cancel'),
195 );
196 $this->addButtons($buttons);
6a488035
TO
197 }
198 else {
199 $buttons = array();
200 if (!$this->_first) {
201 $buttons[] = array(
202 'type' => 'back',
f212d37d 203 'name' => ts('Previous'),
6a488035
TO
204 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
205 );
206 }
207 $buttons[] = array(
208 'type' => 'next',
f212d37d 209 'name' => ts('Continue'),
6a488035
TO
210 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
211 'isDefault' => TRUE,
212 );
213 $buttons[] = array(
214 'type' => 'cancel',
215 'name' => ts('Cancel'),
216 );
217
218 $this->addButtons($buttons);
219 }
220
221 $session->replaceUserContext($this->_cancelURL);
222 // views are implemented as frozen form
223 if ($this->_action & CRM_Core_Action::VIEW) {
224 $this->freeze();
225 $this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/custom/group?reset=1&action=browse'"));
226 }
133e2c99 227
1ebebaab
AH
228 // don't show option for contribution amounts section if membership price set
229 // this flag is sent to template
133e2c99 230
1ebebaab
AH
231 $membershipBlock = new CRM_Member_DAO_MembershipBlock();
232 $membershipBlock->entity_table = 'civicrm_contribution_page';
233 $membershipBlock->entity_id = $this->_id;
234 $membershipBlock->is_active = 1;
235 $hasMembershipBlk = FALSE;
133e2c99 236 if ($membershipBlock->find(TRUE) &&
1ebebaab
AH
237 ($setID = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, NULL, 1))
238 ) {
239 $extends = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'extends');
240 if ($extends && $extends == CRM_Core_Component::getComponentID('CiviMember')) {
241 $hasMembershipBlk = TRUE;
242 }
243 }
244 // set value in DOM that membership price set exists
245 CRM_Core_Resources::singleton()->addSetting(array('memberPriceset' => $hasMembershipBlk));
6a488035
TO
246 }
247
248 /**
c490a46a 249 * Set default values for the form. Note that in edit/view mode
6a488035
TO
250 * the default values are retrieved from the database
251 *
6a488035 252 *
a6c01b45
CW
253 * @return array
254 * defaults
6a488035 255 */
00be9182 256 public function setDefaultValues() {
6a488035
TO
257 //some child classes calling setdefaults directly w/o preprocess.
258 $this->_values = $this->get('values');
259 if (!is_array($this->_values)) {
260 $this->_values = array();
261 if (isset($this->_id) && $this->_id) {
262 $params = array('id' => $this->_id);
263 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $this->_values);
264 }
265 $this->set('values', $this->_values);
266 }
267 $defaults = $this->_values;
268
269 $config = CRM_Core_Config::singleton();
270 if (isset($this->_id)) {
271
272 //set defaults for pledgeBlock values.
273 $pledgeBlockParams = array(
274 'entity_id' => $this->_id,
275 'entity_table' => ts('civicrm_contribution_page'),
276 );
277 $pledgeBlockDefaults = array();
278 CRM_Pledge_BAO_PledgeBlock::retrieve($pledgeBlockParams, $pledgeBlockDefaults);
279 if ($this->_pledgeBlockID = CRM_Utils_Array::value('id', $pledgeBlockDefaults)) {
280 $defaults['is_pledge_active'] = TRUE;
281 }
282 $pledgeBlock = array(
353ffa53
TO
283 'is_pledge_interval',
284 'max_reminders',
285 'initial_reminder_day',
286 'additional_reminder_day',
6a488035
TO
287 );
288 foreach ($pledgeBlock as $key) {
289 $defaults[$key] = CRM_Utils_Array::value($key, $pledgeBlockDefaults);
290 }
a7488080 291 if (!empty($pledgeBlockDefaults['pledge_frequency_unit'])) {
6a488035 292 $defaults['pledge_frequency_unit'] = array_fill_keys(explode(CRM_Core_DAO::VALUE_SEPARATOR,
353ffa53
TO
293 $pledgeBlockDefaults['pledge_frequency_unit']
294 ), '1');
6a488035
TO
295 }
296
297 // fix the display of the monetary value, CRM-4038
298 if (isset($defaults['goal_amount'])) {
299 $defaults['goal_amount'] = CRM_Utils_Money::format($defaults['goal_amount'], NULL, '%a');
300 }
301
302 // get price set of type contributions
303 //this is the value for stored in db if price set extends contribution
304 $usedFor = 2;
9da8dc8c 305 $this->_priceSetID = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, $usedFor, 1);
6a488035
TO
306 if ($this->_priceSetID) {
307 $defaults['price_set_id'] = $this->_priceSetID;
308 }
309
a7488080 310 if (!empty($defaults['end_date'])) {
6a488035
TO
311 list($defaults['end_date'], $defaults['end_date_time']) = CRM_Utils_Date::setDateDefaults($defaults['end_date']);
312 }
313
a7488080 314 if (!empty($defaults['start_date'])) {
6a488035
TO
315 list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults($defaults['start_date']);
316 }
317 }
318 else {
319 $defaults['is_active'] = 1;
320 // set current date as start date
321 list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults();
322 }
323
a7488080 324 if (!empty($defaults['recur_frequency_unit'])) {
6a488035 325 $defaults['recur_frequency_unit'] = array_fill_keys(explode(CRM_Core_DAO::VALUE_SEPARATOR,
353ffa53
TO
326 $defaults['recur_frequency_unit']
327 ), '1');
6a488035
TO
328 }
329 else {
330 # CRM 10860
331 $defaults['recur_frequency_unit'] = array('month' => 1);
332 }
333
6a488035
TO
334 // confirm page starts out enabled
335 if (!isset($defaults['is_confirm_enabled'])) {
336 $defaults['is_confirm_enabled'] = 1;
337 }
338
339 return $defaults;
340 }
341
342 /**
fe482240 343 * Process the form.
6a488035
TO
344 */
345 public function postProcess() {
346 $pageId = $this->get('id');
347 //page is newly created.
348 if ($pageId && !$this->_id) {
349 $session = CRM_Core_Session::singleton();
350 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1'));
351 }
352 }
353
00be9182 354 public function endPostProcess() {
6a488035
TO
355 // make submit buttons keep the current working tab opened, or save and next tab
356 if ($this->_action & CRM_Core_Action::UPDATE) {
357 $className = CRM_Utils_String::getClassName($this->_name);
358
359 //retrieve list of pages from StateMachine and find next page
360 //this is quite painful because StateMachine is full of protected variables
361 //so we have to retrieve all pages, find current page, and then retrieve next
362 $stateMachine = new CRM_Contribute_StateMachine_ContributionPage($this);
353ffa53
TO
363 $states = $stateMachine->getStates();
364 $statesList = array_keys($states);
365 $currKey = array_search($className, $statesList);
366 $nextPage = (array_key_exists($currKey + 1, $statesList)) ? $statesList[$currKey + 1] : '';
6a488035
TO
367
368 //unfortunately, some classes don't map to subpage names, so we alter the exceptions
369
370 switch ($className) {
371 case 'Contribute':
353ffa53
TO
372 $attributes = $this->getVar('_attributes');
373 $subPage = strtolower(basename(CRM_Utils_Array::value('action', $attributes)));
874c9be7 374 $subPageName = ucfirst($subPage);
6a488035
TO
375 if ($subPage == 'friend') {
376 $nextPage = 'custom';
377 }
378 else {
379 $nextPage = 'settings';
380 }
381 break;
382
383 case 'MembershipBlock':
353ffa53 384 $subPage = 'membership';
6a488035 385 $subPageName = 'MembershipBlock';
353ffa53 386 $nextPage = 'thankyou';
6a488035
TO
387 break;
388
389 default:
353ffa53 390 $subPage = strtolower($className);
6a488035 391 $subPageName = $className;
353ffa53 392 $nextPage = strtolower($nextPage);
6a488035
TO
393
394 if ($subPage == 'amount') {
395 $nextPage = 'membership';
396 }
397 elseif ($subPage == 'thankyou') {
398 $nextPage = 'friend';
399 }
400 break;
401 }
402
403 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
353ffa53
TO
404 array(1 => $subPageName)
405 ), ts('Saved'), 'success');
6a488035
TO
406
407 $this->postProcessHook();
408
409 if ($this->controller->getButtonName('submit') == "_qf_{$className}_next") {
410 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute/{$subPage}",
353ffa53
TO
411 "action=update&reset=1&id={$this->_id}"
412 ));
6a488035
TO
413 }
414 elseif ($this->controller->getButtonName('submit') == "_qf_{$className}_submit_savenext") {
415 if ($nextPage) {
416 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute/{$nextPage}",
353ffa53
TO
417 "action=update&reset=1&id={$this->_id}"
418 ));
6a488035
TO
419 }
420 else {
421 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute",
353ffa53
TO
422 "reset=1"
423 ));
6a488035
TO
424 }
425 }
426 else {
427 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute", 'reset=1'));
428 }
429 }
430 }
431
186c9c17 432 /**
fe482240 433 * Use the form name to create the tpl file name.
186c9c17
EM
434 *
435 * @return string
186c9c17
EM
436 */
437 /**
438 * @return string
439 */
00be9182 440 public function getTemplateFileName() {
cc4f2812 441 if ($this->controller->getPrint() || $this->getVar('_id') <= 0 ||
6a488035
TO
442 ($this->_action & CRM_Core_Action::DELETE) ||
443 (CRM_Utils_String::getClassName($this->_name) == 'AddProduct')
444 ) {
445 return parent::getTemplateFileName();
446 }
447 else {
448 // hack lets suppress the form rendering for now
449 self::$_template->assign('isForm', FALSE);
450 return 'CRM/Contribute/Form/ContributionPage/Tab.tpl';
451 }
452 }
96025800 453
6a488035 454}