From 11630f6a0abfb6718337d317ca1fe1fb0869532e Mon Sep 17 00:00:00 2001
From: Seamus Lee <seamuslee001@gmail.com>
Date: Wed, 12 May 2021 09:56:09 +1000
Subject: [PATCH] [REF] Make use of recently added default pager size setting
 in Reports

Update code as per feedback from Dave D
---
 CRM/Report/Form.php                           | 39 ++++++++++++++++---
 CRM/Report/Form/ActivitySummary.php           |  5 ++-
 CRM/Report/Form/Contact/Detail.php            | 12 +++---
 .../Form/Contribute/DeferredRevenue.php       |  5 ++-
 CRM/Report/Form/Contribute/History.php        | 10 +++--
 CRM/Report/Form/Contribute/TopDonor.php       |  3 +-
 CRM/Report/Form/Event/Income.php              | 21 +++++-----
 7 files changed, 66 insertions(+), 29 deletions(-)

diff --git a/CRM/Report/Form.php b/CRM/Report/Form.php
index fa1a841bf9..b7a9a70982 100644
--- a/CRM/Report/Form.php
+++ b/CRM/Report/Form.php
@@ -13,6 +13,9 @@
  * Class CRM_Report_Form
  */
 class CRM_Report_Form extends CRM_Core_Form {
+  /**
+   * Deprecated constant, Reports should be updated to use the getRowCount function.
+   */
   const ROW_COUNT_LIMIT = 50;
 
   /**
@@ -37,6 +40,12 @@ class CRM_Report_Form extends CRM_Core_Form {
    */
   protected $_id;
 
+  /**
+   * The Number of rows to display on screen
+   * @var int
+   */
+  protected $_rowCount;
+
   /**
    * The id of the report template
    *
@@ -518,12 +527,30 @@ class CRM_Report_Form extends CRM_Core_Form {
    */
   public $optimisedForOnlyFullGroupBy = TRUE;
 
+  /**
+   * Get the number of rows to show
+   * @return int
+   */
+  public function getRowCount(): int {
+    return $this->_rowCount;
+  }
+
+  /**
+   * set the number of rows to show
+   * @param $rowCount int
+   */
+  public function setRowCount($rowCount): void {
+    $this->_rowCount = $rowCount;
+  }
+
   /**
    * Class constructor.
    */
   public function __construct() {
     parent::__construct();
 
+    $this->setRowCount(\Civi::settings()->get('default_pager_size'));
+
     $this->addClass('crm-report-form');
 
     if ($this->_tagFilter) {
@@ -2409,7 +2436,7 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND
 
     $this->moveSummaryColumnsToTheRightHandSide();
 
-    if ($this->_limit && count($rows) >= self::ROW_COUNT_LIMIT) {
+    if ($this->_limit && count($rows) >= $this->getRowCount()) {
       return FALSE;
     }
 
@@ -3576,11 +3603,12 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND
   /**
    * Set limit.
    *
-   * @param int $rowCount
+   * @param int|null $rowCount
    *
    * @return array
    */
-  public function limit($rowCount = self::ROW_COUNT_LIMIT) {
+  public function limit($rowCount = NULL) {
+    $rowCount = $rowCount ?? $this->getRowCount();
     // lets do the pager if in html mode
     $this->_limit = NULL;
 
@@ -3628,9 +3656,10 @@ WHERE cg.extends IN ('" . implode("','", $this->_customGroupExtends) . "') AND
   /**
    * Set pager.
    *
-   * @param int $rowCount
+   * @param int|null $rowCount
    */
-  public function setPager($rowCount = self::ROW_COUNT_LIMIT) {
+  public function setPager($rowCount = NULL) {
+    $rowCount = $rowCount ?? $this->getRowCount();
     // CRM-14115, over-ride row count if rowCount is specified in URL
     if ($this->_dashBoardRowCount) {
       $rowCount = $this->_dashBoardRowCount;
diff --git a/CRM/Report/Form/ActivitySummary.php b/CRM/Report/Form/ActivitySummary.php
index 59dc3de711..38e20f360e 100644
--- a/CRM/Report/Form/ActivitySummary.php
+++ b/CRM/Report/Form/ActivitySummary.php
@@ -502,9 +502,10 @@ class CRM_Report_Form_ActivitySummary extends CRM_Report_Form {
   /**
    * Set pager.
    *
-   * @param int $rowCount
+   * @param int|null $rowCount
    */
-  public function setPager($rowCount = self::ROW_COUNT_LIMIT) {
+  public function setPager($rowCount = NULL) {
+    $rowCount = $rowCount ?? $this->getRowCount();
     $this->_rowsFound = $this->totalRows;
     parent::setPager($rowCount);
   }
diff --git a/CRM/Report/Form/Contact/Detail.php b/CRM/Report/Form/Contact/Detail.php
index 47c98364f3..b2f35bb94f 100644
--- a/CRM/Report/Form/Contact/Detail.php
+++ b/CRM/Report/Form/Contact/Detail.php
@@ -15,7 +15,6 @@
  * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 class CRM_Report_Form_Contact_Detail extends CRM_Report_Form {
-  const ROW_COUNT_LIMIT = 10;
 
   protected $_summary = NULL;
 
@@ -364,6 +363,7 @@ class CRM_Report_Form_Contact_Detail extends CRM_Report_Form {
     $this->_groupFilter = TRUE;
     $this->_tagFilter = TRUE;
     parent::__construct();
+    $this->setRowCount(10);
   }
 
   public function preProcess() {
@@ -805,17 +805,19 @@ HERESQL;
 
   /**
    * Override to set limit is 10
-   * @param int $rowCount
+   * @param int|null $rowCount
    */
-  public function limit($rowCount = self::ROW_COUNT_LIMIT) {
+  public function limit($rowCount = NULL) {
+    $rowCount = $rowCount ?? $this->getRowCount();
     parent::limit($rowCount);
   }
 
   /**
    * Override to set pager with limit is 10
-   * @param int $rowCount
+   * @param int|null $rowCount
    */
-  public function setPager($rowCount = self::ROW_COUNT_LIMIT) {
+  public function setPager($rowCount = NULL) {
+    $rowCount = $rowCount ?? $this->getRowCount();
     parent::setPager($rowCount);
   }
 
diff --git a/CRM/Report/Form/Contribute/DeferredRevenue.php b/CRM/Report/Form/Contribute/DeferredRevenue.php
index d4cbdb557b..49ff34962a 100644
--- a/CRM/Report/Form/Contribute/DeferredRevenue.php
+++ b/CRM/Report/Form/Contribute/DeferredRevenue.php
@@ -349,9 +349,10 @@ class CRM_Report_Form_Contribute_DeferredRevenue extends CRM_Report_Form {
   /**
    * Set limit.
    *
-   * @param int $rowCount
+   * @param int|null $rowCount
    */
-  public function limit($rowCount = self::ROW_COUNT_LIMIT) {
+  public function limit($rowCount = NULL) {
+    $rowCount = $rowCount ?? $this->getRowCount();
     $this->_limit = NULL;
   }
 
diff --git a/CRM/Report/Form/Contribute/History.php b/CRM/Report/Form/Contribute/History.php
index bc1a49fe02..1db29aa24c 100644
--- a/CRM/Report/Form/Contribute/History.php
+++ b/CRM/Report/Form/Contribute/History.php
@@ -477,20 +477,22 @@ class CRM_Report_Form_Contribute_History extends CRM_Report_Form {
   /**
    * Override to set limit to 10.
    *
-   * @param int $rowCount
+   * @param int|null $rowCount
    *
    * @return array
    */
-  public function limit($rowCount = self::ROW_COUNT_LIMIT) {
+  public function limit($rowCount = NULL) {
+    $rowCount = $rowCount ?? $this->getRowCount();
     return parent::limit($rowCount);
   }
 
   /**
    * Override to set pager with limit is 10.
    *
-   * @param int $rowCount
+   * @param int|null $rowCount
    */
-  public function setPager($rowCount = self::ROW_COUNT_LIMIT) {
+  public function setPager($rowCount = NULL) {
+    $rowCount = $rowCount ?? $this->getRowCount();
     parent::setPager($rowCount);
   }
 
diff --git a/CRM/Report/Form/Contribute/TopDonor.php b/CRM/Report/Form/Contribute/TopDonor.php
index 7b6c14f51f..c5e05b0e16 100644
--- a/CRM/Report/Form/Contribute/TopDonor.php
+++ b/CRM/Report/Form/Contribute/TopDonor.php
@@ -330,7 +330,8 @@ class CRM_Report_Form_Contribute_TopDonor extends CRM_Report_Form {
   /**
    * @param int $rowCount
    */
-  public function limit($rowCount = CRM_Report_Form::ROW_COUNT_LIMIT) {
+  public function limit($rowCount = NULL) {
+    $rowCount = $rowCount ?? $this->getRowCount();
     // lets do the pager if in html mode
     $this->_limit = NULL;
 
diff --git a/CRM/Report/Form/Event/Income.php b/CRM/Report/Form/Event/Income.php
index 51caf3d4b1..d25a840054 100644
--- a/CRM/Report/Form/Event/Income.php
+++ b/CRM/Report/Form/Event/Income.php
@@ -15,7 +15,6 @@
  * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 class CRM_Report_Form_Event_Income extends CRM_Report_Form {
-  const ROW_COUNT_LIMIT = 2;
 
   protected $_summary = NULL;
   protected $_noFields = TRUE;
@@ -27,7 +26,6 @@ class CRM_Report_Form_Event_Income extends CRM_Report_Form {
    * Class constructor.
    */
   public function __construct() {
-
     $this->_columns = [
       'civicrm_event' => [
         'dao' => 'CRM_Event_DAO_Event',
@@ -43,6 +41,7 @@ class CRM_Report_Form_Event_Income extends CRM_Report_Form {
     ];
 
     parent::__construct();
+    $this->setRowCount(2);
   }
 
   public function preProcess() {
@@ -225,27 +224,29 @@ class CRM_Report_Form_Event_Income extends CRM_Report_Form {
   /**
    * @inheritDoc
    */
-  public function limit($rowCount = self::ROW_COUNT_LIMIT) {
+  public function limit($rowCount = NULL) {
+    $rowCount = $rowCount ?? $this->getRowCount();
     parent::limit($rowCount);
 
     // Modify limit.
     $pageId = $this->get(CRM_Utils_Pager::PAGE_ID);
 
     //if pageId is greater than last page then display last page.
-    if ((($pageId * self::ROW_COUNT_LIMIT) - 1) > $this->_rowsFound) {
-      $pageId = ceil((float) $this->_rowsFound / (float) self::ROW_COUNT_LIMIT);
+    if ((($pageId * $rowCount) - 1) > $this->_rowsFound) {
+      $pageId = ceil((float) $this->_rowsFound / (float) $rowCount);
       $this->set(CRM_Utils_Pager::PAGE_ID, $pageId);
     }
-    $this->_limit = ($pageId - 1) * self::ROW_COUNT_LIMIT;
+    $this->_limit = ($pageId - 1) * $rowCount;
   }
 
   /**
-   * @param int $rowCount
+   * @param int|null $rowCount
    */
-  public function setPager($rowCount = self::ROW_COUNT_LIMIT) {
+  public function setPager($rowCount = NULL) {
+    $rowCount = $rowCount ?? $this->getRowCount();
     $params = [
       'total' => $this->_rowsFound,
-      'rowCount' => self::ROW_COUNT_LIMIT,
+      'rowCount' => $rowCount,
       'status' => ts('Records %%StatusMessage%%'),
       'buttonBottom' => 'PagerBottomButton',
       'buttonTop' => 'PagerTopButton',
@@ -297,7 +298,7 @@ class CRM_Report_Form_Event_Income extends CRM_Report_Form {
       $numRows = $this->_limit;
 
       if (CRM_Utils_Array::value('id_op', $this->_params, 'in') == 'in' || $noSelection) {
-        while ($count < self::ROW_COUNT_LIMIT) {
+        while ($count < $rowCount) {
           if (!isset($this->_params['id_value'][$numRows])) {
             break;
           }
-- 
2.25.1