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