.

WidgetPagerCalculator Class Reference
[View]

This is a helper class to calculate this and that required for rendering a pager. More...

Inheritance diagram for WidgetPagerCalculator:
IPolicyHolder

List of all members.

Public Member Functions

  get_current_page ()
  Returns current page (one-based).
  get_data_item ($key, $default)
  Returns item from data array.
  get_next_link ($policy=WidgetPager::NONE, $cls= '')
  Returns next link.
  get_page_link ($page, $text, $cls= '', $policy=self::NONE)
  Returns link for page $page, with text $text, unless $page is current page in which case it returns either nothing or a span containing $text, depending on $policy.
  get_page_links_array ($total=0, $policy=WidgetPager::NONE, $gap= '...')
  Returns array of page links.
  get_page_url ($page)
  Returns URL for page $page.
  get_page_x_of_n ($policy=WidgetPager::NONE, $cls= '')
  Returns snippet "page x of n".
  get_policy ()
  Get policy.
  get_previous_link ($policy=WidgetPager::NONE, $cls= '')
  Returns previous link.
  get_total_pages ()
  has_policy ($policy)
  Returns true, if client has given policy.
  set_data ($data)
  Set pager data.
  set_policy ($policy)
  Set Policy.

Protected Member Functions

  create_page_array_item ($text)
  Returns page array item.
  create_page_link_array_item ($page)
  get_next_prev_link ($link, $text, $cls, $policy)
  Returns a next or prev link, depending on policy.
  strip_pages ($current_page, $total_pages, $num_extract, $policy)
  Shortens pages to - say - 10 , laeving gaps.

Protected Attributes

  $adapter
  $data
  $policy

Detailed Description

This is a helper class to calculate this and that required for rendering a pager.

Author:
Gerd Riesselmann

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


Member Function Documentation

WidgetPagerCalculator::create_page_array_item ( text  )  [protected]

Returns page array item.

Parameters:
string  $text
Returns:
array

Definition at line 286 of file pager.widget.php.

00286                                                          {
00287                 return array(
00288                         'page' => $text,
00289                         'url' => ''
00290                 );
00291         }
WidgetPagerCalculator::create_page_link_array_item ( page  )  [protected]

Definition at line 294 of file pager.widget.php.

00294                                                               {
00295                 return array(
00296                         'page' => $page,
00297                         'url' => $this->get_page_url($page)
00298                 );
00299         }
WidgetPagerCalculator::get_current_page (  ) 

Returns current page (one-based).

Returns:
int

Definition at line 70 of file pager.widget.php.

00070                                            {
00071                 return $this->data['page'];
00072         }
WidgetPagerCalculator::get_data_item ( key,
default  
)

Returns item from data array.

Parameters:
string  $key
mixed  $default
Returns:
mixed

Definition at line 23 of file pager.widget.php.

00023                                                       {
00024                 return Arr::get_item($this->data, $key, $default);
00025         }
WidgetPagerCalculator::get_next_link ( policy = WidgetPager::NONE,
cls = ''  
)

Returns next link.

Parameters:
int  $policy
string  $cls CSS class
Returns:
string

Definition at line 102 of file pager.widget.php.

00102                                                                             {
00103                 $text = tr('Next Page >', array('app', 'core'));
00104                 $link = Arr::get_item($this->data, 'next_link', '');
00105                 return $this->get_next_prev_link($link, $text, $cls, $policy);
00106         }
WidgetPagerCalculator::get_next_prev_link ( link,
text,
cls,
policy  
) [protected]

Returns a next or prev link, depending on policy.

Parameters:
string  $link
string  $text
int  $policy
Returns:
string

Definition at line 193 of file pager.widget.php.

00193                                                                            {
00194                 $policy |= $this->policy;
00195                 $ret = '';
00196                 if ($link !== '') {
00197                         $ret .= html::a($text, $link, '', array('class' => $cls));
00198                 }
00199                 else if (!Common::flag_is_set($policy, WidgetPager::HIDE_INACTIVE_LINKS)) {
00200                         $ret .= html::span($text, $cls);
00201                 }               
00202                 return $ret;
00203         }
WidgetPagerCalculator::get_page_link ( page,
text,
cls = '',
policy = self::NONE  
)

