.

Pager Class Reference
[Controller]

Class to handle pagination. More...

Inheritance diagram for Pager:
IDBQueryModifier

List of all members.

Public Member Functions

  __construct ($page_data, $items_total, $items_per_page=false, $adapter=false)
  Constructor.
  apply ($query)
  Apply this filter to a query.
  get_items_per_page ()
  Returns number of items per page.
  get_start ()
  Returns startign record (0-based).
  prepare_view ($view)
  Generate pager realted data and set it on view.

Static Public Member Functions

static  apply_to_url ($url, $page)
  Prepare URL so filter gets applied.

Public Attributes

const  DO_INDEX_PAGE_2PP = 0
const  DONT_INDEX_PAGE_2PP = 1

Protected Member Functions

  get_url_for_page ($page)
  Compute url for page.

Protected Attributes

  $adapter = null
  $pager_data = array()

Detailed Description

Class to handle pagination.

Author:
Gerd Riesselmann

Definition at line 8 of file pager.cls.php.


Constructor & Destructor Documentation

Pager::__construct ( page_data,
items_total,
items_per_page = false,
adapter = false  
)

Constructor.

Parameters:
PageData  $page_data
int|ISearchAdapter  $items_total Total numbers of items or instance of ISearchadapter. In later case, count() is invoked on search adapter
int  Number of items per page
IPagerAdapter  Adapter. For compatability reasons, this is also interpreted as a policy, if you pass an integer

Definition at line 36 of file pager.cls.php.

00036                                                                                                          {
00037                 $this->adapter = ($adapter instanceof IPagerAdapter) ? $adapter : new PagerDefaultAdapter($page_data, 'page');
00038                 if ($items_per_page === false) {
00039                         $items_per_page = Config::get_value(Config::ITEMS_PER_PAGE);
00040                 }
00041                 if ($items_total instanceof ISearchAdapter) {
00042                         $items_total = $items_total->count();
00043                 }
00044                 
00045                 $this->pager_data['page_data'] = $page_data;
00046                 $this->pager_data['policy'] = (is_int($adapter)) ? $adapter : 0;
00047                 $this->pager_data['items_per_page'] = $items_per_page;
00048                 $this->pager_data['items_total'] = $items_total;
00049                 //$this->pager_data['pages'] = array();
00050                 $this->pager_data['previous_link'] = '';
00051                 $this->pager_data['next_link'] = '';
00052                 $this->pager_data['adapter'] = $this->adapter;
00053                 
00054                 // We asume an url like x/y/?page=2, where page=2 indicates page 2 of pagination
00055                 $page_raw = $this->adapter->get_current_page();
00056                 $page = Cast::int($page_raw);
00057                 if ($page <= 0) {
00058                         // Oops, weirdo page param
00059                         $page_test = intval($page_raw);
00060                         // Test for something like page=2b
00061                         if ($page_test != $page && $page_test > 0) {
00062                                 // Redirect to page
00063                                 $url = $this->adapter->get_url_for_page($page_test);
00064                                 $url->redirect(Url::PERMANENT);
00065                         } else {
00066                                 // Redirect to first page
00067                                 $url = $this->adapter->get_url_for_page(1);
00068                                 $url->redirect(Url::PERMANENT);
00069                         }
00070                         exit;                   
00071                 } elseif ($page_raw == 1) {
00072                         $url = $this->adapter->get_url_for_page(1);
00073                         if ($url->build() != Url::current()->build()) {
00074                                 $url->redirect(Url::PERMANENT);
00075                                 exit;
00076                         }
00077                 }
00078 
00079                 $page_total = 0;
00080                 if ($items_per_page > 0) {
00081                         $page_total = ceil($items_total / (float)$items_per_page);
00082                 }
00083                 if ($page_total <= 0) {
00084                         $page_total = 1;
00085                 }               
00086 
00087                 if ($page > $page_total) {
00088                         // Oops, not that much items
00089                         // Redirect to last page
00090                         $url = $this->adapter->get_url_for_page($page_total);
00091                         $url->redirect(Url::TEMPORARY);
00092                         exit;
00093                 }
00094                 
00095                 $this->pager_data['page'] = $page;
00096                 $this->pager_data['pages_total'] = $page_total;
00097                 
00098                 $this->pager_data['start_record'] = ($page - 1) * $items_per_page;
00099         } 

Member Function Documentation

Pager::apply ( query  ) 

Apply this filter to a query.

Implements IDBQueryModifier.

Definition at line 152 of file pager.cls.php.

00152                                       {
00153                 $query->limit($this->get_start(), $this->get_items_per_page());
00154         }               
static Pager::apply_to_url ( url,
page  
) [static]

Prepare URL so filter gets applied.

Parameters:
Url  Instance of URL class. This instance is changed.
string  Filter to append
Returns:
void
Deprecated:
Use PagerDefaultAdapter::apply_to_url() instead

Definition at line 166 of file pager.cls.php.

00166                                                          {
00167                 PagerDefaultAdapter::apply_to_url($url, $page);
00168         }               
Pager::get_items_per_page (  ) 

Returns number of items per page.

Definition at line 111 of file pager.cls.php.

00111                                              {
00112                 return $this->pager_data['items_per_page'];
00113         }
Pager::get_start (  ) 

Returns startign record (0-based).

Definition at line 104 of file pager.cls.php.

00104                                     {
00105                 return $this->pager_data['start_record'];
00106         }       
Pager::get_url_for_page ( page  )  [protected]

Compute url for page.

Definition at line 144 of file pager.cls.php.

00144                                                    {
00145                 $url = $this->adapter->get_url_for_page($page);
00146                 return $url->build(Url::RELATIVE);
00147         }
Pager::prepare_view ( view  ) 

Generate pager realted data and set it on view.

Definition at line 118 of file pager.cls.php.

00118                                             {
00119                 $page = $this->pager_data['page'];
00120                 $page_total = $this->pager_data['pages_total'];         
00121                 if ($page_total > 1) {
00122                         if ($page > 1) {
00123                                 $this->pager_data['first_link'] = $this->get_url_for_page(1);
00124                                 $this->pager_data['previous_link'] = $this->get_url_for_page($page - 1);
00125                         }
00126                         if ($page < $page_total) {
00127                                 $this->pager_data['next_link'] = $this->get_url_for_page($page + 1);
00128                                 $this->pager_data['last_link'] = $this->get_url_for_page($page_total);
00129                         }
00130                 
00131 //                      for($i = 1; $i <= $page_total; $i++) {
00132 //                              $this->pager_data['pages'][] = array(
00133 //                                      'page' => $i,
00134 //                                      'url' => $this->get_url_for_page($i)
00135 //                              );
00136 //                      }
00137                 }
00138                 $view->assign('pager_data', $this->pager_data);
00139         }

Member Data Documentation

Pager::$adapter = null [protected]

Definition at line 24 of file pager.cls.php.

Pager::$pager_data = array() [protected]

Definition at line 18 of file pager.cls.php.

Deprecated:
Use Widget Constants instead!

Definition at line 16 of file pager.cls.php.

Deprecated:
Use Widget Constants instead!

Definition at line 12 of file pager.cls.php.


The documentation for this class was generated from the following file: