Merge pull request #13298 from colemanw/extCompat
[civicrm-core.git] / CRM / Contribute / Page / ContributionPage.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33
34 /**
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
39 * accessed.
40 *
41 * This page provides a top level browse view
42 * of all the contribution pages in the system.
43 *
44 */
45 class CRM_Contribute_Page_ContributionPage extends CRM_Core_Page {
46
47 /**
48 * The action links that we need to display for the browse screen.
49 *
50 * @var array
51 */
52 private static $_actionLinks;
53 private static $_contributionLinks;
54 private static $_configureActionLinks;
55 private static $_onlineContributionLinks;
56
57 /**
58 * @var CRM_Utils_Pager
59 */
60 protected $_pager = NULL;
61
62 /**
63 * @var string
64 */
65 protected $_sortByCharacter;
66
67 /**
68 * Get the action links for this page.
69 *
70 * @return array
71 */
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?');
78
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 . '\');"',
86 ),
87 CRM_Core_Action::DISABLE => array(
88 'name' => ts('Disable'),
89 'title' => ts('Disable'),
90 'ref' => 'crm-enable-disable',
91 ),
92 CRM_Core_Action::ENABLE => array(
93 'name' => ts('Enable'),
94 'ref' => 'crm-enable-disable',
95 'title' => ts('Enable'),
96 ),
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 . '\');"',
103 ),
104 );
105 }
106 return self::$_actionLinks;
107 }
108
109 /**
110 * Get the configure action links for this page.
111 *
112 * @return array
113 */
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%%';
119
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',
125 'qs' => $urlParams,
126 'uniqueName' => 'settings',
127 ),
128 CRM_Core_Action::UPDATE => array(
129 'name' => ts('Contribution Amounts'),
130 'title' => ts('Contribution Amounts'),
131 'url' => $urlString . 'amount',
132 'qs' => $urlParams,
133 'uniqueName' => 'amount',
134 ),
135 CRM_Core_Action::VIEW => array(
136 'name' => ts('Membership Settings'),
137 'title' => ts('Membership Settings'),
138 'url' => $urlString . 'membership',
139 'qs' => $urlParams,
140 'uniqueName' => 'membership',
141 ),
142 CRM_Core_Action::EXPORT => array(
143 'name' => ts('Thank-you and Receipting'),
144 'title' => ts('Thank-you and Receipting'),
145 'url' => $urlString . 'thankyou',
146 'qs' => $urlParams,
147 'uniqueName' => 'thankyou',
148 ),
149 CRM_Core_Action::BASIC => array(
150 'name' => ts('Tell a Friend'),
151 'title' => ts('Tell a Friend'),
152 'url' => $urlString . 'friend',
153 'qs' => $urlParams,
154 'uniqueName' => 'friend',
155 ),
156 CRM_Core_Action::PROFILE => array(
157 'name' => ts('Include Profiles'),
158 'title' => ts('Include Profiles'),
159 'url' => $urlString . 'custom',
160 'qs' => $urlParams,
161 'uniqueName' => 'custom',
162 ),
163 CRM_Core_Action::MAP => array(
164 'name' => ts('Contribution Widget'),
165 'title' => ts('Contribution Widget'),
166 'url' => $urlString . 'widget',
167 'qs' => $urlParams,
168 'uniqueName' => 'widget',
169 ),
170 CRM_Core_Action::FOLLOWUP => array(
171 'name' => ts('Premiums'),
172 'title' => ts('Premiums'),
173 'url' => $urlString . 'premium',
174 'qs' => $urlParams,
175 'uniqueName' => 'premium',
176 ),
177 CRM_Core_Action::ADVANCED => array(
178 'name' => ts('Personal Campaign Pages'),
179 'title' => ts('Personal Campaign Pages'),
180 'url' => $urlString . 'pcp',
181 'qs' => $urlParams,
182 'uniqueName' => 'pcp',
183 ),
184 );
185 $context = array(
186 'urlString' => $urlString,
187 'urlParams' => $urlParams,
188 );
189 CRM_Utils_Hook::tabset('civicrm/admin/contribute', self::$_configureActionLinks, $context);
190 }
191
192 return self::$_configureActionLinks;
193 }
194
195 /**
196 * Get the online contribution links.
197 *
198 * @return array
199 */
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'),
208 'url' => $urlString,
209 'qs' => $urlParams,
210 'fe' => TRUE,
211 'uniqueName' => 'live_page',
212 ),
213 CRM_Core_Action::PREVIEW => array(
214 'name' => ts('Test-drive'),
215 'title' => ts('Test-drive'),
216 'url' => $urlString,
217 'qs' => $urlParams . '&action=preview',
218 'fe' => TRUE, // Addresses https://lab.civicrm.org/dev/core/issues/658
219 'uniqueName' => 'test_drive',
220 ),
221 );
222 }
223
224 return self::$_onlineContributionLinks;
225 }
226
227 /**
228 * Get the contributions links.
229 *
230 * @return array
231 */
232 public function &contributionLinks() {
233 if (!isset(self::$_contributionLinks)) {
234 //get contribution dates.
235 $dates = CRM_Contribute_BAO_Contribution::getContributionDates();
236 $now = $dates['now'];
237 $yearDate = $dates['yearDate'];
238 $monthDate = $dates['monthDate'];
239 $yearNow = $yearDate + 10000;
240
241 $urlString = 'civicrm/contribute/search';
242 $urlParams = 'reset=1&pid=%%id%%&force=1&test=0';
243
244 self::$_contributionLinks = array(
245 CRM_Core_Action::DETACH => array(
246 'name' => ts('Current Month-To-Date'),
247 'title' => ts('Current Month-To-Date'),
248 'url' => $urlString,
249 'qs' => "{$urlParams}&start={$monthDate}&end={$now}",
250 'uniqueName' => 'current_month_to_date',
251 ),
252 CRM_Core_Action::REVERT => array(
253 'name' => ts('Fiscal Year-To-Date'),
254 'title' => ts('Fiscal Year-To-Date'),
255 'url' => $urlString,
256 'qs' => "{$urlParams}&start={$yearDate}&end={$yearNow}",
257 'uniqueName' => 'fiscal_year_to_date',
258 ),
259 CRM_Core_Action::BROWSE => array(
260 'name' => ts('Cumulative'),
261 'title' => ts('Cumulative'),
262 'url' => $urlString,
263 'qs' => "{$urlParams}&start=&end=$now",
264 'uniqueName' => 'cumulative',
265 ),
266 );
267 }
268
269 return self::$_contributionLinks;
270 }
271
272 /**
273 * Run the page.
274 *
275 * This method is called after the page is created. It checks for the
276 * type of action and executes that action.
277 * Finally it calls the parent's run method.
278 *
279 * @return mixed
280 */
281 public function run() {
282 // get the requested action
283 $action = CRM_Utils_Request::retrieve('action', 'String',
284 // default to 'browse'
285 $this, FALSE, 'browse'
286 );
287
288 // assign vars to templates
289 $this->assign('action', $action);
290 $id = CRM_Utils_Request::retrieve('id', 'Positive',
291 $this, FALSE, 0
292 );
293
294 // set breadcrumb to append to 2nd layer pages
295 $breadCrumb = array(
296 array(
297 'title' => ts('Manage Contribution Pages'),
298 'url' => CRM_Utils_System::url(CRM_Utils_System::currentPath(),
299 'reset=1'
300 ),
301 ),
302 );
303
304 // what action to take ?
305 if ($action & CRM_Core_Action::ADD) {
306 $session = CRM_Core_Session::singleton();
307 $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(),
308 'action=browse&reset=1'
309 ));
310
311 $controller = new CRM_Contribute_Controller_ContributionPage(NULL, $action);
312 CRM_Utils_System::setTitle(ts('Manage Contribution Page'));
313 CRM_Utils_System::appendBreadCrumb($breadCrumb);
314 return $controller->run();
315 }
316 elseif ($action & CRM_Core_Action::UPDATE) {
317 $config = CRM_Core_Config::singleton();
318
319 // assign vars to templates
320 $this->assign('id', $id);
321 $this->assign('title', CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $id, 'title'));
322 $this->assign('is_active', CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $id, 'is_active'));
323 if (in_array('CiviMember', $config->enableComponents)) {
324 $this->assign('CiviMember', TRUE);
325 }
326 }
327 elseif ($action & CRM_Core_Action::COPY) {
328 // @todo Unused local variable can be safely removed.
329 // But are there any side effects of CRM_Core_Session::singleton() that we
330 // need to preserve?
331 $session = CRM_Core_Session::singleton();
332 CRM_Core_Session::setStatus(ts('A copy of the contribution page has been created'), ts('Successfully Copied'), 'success');
333 $this->copy();
334 }
335 elseif ($action & CRM_Core_Action::DELETE) {
336 CRM_Utils_System::appendBreadCrumb($breadCrumb);
337
338 $session = CRM_Core_Session::singleton();
339 $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(),
340 'reset=1&action=browse'
341 ));
342
343 $id = CRM_Utils_Request::retrieve('id', 'Positive',
344 $this, FALSE, 0
345 );
346 $query = "
347 SELECT ccp.title
348 FROM civicrm_contribution_page ccp
349 JOIN civicrm_pcp cp ON ccp.id = cp.page_id
350 WHERE cp.page_id = {$id}
351 AND cp.page_type = 'contribute'
352 ";
353
354 if ($pageTitle = CRM_Core_DAO::singleValueQuery($query)) {
355 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');
356
357 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1'));
358 }
359
360 $controller = new CRM_Core_Controller_Simple('CRM_Contribute_Form_ContributionPage_Delete',
361 'Delete Contribution Page',
362 CRM_Core_Action::DELETE
363 );
364 $controller->set('id', $id);
365 $controller->process();
366 return $controller->run();
367 }
368 else {
369 // finally browse the contribution pages
370 $this->browse();
371
372 CRM_Utils_System::setTitle(ts('Manage Contribution Pages'));
373 }
374
375 return parent::run();
376 }
377
378 /**
379 * Make a copy of a contribution page, including all the fields in the page.
380 */
381 public function copy() {
382 $gid = CRM_Utils_Request::retrieve('gid', 'Positive',
383 $this, TRUE, 0, 'GET'
384 );
385
386 CRM_Contribute_BAO_ContributionPage::copy($gid);
387
388 CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1'));
389 }
390
391 /**
392 * Browse all contribution pages.
393 *
394 * @param mixed $action
395 * Unused parameter.
396 */
397 public function browse($action = NULL) {
398 Civi::resources()->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header');
399
400 $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter',
401 'String',
402 $this
403 );
404 // @todo Unused local variable can be safely removed.
405 // But are there any side effects of CRM_Utils_Request::retrieve() that we
406 // need to preserve?
407 $createdId = CRM_Utils_Request::retrieve('cid', 'Positive',
408 $this, FALSE, 0
409 );
410
411 if ($this->_sortByCharacter == 'all' ||
412 !empty($_POST)
413 ) {
414 $this->_sortByCharacter = '';
415 $this->set('sortByCharacter', '');
416 }
417
418 $this->search();
419
420 $params = array();
421
422 $whereClause = $this->whereClause($params, FALSE);
423 $this->pagerAToZ($whereClause, $params);
424
425 $params = array();
426 $whereClause = $this->whereClause($params, TRUE);
427 $this->pager($whereClause, $params);
428
429 list($offset, $rowCount) = $this->_pager->getOffsetAndRowCount();
430
431 //check for delete CRM-4418
432 $allowToDelete = CRM_Core_Permission::check('delete in CiviContribute');
433
434 $query = "
435 SELECT id
436 FROM civicrm_contribution_page
437 WHERE $whereClause
438 ORDER BY is_active desc, title asc
439 LIMIT $offset, $rowCount";
440 $contribPage = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Contribute_DAO_ContributionPage');
441 $contribPageIds = array();
442 while ($contribPage->fetch()) {
443 $contribPageIds[$contribPage->id] = $contribPage->id;
444 }
445 //get all section info.
446 $contriPageSectionInfo = CRM_Contribute_BAO_ContributionPage::getSectionInfo($contribPageIds);
447
448 $query = "
449 SELECT *
450 FROM civicrm_contribution_page
451 WHERE $whereClause
452 ORDER BY is_active desc, title asc
453 LIMIT $offset, $rowCount";
454
455 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Contribute_DAO_ContributionPage');
456
457 //get all campaigns.
458 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
459
460 //get configure actions links.
461 $configureActionLinks = self::configureActionLinks();
462
463 while ($dao->fetch()) {
464 $contribution[$dao->id] = array();
465 CRM_Core_DAO::storeValues($dao, $contribution[$dao->id]);
466
467 // form all action links
468 $action = array_sum(array_keys(self::actionLinks()));
469
470 //add configure actions links.
471 $action += array_sum(array_keys($configureActionLinks));
472
473 //add online contribution links.
474 $action += array_sum(array_keys(self::onlineContributionLinks()));
475
476 //add contribution search links.
477 $action += array_sum(array_keys(self::contributionLinks()));
478
479 if ($dao->is_active) {
480 $action -= (int) CRM_Core_Action::ENABLE;
481 }
482 else {
483 $action -= (int) CRM_Core_Action::DISABLE;
484 }
485
486 //CRM-4418
487 if (!$allowToDelete) {
488 $action -= (int) CRM_Core_Action::DELETE;
489 }
490
491 //build the configure links.
492 $sectionsInfo = CRM_Utils_Array::value($dao->id, $contriPageSectionInfo, array());
493 $contribution[$dao->id]['configureActionLinks'] = CRM_Core_Action::formLink(self::formatConfigureLinks($sectionsInfo),
494 $action,
495 array('id' => $dao->id),
496 ts('Configure'),
497 TRUE,
498 'contributionpage.configure.actions',
499 'ContributionPage',
500 $dao->id
501 );
502
503 //build the contributions links.
504 $contribution[$dao->id]['contributionLinks'] = CRM_Core_Action::formLink(self::contributionLinks(),
505 $action,
506 array('id' => $dao->id),
507 ts('Contributions'),
508 TRUE,
509 'contributionpage.contributions.search',
510 'ContributionPage',
511 $dao->id
512 );
513
514 //build the online contribution links.
515 $contribution[$dao->id]['onlineContributionLinks'] = CRM_Core_Action::formLink(self::onlineContributionLinks(),
516 $action,
517 array('id' => $dao->id),
518 ts('Links'),
519 TRUE,
520 'contributionpage.online.links',
521 'ContributionPage',
522 $dao->id
523 );
524
525 //build the normal action links.
526 $contribution[$dao->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(),
527 $action,
528 array('id' => $dao->id),
529 ts('more'),
530 TRUE,
531 'contributionpage.action.links',
532 'ContributionPage',
533 $dao->id
534 );
535
536 //show campaigns on selector.
537 $contribution[$dao->id]['campaign'] = CRM_Utils_Array::value($dao->campaign_id, $allCampaigns);
538 }
539
540 if (isset($contribution)) {
541 $this->assign('rows', $contribution);
542 }
543 }
544
545 public function search() {
546 if (isset($this->_action) & (CRM_Core_Action::ADD |
547 CRM_Core_Action::UPDATE |
548 CRM_Core_Action::DELETE
549 )
550 ) {
551 return;
552 }
553
554 $form = new CRM_Core_Controller_Simple('CRM_Contribute_Form_SearchContribution',
555 ts('Search Contribution'),
556 CRM_Core_Action::ADD
557 );
558 $form->setEmbedded(TRUE);
559 $form->setParent($this);
560 $form->process();
561 $form->run();
562 }
563
564 /**
565 * @param array $params
566 * @param bool $sortBy
567 *
568 * @return int|string
569 */
570 public function whereClause(&$params, $sortBy = TRUE) {
571 // @todo Unused local variable can be safely removed.
572 $values = $clauses = array();
573 $title = $this->get('title');
574 $createdId = $this->get('cid');
575
576 if ($createdId) {
577 $clauses[] = "(created_id = {$createdId})";
578 }
579
580 if ($title) {
581 $clauses[] = "title LIKE %1";
582 if (strpos($title, '%') !== FALSE) {
583 $params[1] = array(trim($title), 'String', FALSE);
584 }
585 else {
586 $params[1] = array(trim($title), 'String', TRUE);
587 }
588 }
589
590 $value = $this->get('financial_type_id');
591 $val = array();
592 if ($value) {
593 if (is_array($value)) {
594 foreach ($value as $k => $v) {
595 if ($v) {
596 $val[$k] = $k;
597 }
598 }
599 $type = implode(',', $val);
600 }
601 // @todo Variable 'type' might not have been defined.
602 $clauses[] = "financial_type_id IN ({$type})";
603 }
604
605 if ($sortBy && $this->_sortByCharacter !== NULL) {
606 $clauses[] = "title LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_sortByCharacter)) . "%'";
607 }
608
609 $campaignIds = $this->getCampaignIds();
610 if (count($campaignIds) >= 1) {
611 $clauses[] = '( campaign_id IN ( ' . implode(' , ', $campaignIds) . ' ) )';
612 }
613
614 if (empty($clauses)) {
615 // Let template know if user has run a search or not
616 $this->assign('isSearch', 0);
617 return 1;
618 }
619 else {
620 $this->assign('isSearch', 1);
621 }
622
623 return implode(' AND ', $clauses);
624 }
625
626 /**
627 * Gets the campaign ids from the session.
628 *
629 * @return int[]
630 */
631 public function getCampaignIds() {
632 // The unfiltered value from the session cannot be trusted, it needs to be
633 // processed to get a clean array of positive integers.
634 $ids = array();
635 foreach ((array) $this->get('campaign_id') as $id) {
636 if ((string) (int) $id === (string) $id && $id > 0) {
637 $ids[] = $id;
638 }
639 }
640 return $ids;
641 }
642
643 /**
644 * @param $whereClause
645 * @param array $whereParams
646 */
647 public function pager($whereClause, $whereParams) {
648
649 $params['status'] = ts('Contribution %%StatusMessage%%');
650 $params['csvString'] = NULL;
651 $params['buttonTop'] = 'PagerTopButton';
652 $params['buttonBottom'] = 'PagerBottomButton';
653 $params['rowCount'] = $this->get(CRM_Utils_Pager::PAGE_ROWCOUNT);
654 if (!$params['rowCount']) {
655 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
656 }
657
658 $query = "
659 SELECT count(id)
660 FROM civicrm_contribution_page
661 WHERE $whereClause";
662
663 $params['total'] = CRM_Core_DAO::singleValueQuery($query, $whereParams);
664
665 $this->_pager = new CRM_Utils_Pager($params);
666 $this->assign_by_ref('pager', $this->_pager);
667 }
668
669 /**
670 * @param $whereClause
671 * @param array $whereParams
672 */
673 public function pagerAtoZ($whereClause, $whereParams) {
674
675 $query = "
676 SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name
677 FROM civicrm_contribution_page
678 WHERE $whereClause
679 ORDER BY UPPER(LEFT(title, 1))
680 ";
681 $dao = CRM_Core_DAO::executeQuery($query, $whereParams);
682
683 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE);
684 $this->assign('aToZ', $aToZBar);
685 }
686
687 /**
688 * @param array $sectionsInfo
689 *
690 * @return array
691 */
692 public function formatConfigureLinks($sectionsInfo) {
693 // build the formatted configure links.
694 $formattedConfLinks = self::configureActionLinks();
695 foreach ($formattedConfLinks as $act => & $link) {
696 $sectionName = CRM_Utils_Array::value('uniqueName', $link);
697 if (!$sectionName) {
698 continue;
699 }
700
701 if (empty($sectionsInfo[$sectionName])) {
702 $classes = array();
703 if (isset($link['class'])) {
704 $classes = $link['class'];
705 }
706 $link['class'] = array_merge($classes, array('disabled'));
707 }
708 }
709
710 return $formattedConfLinks;
711 }
712
713 }