source: temp/test-xoops.ec-cube.net/html/class/pagenav.php @ 405

Revision 405, 8.4 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: pagenav.php,v 1.5 2005/09/04 20:46:08 onokazu Exp $
3//  ------------------------------------------------------------------------ //
4//                XOOPS - PHP Content Management System                      //
5//                    Copyright (c) 2000 XOOPS.org                           //
6//                       <http://www.xoops.org/>                             //
7//  ------------------------------------------------------------------------ //
8//  This program is free software; you can redistribute it and/or modify     //
9//  it under the terms of the GNU General Public License as published by     //
10//  the Free Software Foundation; either version 2 of the License, or        //
11//  (at your option) any later version.                                      //
12//                                                                           //
13//  You may not change or alter any portion of this comment or credits       //
14//  of supporting developers from this source code or any supporting         //
15//  source code which is considered copyrighted (c) material of the          //
16//  original comment or credit authors.                                      //
17//                                                                           //
18//  This program is distributed in the hope that it will be useful,          //
19//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21//  GNU General Public License for more details.                             //
22//                                                                           //
23//  You should have received a copy of the GNU General Public License        //
24//  along with this program; if not, write to the Free Software              //
25//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26//  ------------------------------------------------------------------------ //
27// Author: Kazumi Ono (AKA onokazu)                                          //
28// URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
29// Project: The XOOPS Project                                                //
30// ------------------------------------------------------------------------- //
31
32/**
33 * Class to facilitate navigation in a multi page document/list
34 *
35 * @package     kernel
36 * @subpackage  util
37 *
38 * @author      Kazumi Ono  <[email protected]>
39 * @copyright   (c) 2000-2003 The Xoops Project - www.xoops.org
40 */
41class XoopsPageNav
42{
43    /**#@+
44     * @access  private
45     */
46    var $total;
47    var $perpage;
48    var $current;
49    var $url;
50    /**#@-*/
51
52    /**
53     * Constructor
54     *
55     * @param   int     $total_items    Total number of items
56     * @param   int     $items_perpage  Number of items per page
57     * @param   int     $current_start  First item on the current page
58     * @param   string  $start_name     Name for "start" or "offset"
59     * @param   string  $extra_arg      Additional arguments to pass in the URL
60     **/
61    function XoopsPageNav($total_items, $items_perpage, $current_start, $start_name="start", $extra_arg="")
62    {
63        $this->total = intval($total_items);
64        $this->perpage = intval($items_perpage);
65        $this->current = intval($current_start);
66        if ( $extra_arg != '' && ( substr($extra_arg, -5) != '&amp;' || substr($extra_arg, -1) != '&' ) ) {
67            $extra_arg .= '&amp;';
68        }
69        $this->url = xoops_getenv('PHP_SELF').'?'.$extra_arg.trim($start_name).'=';
70    }
71
72    /**
73     * Create text navigation
74     *
75     * @param   integer $offset
76     * @return  string
77     **/
78    function renderNav($offset = 4)
79    {
80        $ret = '';
81        if ( $this->total <= $this->perpage ) {
82            return $ret;
83        }
84        $total_pages = ceil($this->total / $this->perpage);
85        if ( $total_pages > 1 ) {
86            $prev = $this->current - $this->perpage;
87            if ( $prev >= 0 ) {
88                $ret .= '<a href="'.$this->url.$prev.'"><u>&laquo;</u></a> ';
89            }
90            $counter = 1;
91            $current_page = intval(floor(($this->current + $this->perpage) / $this->perpage));
92            while ( $counter <= $total_pages ) {
93                if ( $counter == $current_page ) {
94                    $ret .= '<b>('.$counter.')</b> ';
95                } elseif ( ($counter > $current_page-$offset && $counter < $current_page + $offset ) || $counter == 1 || $counter == $total_pages ) {
96                    if ( $counter == $total_pages && $current_page < $total_pages - $offset ) {
97                        $ret .= '... ';
98                    }
99                    $ret .= '<a href="'.$this->url.(($counter - 1) * $this->perpage).'">'.$counter.'</a> ';
100                    if ( $counter == 1 && $current_page > 1 + $offset ) {
101                        $ret .= '... ';
102                    }
103                }
104                $counter++;
105            }
106            $next = $this->current + $this->perpage;
107            if ( $this->total > $next ) {
108                $ret .= '<a href="'.$this->url.$next.'"><u>&raquo;</u></a> ';
109            }
110        }
111        return $ret;
112    }
113
114    /**
115     * Create a navigational dropdown list
116     *
117     * @param   boolean     $showbutton Show the "Go" button?
118     * @return  string
119     **/
120    function renderSelect($showbutton = false)
121    {
122        if ( $this->total < $this->perpage ) {
123            return;
124        }
125        $total_pages = ceil($this->total / $this->perpage);
126        $ret = '';
127        if ( $total_pages > 1 ) {
128            $ret = '<form name="pagenavform" action="'.xoops_getenv('PHP_SELF').'">';
129            $ret .= '<select name="pagenavselect" onchange="location=this.options[this.options.selectedIndex].value;">';
130            $counter = 1;
131            $current_page = intval(floor(($this->current + $this->perpage) / $this->perpage));
132            while ( $counter <= $total_pages ) {
133                if ( $counter == $current_page ) {
134                    $ret .= '<option value="'.$this->url.(($counter - 1) * $this->perpage).'" selected="selected">'.$counter.'</option>';
135                } else {
136                    $ret .= '<option value="'.$this->url.(($counter - 1) * $this->perpage).'">'.$counter.'</option>';
137                }
138                $counter++;
139            }
140            $ret .= '</select>';
141            if ($showbutton) {
142                $ret .= '&nbsp;<input type="submit" value="'._GO.'" />';
143            }
144            $ret .= '</form>';
145        }
146        return $ret;
147    }
148
149    /**
150     * Create navigation with images
151     *
152     * @param   integer     $offset
153     * @return  string
154     **/
155    function renderImageNav($offset = 4)
156    {
157        if ( $this->total < $this->perpage ) {
158            return;
159        }
160        $total_pages = ceil($this->total / $this->perpage);
161        $ret = '';
162        if ( $total_pages > 1 ) {
163            $ret = '<table><tr>';
164            $prev = $this->current - $this->perpage;
165            if ( $prev >= 0 ) {
166                $ret .= '<td class="pagneutral"><a href="'.$this->url.$prev.'">&lt;</a></td><td><img src="'.XOOPS_URL.'/images/blank.gif" width="6" alt="" /></td>';
167            }
168            $counter = 1;
169            $current_page = intval(floor(($this->current + $this->perpage) / $this->perpage));
170            while ( $counter <= $total_pages ) {
171                if ( $counter == $current_page ) {
172                    $ret .= '<td class="pagact"><b>'.$counter.'</b></td>';
173                } elseif ( ($counter > $current_page-$offset && $counter < $current_page + $offset ) || $counter == 1 || $counter == $total_pages ) {
174                    if ( $counter == $total_pages && $current_page < $total_pages - $offset ) {
175                        $ret .= '<td class="paginact">...</td>';
176                    }
177                    $ret .= '<td class="paginact"><a href="'.$this->url.(($counter - 1) * $this->perpage).'">'.$counter.'</a></td>';
178                    if ( $counter == 1 && $current_page > 1 + $offset ) {
179                        $ret .= '<td class="paginact">...</td>';
180                    }
181                }
182                $counter++;
183            }
184            $next = $this->current + $this->perpage;
185            if ( $this->total > $next ) {
186                $ret .= '<td><img src="'.XOOPS_URL.'/images/blank.gif" width="6" alt="" /></td><td class="pagneutral"><a href="'.$this->url.$next.'">&gt;</a></td>';
187            }
188            $ret .= '</tr></table>';
189        }
190        return $ret;
191    }
192}
193
194?>
Note: See TracBrowser for help on using the repository browser.