Returns link for page $page, with text $text, unless $page is current page in which case it returns either nothing or a span containing $text, depending on $policy.

Parameters:
int  $page Page, one-based
string  $text Link text
string  $cls CSS class
int  $policy Policy

Definition at line 173 of file pager.widget.php.

00173                                                                                    {
00174                 $policy |= $this->policy;
00175                 $ret = '';
00176                 if ($page != $this->get_current_page()) {
00177                         $ret .= html::a($text, $this->get_page_url($page), '', array('class' => $cls));
00178                 }
00179                 else if (!Common::flag_is_set($policy, WidgetPager::HIDE_INACTIVE_LINKS)) {
00180                         $ret .= html::span($text, $cls);
00181                 }               
00182                 return $ret;                            
00183         }
WidgetPagerCalculator::get_page_links_array ( total = 0,
policy = WidgetPager::NONE,
gap = '...'  
)

Returns array of page links.

Parameters:
int  $total Total number of navigation links
int  $policy
string  $gap Text to use for marking gaps in navigation
Returns:
array

Definition at line 116 of file pager.widget.php.

00116                                                                                                   {
00117                 // Build HTML
00118                 $pages = $this->strip_pages($this->data['page'], $this->data['pages_total'], $total, $policy);
00119                 $ret = array();
00120                 foreach ($pages as $page) {
00121                         $text = String::escape($page['page']);
00122                         if ($page['url']) {
00123                                 $ret[] = html::a($text, $page['url'], tr('Show page %page', array('app', 'core'), array('%page' => $text)));
00124                         } else {
00125                                 if ($text === '') { 
00126                                         $ret[] = $gap; 
00127                                 }
00128                                 else {
00129                                         $ret[] = html::span($text, 'currentpage');
00130                                 }
00131                         }
00132                 }                       
00133                 return $ret;            
00134         }
WidgetPagerCalculator::get_page_url ( page  ) 

Returns URL for page $page.

Definition at line 155 of file pager.widget.php.

00155                                             {
00156                 if ($page != $this->get_data_item('page', 0)) {
00157                         return $this->adapter->get_url_for_page($page);
00158                 } else {
00159                         return '';
00160                 }
00161         }
WidgetPagerCalculator::get_page_x_of_n ( policy = WidgetPager::NONE,
cls = ''  
)

Returns snippet "page x of n".

Parameters:
int  $policy
string  $cls CSS class
Returns:
string

Definition at line 143 of file pager.widget.php.

00143                                                                               {
00144                 $policy |= $this->policy;
00145                 $ret = '';
00146                 if (!Common::flag_is_set($policy, WidgetPager::NO_PAGE_X_OF_N)) {
00147                         $ret = tr('Page %page of %total', array('app', 'core'), array('%page' => $this->data['page'], '%total' => $this->data['pages_total']));
00148                 }
00149                 return $ret;            
00150         }
WidgetPagerCalculator::get_policy (  ) 

Get policy.

Returns:
int

Implements IPolicyHolder.

Definition at line 43 of file pager.widget.php.

00043                                      {
00044                 return $this->policy;
00045         }
WidgetPagerCalculator::get_previous_link ( policy = WidgetPager::NONE,
cls = ''  
)

Returns previous link.

Parameters:
int  $policy
string  $cls CSS class
Returns:
string

Definition at line 89 of file pager.widget.php.

00089                                                                                 {
00090                 $text = tr('< Previous Page', array('app', 'core'));
00091                 $link = Arr::get_item($this->data, 'previous_link', '');
00092                 return $this->get_next_prev_link($link, $text, $cls, $policy);
00093         }
WidgetPagerCalculator::get_total_pages (  ) 

Definition at line 78 of file pager.widget.php.

00078                                           {
00079                 return $this->data['pages_total'];
00080         }
WidgetPagerCalculator::has_policy ( policy  ) 

Returns true, if client has given policy.

Parameters:
int  $policy
Returns:
bool

Implements IPolicyHolder.

Definition at line 62 of file pager.widget.php.

00062                                             {
00063                 return Common::flag_is_set($this->policy, $policy);     
00064         }
WidgetPagerCalculator::set_data ( data  ) 

