![]() |
|
Snippets |
|
This is helper is mostly inspired by Pagination navigation helper, but there are some differences:
function pagination($pager) { $uri = sfRouting :: getInstance()->getCurrentInternalUri(); $html = ''; if ($pager->haveToPaginate()) { $uri .= strstr($uri, '?') ? '&page=' : '?page='; if ($pager->getPage() != 1) { $html .= '<li>' . link_to('first', $uri . '1') . '</li>'; $html .= '<li>' . link_to('previous', $uri . $pager->getPreviousPage()) . '</li>'; } foreach ($pager->getLinks() as $page) { if ($page == $pager->getPage()) $html .= '<li><strong>' . link_to($page, $uri . $page) . '</strong></li>'; else $html .= '<li>' . link_to($page, $uri . $page) . '</li>'; } if ($pager->getPage() != $pager->getLastPage()) { $html .= '<li>' . link_to('next', $uri . $pager->getNextPage()) . '</li>'; $html .= '<li>' . link_to('last', $uri . $pager->getLastPage()) . '</li>'; } $html = '<ul class="pagination">' . $html . '</ul>'; } return $html; }
ul.pagination li { display: inline; list-style-type: none; padding-right: 1em; }
<?php echo use_helper('Pagination') ?> <?php echo pagination($pager) ?>
Comments on this snippet
Maybe you can make a better CSS to display your pagination but it is nice
I think you could use the link_to_if() helper to display either a link to a page or the sole name of the page without link if it's the current one.
yes francois, i could change the lines (in the foreach loop) by this one
$html .= '<li>' . link_to_unless ($page == $pager->getPage(), $page, $uri . $page) . '</li>';
but we loose the emphasize (done with strong) of the current page...
I found a bug, a stupid bug. The "getCurrentInternalUri()" method won't give a correct route name, if you use action forwarding. I'm using it, because I want to fake modules beeing part of another module.
I use it for a nice navigation - I have the area "account management" where I have the modules groups, roles, actions, apps, modules, users - but I want to know if this module is part of the users-array via $sf_first_module, so I wrote a wrapping action executeForward() in the user module, and the routes contain values like forward_module / forward_ation, so the executeForward action just executes this route forwarding routes.
But if I am on a forwarded page, e.g. I'm listening the roles in the database, and I must paginate them on the end of the list, getCurrentInternalUri() won't give me the forwarded route (user_roles_list), instead I get "forward?page=". That's right, the last action/route was the forwarding, but now I can't paginate.
So, instead, try to use the method gettCurrentRouteName() and add a simple '@' in front of it.
<code>$uri = '@'.sfRouting::getInstance()->getCurrentRouteName();</code>
This simply works.
And I found another problem (omg, it's bug-clean-day!). The route must contain only the page-var, no others, otherwise they won't be put in. So, I added a simple mechanism to add custom route values.
<code>function pagination($pager, $params = array()) { $uri = '@'.sfRouting::getInstance()->getCurrentRouteName(); if (is_array($params) and ($count = count($params)) !== false and $count > 0) { $counter = 0; foreach ($params as $key => $value) { $uri .= (strstr($uri, '?') ? '&' : '?') . $key . '=' . $value; } } ... </code>
That's it. Now you can use it like:
<code><?php echo pagination($users, array('user_role_name' => $list_user_role->getName())) ?></code>