3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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
);
139 CRM_Contribute_BAO_ContributionPage
::setValues($this->_id
, $this->_values
);
141 $this->set('values', $this->_values
);
144 // Check permission to edit contribution page
145 if (CRM_Financial_BAO_FinancialType
::isACLFinancialTypeStatus() && $this->_action
& CRM_Core_Action
::UPDATE
) {
146 $financialTypeID = CRM_Contribute_PseudoConstant
::financialType($this->_values
['financial_type_id']);
147 if (!CRM_Core_Permission
::check('edit contributions of type ' . $financialTypeID)) {
148 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
152 // Preload libraries required by the "Profiles" tab
153 $schemas = array('IndividualModel', 'OrganizationModel', 'ContributionModel');
154 if (in_array('CiviMember', CRM_Core_Config
::singleton()->enableComponents
)) {
155 $schemas[] = 'MembershipModel';
157 CRM_UF_Page_ProfileEditor
::registerProfileScripts();
158 CRM_UF_Page_ProfileEditor
::registerSchemas($schemas);
162 * Build the form object.
164 public function buildQuickForm() {
165 $this->applyFilter('__ALL__', 'trim');
167 $session = CRM_Core_Session
::singleton();
168 $this->_cancelURL
= CRM_Utils_Array
::value('cancelURL', $_POST);
170 if (!$this->_cancelURL
) {
171 $this->_cancelURL
= CRM_Utils_System
::url('civicrm/admin/contribute', 'reset=1');
174 if ($this->_cancelURL
) {
175 $this->addElement('hidden', 'cancelURL', $this->_cancelURL
);
178 if ($this->_single
) {
182 'name' => ts('Save'),
183 'spacing' => ' ',
188 'name' => ts('Save and Done'),
189 'spacing' => ' ',
196 'name' => ts('Save and Next'),
197 'spacing' => ' ',
198 'subName' => 'savenext',
203 'name' => ts('Cancel'),
205 $this->addButtons($buttons);
209 if (!$this->_first
) {
212 'name' => ts('Previous'),
213 'spacing' => ' ',
218 'name' => ts('Continue'),
219 'spacing' => ' ',
224 'name' => ts('Cancel'),
227 $this->addButtons($buttons);
230 $session->replaceUserContext($this->_cancelURL
);
231 // views are implemented as frozen form
232 if ($this->_action
& CRM_Core_Action
::VIEW
) {
234 $this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/custom/group?reset=1&action=browse'"));
237 // don't show option for contribution amounts section if membership price set
238 // this flag is sent to template
240 $membershipBlock = new CRM_Member_DAO_MembershipBlock();
241 $membershipBlock->entity_table
= 'civicrm_contribution_page';
242 $membershipBlock->entity_id
= $this->_id
;
243 $membershipBlock->is_active
= 1;
244 $hasMembershipBlk = FALSE;
245 if ($membershipBlock->find(TRUE) &&
246 ($setID = CRM_Price_BAO_PriceSet
::getFor('civicrm_contribution_page', $this->_id
, NULL, 1))
248 $extends = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'extends');
249 if ($extends && $extends == CRM_Core_Component
::getComponentID('CiviMember')) {
250 $hasMembershipBlk = TRUE;
253 // set value in DOM that membership price set exists
254 CRM_Core_Resources
::singleton()->addSetting(array('memberPriceset' => $hasMembershipBlk));
258 * Set default values for the form. Note that in edit/view mode
259 * the default values are retrieved from the database
265 public function setDefaultValues() {
266 //some child classes calling setdefaults directly w/o preprocess.
267 $this->_values
= $this->get('values');
268 if (!is_array($this->_values
)) {
269 $this->_values
= array();
270 if (isset($this->_id
) && $this->_id
) {
271 $params = array('id' => $this->_id
);
272 CRM_Core_DAO
::commonRetrieve('CRM_Contribute_DAO_ContributionPage', $params, $this->_values
);
274 $this->set('values', $this->_values
);
276 $defaults = $this->_values
;
278 $config = CRM_Core_Config
::singleton();
279 if (isset($this->_id
)) {
281 //set defaults for pledgeBlock values.
282 $pledgeBlockParams = array(
283 'entity_id' => $this->_id
,
284 'entity_table' => ts('civicrm_contribution_page'),
286 $pledgeBlockDefaults = array();
287 CRM_Pledge_BAO_PledgeBlock
::retrieve($pledgeBlockParams, $pledgeBlockDefaults);
288 if ($this->_pledgeBlockID
= CRM_Utils_Array
::value('id', $pledgeBlockDefaults)) {
289 $defaults['is_pledge_active'] = TRUE;
291 $pledgeBlock = array(
292 'is_pledge_interval',
294 'initial_reminder_day',
295 'additional_reminder_day',
297 'is_pledge_start_date_visible',
298 'is_pledge_start_date_editable',
300 foreach ($pledgeBlock as $key) {
301 $defaults[$key] = CRM_Utils_Array
::value($key, $pledgeBlockDefaults);
302 if ($key == 'pledge_start_date' && CRM_Utils_Array
::value($key, $pledgeBlockDefaults)) {
303 $defaultPledgeDate = (array) json_decode($pledgeBlockDefaults['pledge_start_date']);
304 $pledgeDateFields = array(
305 'pledge_calendar_date' => 'calendar_date',
306 'pledge_calendar_month' => 'calendar_month',
308 $defaults['pledge_default_toggle'] = key($defaultPledgeDate);
309 foreach ($pledgeDateFields as $key => $value) {
310 if (array_key_exists($value, $defaultPledgeDate)) {
311 $defaults[$key] = reset($defaultPledgeDate);
312 $this->assign($key, reset($defaultPledgeDate));
317 if (!empty($pledgeBlockDefaults['pledge_frequency_unit'])) {
318 $defaults['pledge_frequency_unit'] = array_fill_keys(explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
319 $pledgeBlockDefaults['pledge_frequency_unit']
323 // fix the display of the monetary value, CRM-4038
324 if (isset($defaults['goal_amount'])) {
325 $defaults['goal_amount'] = CRM_Utils_Money
::format($defaults['goal_amount'], NULL, '%a');
328 // get price set of type contributions
329 //this is the value for stored in db if price set extends contribution
331 $this->_priceSetID
= CRM_Price_BAO_PriceSet
::getFor('civicrm_contribution_page', $this->_id
, $usedFor, 1);
332 if ($this->_priceSetID
) {
333 $defaults['price_set_id'] = $this->_priceSetID
;
336 if (!empty($defaults['end_date'])) {
337 list($defaults['end_date'], $defaults['end_date_time']) = CRM_Utils_Date
::setDateDefaults($defaults['end_date']);
340 if (!empty($defaults['start_date'])) {
341 list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date
::setDateDefaults($defaults['start_date']);
345 $defaults['is_active'] = 1;
346 // set current date as start date
347 list($defaults['start_date'], $defaults['start_date_time']) = CRM_Utils_Date
::setDateDefaults();
350 if (!empty($defaults['recur_frequency_unit'])) {
351 $defaults['recur_frequency_unit'] = array_fill_keys(explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
352 $defaults['recur_frequency_unit']
357 $defaults['recur_frequency_unit'] = array('month' => 1);
360 // confirm page starts out enabled
361 if (!isset($defaults['is_confirm_enabled'])) {
362 $defaults['is_confirm_enabled'] = 1;
371 public function postProcess() {
372 $pageId = $this->get('id');
373 //page is newly created.
374 if ($pageId && !$this->_id
) {
375 $session = CRM_Core_Session
::singleton();
376 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/contribute', 'reset=1'));
380 public function endPostProcess() {
381 // make submit buttons keep the current working tab opened, or save and next tab
382 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
383 $className = CRM_Utils_String
::getClassName($this->_name
);
385 //retrieve list of pages from StateMachine and find next page
386 //this is quite painful because StateMachine is full of protected variables
387 //so we have to retrieve all pages, find current page, and then retrieve next
388 $stateMachine = new CRM_Contribute_StateMachine_ContributionPage($this);
389 $states = $stateMachine->getStates();
390 $statesList = array_keys($states);
391 $currKey = array_search($className, $statesList);
392 $nextPage = (array_key_exists($currKey +
1, $statesList)) ?
$statesList[$currKey +
1] : '';
394 //unfortunately, some classes don't map to subpage names, so we alter the exceptions
396 switch ($className) {
398 $attributes = $this->getVar('_attributes');
399 $subPage = strtolower(basename(CRM_Utils_Array
::value('action', $attributes)));
400 $subPageName = ucfirst($subPage);
401 if ($subPage == 'friend') {
402 $nextPage = 'custom';
405 $nextPage = 'settings';
409 case 'MembershipBlock':
410 $subPage = 'membership';
411 $subPageName = 'MembershipBlock';
412 $nextPage = 'thankyou';
416 $subPage = strtolower($className);
417 $subPageName = $className;
418 $nextPage = strtolower($nextPage);
420 if ($subPage == 'amount') {
421 $nextPage = 'membership';
423 elseif ($subPage == 'thankyou') {
424 $nextPage = 'friend';
429 CRM_Core_Session
::setStatus(ts("'%1' information has been saved.",
430 array(1 => $subPageName)
431 ), ts('Saved'), 'success');
433 $this->postProcessHook();
435 if ($this->controller
->getButtonName('submit') == "_qf_{$className}_next") {
436 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/admin/contribute/{$subPage}",
437 "action=update&reset=1&id={$this->_id}"
440 elseif ($this->controller
->getButtonName('submit') == "_qf_{$className}_submit_savenext") {
442 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/admin/contribute/{$nextPage}",
443 "action=update&reset=1&id={$this->_id}"
447 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/admin/contribute",
453 CRM_Utils_System
::redirect(CRM_Utils_System
::url("civicrm/admin/contribute", 'reset=1'));
459 * Use the form name to create the tpl file name.
466 public function getTemplateFileName() {
467 if ($this->controller
->getPrint() ||
$this->getVar('_id') <= 0 ||
468 ($this->_action
& CRM_Core_Action
::DELETE
) ||
469 (CRM_Utils_String
::getClassName($this->_name
) == 'AddProduct')
471 return parent
::getTemplateFileName();
474 // hack lets suppress the form rendering for now
475 self
::$_template->assign('isForm', FALSE);
476 return 'CRM/Contribute/Form/ContributionPage/Tab.tpl';