3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
35 * Create a page for displaying Contribute Pages
36 * Contribute Pages are pages that are used to display
37 * contributions of different types. Pages consist
38 * of many customizable sections which can be
41 * This page provides a top level browse view
42 * of all the contribution pages in the system.
45 class CRM_Contribute_Page_ContributionPage
extends CRM_Core_Page
{
48 * The action links that we need to display for the browse screen.
52 private static $_actionLinks;
53 private static $_contributionLinks;
54 private static $_configureActionLinks;
55 private static $_onlineContributionLinks;
58 * @var CRM_Utils_Pager
60 protected $_pager = NULL;
65 protected $_sortByCharacter;
68 * Get the action links for this page.
72 public static function &actionLinks() {
73 // check if variable _actionsLinks is populated
74 if (!isset(self
::$_actionLinks)) {
75 // helper variable for nicer formatting
76 $deleteExtra = ts('Are you sure you want to delete this Contribution page?');
77 $copyExtra = ts('Are you sure you want to make a copy of this Contribution page?');
79 self
::$_actionLinks = array(
80 CRM_Core_Action
::COPY
=> array(
81 'name' => ts('Make a Copy'),
82 'url' => CRM_Utils_System
::currentPath(),
83 'qs' => 'action=copy&gid=%%id%%',
84 'title' => ts('Make a Copy of CiviCRM Contribution Page'),
85 'extra' => 'onclick = "return confirm(\'' . $copyExtra . '\');"',
87 CRM_Core_Action
::DISABLE
=> array(
88 'name' => ts('Disable'),
89 'title' => ts('Disable'),
90 'ref' => 'crm-enable-disable',
92 CRM_Core_Action
::ENABLE
=> array(
93 'name' => ts('Enable'),
94 'ref' => 'crm-enable-disable',
95 'title' => ts('Enable'),
97 CRM_Core_Action
::DELETE
=> array(
98 'name' => ts('Delete'),
99 'url' => CRM_Utils_System
::currentPath(),
100 'qs' => 'action=delete&reset=1&id=%%id%%',
101 'title' => ts('Delete Custom Field'),
102 'extra' => 'onclick = "return confirm(\'' . $deleteExtra . '\');"',
106 return self
::$_actionLinks;
110 * Get the configure action links for this page.
114 public function &configureActionLinks() {
115 // check if variable _actionsLinks is populated
116 if (!isset(self
::$_configureActionLinks)) {
117 $urlString = 'civicrm/admin/contribute/';
118 $urlParams = 'reset=1&action=update&id=%%id%%';
120 self
::$_configureActionLinks = array(
121 CRM_Core_Action
::ADD
=> array(
122 'name' => ts('Title and Settings'),
123 'title' => ts('Title and Settings'),
124 'url' => $urlString . 'settings',
126 'uniqueName' => 'settings',
128 CRM_Core_Action
::UPDATE
=> array(
129 'name' => ts('Contribution Amounts'),
130 'title' => ts('Contribution Amounts'),
131 'url' => $urlString . 'amount',
133 'uniqueName' => 'amount',
135 CRM_Core_Action
::VIEW
=> array(
136 'name' => ts('Membership Settings'),
137 'title' => ts('Membership Settings'),
138 'url' => $urlString . 'membership',
140 'uniqueName' => 'membership',
142 CRM_Core_Action
::EXPORT
=> array(
143 'name' => ts('Thank-you and Receipting'),
144 'title' => ts('Thank-you and Receipting'),
145 'url' => $urlString . 'thankyou',
147 'uniqueName' => 'thankyou',
149 CRM_Core_Action
::BASIC
=> array(
150 'name' => ts('Tell a Friend'),
151 'title' => ts('Tell a Friend'),
152 'url' => $urlString . 'friend',
154 'uniqueName' => 'friend',
156 CRM_Core_Action
::PROFILE
=> array(
157 'name' => ts('Include Profiles'),
158 'title' => ts('Include Profiles'),
159 'url' => $urlString . 'custom',
161 'uniqueName' => 'custom',
163 CRM_Core_Action
::MAP
=> array(
164 'name' => ts('Contribution Widget'),
165 'title' => ts('Contribution Widget'),
166 'url' => $urlString . 'widget',
168 'uniqueName' => 'widget',
170 CRM_Core_Action
::FOLLOWUP
=> array(
171 'name' => ts('Premiums'),
172 'title' => ts('Premiums'),
173 'url' => $urlString . 'premium',
175 'uniqueName' => 'premium',
177 CRM_Core_Action
::ADVANCED
=> array(
178 'name' => ts('Personal Campaign Pages'),
179 'title' => ts('Personal Campaign Pages'),
180 'url' => $urlString . 'pcp',
182 'uniqueName' => 'pcp',
186 'urlString' => $urlString,
187 'urlParams' => $urlParams,
189 CRM_Utils_Hook
::tabset('civicrm/admin/contribute', self
::$_configureActionLinks, $context);
192 return self
::$_configureActionLinks;
196 * Get the online contribution links.
200 public function &onlineContributionLinks() {
201 if (!isset(self
::$_onlineContributionLinks)) {
202 $urlString = 'civicrm/contribute/transact';
203 $urlParams = 'reset=1&id=%%id%%';
204 self
::$_onlineContributionLinks = array(
205 CRM_Core_Action
::RENEW
=> array(
206 'name' => ts('Live Page'),
207 'title' => ts('Live Page'),
211 'uniqueName' => 'live_page',
213 CRM_Core_Action
::PREVIEW
=> array(
214 'name' => ts('Test-drive'),
215 'title' => ts('Test-drive'),
217 'qs' => $urlParams . '&action=preview',
218 // Addresses https://lab.civicrm.org/dev/core/issues/658
220 'uniqueName' => 'test_drive',
225 return self
::$_onlineContributionLinks;
229 * Get the contributions links.
233 public function &contributionLinks() {
234 if (!isset(self
::$_contributionLinks)) {
235 //get contribution dates.
236 $dates = CRM_Contribute_BAO_Contribution
::getContributionDates();
237 $now = $dates['now'];
238 $yearDate = $dates['yearDate'];
239 $monthDate = $dates['monthDate'];
240 $yearNow = $yearDate +
10000;
242 $urlString = 'civicrm/contribute/search';
243 $urlParams = 'reset=1&pid=%%id%%&force=1&test=0';
245 self
::$_contributionLinks = array(
246 CRM_Core_Action
::DETACH
=> array(
247 'name' => ts('Current Month-To-Date'),
248 'title' => ts('Current Month-To-Date'),
250 'qs' => "{$urlParams}&receive_date_low={$monthDate}&receive_date_high={$now}",
251 'uniqueName' => 'current_month_to_date',
253 CRM_Core_Action
::REVERT
=> array(
254 'name' => ts('Fiscal Year-To-Date'),
255 'title' => ts('Fiscal Year-To-Date'),
257 'qs' => "{$urlParams}&receive_date_low={$yearDate}&receive_date_high={$yearNow}",
258 'uniqueName' => 'fiscal_year_to_date',
260 CRM_Core_Action
::BROWSE
=> array(
261 'name' => ts('Cumulative'),
262 'title' => ts('Cumulative'),
264 'qs' => "{$urlParams}&receive_date_low=&receive_date_high=$now",
265 'uniqueName' => 'cumulative',
270 return self
::$_contributionLinks;
276 * This method is called after the page is created. It checks for the
277 * type of action and executes that action.
278 * Finally it calls the parent's run method.
282 public function run() {
283 // get the requested action
284 $action = CRM_Utils_Request
::retrieve('action', 'String',
285 // default to 'browse'
286 $this, FALSE, 'browse'
289 // assign vars to templates
290 $this->assign('action', $action);
291 $id = CRM_Utils_Request
::retrieve('id', 'Positive',
295 // set breadcrumb to append to 2nd layer pages
298 'title' => ts('Manage Contribution Pages'),
299 'url' => CRM_Utils_System
::url(CRM_Utils_System
::currentPath(),
305 // what action to take ?
306 if ($action & CRM_Core_Action
::ADD
) {
307 $session = CRM_Core_Session
::singleton();
308 $session->pushUserContext(CRM_Utils_System
::url(CRM_Utils_System
::currentPath(),
309 'action=browse&reset=1'
312 $controller = new CRM_Contribute_Controller_ContributionPage(NULL, $action);
313 CRM_Utils_System
::setTitle(ts('Manage Contribution Page'));
314 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
315 return $controller->run();
317 elseif ($action & CRM_Core_Action
::UPDATE
) {
318 $config = CRM_Core_Config
::singleton();
320 // assign vars to templates
321 $this->assign('id', $id);
322 $this->assign('title', CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $id, 'title'));
323 $this->assign('is_active', CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $id, 'is_active'));
324 if (in_array('CiviMember', $config->enableComponents
)) {
325 $this->assign('CiviMember', TRUE);
328 elseif ($action & CRM_Core_Action
::COPY
) {
329 // @todo Unused local variable can be safely removed.
330 // But are there any side effects of CRM_Core_Session::singleton() that we
332 $session = CRM_Core_Session
::singleton();
333 CRM_Core_Session
::setStatus(ts('A copy of the contribution page has been created'), ts('Successfully Copied'), 'success');
336 elseif ($action & CRM_Core_Action
::DELETE
) {
337 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
339 $session = CRM_Core_Session
::singleton();
340 $session->pushUserContext(CRM_Utils_System
::url(CRM_Utils_System
::currentPath(),
341 'reset=1&action=browse'
344 $id = CRM_Utils_Request
::retrieve('id', 'Positive',
349 FROM civicrm_contribution_page ccp
350 JOIN civicrm_pcp cp ON ccp.id = cp.page_id
351 WHERE cp.page_id = {$id}
352 AND cp.page_type = 'contribute'
355 if ($pageTitle = CRM_Core_DAO
::singleValueQuery($query)) {
356 CRM_Core_Session
::setStatus(ts('The \'%1\' cannot be deleted! You must Delete all Personal Campaign Page(s) related with this contribution page prior to deleting the page.', array(1 => $pageTitle)), ts('Deletion Error'), 'error');
358 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/admin/contribute', 'reset=1'));
361 $controller = new CRM_Core_Controller_Simple('CRM_Contribute_Form_ContributionPage_Delete',
362 'Delete Contribution Page',
363 CRM_Core_Action
::DELETE
365 $controller->set('id', $id);
366 $controller->process();
367 return $controller->run();
370 // finally browse the contribution pages
373 CRM_Utils_System
::setTitle(ts('Manage Contribution Pages'));
376 return parent
::run();
380 * Make a copy of a contribution page, including all the fields in the page.
382 public function copy() {
383 $gid = CRM_Utils_Request
::retrieve('gid', 'Positive',
384 $this, TRUE, 0, 'GET'
387 $copy = CRM_Contribute_BAO_ContributionPage
::copy($gid);
389 $urlString = CRM_Utils_System
::currentPath();
390 $urlParams = 'reset=1';
392 // Redirect to copied contribution page
394 $urlString = 'civicrm/admin/contribute/settings';
395 $urlParams .= '&action=update&id=' . $copy->id
;
398 CRM_Utils_System
::redirect(CRM_Utils_System
::url($urlString, $urlParams));
402 * Browse all contribution pages.
404 * @param mixed $action
407 public function browse($action = NULL) {
408 Civi
::resources()->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header');
410 $this->_sortByCharacter
= CRM_Utils_Request
::retrieve('sortByCharacter',
414 // @todo Unused local variable can be safely removed.
415 // But are there any side effects of CRM_Utils_Request::retrieve() that we
417 $createdId = CRM_Utils_Request
::retrieve('cid', 'Positive',
421 if ($this->_sortByCharacter
== 'all' ||
424 $this->_sortByCharacter
= '';
425 $this->set('sortByCharacter', '');
432 $whereClause = $this->whereClause($params, FALSE);
433 $config = CRM_Core_Config
::singleton();
434 if ($config->includeAlphabeticalPager
) {
435 $this->pagerAToZ($whereClause, $params);
438 $whereClause = $this->whereClause($params, TRUE);
439 $this->pager($whereClause, $params);
441 list($offset, $rowCount) = $this->_pager
->getOffsetAndRowCount();
443 //check for delete CRM-4418
444 $allowToDelete = CRM_Core_Permission
::check('delete in CiviContribute');
448 FROM civicrm_contribution_page
450 ORDER BY is_active desc, title asc
451 LIMIT $offset, $rowCount";
452 $contribPage = CRM_Core_DAO
::executeQuery($query, $params, TRUE, 'CRM_Contribute_DAO_ContributionPage');
453 $contribPageIds = array();
454 while ($contribPage->fetch()) {
455 $contribPageIds[$contribPage->id
] = $contribPage->id
;
457 //get all section info.
458 $contriPageSectionInfo = CRM_Contribute_BAO_ContributionPage
::getSectionInfo($contribPageIds);
462 FROM civicrm_contribution_page
464 ORDER BY is_active desc, title asc
465 LIMIT $offset, $rowCount";
467 $dao = CRM_Core_DAO
::executeQuery($query, $params, TRUE, 'CRM_Contribute_DAO_ContributionPage');
470 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
472 //get configure actions links.
473 $configureActionLinks = self
::configureActionLinks();
475 while ($dao->fetch()) {
476 $contribution[$dao->id
] = array();
477 CRM_Core_DAO
::storeValues($dao, $contribution[$dao->id
]);
479 // form all action links
480 $action = array_sum(array_keys(self
::actionLinks()));
482 //add configure actions links.
483 $action +
= array_sum(array_keys($configureActionLinks));
485 //add online contribution links.
486 $action +
= array_sum(array_keys(self
::onlineContributionLinks()));
488 //add contribution search links.
489 $action +
= array_sum(array_keys(self
::contributionLinks()));
491 if ($dao->is_active
) {
492 $action -= (int) CRM_Core_Action
::ENABLE
;
495 $action -= (int) CRM_Core_Action
::DISABLE
;
499 if (!$allowToDelete) {
500 $action -= (int) CRM_Core_Action
::DELETE
;
503 //build the configure links.
504 $sectionsInfo = CRM_Utils_Array
::value($dao->id
, $contriPageSectionInfo, array());
505 $contribution[$dao->id
]['configureActionLinks'] = CRM_Core_Action
::formLink(self
::formatConfigureLinks($sectionsInfo),
507 array('id' => $dao->id
),
510 'contributionpage.configure.actions',
515 //build the contributions links.
516 $contribution[$dao->id
]['contributionLinks'] = CRM_Core_Action
::formLink(self
::contributionLinks(),
518 array('id' => $dao->id
),
521 'contributionpage.contributions.search',
526 //build the online contribution links.
527 $contribution[$dao->id
]['onlineContributionLinks'] = CRM_Core_Action
::formLink(self
::onlineContributionLinks(),
529 array('id' => $dao->id
),
532 'contributionpage.online.links',
537 //build the normal action links.
538 $contribution[$dao->id
]['action'] = CRM_Core_Action
::formLink(self
::actionLinks(),
540 array('id' => $dao->id
),
543 'contributionpage.action.links',
548 //show campaigns on selector.
549 $contribution[$dao->id
]['campaign'] = CRM_Utils_Array
::value($dao->campaign_id
, $allCampaigns);
552 if (isset($contribution)) {
553 $this->assign('rows', $contribution);
557 public function search() {
558 if (isset($this->_action
) & (CRM_Core_Action
::ADD |
559 CRM_Core_Action
::UPDATE |
560 CRM_Core_Action
::DELETE
566 $form = new CRM_Core_Controller_Simple('CRM_Contribute_Form_SearchContribution',
567 ts('Search Contribution'),
570 $form->setEmbedded(TRUE);
571 $form->setParent($this);
577 * @param array $params
578 * @param bool $sortBy
582 public function whereClause(&$params, $sortBy = TRUE) {
583 // @todo Unused local variable can be safely removed.
584 $values = $clauses = array();
585 $title = $this->get('title');
586 $createdId = $this->get('cid');
589 $clauses[] = "(created_id = {$createdId})";
593 $clauses[] = "title LIKE %1";
594 if (strpos($title, '%') !== FALSE) {
595 $params[1] = array(trim($title), 'String', FALSE);
598 $params[1] = array(trim($title), 'String', TRUE);
602 $value = $this->get('financial_type_id');
605 if (is_array($value)) {
606 foreach ($value as $k => $v) {
611 $type = implode(',', $val);
613 // @todo Variable 'type' might not have been defined.
614 $clauses[] = "financial_type_id IN ({$type})";
617 if ($sortBy && $this->_sortByCharacter
!== NULL) {
618 $clauses[] = "title LIKE '" . strtolower(CRM_Core_DAO
::escapeWildCardString($this->_sortByCharacter
)) . "%'";
621 $campaignIds = $this->getCampaignIds();
622 if (count($campaignIds) >= 1) {
623 $clauses[] = '( campaign_id IN ( ' . implode(' , ', $campaignIds) . ' ) )';
626 if (empty($clauses)) {
627 // Let template know if user has run a search or not
628 $this->assign('isSearch', 0);
632 $this->assign('isSearch', 1);
635 return implode(' AND ', $clauses);
639 * Gets the campaign ids from the session.
643 public function getCampaignIds() {
644 // The unfiltered value from the session cannot be trusted, it needs to be
645 // processed to get a clean array of positive integers.
647 foreach ((array) $this->get('campaign_id') as $id) {
648 if ((string) (int) $id === (string) $id && $id > 0) {
656 * @param $whereClause
657 * @param array $whereParams
659 public function pager($whereClause, $whereParams) {
661 $params['status'] = ts('Contribution %%StatusMessage%%');
662 $params['csvString'] = NULL;
663 $params['buttonTop'] = 'PagerTopButton';
664 $params['buttonBottom'] = 'PagerBottomButton';
665 $params['rowCount'] = $this->get(CRM_Utils_Pager
::PAGE_ROWCOUNT
);
666 if (!$params['rowCount']) {
667 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
672 FROM civicrm_contribution_page
675 $params['total'] = CRM_Core_DAO
::singleValueQuery($query, $whereParams);
677 $this->_pager
= new CRM_Utils_Pager($params);
678 $this->assign_by_ref('pager', $this->_pager
);
682 * @param $whereClause
683 * @param array $whereParams
685 public function pagerAtoZ($whereClause, $whereParams) {
688 SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name
689 FROM civicrm_contribution_page
691 ORDER BY UPPER(LEFT(title, 1))
693 $dao = CRM_Core_DAO
::executeQuery($query, $whereParams);
695 $aToZBar = CRM_Utils_PagerAToZ
::getAToZBar($dao, $this->_sortByCharacter
, TRUE);
696 $this->assign('aToZ', $aToZBar);
700 * @param array $sectionsInfo
704 public function formatConfigureLinks($sectionsInfo) {
705 // build the formatted configure links.
706 $formattedConfLinks = self
::configureActionLinks();
707 foreach ($formattedConfLinks as $act => & $link) {
708 $sectionName = CRM_Utils_Array
::value('uniqueName', $link);
713 if (empty($sectionsInfo[$sectionName])) {
715 if (isset($link['class'])) {
716 $classes = $link['class'];
718 $link['class'] = array_merge($classes, array('disabled'));
722 return $formattedConfLinks;