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