3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 * Contribution Page form.
37 class CRM_Contribute_Form_ContributionPage
extends CRM_Core_Form
{
40 * The page id saved to the session for an update.
47 * The pledgeBlock id saved to the session for an update.
51 protected $_pledgeBlockID;
54 * Are we in single form mode or wizard mode?
61 * Is this the first page?
65 protected $_first = FALSE;
68 * Is this the last page?
72 protected $_last = FALSE;
79 protected $_priceSetID = NULL;
84 * Explicitly declare the entity api name.
86 public function getDefaultEntity() {
87 return 'Contribution';
91 * Set variables up before form is built.
93 public function preProcess() {
94 // current contribution page id
95 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive',
96 $this, FALSE, NULL, 'REQUEST'
98 $this->assign('contributionPageID', $this->_id
);
100 // get the requested action
101 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String',
102 // default to 'browse'
103 $this, FALSE, 'browse'
106 // setting title and 3rd level breadcrumb for html page if contrib page exists
108 $title = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id
, 'title');
110 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
111 $this->_single
= TRUE;
115 // CRM-16776 - show edit/copy/create buttons on Profiles Tab if user has required permission.
116 if (CRM_Core_Permission
::check('administer CiviCRM')) {
117 $this->assign('perm', TRUE);
120 CRM_Contribute_Form_ContributionPage_TabHeader
::build($this);
122 if ($this->_action
== CRM_Core_Action
::UPDATE
) {
123 CRM_Utils_System
::setTitle(ts('Configure Page - %1', array(1 => $title)));
125 elseif ($this->_action
== CRM_Core_Action
::VIEW
) {
126 CRM_Utils_System
::setTitle(ts('Preview Page - %1', array(1 => $title)));
128 elseif ($this->_action
== CRM_Core_Action
::DELETE
) {
129 CRM_Utils_System
::setTitle(ts('Delete Page - %1', array(1 => $title)));
133 $this->_values
= $this->get('values');
134 if (!is_array($this->_values
)) {
135 $this->_values
= array();
136 if (isset($this->_id
) && $this->_id
) {
137 $params = array('id' => $this->_id
);
138 CRM_Core_DAO
::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $this->_values
);
140 $this->set('values', $this->_values
);
143 // Preload libraries required by the "Profiles" tab
144 $schemas = array('IndividualModel', 'OrganizationModel', 'ContributionModel');
145 if (in_array('CiviMember', CRM_Core_Config
::singleton()->enableComponents
)) {
146 $schemas[] = 'MembershipModel';
148 CRM_UF_Page_ProfileEditor
::registerProfileScripts();
149 CRM_UF_Page_ProfileEditor
::registerSchemas($schemas);
153 * Build the form object.
155 public function buildQuickForm() {
156 $this->applyFilter('__ALL__', 'trim');
158 $session = CRM_Core_Session
::singleton();
159 $this->_cancelURL
= CRM_Utils_Array
::value('cancelURL', $_POST);
161 if (!$this->_cancelURL
) {
162 $this->_cancelURL
= CRM_Utils_System
::url('civicrm/admin/contribute', 'reset=1');
165 if ($this->_cancelURL
) {
166 $this->addElement('hidden', 'cancelURL', $this->_cancelURL
);
169 if ($this->_single
) {
173 'name' => ts('Save'),
174 'spacing' => ' ',
179 'name' => ts('Save and Done'),
180 'spacing' => ' ',
187 'name' => ts('Save and Next'),
188 'spacing' => ' ',
189 'subName' => 'savenext',
194 'name' => ts('Cancel'),
196 $this->addButtons($buttons);
200 if (!$this->_first
) {
203 'name' => ts('Previous'),
204 'spacing' => ' ',
209 'name' => ts('Continue'),
210 'spacing' => ' ',
215 'name' => ts('Cancel'),
218 $this->addButtons($buttons);
221 $session->replaceUserContext($this->_cancelURL
);
222 // views are implemented as frozen form
223 if ($this->_action
& CRM_Core_Action
::VIEW
) {
225 $this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/custom/group?reset=1&action=browse'"));
228 // don't show option for contribution amounts section if membership price set
229 // this flag is sent to template
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))
239 $extends = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'extends');
240 if ($extends && $extends == CRM_Core_Component
::getComponentID('CiviMember')) {
241 $hasMembershipBlk = TRUE;
244 // set value in DOM that membership price set exists
245 CRM_Core_Resources
::singleton()->addSetting(array('memberPriceset' => $hasMembershipBlk));
249 * Set default values for the form. Note that in edit/view mode
250 * the default values are retrieved from the database
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
);
265 $this->set('values', $this->_values
);
267 $defaults = $this->_values
;
269 $config = CRM_Core_Config
::singleton();
270 if (isset($this->_id
)) {
272 //set defaults for pledgeBlock values.
273 $pledgeBlockParams = array(
274 'entity_id' => $this->_id
,
275 'entity_table' => ts('civicrm_contribution_page'),
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;
282 $pledgeBlock = array(
283 'is_pledge_interval',
285 'initial_reminder_day',
286 'additional_reminder_day',
288 foreach ($pledgeBlock as $key) {
289 $defaults[$key] = CRM_Utils_Array
::value($key, $pledgeBlockDefaults);
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']
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');
302 // get price set of type contributions
303 //this is the value for stored in db if price set extends contribution
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
;
310 if (!empty($defaults['end_date'])) {
311 list($defaults['end_date'], $defaults['end_date_time']) = CRM_Utils_Date
::setDateDefaults($defaults['end_date']);
314 if (!empty($defaults['start_date'])) {
315 list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date
::setDateDefaults($defaults['start_date']);
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();
324 if (!isset($defaults['for_organization'])) {
325 $defaults['for_organization'] = ts('I am contributing on behalf of an organization.');
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']
335 $defaults['recur_frequency_unit'] = array('month' => 1);
338 if (!empty($defaults['is_for_organization'])) {
339 $defaults['is_organization'] = 1;
342 $defaults['is_for_organization'] = 1;
345 // confirm page starts out enabled
346 if (!isset($defaults['is_confirm_enabled'])) {
347 $defaults['is_confirm_enabled'] = 1;
356 public function postProcess() {
357 $pageId = $this->get('id');
358 //page is newly created.
359 if ($pageId && !$this->_id
) {
360 $session = CRM_Core_Session
::singleton();
361 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/contribute', 'reset=1'));
365 public function endPostProcess() {
366 // make submit buttons keep the current working tab opened, or save and next tab
367 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
368 $className = CRM_Utils_String
::getClassName($this->_name
);
370 //retrieve list of pages from StateMachine and find next page
371 //this is quite painful because StateMachine is full of protected variables
372 //so we have to retrieve all pages, find current page, and then retrieve next
373 $stateMachine = new CRM_Contribute_StateMachine_ContributionPage($this);
374 $states = $stateMachine->getStates();
375 $statesList = array_keys($states);
376 $currKey = array_search($className, $statesList);
377 $nextPage = (array_key_exists($currKey +
1, $statesList)) ?
$statesList[$currKey +
1] : '';
379 //unfortunately, some classes don't map to subpage names, so we alter the exceptions
381 switch ($className) {
383 $attributes = $this->getVar('_attributes');
384 $subPage = strtolower(basename(CRM_Utils_Array
::value('action', $attributes)));
385 $subPageName = ucfirst($subPage);
386 if ($subPage == 'friend') {
387 $nextPage = 'custom';
390 $nextPage = 'settings';
394 case 'MembershipBlock':
395 $subPage = 'membership';
396 $subPageName = 'MembershipBlock';
397 $nextPage = 'thankyou';
401 $subPage = strtolower($className);
402 $subPageName = $className;
403 $nextPage = strtolower($nextPage);
405 if ($subPage == 'amount') {
406 $nextPage = 'membership';
408 elseif ($subPage == 'thankyou') {
409 $nextPage = 'friend';
414 CRM_Core_Session
::setStatus(ts("'%1' information has been saved.",
415 array(1 => $subPageName)
416 ), ts('Saved'), 'success');
418 $this->postProcessHook();
420 if ($this->controller
->getButtonName('submit') == "_qf_{$className}_next") {
421 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/admin/contribute/{$subPage}",
422 "action=update&reset=1&id={$this->_id}"
425 elseif ($this->controller
->getButtonName('submit') == "_qf_{$className}_submit_savenext") {
427 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/admin/contribute/{$nextPage}",
428 "action=update&reset=1&id={$this->_id}"
432 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/admin/contribute",
438 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/admin/contribute", 'reset=1'));
444 * Use the form name to create the tpl file name.
451 public function getTemplateFileName() {
452 if ($this->controller
->getPrint() ||
$this->getVar('_id') <= 0 ||
453 ($this->_action
& CRM_Core_Action
::DELETE
) ||
454 (CRM_Utils_String
::getClassName($this->_name
) == 'AddProduct')
456 return parent
::getTemplateFileName();
459 // hack lets suppress the form rendering for now
460 self
::$_template->assign('isForm', FALSE);
461 return 'CRM/Contribute/Form/ContributionPage/Tab.tpl';