ManiaLib 1.0b1 API Reference

Source for file MultipageList.class.php

Documentation is available at MultipageList.class.php

  1. <?php
  2. /**
  3.  * @author Maxime Raoust
  4.  * @copyright 2009-2010 NADEO
  5.  * @package ManiaLib
  6.  */
  7.  
  8. /**
  9.  * This class helps to create multipage lists. Maybe difficult to use at
  10.  * first... Doc should be written about it...
  11.  * @package ManiaLib
  12.  * @subpackage Helpers
  13.  */
  14. {
  15.     /**#@+
  16.      * @ignore
  17.      */
  18.     protected $size;
  19.     protected $urlParamName 'page';
  20.     protected $urlPageName null;
  21.     protected $currentPage;
  22.     protected $defaultPage=1;
  23.     protected $perPage;
  24.     protected $pageNumber;
  25.     protected $hasMorePages;
  26.     /**#@-*/
  27.     
  28.     /**
  29.      * @var PageNavigator 
  30.      */
  31.     public $pageNavigator;
  32.  
  33.     function __construct($perPage 8)
  34.     {
  35.         $this->pageNavigator = new PageNavigator;
  36.         $this->perPage $perPage;
  37.     }
  38.  
  39.     function setSize($size)
  40.     {
  41.         $this->size $size;
  42.         if($this->getCurrentPage($this->getPageNumber())
  43.             $this->currentPage $this->getPageNumber();
  44.     }
  45.     
  46.     function setPerPage($perPage)
  47.     {
  48.         $this->perPage $perPage;
  49.     }
  50.     
  51.     function setCurrentPage($page)
  52.     {
  53.         $this->currentPage $page;
  54.     }
  55.     
  56.     function setDefaultPage($page)
  57.     {
  58.         $this->defaultPage $page;
  59.     }
  60.     
  61.     function setUrlParamName($name)
  62.     {
  63.         $this->urlParamName $name;
  64.     }
  65.     
  66.     function setUrlPageName($file)
  67.     {
  68.         $this->urlPageName $file;
  69.     }
  70.     
  71.     function getCurrentPage()
  72.     {
  73.         if($this->currentPage === null)
  74.         {
  75.             $request call_user_func(array(APP_FRAMEWORK_REQUEST_ENGINE_CLASS'getInstance'));
  76.             $this->currentPage = (int) $request->get($this->urlParamName$this->defaultPage);
  77.         }
  78.         if$this->currentPage 1)
  79.         {
  80.             $this->currentPage 1;
  81.         }
  82.         return $this->currentPage;
  83.         
  84.     }
  85.         
  86.     function getPageNumber()
  87.     {
  88.         if(!$this->pageNumber && $this->perPage)
  89.         {
  90.             $this->pageNumber ceil($this->size/$this->perPage);
  91.         }
  92.         return $this->pageNumber;
  93.     }
  94.     
  95.     /**
  96.      * @return array[int] offset, length
  97.      */
  98.     function getLimit()
  99.     {
  100.         $offset ($this->getCurrentPage()-1)*$this->perPage;
  101.         $length $this->perPage;
  102.         return array($offset$length)
  103.     }
  104.     
  105.     function setHasMorePages($hasMorePages)
  106.     {
  107.         $this->hasMorePages $hasMorePages;
  108.     }
  109.     
  110.     function checkArrayForMorePages(&$array)
  111.     {
  112.         list($offset$length$this->getLimit();
  113.         $hasMorePages (count($array== $length 1);
  114.         if($hasMorePages)
  115.         {
  116.             array_pop($array);
  117.         
  118.         $this->hasMorePages $hasMorePages;    
  119.     }
  120.     
  121.      function addPlayerId()
  122.      {
  123.         $this->pageNavigator->arrowNext->addPlayerId();
  124.         $this->pageNavigator->arrowPrev->addPlayerId();
  125.         $this->pageNavigator->arrowFastNext->addPlayerId();
  126.         $this->pageNavigator->arrowFastPrev->addPlayerId();
  127.         $this->pageNavigator->arrowLast->addPlayerId();
  128.         $this->pageNavigator->arrowFirst->addPlayerId();
  129.      }
  130.     
  131.     function savePageNavigator()
  132.     {
  133.         $request call_user_func(array(APP_FRAMEWORK_REQUEST_ENGINE_CLASS'getInstance'));
  134.         
  135.         if($this->hasMorePages !== null)
  136.         {
  137.             if($this->hasMorePages)
  138.             {
  139.                 $this->setSize($this->getCurrentPage()*$this->perPage 1);
  140.             }
  141.             else
  142.             {
  143.                 $this->setSize($this->getCurrentPage()*$this->perPage);
  144.             }
  145.         }
  146.         
  147.         if($this->getPageNumber(1)
  148.         {
  149.             $ui $this->pageNavigator;        
  150.             $ui->setPageNumber($this->getPageNumber());
  151.             $ui->setCurrentPage($this->getCurrentPage());
  152.  
  153.             if($ui->isLastShown())
  154.             {
  155.                 $request->set($this->urlParamName1);
  156.                 $ui->arrowFirst->setManialink($request->createLink($this->urlPageName));
  157.                 
  158.                 $request->set($this->urlParamName$this->getPageNumber());
  159.                 $ui->arrowLast->setManialink($request->createLink($this->urlPageName));
  160.             }
  161.             
  162.             if($ui->isFastNextShown())
  163.             {
  164.                 $request->set($this->urlParamName$this->currentPage+5);
  165.                 $ui->arrowFastNext->setManialink($request->createLink($this->urlPageName));
  166.                 
  167.                 $request->set($this->urlParamName$this->currentPage-5);
  168.                 $ui->arrowFastPrev->setManialink($request->createLink($this->urlPageName));
  169.             }
  170.                 
  171.             
  172.             if($this->currentPage $this->pageNumber)
  173.             {
  174.                 $request->set($this->urlParamName$this->currentPage+1);
  175.                 $ui->arrowNext->setManialink($request->createLink($this->urlPageName));
  176.             }
  177.             
  178.             if($this->currentPage 1)
  179.             {
  180.                 $request->set($this->urlParamName$this->currentPage-1);
  181.                 $ui->arrowPrev->setManialink($request->createLink($this->urlPageName));
  182.             }
  183.             
  184.             $request->set($this->urlParamName$this->currentPage);
  185.             
  186.             $ui->save();
  187.         }
  188.     }
  189. }
  190.  
  191.  
  192. ?>