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