Просмотр исходного кода

Ticket #4563 - Pagination: Add possibility to show Total number of results.

AntonLV 2 лет назад
Родитель
Сommit
cc6b691f17

+ 41 - 23
inc/classes/BxDolPaginate.php

@@ -66,9 +66,14 @@ abstract class BxDolPaginate extends BxDol
 
     protected $_oTemplate;
 
+    protected $_aParams; ///< an array with initially provided params
+
     protected $_iStart; ///< start display items from this number
-    protected $_iNum; ///< available results, you need to query per_page + 1 results, so paginate can determine last page
     protected $_iPerPage; ///< results per page
+    protected $_iNum; ///< available results, you need to query per_page + 1 results, so paginate can determine last page
+
+    protected $_iTotal; ///< total number of results. (Optional)
+    protected $_bTotal; ///< show displayed items info with total value
 
     protected $_sPageUrl; ///< page url of next/prev page, special markers will be replaced here automatically
     protected $_sOnChangePage; ///< on click of next/prev page, special markers will be replaced here automatically
@@ -96,9 +101,14 @@ abstract class BxDolPaginate extends BxDol
            trigger_error ('Paginate "count" is deprecated - use "num" instead: ' . get_class($this), E_USER_ERROR);
 
         //--- Main settings ---//
+        $this->_aParams = $aParams;
+
         $this->_iStart = isset($aParams['start']) && (int)$aParams['start'] > 0 ? (int)$aParams['start'] : 0;
-        $this->_iNum = isset($aParams['num']) ? (int)$aParams['num'] : 0;
         $this->_iPerPage = isset($aParams['per_page']) && (int)$aParams['per_page'] > 0 ? (int)$aParams['per_page'] : BX_DOL_PAGINATE_PER_PAGE_DEFAULT;
+        $this->_iNum = isset($aParams['num']) ? (int)$aParams['num'] : 0;
+
+        $this->_iTotal = isset($aParams['total']) ? (int)$aParams['total'] : 0;
+        $this->_bTotal = $this->_iTotal > 0;
 
         $this->_bInfo = isset($aParams['info']) ? (bool)$aParams['info'] : true;
         $this->_sButtonsClass = isset($aParams['buttons_class']) ? $aParams['buttons_class'] : '';
@@ -278,37 +288,43 @@ abstract class BxDolPaginate extends BxDol
 
         $sClassAdd = ($this->_sButtonsClass ? ' ' . $this->_sButtonsClass : '');
 
-        $aVariables = array(
-            'bx_if:info' => array (
-                'condition' => (bool)$this->_bInfo,
-                'content' => array(
+        $aVariables = [
+            'bx_if:info' => [
+                'condition' => $this->_bInfo && !$this->_bTotal,
+                'content' => [
                     'text' => _t('_sys_paginate_info', $this->_iStart + 1, $this->_iStart + ($this->isNextAvail () ? $this->_iPerPage : $this->_iNum)),
-                ),
-            ),
-            'bx_if:view_all' => array(
+                ],
+            ],
+            'bx_if:total' => [
+                'condition' => $this->_bTotal,
+                'content' => [
+                    'text' => _t('_sys_paginate_total', $this->_iStart + 1, $this->_iStart + ($this->isNextAvail() ? $this->_iPerPage : $this->_iNum), $this->_iTotal),
+                ],
+            ],
+            'bx_if:view_all' => [
                 'condition' => (bool)$this->_sViewAllUrl,
-                'content' => array(
+                'content' => [
                     'lnk_url' => $this->_sViewAllUrl,
                     'lnk_title' => $this->_sViewAllCaption,
                     'lnk_content' => $this->_sViewAllCaption,
-                ),
-            ),
-            'btn_prev' => $this->_getButton('prev', array(
+                ],
+            ],
+            'btn_prev' => $this->_getButton('prev', [
                 'class' => $sClassAdd . $sPrevClassAdd,
                 'href' => $sPrevLnkUrl,
                 'onclick' => $sPrevLnkClick,
-            )),
-            'btn_next' => $this->_getButton('next', array(
+            ]),
+            'btn_next' => $this->_getButton('next', [
                 'class' => $sClassAdd . $sNextClassAdd,
                 'href' => $sNextLnkUrl,
                 'onclick' => $sNextLnkClick,
-            )),
+            ]),
             'paginate_class' => $this->_sPaginateClass,
-			'bx_repeat:attrs' => array(
-                array('key' => 'bx-data-start', 'value' => $this->_iStart),
-                array('key' => 'bx-data-perpage', 'value' => $this->_iPerPage)
-            )
-        );
+            'bx_repeat:attrs' => [
+                ['key' => 'bx-data-start', 'value' => $this->_iStart],
+                ['key' => 'bx-data-perpage', 'value' => $this->_iPerPage]
+            ]
+        ];
 
         $this->addCssJs();
         return $this->_oTemplate->parseHtmlByName('paginate.html', $aVariables);
