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