INFRA-132 - CRM/Contribute - phpcbf
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 */
46 protected $_id;
47
48 /**
49 * The pledgeBlock id saved to the session for an update
50 *
51 * @var int
52 */
53 protected $_pledgeBlockID;
54
55 /**
56 * Are we in single form mode or wizard mode?
57 *
58 * @var boolean
59 */
60 protected $_single;
61
62 /**
63 * Is this the first page?
64 *
65 * @var boolean
66 */
67 protected $_first = FALSE;
68
69 /**
70 * Store price set id.
71 *
72 * @var int
73 */
74 protected $_priceSetID = NULL;
75
76 protected $_values;
77
78 /**
79 * Set variables up before form is built
80 *
81 * @return void
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 }
128
129 // Preload libraries required by the "Profiles" tab
130 $schemas = array('IndividualModel', 'OrganizationModel', 'ContributionModel');
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);
136 }
137
138 /**
139 * Build the form object
140 *
141 * @return void
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
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 // don't show option for contribution amounts section if membership price set
215 // this flag is sent to template
216
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;
222 if ($membershipBlock->find(TRUE) &&
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));
232 }
233
234 /**
235 * Set default values for the form. Note that in edit/view mode
236 * the default values are retrieved from the database
237 *
238 *
239 * @return array defaults
240 */
241 public function setDefaultValues() {
242 //some child classes calling setdefaults directly w/o preprocess.
243 $this->_values = $this->get('values');
244 if (!is_array($this->_values)) {
245 $this->_values = array();
246 if (isset($this->_id) && $this->_id) {
247 $params = array('id' => $this->_id);
248 CRM_Core_DAO::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $this->_values);
249 }
250 $this->set('values', $this->_values);
251 }
252 $defaults = $this->_values;
253
254 $config = CRM_Core_Config::singleton();
255 if (isset($this->_id)) {
256
257 //set defaults for pledgeBlock values.
258 $pledgeBlockParams = array(
259 'entity_id' => $this->_id,
260 'entity_table' => ts('civicrm_contribution_page'),
261 );
262 $pledgeBlockDefaults = array();
263 CRM_Pledge_BAO_PledgeBlock::retrieve($pledgeBlockParams, $pledgeBlockDefaults);
264 if ($this->_pledgeBlockID = CRM_Utils_Array::value('id', $pledgeBlockDefaults)) {
265 $defaults['is_pledge_active'] = TRUE;
266 }
267 $pledgeBlock = array(
268 'is_pledge_interval', 'max_reminders',
269 'initial_reminder_day', 'additional_reminder_day',
270 );
271 foreach ($pledgeBlock as $key) {
272 $defaults[$key] = CRM_Utils_Array::value($key, $pledgeBlockDefaults);
273 }
274 if (!empty($pledgeBlockDefaults['pledge_frequency_unit'])) {
275 $defaults['pledge_frequency_unit'] = array_fill_keys(explode(CRM_Core_DAO::VALUE_SEPARATOR,
276 $pledgeBlockDefaults['pledge_frequency_unit']
277 ), '1');
278 }
279
280 // fix the display of the monetary value, CRM-4038
281 if (isset($defaults['goal_amount'])) {
282 $defaults['goal_amount'] = CRM_Utils_Money::format($defaults['goal_amount'], NULL, '%a');
283 }
284
285 // get price set of type contributions
286 //this is the value for stored in db if price set extends contribution
287 $usedFor = 2;
288 $this->_priceSetID = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $this->_id, $usedFor, 1);
289 if ($this->_priceSetID) {
290 $defaults['price_set_id'] = $this->_priceSetID;
291 }
292
293 if (!empty($defaults['end_date'])) {
294 list($defaults['end_date'], $defaults['end_date_time']) = CRM_Utils_Date::setDateDefaults($defaults['end_date']);
295 }
296
297 if (!empty($defaults['start_date'])) {
298 list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults($defaults['start_date']);
299 }
300 }
301 else {
302 $defaults['is_active'] = 1;
303 // set current date as start date
304 list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date::setDateDefaults();
305 }
306
307 if (!isset($defaults['for_organization'])) {
308 $defaults['for_organization'] = ts('I am contributing on behalf of an organization.');
309 }
310
311 if (!empty($defaults['recur_frequency_unit'])) {
312 $defaults['recur_frequency_unit'] = array_fill_keys(explode(CRM_Core_DAO::VALUE_SEPARATOR,
313 $defaults['recur_frequency_unit']
314 ), '1');
315 }
316 else {
317 # CRM 10860
318 $defaults['recur_frequency_unit'] = array('month' => 1);
319 }
320
321 if (!empty($defaults['is_for_organization'])) {
322 $defaults['is_organization'] = 1;
323 }
324 else {
325 $defaults['is_for_organization'] = 1;
326 }
327
328 // confirm page starts out enabled
329 if (!isset($defaults['is_confirm_enabled'])) {
330 $defaults['is_confirm_enabled'] = 1;
331 }
332
333 return $defaults;
334 }
335
336 /**
337 * Process the form
338 *
339 * @return void
340 */
341 public function postProcess() {
342 $pageId = $this->get('id');
343 //page is newly created.
344 if ($pageId && !$this->_id) {
345 $session = CRM_Core_Session::singleton();
346 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1'));
347 }
348 }
349
350 public function endPostProcess() {
351 // make submit buttons keep the current working tab opened, or save and next tab
352 if ($this->_action & CRM_Core_Action::UPDATE) {
353 $className = CRM_Utils_String::getClassName($this->_name);
354
355 //retrieve list of pages from StateMachine and find next page
356 //this is quite painful because StateMachine is full of protected variables
357 //so we have to retrieve all pages, find current page, and then retrieve next
358 $stateMachine = new CRM_Contribute_StateMachine_ContributionPage($this);
359 $states = $stateMachine->getStates();
360 $statesList = array_keys($states);
361 $currKey = array_search($className, $statesList);
362 $nextPage = (array_key_exists($currKey + 1, $statesList)) ? $statesList[$currKey + 1] : '';
363
364 //unfortunately, some classes don't map to subpage names, so we alter the exceptions
365
366 switch ($className) {
367 case 'Contribute':
368 $attributes = $this->getVar('_attributes');
369 $subPage = strtolower(basename(CRM_Utils_Array::value('action', $attributes)));
370 $subPageName = ucfirst($subPage);
371 if ($subPage == 'friend') {
372 $nextPage = 'custom';
373 }
374 else {
375 $nextPage = 'settings';
376 }
377 break;
378
379 case 'MembershipBlock':
380 $subPage = 'membership';
381 $subPageName = 'MembershipBlock';
382 $nextPage = 'thankyou';
383 break;
384
385 default:
386 $subPage = strtolower($className);
387 $subPageName = $className;
388 $nextPage = strtolower($nextPage);
389
390 if ($subPage == 'amount') {
391 $nextPage = 'membership';
392 }
393 elseif ($subPage == 'thankyou') {
394 $nextPage = 'friend';
395 }
396 break;
397 }
398
399 CRM_Core_Session::setStatus(ts("'%1' information has been saved.",
400 array(1 => $subPageName)
401 ), ts('Saved'), 'success');
402
403 $this->postProcessHook();
404
405 if ($this->controller->getButtonName('submit') == "_qf_{$className}_next") {
406 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute/{$subPage}",
407 "action=update&reset=1&id={$this->_id}"
408 ));
409 }
410 elseif ($this->controller->getButtonName('submit') == "_qf_{$className}_submit_savenext") {
411 if ($nextPage) {
412 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute/{$nextPage}",
413 "action=update&reset=1&id={$this->_id}"
414 ));
415 }
416 else {
417 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute",
418 "reset=1"
419 ));
420 }
421 }
422 else {
423 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/admin/contribute", 'reset=1'));
424 }
425 }
426 }
427
428 /**
429 * Use the form name to create the tpl file name
430 *
431 * @return string
432 */
433 /**
434 * @return string
435 */
436 public function getTemplateFileName() {
437 if ($this->controller->getPrint() || $this->getVar('_id') <= 0 ||
438 ($this->_action & CRM_Core_Action::DELETE) ||
439 (CRM_Utils_String::getClassName($this->_name) == 'AddProduct')
440 ) {
441 return parent::getTemplateFileName();
442 }
443 else {
444 // hack lets suppress the form rendering for now
445 self::$_template->assign('isForm', FALSE);
446 return 'CRM/Contribute/Form/ContributionPage/Tab.tpl';
447 }
448 }
449 }