Merge remote branch 'canonical/master' into merge-20140827
[civicrm-core.git] / CRM / Utils / Pager.php
index db8f3f4148c47d7d36c0c69086e5660993051488..9615cccdcca9f94a5893c51d96d9aa11c0ea0527 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
  */
 
 require_once 'Pager/Sliding.php';
+
+/**
+ * Class CRM_Utils_Pager
+ */
 class CRM_Utils_Pager extends Pager_Sliding {
 
   /**
@@ -64,18 +68,17 @@ class CRM_Utils_Pager extends Pager_Sliding {
    * We have embedded some html in this class. Need to figure out how to export this
    * to the top level at some point in time
    *
-   * @param int     total        the total count of items to be displayed
-   * @param int     currentPage  the page currently being displayed
-   * @param string  status       the status message to be displayed. It embeds a token
+   * @param $params
+   *
+   * @internal param \total $int the total count of items to be displayed
+   * @internal param \currentPage $int the page currently being displayed
+   * @internal param \status $string the status message to be displayed. It embeds a token
    *                             %%statusMessage%% that will be replaced with which items
    *                             are currently being displayed
-   * @param string  csvString    the title of the link to be displayed for the export
-   * @param int     perPage      the number of items displayed per page
-   *
-   * @return object              the newly created and initialized pager object
-   *
-   * @access public
+   * @internal param \csvString $string the title of the link to be displayed for the export
+   * @internal param \perPage $int the number of items displayed per page
    *
+   * @return \CRM_Utils_Pager the newly created and initialized pager object@access public
    */
   function __construct($params) {
     if ($params['status'] === NULL) {
@@ -112,9 +115,7 @@ class CRM_Utils_Pager extends Pager_Sliding {
       'status' => CRM_Utils_Array::value('status', $params),
       'buttonTop' => CRM_Utils_Array::value('buttonTop', $params),
       'buttonBottom' => CRM_Utils_Array::value('buttonBottom', $params),
-      'twentyfive' => $this->getPerPageLink(25),
-      'fifty' => $this->getPerPageLink(50),
-      'onehundred' => $this->getPerPageLink(100),
+      'currentLocation' => $this->getCurrentLocation(),
     );
 
     /**
@@ -189,11 +190,12 @@ class CRM_Utils_Pager extends Pager_Sliding {
    * POST over-rides a GET, a POST at the top overrides
    * a POST at the bottom (of the page)
    *
-   * @param int defaultPageId   current pageId
+   * @param int $defaultPageId defaultPageId   current pageId
+   *
+   * @param $params
    *
    * @return int                new pageId to display to the user
    * @access public
-   *
    */
   function getPageID($defaultPageId = 1, &$params) {
     // POST has higher priority than GET vars
@@ -264,74 +266,57 @@ class CRM_Utils_Pager extends Pager_Sliding {
   }
 
   /**
-   * given a number create a link that will display the number of
-   * rows as specified by that link
-   *
-   * @param int $perPage the number of rows
-   *
-   * @return string      the link
-   * @access void
+   * @return string
    */
-  function getPerPageLink($perPage) {
-    if ($perPage != $this->_perPage) {
-      $href = $this->makeURL(self::PAGE_ROWCOUNT, $perPage);
-      $link = sprintf('<a href="%s" %s>%s</a>',
-        $href,
-        $this->_classString,
-        $perPage
-      ) . $this->_spacesBefore . $this->_spacesAfter;
-    }
-    else {
-      $link = $this->_spacesBefore . $perPage . $this->_spacesAfter;
-    }
-
-    return $link;
+  function getCurrentLocation() {
+    $config = CRM_Core_Config::singleton();
+    $path = CRM_Utils_Array::value($config->userFrameworkURLVar, $_GET);
+    return CRM_Utils_System::url($path, CRM_Utils_System::getLinksUrl(self::PAGE_ID, FALSE, TRUE), FALSE, NULL, FALSE) . $this->getCurrentPageID();
   }
 
+  /**
+   * @return string
+   */
   function getFirstPageLink() {
     if ($this->isFirstPage()) {
       return '';
     }
-
     $href = $this->makeURL(self::PAGE_ID, 1);
-    return sprintf('<a href="%s" title="%s">%s</a>',
-      $href,
-      str_replace('%d', 1, $this->_altFirst),
-      $this->_firstPagePre . $this->_firstPageText . $this->_firstPagePost
-    ) . $this->_spacesBefore . $this->_spacesAfter;
+    return $this->formatLink($href, str_replace('%d', 1, $this->_altFirst), $this->_firstPagePre . $this->_firstPageText . $this->_firstPagePost) .
+      $this->_spacesBefore . $this->_spacesAfter;
   }
 
+  /**
+   * @return string
+   */
   function getLastPageLink() {
     if ($this->isLastPage()) {
       return '';
     }
-
     $href = $this->makeURL(self::PAGE_ID, $this->_totalPages);
-    return sprintf('<a href="%s" title="%s">%s</a>',
-      $href,
-      str_replace('%d', $this->_totalPages, $this->_altLast),
-      $this->_lastPagePre . $this->_lastPageText . $this->_lastPagePost
-    );
+    return $this->formatLink($href, str_replace('%d', $this->_totalPages, $this->_altLast), $this->_lastPagePre . $this->_lastPageText . $this->_lastPagePost);
   }
 
+  /**
+   * @return string
+   */
   function getBackPageLink() {
     if ($this->_currentPage > 1) {
       $href = $this->makeURL(self::PAGE_ID, $this->getPreviousPageID());
-      return sprintf('<a href="%s" title="%s">%s</a>',
-        $href,
-        $this->_altPrev, $this->_prevImg
-      ) . $this->_spacesBefore . $this->_spacesAfter;
+      return $this->formatLink($href, $this->_altPrev, $this->_prevImg) . $this->_spacesBefore . $this->_spacesAfter;
     }
     return '';
   }
 
+  /**
+   * @return string
+   */
   function getNextPageLink() {
     if ($this->_currentPage < $this->_totalPages) {
       $href = $this->makeURL(self::PAGE_ID, $this->getNextPageID());
-      return $this->_spacesAfter . sprintf('<a href="%s" title="%s">%s</a>',
-        $href,
-        $this->_altNext, $this->_nextImg
-      ) . $this->_spacesBefore . $this->_spacesAfter;
+      return $this->_spacesAfter .
+        $this->formatLink($href, $this->_altNext, $this->_nextImg) .
+        $this->_spacesBefore . $this->_spacesAfter;
     }
     return '';
   }
@@ -340,12 +325,23 @@ class CRM_Utils_Pager extends Pager_Sliding {
    * Build a url for pager links
    */
   function makeURL($key, $value) {
-    $href = CRM_Utils_System::makeURL($key);
+    $href = CRM_Utils_System::makeURL($key, TRUE);
     // CRM-12212 Remove alpha sort param
     if (strpos($href, '&amp;sortByCharacter=')) {
       $href = preg_replace('#(.*)\&amp;sortByCharacter=[^&]*(.*)#', '\1\2', $href);
     }
     return $href . $value;
   }
+
+  /**
+   * Output the html pager link
+   * @param string $href
+   * @param string $title
+   * @param string $image
+   * @return string
+   */
+  private function formatLink($href, $title, $image) {
+    return sprintf('<a class="crm-pager-link action-item crm-hover-button" href="%s" title="%s">%s</a>', $href, $title, $image);
+  }
 }