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