@@ -324,10 +340,12 @@ abstract class BxDolPaginate extends BxDol
      */
     public function getSimplePaginate($sViewAllUrl = '', $iStart = -1, $iNum = -1, $iPerPage = -1)
     {
-        if ($sViewAllUrl)
+        if($sViewAllUrl)
             $this->_sViewAllUrl = $sViewAllUrl;
 
-        $this->_bInfo = false;
+        if(!isset($this->_aParams['info']))
+            $this->_bInfo = false;
+
         $this->_sButtonsClass .= ($this->_sButtonsClass ? ' ' : '') . 'bx-btn-small bx-btn-symbol-small';
         $this->_sPaginateClass = 'bx-paginate-simple';
 

+ 12 - 1
inc/classes/BxDolSearch.php

@@ -1011,10 +1011,21 @@ class BxDolSearchResult implements iBxDolReplaceable
     function getNum ()
     {
         $aJoins = $this->getJoins(false);
-        $sqlQuery =  "SELECT * FROM `{$this->aCurrent['table']}` " . (isset($aJoins['join']) ? ' ' . $aJoins['join'] : '' ). $this->getRestriction() . (isset($aJoins['groupBy']) ? ' ' . $aJoins['groupBy'] : '') . ' ' . $this->getLimit(true);
+        $sqlQuery =  "SELECT * FROM `{$this->aCurrent['table']}` " . (isset($aJoins['join']) ? ' ' . $aJoins['join'] : '' ) . $this->getRestriction() . (isset($aJoins['groupBy']) ? ' ' . $aJoins['groupBy'] : '') . ' ' . $this->getLimit(true);
         return count(BxDolDb::getInstance()->getAll($sqlQuery));
     }
 
+    /**
+     * Get total number of records
+     * return total number of records
+     */
+    function getTotal ()
+    {
+        $aJoins = $this->getJoins(false);
+        $sqlQuery =  "SELECT COUNT(*) FROM `{$this->aCurrent['table']}` " . (isset($aJoins['join']) ? ' ' . $aJoins['join'] : '' ) . $this->getRestriction() . (isset($aJoins['groupBy']) ? ' ' . $aJoins['groupBy'] : '');
+        return BxDolDb::getInstance()->getOne($sqlQuery);
+    }
+
     /**
      * Check restriction params and make condition part of query
      * return $sqlWhere sql code of query for WHERE part

+ 1 - 0
modules/boonex/english/data/langs/system/en.xml

@@ -3443,6 +3443,7 @@ If it is not enabled then please consider implement this optimization, since it
     <string name="_sys_page_type_standard"><![CDATA[Standard]]></string>
     <string name="_sys_page_type_wo_hf"><![CDATA[Without header/footer]]></string>
     <string name="_sys_paginate_info"><![CDATA[{0}-{1}]]></string>
+    <string name="_sys_paginate_total"><![CDATA[{0}-{1} of {2}]]></string>
     <string name="_sys_paginate_view_all"><![CDATA[View All]]></string>
     <string name="_sys_payments_err_no_payments"><![CDATA[There is no installed payment processing module.]]></string>
     <string name="_sys_payments_err_not_accept_payments"><![CDATA[You need to activate at least one payment provider. You may do it via My Account menu -> Settings page -> <a href="{0}">Payments</a> tab.]]></string>

+ 1 - 0
modules/boonex/russian/data/langs/system/ru.xml

@@ -3396,6 +3396,7 @@
         <string name="_sys_page_type_standard"><![CDATA[Стандартная]]></string>
 	<string name="_sys_page_type_wo_hf"><![CDATA[Без заголовка и основания]]></string>
 	<string name="_sys_paginate_info"><![CDATA[{0}-{1}]]></string>
+        <string name="_sys_paginate_total"><![CDATA[{0}-{1} из {2}]]></string>
 	<string name="_sys_paginate_view_all"><![CDATA[Просмотреть все]]></string>
         <string name="_sys_payments_err_no_payments"><![CDATA[Нет ни одного установленного модуля обработки платежей.]]></string>
         <string name="_sys_payments_err_not_accept_payments"><![CDATA[Вам необходимо активировать хотя бы одину платежную систему. Вы можете сделать это через меню Мой аккаунт -> страница Настройки -> <a href="{0}">вкладка Платёжи</a>.]]></string>

+ 5 - 0
template/paginate.html

@@ -8,6 +8,11 @@
             __text__
         </div>
     </bx_if:info>
+    <bx_if:total>
+        <div class="bx-paginate-info bx-pi-total pl-4">
+            __text__
+        </div>
+    </bx_if:total>
     <bx_if:view_all>
         <div class="bx-paginate-view-all pl-4">
             <a href="__lnk_url__" title="__lnk_title__">__lnk_content__</a>

+ 28 - 23
template/scripts/BxBaseSearchResult.php

@@ -107,22 +107,22 @@ class BxBaseSearchResult extends BxDolSearchResult
 
     function displaySearchBox ($sContent, $sPaginate = '')
     {
-		$sContent = BxDolTemplate::getInstance()->parseHtmlByName('paginate_block.html', array(
-			'bx_if:paginate_top' => array(
-				'condition' => (isset($this->aCurrent['paginate']['on_top']) && $this->aCurrent['paginate']['on_top'] == true) && $sPaginate != '',
-				'content' => array(
-					'paginate' => $sPaginate
-				),
-			),
-			'bx_if:paginate_bottom' => array(
-				'condition' => (!isset($this->aCurrent['paginate']['on_bottom']) || $this->aCurrent['paginate']['on_bottom'] != false) && $sPaginate != '',
-				'content' => array(
-					'paginate' => $sPaginate
-				),
-			),
-			'content' => $sContent
-		));
-		
+        $sContent = BxDolTemplate::getInstance()->parseHtmlByName('paginate_block.html', array(
+            'bx_if:paginate_top' => array(
+                'condition' => (isset($this->aCurrent['paginate']['on_top']) && $this->aCurrent['paginate']['on_top'] == true) && $sPaginate != '',
+                'content' => array(
+                    'paginate' => $sPaginate
+                ),
+            ),
+            'bx_if:paginate_bottom' => array(
+                'condition' => (!isset($this->aCurrent['paginate']['on_bottom']) || $this->aCurrent['paginate']['on_bottom'] != false) && $sPaginate != '',
+                'content' => array(
+                    'paginate' => $sPaginate
+                ),
+            ),
+            'content' => $sContent
+        ));
+	
         $sMenu = $this->getDesignBoxMenu();
 
         if ($this->id) {
@@ -239,19 +239,24 @@ class BxBaseSearchResult extends BxDolSearchResult
 
     function showPagination($bAdmin = false, $bChangePage = true, $bPageReload = true)
     {
-        $oMain = $this->getMain();
-
-        $sPageUrl = $this->getCurrentUrl(array(), false);
-        $sOnClick = $this->getCurrentOnclick(array(), false);
+        $sOnClick = $this->getCurrentOnclick([], false);
 
-        $oPaginate = new BxTemplPaginate(array(
-            'page_url' => $sPageUrl,
+        $aPaginate = [
+            'page_url' => $this->getCurrentUrl([], false),
             'on_change_page' => $sOnClick,
             'num' => $this->aCurrent['paginate']['num'],
             'per_page' => $this->aCurrent['paginate']['perPage'],
             'start' => $this->aCurrent['paginate']['start'],
-        ));
+        ];
+
+        if(!empty($this->aCurrent['paginate']['total'])) {
+            if(is_numeric($this->aCurrent['paginate']['total']))
+                $aPaginate['total'] = (int)$this->aCurrent['paginate']['total'];
+            else
+                $aPaginate['total'] = $this->getTotal();
+        }
 
+        $oPaginate = new BxTemplPaginate($aPaginate);
         return $sOnClick ? $oPaginate->getSimplePaginate() : $oPaginate->getPaginate();
     }