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