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