Update version.php to 3.3.0
[isso.git] / Pagination.php
index a2325f76fd6d4b47313b4b76798054b41e20c84c..e66af2370cd0b8caa1f841a0abf6262970185b85 100644 (file)
@@ -2,7 +2,7 @@
 /*=====================================================================*\
 || ###################################################################
 || # Blue Static ISSO Framework
-|| # Copyright (c)2005-2008 Blue Static
+|| # Copyright (c)2005-2009 Blue Static
 || #
 || # This program is free software; you can redistribute it and/or modify
 || # it under the terms of the GNU General Public License as published by
 \*=====================================================================*/
 
 /**
-* Pagination System (Pagination.php)
-*
-* @package     ISSO
-*/
+ * Pagination System (Pagination.php)
+ *
+ * @package    ISSO
+ */
 
 /**
-* Pagination System
-*
-* On many pages, it is necessary to limit the amount of records to display.
-* Using this class, you can set the maximum and minimum values to display,
-* and then the input variables for page number and perpage. This will
-* then create a page navigator and manage the SQL LIMIT statements.
-*
-* @author              Blue Static
-* @copyright   Copyright (c)2005 - 2008, Blue Static
-* @package             ISSO
-* 
-*/
-class BSPagination
+ * Pagination System
+ *
+ * On many pages, it is necessary to limit the amount of records to display.
+ * Using this class, you can set the maximum and minimum values to display,
+ * and then the input variables for page number and perpage. This will
+ * then create a page navigator and manage the SQL LIMIT statements.
+ *
+ * @author             Blue Static
+ * @copyright  Copyright (c)2005 - 2009, Blue Static
+ * @package            ISSO
+ 
+ */
+abstract class BSPagination
 {
        /**
-       * Current page number
-       * @var  integer
-       */
-       private $page;
+        * Current page number
+        * @var integer
+        */
+       public $page;
        
        /**
-       * Per-page value
-       * @var  integer
-       */
-       private $perpage;
+        * Per-page value
+        * @var integer
+        */
+       public $perpage;
        
        /**
-       * Number of page links
-       * @var  integer
-       */
-       private $pagelinks;
+        * Number of page links
+        * @var integer
+        */
+       protected $pagelinks;
        
        /**
-       * Total number of results
-       * @var  integer
-       */
-       private $total;
+        * Total number of results
+        * @var integer
+        */
+       protected $total;
        
        /**
-       * Total number of pages
-       * @var  integer
-       */
-       private $pagecount;
+        * Total number of pages
+        * @var integer
+        */
+       protected $pagecount;
        
        /**
-       * Name of page variable
-       * @var  array
-       */
-       private $pagevar = 'p';
+        * Maximum number of per-page results
+        * @var integer
+        */
+       protected $maxperpage = 100;
        
        /**
-       * Name of per-page variable
-       * @var  integer
-       */
-       private $perpagevar = 'pp';
+        * Default number of per-page results
+        * @var integer
+        */
+       protected $defaultperpage = 20;
        
        /**
-       * Maximum number of per-page results
-       * @var  integer
-       */
-       private $maxperpage = 100;
+        * Callback public function for the processing of an indivdual page link
+        * 
+        * @param       string  The base link
+        * @param       boolean Wether or not the item is the current page
+        * @param       integer Page number
+        * 
+        * @return      string  Compiled HTML
+        */
+       protected abstract function _bitProcessor($baselink, $isCurrent, $pagenumber);
        
        /**
-       * Default number of per-page results
-       * @var  integer
-       */
-       private $defaultperpage = 20;
+        * Callback public function for the processing the entire page navigator
+        * 
+        * @param       string  The base link
+        * @param       integer Next page number
+        * @param       integer Previous page number
+        * @param       array   Show options: array('first' => (boolean)first page link, 'last' => boolean, 'prev' => boolean, 'next' => boolean)
+        * @param       string  Bits to process
+        *
+        * @return      string  Compiled HTML
+        */
+       protected abstract function _navigationProcessor($baselink, $next, $prev, $show, $bits);
        
        /**
-       * The processing callback public function for individual pagenav bits
-       * @var string
-       */
-       private $bitprocessor = ':undefined:';
+        * Set the Pagination->perpage and Pagination->page variables
+        */
+       protected abstract function _setVariables();
        
        /**
-       * The processing callback public function for the entire pagenav system
-       * @var string
-       */
-       private $pagenavprocessor = ':undefined:';
-       
-       // ###################################################################
-       /**
-       * Callback public function for the processing of an indivdual page. Needs
-       * the signature:
-       * public string callback(string $baseLink, boolean $noLink, integer $pageNumber, Pagination $this)
-       *
-       * @param        string  Callback function
-       */
-       public function setBitProcessor($callback)
+        * Constructor
+        */
+       public function __construct()
        {
-               $this->bitprocessor = $callback;
-       }
-       
-       // ###################################################################
-       /**
-       * Callback public function for the processing the entire page navigator. Needs
-       * the signature:
-       * public string callback(string $baseLink, integer $nextPage, integer $prevPage array $show['first'=>first page, 'last'=>last page, 'prev'=>previous page, 'next'=>next page], string $bits, Pagination $this)
-       *
-       * @param        string  Callback function
-       */
-       public function setNavigatorProcessor($callback)
-       {
-               $this->pagenavprocessor = $callback;
+               if (!BSApp::$input instanceof BSInput)
+               {
+                       throw new Exception('BSApp::$input is not an instance of BSInput');
+               }
        }
        
-       // ###################################################################
        /**
-       * Returns the current page number
-       *
-       * @return       integer Current page
-       */
+        * Returns the current page number
+        *
+        * @return      integer Current page
+        */
        public function getPage()
        {
                return $this->page;
        }
        
-       // ###################################################################
        /**
-       * Returns the current perpage value
-       *
-       * @return       integer Current perpage
-       */
+        * Returns the current perpage value
+        *
+        * @return      integer Current perpage
+        */
        public function getPerPage()
        {
                return $this->perpage;
        }
        
-       // ###################################################################
        /**
-       * Sets total
-       *
-       * @param        integer Total number
-       */
+        * Sets total
+        *
+        * @param       integer Total number
+        */
        public function setTotal($total)
        {
                $this->total = $total;
        }
        
-       // ###################################################################
        /**
-       * Returns the number of pages to be in the navigator
-       *
-       * @param        integer Number of pages
-       */
+        * Returns the number of pages to be in the navigator
+        *
+        * @param       integer Number of pages
+        */
        public function getPageCount()
        {
                return $this->pagecount;
        }
-       
-       // ###################################################################
-       /**
-       * Sets pagelinks
-       *
-       * @param        integer Number of page links
-       */
-       public function setPageLinks($pagelinks)
-       {
-               $this->pagelinks = $pagelinks;
-       }
-       
-       // ###################################################################
-       /**
-       * Sets pagevar
-       *
-       * @param        string  Page variable
-       */
-       public function setPageVar($pagevar)
-       {
-               $this->pagevar = $pagevar;
-       }
-       
-       // ###################################################################
-       /**
-       * Sets perpagevar
-       *
-       * @param        string  Per-page variable
-       */
-       public function setPerPageVar($perpagevar)
-       {
-               $this->perpagevar = $perpagevar;
-       }
-       
-       // ###################################################################
-       /**
-       * Sets maxperpage
-       *
-       * @param        integer Maximum number per page
-       */
-       public function setMaxPerPage($maxperpage)
-       {
-               $this->maxperpage = $maxperpage;
-       }
-       
-       // ###################################################################
-       /**
-       * Sets defaultperpage
-       *
-       * @param        integer Total number
-       */
-       public function setDefaultPerPage($defaultperpage)
-       {
-               $this->defaultperpage = $defaultperpage;
-       }
-       
-       // ###################################################################
+               
        /**
-       * Takes all of the information from the set() functions and then
-       * prepares all of the data through verification
-       */
+        * Takes all of the information from the set() functions and then
+        * prepares all of the data through verification
+        */
        public function processIncomingData()
        {
-               $input = BSApp::GetType('Input');
-               if ($input == null)
-               {
-                       BSApp::debug(ISSO . '/Input not loaded, so manually doing so');
-                       $input = BSApp::load_module('Input');
-               }
-               
-               $this->page = $input->inputClean($this->pagevar, TYPE_INT);
-               $this->perpage = $input->inputClean($this->perpagevar, TYPE_INT);
-               $this->pagelinks = $input->clean($this->pagelinks, TYPE_INT);
+               $this->_setVariables();
+               $this->page = BSApp::$input->clean($this->page, TYPE_INT);
+               $this->perpage = BSApp::$input->clean($this->perpage, TYPE_INT);
+               $this->pagelinks = BSApp::$input->clean($this->pagelinks, TYPE_INT);
 
                if ($this->page <= 0)
                {
@@ -263,13 +187,12 @@ class BSPagination
                        $this->perpage = $this->maxperpage;
                }
                
-               $this->perpage = $input->clean($this->perpage, TYPE_INT);
+               $this->perpage = BSApp::$input->clean($this->perpage, TYPE_INT);
        }
        
-       // ###################################################################
        /**
-       * Takes the variables and splits up the pages
-       */
+        * Takes the variables and splits up the pages
+        */
        public function splitPages()
        {
                $this->pagecount = ceil($this->total / $this->perpage);
@@ -279,14 +202,13 @@ class BSPagination
                }
        }
        
-       // ###################################################################
        /**
-       * Returns the lower limit of the pages
-       *
-       * @param        integer Page number
-       *
-       * @return       integer Lower result limit
-       */
+        * Returns the lower limit of the pages
+        *
+        * @param       integer Page number
+        *
+        * @return      integer Lower result limit
+        */
        public function fetchLimit($page = null)
        {
                if ($page === null)
@@ -321,19 +243,16 @@ class BSPagination
                }
        }
        
-       // ###################################################################
        /**
-       * Constructs the page navigator
-       *
-       * @param        string  Base link path
-       * @param        bool    Add a ? or a & to the path so it's link-friendly
-       *
-       * @return       string  Generated HTML page navigator
-       */
+        * Constructs the page navigator
+        *
+        * @param       string  Base link path
+        * @param       bool    Add a ? or a & to the path so it's link-friendly
+        *
+        * @return      string  Generated HTML page navigator
+        */
        public function constructPageNav($baselink, $addParam = true)
        {
-               global $bugsys;
-               
                // handle base link
                if ($addParam)
                {
@@ -341,7 +260,7 @@ class BSPagination
                        {
                                $baselink .= '?';
                        }
-                       else if (!preg_match('#\?$#', $baselink) AND !preg_match('#(&|&amp;)$#', $baselink))
+                       else if (!strpos($baselink, '#\?$#') && !strpos($baselink, '&'))
                        {
                                $baselink .= '&amp;';
                        }
@@ -405,7 +324,6 @@ class BSPagination
                
                // construct the page bits
                $bits = '';
-               $call = $this->bitprocessor;
                for ($i = $startpage; $i <= $endpage; $i++)
                {
                        if ($i == $this->page)
@@ -417,11 +335,10 @@ class BSPagination
                                $nolink = false;
                        }
                        
-                       $bits .= $call($baselink, $nolink, $i, $this);
+                       $bits .= $this->_bitProcessor($baselink, $nolink, $i);
                }
-                               
-               $call = $this->pagenavprocessor;
-               return $call($baselink, $nextpage, $prevpage, $show, $bits, $this);
+               
+               return $this->_navigationProcessor($baselink, $nextpage, $prevpage, $show, $bits);
        }
 }