Merge pull request #4950 from atif-shaikh/CRM-15826
[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 +--------------------------------------------------------------------+
26*/
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 /**
100fef9d 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 /**
100fef9d 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
69 /**
100fef9d 70 * Store price set id.
6a488035
TO
71 *
72 * @var int
6a488035
TO
73 */
74 protected $_priceSetID = NULL;
75
76 protected $_values;
77
78 /**
100fef9d 79 * Set variables up before form is built
6a488035
TO
80 *
81 * @return void
6a488035
TO
82 */
83 public function preProcess() {
84 // current contribution page id
85 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive',
86 $this, FALSE, NULL, 'REQUEST'
87 );
88 $this->assign('contributionPageID', $this->_id);
89
90 // get the requested action
91 $this->_action = CRM_Utils_Request::retrieve('action', 'String',
92 // default to 'browse'
93 $this, FALSE, 'browse'
94 );
95
96 // setting title and 3rd level breadcrumb for html page if contrib page exists
97 if ($this->_id) {
98 $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'title');
99
100 if ($this->_action == CRM_Core_Action::UPDATE) {
101 $this->_single = TRUE;
102 }
103 }
104
105 // set up tabs
106 CRM_Contribute_Form_ContributionPage_TabHeader::build($this);
107
108 if ($this->_action == CRM_Core_Action::UPDATE) {
109 CRM_Utils_System::setTitle(ts('Configure Page - %1', array(1 => $title)));
110 }
111 elseif ($this->_action == CRM_Core_Action::VIEW) {
112 CRM_Utils_System::setTitle(ts('Preview Page - %1', array(1 => $title)));
113 }
114 elseif ($this->_action == CRM_Core_Action::DELETE) {
115 CRM_Utils_System::setTitle(ts('Delete Page - %1', array(1 => $title)));
116 }
117
118 //cache values.
119 $this->_values = $this->get('values');
120 if (!is_array($this->_values)) {
121 $this->_values = array();
122 if (isset($this->_id) && $this->_id) {
123 $params = array('id' => $this->_id);
124 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $this->_values);
125 }
126 $this->set('values', $this->_values);
127 }
c50f55b5
CW
128
129 // Preload libraries required by the "Profiles" tab
133e2c99 130 $schemas = array('IndividualModel', 'OrganizationModel', 'ContributionModel');
c50f55b5
CW
131 if (in_array('CiviMember', CRM_Core_Config::singleton()->enableComponents)) {
132 $schemas[] = 'MembershipModel';
133 }
134 CRM_UF_Page_ProfileEditor::registerProfileScripts();
135 CRM_UF_Page_ProfileEditor::registerSchemas($schemas);
6a488035
TO
136 }
137
138 /**
c490a46a 139 * Build the form object
6a488035
TO
140 *
141 * @return void
6a488035
TO
142 */
143 public function buildQuickForm() {
144 $this->applyFilter('__ALL__', 'trim');
145
146 $session = CRM_Core_Session::singleton();
147 $this->_cancelURL = CRM_Utils_Array::value('cancelURL', $_POST);
148
149 if (!$this->_cancelURL) {
150 $this->_cancelURL = CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1');
151 }
152
153 if ($this->_cancelURL) {
154 $this->addElement('hidden', 'cancelURL', $this->_cancelURL);
155 }
156
6a488035
TO
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',
f212d37d 189 'name' => ts('Previous'),
6a488035
TO
190 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
191 );
192 }
193 $buttons[] = array(
194 'type' => 'next',
f212d37d 195 'name' => ts('Continue'),
6a488035
TO
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 }
133e2c99 213
1ebebaab
AH
214 // don't show option for contribution amounts section if membership price set
215 // this flag is sent to template
133e2c99 216
1ebebaab
AH
217 $membershipBlock = new CRM_Member_DAO_MembershipBlock();
218 $membershipBlock->entity_table = 'civicrm_contribution_page';
219 $membershipBlock->entity_id = $this->_id;
220 $membershipBlock->is_active = 1;
221 $hasMembershipBlk = FALSE;
133e2c99 222 if ($membershipBlock->find(TRUE) &&
1ebebaab
AH
223 ($setID = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, NULL, 1))
224 ) {
225 $extends = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'extends');
226 if ($extends && $extends == CRM_Core_Component::getComponentID('CiviMember')) {
227 $hasMembershipBlk = TRUE;
228 }
229 }
230 // set value in DOM that membership price set exists
231 CRM_Core_Resources::singleton()->addSetting(array('memberPriceset' => $hasMembershipBlk));
6a488035
TO
232 }
233
234 /**
c490a46a 235 * Set default values for the form. Note that in edit/view mode
6a488035
TO
236 * the default values are retrieved from the database
237 *
6a488035 238 *
a6c01b45
CW
239 * @return array
240 * defaults
6a488035 241 */
00be9182 242 public function setDefaultValues() {
6a488035
TO
243 //some child classes calling setdefaults directly w/o preprocess.
244 $this->_values = $this->get('values');
245 if (!is_array($this->_values)) {
246 $this->_values = array();
247 if (isset($this->_id) && $this->_id) {
248 $params = array('id' => $this->_id);
249 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $this->_values);
250 }
251 $this->set('values', $this->_values);
252 }
253 $defaults = $this->_values;
254
255 $config = CRM_Core_Config::singleton();
256 if (isset($this->_id)) {
257
258 //set defaults for pledgeBlock values.
259 $pledgeBlockParams = array(
260 'entity_id' => $this->_id,
261 'entity_table' => ts('civicrm_contribution_page'),
262 );
263 $pledgeBlockDefaults = array();
264 CRM_Pledge_BAO_PledgeBlock::retrieve($pledgeBlockParams, $pledgeBlockDefaults);
265 if ($this->_pledgeBlockID = CRM_Utils_Array::value('id', $pledgeBlockDefaults)) {
266 $defaults['is_pledge_active'] = TRUE;
267 }
268 $pledgeBlock = array(
353ffa53
TO
269 'is_pledge_interval',
270 'max_reminders',
271 'initial_reminder_day',
272 'additional_reminder_day',
6a488035
TO
273 );
274 foreach ($pledgeBlock as $key) {
275 $defaults[$key] = CRM_Utils_Array::value($key, $pledgeBlockDefaults);
276 }
a7488080 277 if (!empty($pledgeBlockDefaults['pledge_frequency_unit'])) {
6a488035 278 $defaults['pledge_frequency_unit'] = array_fill_keys(explode(CRM_Core_DAO::VALUE_SEPARATOR,
353ffa53
TO
279 $pledgeBlockDefaults['pledge_frequency_unit']
280 ), '1');
6a488035
TO
281 }
282
283 // fix the display of the monetary value, CRM-4038
284 if (isset($defaults['goal_amount'])) {
285 $defaults['goal_amount'] = CRM_Utils_Money::format($defaults['goal_amount'], NULL, '%a');
286 }
287
288 // get price set of type contributions
289 //this is the value for stored in db if price set extends contribution
290 $usedFor = 2;
9da8dc8c 291 $this->_priceSetID = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, $usedFor, 1);
6a488035
TO
292 if ($this->_priceSetID) {
293 $defaults['price_set_id'] = $this->_priceSetID;
294 }
295
a7488080 296 if (!empty($defaults['end_date'])) {
6a488035
TO
297 list($defaults['end_date'], $defaults['end_date_time']) = CRM_Utils_Date::setDateDefaults($defaults['end_date']);
298 }
299
a7488080 300 if (!empty($defaults['start_date'])) {
6a488035
TO
301 list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults($defaults['start_date']);
302 }
303 }
304 else {
305 $defaults['is_active'] = 1;
306 // set current date as start date
307 list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults();
308 }
309
310 if (!isset($defaults['for_organization'])) {
311 $defaults['for_organization'] = ts('I am contributing on behalf of an organization.');
312 }
313
a7488080 314 if (!empty($defaults['recur_frequency_unit'])) {
6a488035 315 $defaults['recur_frequency_unit'] = array_fill_keys(explode(CRM_Core_DAO::VALUE_SEPARATOR,
353ffa53
TO
316 $defaults['recur_frequency_unit']
317 ), '1');
6a488035
TO
318 }
319 else {
320 # CRM 10860
321 $defaults['recur_frequency_unit'] = array('month' => 1);
322 }
323
a7488080 324 if (!empty($defaults['is_for_organization'])) {
6a488035
TO
325 $defaults['is_organization'] = 1;
326 }
327 else {
328 $defaults['is_for_organization'] = 1;
329 }
330
331 // confirm page starts out enabled
332 if (!isset($defaults['is_confirm_enabled'])) {
333 $defaults['is_confirm_enabled'] = 1;
334 }
335
336 return $defaults;
337 }
338
339 /**
340 * Process the form
341 *
342 * @return void
6a488035
TO
343 */
344 public function postProcess() {
345 $pageId = $this->get('id');
346 //page is newly created.
347 if ($pageId && !$this->_id) {
348 $session = CRM_Core_Session::singleton();
349 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1'));
350 }
351 }
352
00be9182 353 public function endPostProcess() {
6a488035
TO
354 // make submit buttons keep the current working tab opened, or save and next tab
355 if ($this->_action & CRM_Core_Action::UPDATE) {
356 $className = CRM_Utils_String::getClassName($this->_name);
357
358 //retrieve list of pages from StateMachine and find next page
359 //this is quite painful because StateMachine is full of protected variables
360 //so we have to retrieve all pages, find current page, and then retrieve next
361 $stateMachine = new CRM_Contribute_StateMachine_ContributionPage($this);
353ffa53
TO
362 $states = $stateMachine->getStates();
363 $statesList = array_keys($states);
364 $currKey = array_search($className, $statesList);
365 $nextPage = (array_key_exists($currKey + 1, $statesList)) ? $statesList[$currKey + 1] : '';
6a488035
TO
366
367 //unfortunately, some classes don't map to subpage names, so we alter the exceptions
368
369 switch ($className) {
370 case 'Contribute':
353ffa53
TO
371 $attributes = $this->getVar('_attributes');
372 $subPage = strtolower(basename(CRM_Utils_Array::value('action', $attributes)));
874c9be7 373 $subPageName = ucfirst($subPage);
6a488035
TO
374 if ($subPage == 'friend') {
375 $nextPage = 'custom';
376 }
377 else {
378 $nextPage = 'settings';
379 }
380 break;
381
382 case 'MembershipBlock':
353ffa53 383 $subPage = 'membership';
6a488035 384 $subPageName = 'MembershipBlock';
353ffa53 385 $nextPage = 'thankyou';
6a488035
TO
386 break;
387
388 default:
353ffa53 389 $subPage = strtolower($className);
6a488035 390 $subPageName = $className;
353ffa53 391 $nextPage = strtolower($nextPage);
6a488035
TO
392
393 if ($subPage == 'amount') {
394 $nextPage = 'membership';
395 }
396 elseif ($subPage == 'thankyou') {
397 $nextPage = 'friend';
398 }
399 break;
400 }
401
402 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
353ffa53
TO
403 array(1 => $subPageName)
404 ), ts('Saved'), 'success');
6a488035
TO
405
406 $this->postProcessHook();
407
408 if ($this->controller->getButtonName('submit') == "_qf_{$className}_next") {
409 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute/{$subPage}",
353ffa53
TO
410 "action=update&reset=1&id={$this->_id}"
411 ));
6a488035
TO
412 }
413 elseif ($this->controller->getButtonName('submit') == "_qf_{$className}_submit_savenext") {
414 if ($nextPage) {
415 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute/{$nextPage}",
353ffa53
TO
416 "action=update&reset=1&id={$this->_id}"
417 ));
6a488035
TO
418 }
419 else {
420 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute",
353ffa53
TO
421 "reset=1"
422 ));
6a488035
TO
423 }
424 }
425 else {
426 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute", 'reset=1'));
427 }
428 }
429 }
430
186c9c17
EM
431 /**
432 * Use the form name to create the tpl file name
433 *
434 * @return string
186c9c17
EM
435 */
436 /**
437 * @return string
438 */
00be9182 439 public function getTemplateFileName() {
cc4f2812 440 if ($this->controller->getPrint() || $this->getVar('_id') <= 0 ||
6a488035
TO
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}