Set pager data.

Parameters:
array  $data
Returns:
void

Definition at line 33 of file pager.widget.php.

00033                                         {
00034                 $this->data = $data;
00035                 $this->adapter = $this->get_data_item('adapter', new PagerDefaultAdapter($data['page_data']));
00036         }
WidgetPagerCalculator::set_policy ( policy  ) 

Set Policy.

Parameters:
int  $policy

Implements IPolicyHolder.

Definition at line 52 of file pager.widget.php.

00052                                             {
00053                 $this->policy = $policy;
00054         }
WidgetPagerCalculator::strip_pages ( current_page,
total_pages,
num_extract,
policy  
) [protected]

Shortens pages to - say - 10 , laeving gaps.

Parameters:
array  $pages_in
int  $current_page
int  $num_extract,
int  $policy
Returns:
array

Definition at line 215 of file pager.widget.php.

00215                                                                                            {
00216                 $gap = $this->create_page_array_item('');
00217                 $policy |= $this->policy;
00218                 $ret = array();
00219                 // $current_page is one based, while array $pages_in is 0-based!
00220                 $current_page_index = $current_page - 1;
00221                 $total_pages_index = $total_pages - 1;
00222                 
00223                 $num_extract -= 1; // One is for current page!
00224                 // Using ceil and floor leads to for example to the following setting
00225                 // Given 10 links and current is 15: Links are 5 to 14 and 16 to 24
00226                 // (Thats how google does it)
00227                 $first_page_index = $current_page_index - ceil($num_extract / 2.0);
00228                 $last_page_index = $current_page_index + floor($num_extract / 2.0);
00229                 
00230                 // Force total number of links, if wanted
00231                 if ( !Common::flag_is_set($policy, WidgetPager::NO_FORCE_TOTAL_LINKS)) {
00232                         if ($first_page_index < 0) {
00233                                 $last_page_index -= $first_page_index; // $first_page_index is negative!
00234                                 $first_page_index = 0;
00235                         }
00236                         if ($last_page_index > $total_pages_index) {
00237                                 $first_page_index -= ($last_page_index - $total_pages_index);
00238                                 $last_page_index = $total_pages_index;
00239                         }
00240                 }
00241                 // Cut of if too large/small
00242                 if ($first_page_index < 0) { $first_page_index = 0; }
00243                 if ($last_page_index > $total_pages_index) { $last_page_index = $total_pages_index; }
00244                 
00245                 $strip = !Common::flag_is_set($policy, WidgetPager::NO_STRIP);
00246                 if ($strip) {
00247                         // Add leading link to page 1 and evetually some dots to mark the gap
00248                         if ($first_page_index > 0) {
00249                                 $ret[] = $this->create_page_link_array_item(1);
00250                         }
00251                         if ($first_page_index > 1) {
00252                                 $ret[] = $gap;
00253                         }               
00254                 }
00255 
00256                 // Pages before current
00257                 for ($i = $first_page_index; $i < $current_page_index; $i++) {
00258                         $ret[] = $this->create_page_link_array_item($i + 1);
00259                 }
00260                 // Current
00261                 $ret[] = $this->create_page_array_item($current_page);
00262                 // Pages after current
00263                 for ($i = $current_page_index + 1; $i <= $last_page_index; $i++) {
00264                         $ret[] = $this->create_page_link_array_item($i + 1);
00265                 }
00266                 
00267                 if ($strip) {
00268                         // Add link to last page and evetually some dots to mark the gap
00269                         if ($last_page_index < $total_pages_index - 1) {
00270                                 $ret[] = $gap;
00271                         }
00272                         if ($last_page_index < $total_pages_index ) {
00273                                 $ret[] = $this->create_page_link_array_item($total_pages_index + 1);
00274                         }               
00275                 }
00276 
00277                 return $ret;
00278         }       

Member Data Documentation

WidgetPagerCalculator::$adapter [protected]

Definition at line 14 of file pager.widget.php.

WidgetPagerCalculator::$data [protected]

Definition at line 9 of file pager.widget.php.

WidgetPagerCalculator::$policy [protected]

Definition at line 10 of file pager.widget.php.


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