Quelltext der Datei: http://www.michaelster.ch/lernen/class_blaettern_oop.php

Dateigrösse: 5.39 kb

[Anzeige mit Zeilennummern]


<?php

/**************
* PageSlider  *
**************/

class PageSlider
{
    /**
    * Attributes
    */
    private $mPerPage   = 10;         // int: elements/entries per page                                
    private $mPageLabel    = 'start';   // string: key for aktuell page                                
       private $mGetParams = array();     // mix: array for get-Params                                    
    private $mActPage   = 1;          // int: actual page                                            
    private $mLastPage  = 1;         // int: number of last page                                    
    private $mNextPage  = 3;         // int: number of direct links forwards/backwards from ActPage    
    private $mArrTmpl   = array('<<',                                                                
                                '< ',                                                                
                                '##',                                                                
                                ' >',                                                                
                                '>>'  );     // template for links: symbols, icons, gifs...         
                                            // ## = placeholder for current page                     
    private $mBlindGif  = '<img src="img/img_paginate/blind.gif" alt="" border="0" width="22" height="1" />';
    
    public function __construct($i_perpage, $i_lastpage, $i_nextpage, $s_pagelabel, $a_params4link, $a_tmpl, $s_css, $s_csshover)
    {
        $this->mPerPage       = $i_perpage;                            
        $this->mPageLabel     = $s_pagelabel;                            
        $this->mGetParams     = $a_params4link;                        
        $this->mActPage       = $this->mGetParams[$this->mPageLabel];    
        $this->mSuchbegriff = $this->mGetParams['suchbegriff'];        
        $this->mLastPageFull= $i_lastpage;                            
        $this->mLastPage      = floor($i_lastpage/$this->mPerPage);    
        $this->mNextPage      = $i_nextpage;                            
        $this->mArrTmpl       = $a_tmpl;                                
        $this->mCssStyle      = $s_css;                                
        $this->mCssActive     = $s_csshover;                            
        
        if (strpos ($this->mArrTmpl[2], "##") !== FALSE)
        {
            $this->mArrTmpl[2] = str_replace("##", $this->mActPage+1, $this->mArrTmpl[2]);
        }
    }

    public function toHtml()
    {
        $first = ''; $previous = ''; $next = ''; $last = ''; $backward = ''; $forward = '';
        
        if ($this->mActPage > 0)
        {
               for ($i=$this->mNextPage; $i>0; $i--)
               {
                if ($this->mActPage-$i >= 1)
                {
                    $backward .= $this->_aHtml( ($this->mActPage - $i), 
                                                  'zur Seite ' . ($this->mActPage - $i), 
                                                  ($this->mActPage - $i) );
                }
            }
            
            $first     = $this->_aHtml(   0, 
                                          'zur ersten Seite', 
                                          $this->mArrTmpl[0] );
            $previous  = $this->_aHtml(   $this->mActPage-1, 
                                          'zur vorangehenden Seite', 
                                          $this->mArrTmpl[1] );
           }
        else
        {
            $first       = '<span style="'.$this->mCssStyle.'">'.$this->mArrTmpl[5].'</span>';
            $previous = '<span style="'.$this->mCssStyle.'">'.$this->mArrTmpl[6].'</span>';
        }
        
        // If next, i.e. last page, is empty, hide all arrow symbols [ example: (5*4 + 4) / 20 == 1 ]
           // ueberfluessig: if ($this->mActPage < $this->mLastPage)
           if (($this->mActPage*$this->mPerPage + $this->mPerPage) / $this->mLastPageFull < 1)
           {
            for ($i=1; $i<=$this->mNextPage; $i++)
               {
                   if ($this->mActPage+$i <= $this->mLastPage)
                   {
                    $forward .= $this->_aHtml(     ($this->mActPage + $i), 
                                                 'zur Seite ' . ($this->mActPage + $i), 
                                                 ($this->mActPage + $i) );
                }
            }
            
               $next      = $this->_aHtml( $this->mActPage+1, 
                                        'zur n&auml;chsten Seite', 
                                     $this->mArrTmpl[3] );
            
               if($this->mLastPageFull % $this->mPerPage == 0)
            {
              $last  = $this->_aHtml( $this->mLastPage-1, 
                                      'zur letzten Seite', 
                                      $this->mArrTmpl[4] );
            } else {
              $last  = $this->_aHtml( $this->mLastPage, 
                                      'zur letzten Seite', 
                                      $this->mArrTmpl[4] );
            }
        }
        else
        {
            $next = '<span style="'.$this->mCssStyle.'">'.$this->mArrTmpl[7].'</span>';
            $last = '<span style="'.$this->mCssStyle.'">'.$this->mArrTmpl[8].'</span>';
        }
        
        if ($this->mLastPageFull <= $this->mPerPage)
        {
            $slider = '';
        } else {
            $slider .=  $first;
            $slider .=  $previous;
            $slider .=  $backward;
            $slider .=  '<span style="'.$this->mCssActive.'">'.$this->mArrTmpl[2].'</span>';
            $slider .=  $forward;
            $slider .=  $next;
            $slider .=  $last;
        }
        
        return $slider;
    }

    private function _aHtml($actPage, $title, $link, $class='default')
    {
           $tmpA     = '';
        $tmpArr = array();
        $this->mGetParams[$this->mPageLabel] = $actPage*$this->mPerPage;
        foreach ($this->mGetParams as $k => $v)
        {
            $tmpArr[] = $k .'='. $v;
        }
           $href  = $_SERVER['PHP_SELF'] . '?' . implode('&amp;', $tmpArr);
        /***** implode transforms array-elements into a string ****/
           $tmpA .= "\n" . '<a href="' . $href . '" title="' . $title . '" style="' . $this->mCssStyle . '">';
           $tmpA .= $link;
           $tmpA .= "</a>" . "\n";
           return $tmpA;
    }
}

?>