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