Merge pull request #4822 from kurund/indentation-fixes
[civicrm-core.git] / CRM / Contribute / Page / ContributionPage.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 */
47 class 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 'ref' => 'crm-enable-disable',
89 ),
90 CRM_Core_Action::ENABLE => array(
91 'name' => ts('Enable'),
92 'ref' => 'crm-enable-disable',
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 public 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 public 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 public function &contributionLinks() {
228 if (!isset(self::$_contributionLinks)) {
229 //get contribution dates.
230 $dates = CRM_Contribute_BAO_Contribution::getContributionDates();
231 $now = $dates['now'];
232 $yearDate = $dates['yearDate'];
233 $monthDate = $dates['monthDate'];
234 $yearNow = $yearDate + 10000;
235
236 $urlString = 'civicrm/contribute/search';
237 $urlParams = 'reset=1&pid=%%id%%&force=1&test=0';
238
239 self::$_contributionLinks = array(
240 CRM_Core_Action::DETACH => array(
241 'name' => ts('Current Month-To-Date'),
242 'title' => ts('Current Month-To-Date'),
243 'url' => $urlString,
244 'qs' => "{$urlParams}&start={$monthDate}&end={$now}",
245 'uniqueName' => 'current_month_to_date',
246 ),
247 CRM_Core_Action::REVERT => array(
248 'name' => ts('Fiscal Year-To-Date'),
249 'title' => ts('Fiscal Year-To-Date'),
250 'url' => $urlString,
251 'qs' => "{$urlParams}&start={$yearDate}&end={$yearNow}",
252 'uniqueName' => 'fiscal_year_to_date',
253 ),
254 CRM_Core_Action::BROWSE => array(
255 'name' => ts('Cumulative'),
256 'title' => ts('Cumulative'),
257 'url' => $urlString,
258 'qs' => "{$urlParams}&start=&end=$now",
259 'uniqueName' => 'cumulative',
260 ),
261 );
262 }
263
264 return self::$_contributionLinks;
265 }
266
267 /**
268 * Run the page.
269 *
270 * This method is called after the page is created. It checks for the
271 * type of action and executes that action.
272 * Finally it calls the parent's run method.
273 *
274 * @return void
275 *
276 */
277 public function run() {
278 // get the requested action
279 $action = CRM_Utils_Request::retrieve('action', 'String',
280 // default to 'browse'
281 $this, FALSE, 'browse'
282 );
283
284 // assign vars to templates
285 $this->assign('action', $action);
286 $id = CRM_Utils_Request::retrieve('id', 'Positive',
287 $this, FALSE, 0
288 );
289
290 // set breadcrumb to append to 2nd layer pages
291 $breadCrumb = array(array('title' => ts('Manage Contribution Pages'),
292 'url' => CRM_Utils_System::url(CRM_Utils_System::currentPath(),
293 'reset=1'
294 ),
295 ));
296
297 // what action to take ?
298 if ($action & CRM_Core_Action::ADD) {
299 $session = CRM_Core_Session::singleton();
300 $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(),
301 'action=browse&reset=1'
302 ));
303
304 $controller = new CRM_Contribute_Controller_ContributionPage(NULL, $action);
305 CRM_Utils_System::setTitle(ts('Manage Contribution Page'));
306 CRM_Utils_System::appendBreadCrumb($breadCrumb);
307 return $controller->run();
308 }
309 elseif ($action & CRM_Core_Action::UPDATE) {
310 $config = CRM_Core_Config::singleton();
311
312 // assign vars to templates
313 $this->assign('id', $id);
314 $this->assign('title', CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $id, 'title'));
315 $this->assign('is_active', CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $id, 'is_active'));
316 if (in_array('CiviMember', $config->enableComponents)) {
317 $this->assign('CiviMember', TRUE);
318 }
319 }
320 elseif ($action & CRM_Core_Action::COPY) {
321 $session = CRM_Core_Session::singleton();
322 CRM_Core_Session::setStatus(ts('A copy of the contribution page has been created'), ts('Successfully Copied'), 'success');
323 $this->copy();
324 }
325 elseif ($action & CRM_Core_Action::DELETE) {
326 CRM_Utils_System::appendBreadCrumb($breadCrumb);
327
328 $session = CRM_Core_Session::singleton();
329 $session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(),
330 'reset=1&action=browse'
331 ));
332
333 $id = CRM_Utils_Request::retrieve('id', 'Positive',
334 $this, FALSE, 0
335 );
336 $query = "
337 SELECT ccp.title
338 FROM civicrm_contribution_page ccp
339 JOIN civicrm_pcp cp ON ccp.id = cp.page_id
340 WHERE cp.page_id = {$id}
341 AND cp.page_type = 'contribute'
342 ";
343
344 if ($pageTitle = CRM_Core_DAO::singleValueQuery($query)) {
345 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');
346
347 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1'));
348 }
349
350 $controller = new CRM_Core_Controller_Simple('CRM_Contribute_Form_ContributionPage_Delete',
351 'Delete Contribution Page',
352 CRM_Core_Action::DELETE
353 );
354 $controller->set('id', $id);
355 $controller->process();
356 return $controller->run();
357 }
358 else {
359 // finally browse the contribution pages
360 $this->browse();
361
362 CRM_Utils_System::setTitle(ts('Manage Contribution Pages'));
363 }
364
365 return parent::run();
366 }
367
368 /**
369 * This function is to make a copy of a contribution page, including
370 * all the fields in the page
371 *
372 * @return void
373 */
374 public function copy() {
375 $gid = CRM_Utils_Request::retrieve('gid', 'Positive',
376 $this, TRUE, 0, 'GET'
377 );
378
379 CRM_Contribute_BAO_ContributionPage::copy($gid);
380
381 CRM_Utils_System::redirect(CRM_Utils_System::url(CRM_Utils_System::currentPath(), 'reset=1'));
382 }
383
384 /**
385 * Browse all contribution pages
386 *
387 * @param null $action
388 *
389 * @return void
390 * @static
391 */
392 public function browse($action = NULL) {
393 $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter',
394 'String',
395 $this
396 );
397 $createdId = CRM_Utils_Request::retrieve('cid', 'Positive',
398 $this, FALSE, 0
399 );
400
401 if ($this->_sortByCharacter == 1 ||
402 !empty($_POST)
403 ) {
404 $this->_sortByCharacter = '';
405 $this->set('sortByCharacter', '');
406 }
407
408 $this->search();
409
410 $params = array();
411
412 $whereClause = $this->whereClause($params, FALSE);
413 $this->pagerAToZ($whereClause, $params);
414
415 $params = array();
416 $whereClause = $this->whereClause($params, TRUE);
417 $this->pager($whereClause, $params);
418
419 list($offset, $rowCount) = $this->_pager->getOffsetAndRowCount();
420
421 //check for delete CRM-4418
422 $allowToDelete = CRM_Core_Permission::check('delete in CiviContribute');
423
424 $query = "
425 SELECT id
426 FROM civicrm_contribution_page
427 WHERE $whereClause
428 LIMIT $offset, $rowCount";
429 $contribPage = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Contribute_DAO_ContributionPage');
430 $contribPageIds = array();
431 while ($contribPage->fetch()) {
432 $contribPageIds[$contribPage->id] = $contribPage->id;
433 }
434 //get all section info.
435 $contriPageSectionInfo = CRM_Contribute_BAO_ContributionPage::getSectionInfo($contribPageIds);
436
437 $query = "
438 SELECT *
439 FROM civicrm_contribution_page
440 WHERE $whereClause
441 ORDER BY title asc
442 LIMIT $offset, $rowCount";
443
444 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Contribute_DAO_ContributionPage');
445
446 //get all campaigns.
447 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
448
449 //get configure actions links.
450 $configureActionLinks = self::configureActionLinks();
451
452 while ($dao->fetch()) {
453 $contribution[$dao->id] = array();
454 CRM_Core_DAO::storeValues($dao, $contribution[$dao->id]);
455
456 // form all action links
457 $action = array_sum(array_keys($this->actionLinks()));
458
459 //add configure actions links.
460 $action += array_sum(array_keys($configureActionLinks));
461
462 //add online contribution links.
463 $action += array_sum(array_keys(self::onlineContributionLinks()));
464
465 //add contribution search links.
466 $action += array_sum(array_keys(self::contributionLinks()));
467
468 if ($dao->is_active) {
469 $action -= CRM_Core_Action::ENABLE;
470 }
471 else {
472 $action -= CRM_Core_Action::DISABLE;
473 }
474
475 //CRM-4418
476 if (!$allowToDelete) {
477 $action -= CRM_Core_Action::DELETE;
478 }
479
480 //build the configure links.
481 $sectionsInfo = CRM_Utils_Array::value($dao->id, $contriPageSectionInfo, array());
482 $contribution[$dao->id]['configureActionLinks'] = CRM_Core_Action::formLink(self::formatConfigureLinks($sectionsInfo),
483 $action,
484 array('id' => $dao->id),
485 ts('Configure'),
486 TRUE,
487 'contributionpage.configure.actions',
488 'ContributionPage',
489 $dao->id
490 );
491
492 //build the contributions links.
493 $contribution[$dao->id]['contributionLinks'] = CRM_Core_Action::formLink(self::contributionLinks(),
494 $action,
495 array('id' => $dao->id),
496 ts('Contributions'),
497 TRUE,
498 'contributionpage.contributions.search',
499 'ContributionPage',
500 $dao->id
501 );
502
503 //build the online contribution links.
504 $contribution[$dao->id]['onlineContributionLinks'] = CRM_Core_Action::formLink(self::onlineContributionLinks(),
505 $action,
506 array('id' => $dao->id),
507 ts('Links'),
508 TRUE,
509 'contributionpage.online.links',
510 'ContributionPage',
511 $dao->id
512 );
513
514 //build the normal action links.
515 $contribution[$dao->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(),
516 $action,
517 array('id' => $dao->id),
518 ts('more'),
519 TRUE,
520 'contributionpage.action.links',
521 'ContributionPage',
522 $dao->id
523 );
524
525 //show campaigns on selector.
526 $contribution[$dao->id]['campaign'] = CRM_Utils_Array::value($dao->campaign_id, $allCampaigns);
527 }
528
529 if (isset($contribution)) {
530 $this->assign('rows', $contribution);
531 }
532 }
533
534 public function search() {
535 if (isset($this->_action) &
536 (CRM_Core_Action::ADD |
537 CRM_Core_Action::UPDATE |
538 CRM_Core_Action::DELETE
539 )
540 ) {
541 return;
542 }
543
544 $form = new CRM_Core_Controller_Simple('CRM_Contribute_Form_SearchContribution',
545 ts('Search Contribution'),
546 CRM_Core_Action::ADD
547 );
548 $form->setEmbedded(TRUE);
549 $form->setParent($this);
550 $form->process();
551 $form->run();
552 }
553
554 /**
555 * @param array $params
556 * @param bool $sortBy
557 *
558 * @return int|string
559 */
560 public function whereClause(&$params, $sortBy = TRUE) {
561 $values = $clauses = array();
562 $title = $this->get('title');
563 $createdId = $this->get('cid');
564
565 if ($createdId) {
566 $clauses[] = "(created_id = {$createdId})";
567 }
568
569 if ($title) {
570 $clauses[] = "title LIKE %1";
571 if (strpos($title, '%') !== FALSE) {
572 $params[1] = array(trim($title), 'String', FALSE);
573 }
574 else {
575 $params[1] = array(trim($title), 'String', TRUE);
576 }
577 }
578
579 $value = $this->get( 'financial_type_id' );
580 $val = array();
581 if ($value) {
582 if (is_array($value)) {
583 foreach ($value as $k => $v) {
584 if ($v) {
585 $val[$k] = $k;
586 }
587 }
588 $type = implode(',', $val);
589 }
590
591 $clauses[] = "financial_type_id IN ({$type})";
592 }
593
594 if ($sortBy &&
595 $this->_sortByCharacter !== NULL
596 ) {
597 $clauses[] = "title LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_sortByCharacter)) . "%'";
598 }
599
600 $campainIds = $this->get('campaign_id');
601 if (!CRM_Utils_System::isNull($campainIds)) {
602 if (!is_array($campainIds)) {
603 $campaignIds = array($campaignIds);
604 }
605 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
606 }
607
608 if (empty($clauses)) {
609 // Let template know if user has run a search or not
610 $this->assign('isSearch', 0);
611 return 1;
612 }
613 else {
614 $this->assign('isSearch', 1);
615 }
616
617 return implode(' AND ', $clauses);
618 }
619
620 /**
621 * @param $whereClause
622 * @param array $whereParams
623 */
624 public function pager($whereClause, $whereParams) {
625
626 $params['status'] = ts('Contribution %%StatusMessage%%');
627 $params['csvString'] = NULL;
628 $params['buttonTop'] = 'PagerTopButton';
629 $params['buttonBottom'] = 'PagerBottomButton';
630 $params['rowCount'] = $this->get(CRM_Utils_Pager::PAGE_ROWCOUNT);
631 if (!$params['rowCount']) {
632 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
633 }
634
635 $query = "
636 SELECT count(id)
637 FROM civicrm_contribution_page
638 WHERE $whereClause";
639
640 $params['total'] = CRM_Core_DAO::singleValueQuery($query, $whereParams);
641
642 $this->_pager = new CRM_Utils_Pager($params);
643 $this->assign_by_ref('pager', $this->_pager);
644 }
645
646 /**
647 * @param $whereClause
648 * @param array $whereParams
649 */
650 public function pagerAtoZ($whereClause, $whereParams) {
651
652 $query = "
653 SELECT DISTINCT UPPER(LEFT(title, 1)) as sort_name
654 FROM civicrm_contribution_page
655 WHERE $whereClause
656 ORDER BY LEFT(title, 1)
657 ";
658 $dao = CRM_Core_DAO::executeQuery($query, $whereParams);
659
660 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE);
661 $this->assign('aToZ', $aToZBar);
662 }
663
664 /**
665 * @param $sectionsInfo
666 *
667 * @return array
668 */
669 public function formatConfigureLinks($sectionsInfo) {
670 //build the formatted configure links.
671 $formattedConfLinks = self::configureActionLinks();
672 foreach ($formattedConfLinks as $act => & $link) {
673 $sectionName = CRM_Utils_Array::value('uniqueName', $link);
674 if (!$sectionName) {
675 continue;
676 }
677
678 $classes = array();
679 if (isset($link['class'])) {
680 $classes = $link['class'];
681 }
682
683 if (empty($sectionsInfo[$sectionName])) {
684 $classes = array();
685 if (isset($link['class'])) {
686 $classes = $link['class'];
687 }
688 $link['class'] = array_merge($classes, array('disabled'));
689 }
690 }
691
692 return $formattedConfLinks;
693 }
694 }