Oct 9, 2014
0
Level 3
Laravel Pagination: a new one adapted from an old Dreamweaver extension
I'm new to Laravel and I'm trying to port the most important old-procedural-code snippets and libraries I collect in some years
I missed a nice Pagination I had in an old Dreamweaver extension and I tried to replicate it: I asked on Stack Overflow but was unsatisfied, so I decided to play around and convert the Dreamweaver extensions code with a lot of patience.
Here is the result and it works very fine (I tried in Laravel 4.2 but it should work everywhere, as long as there is the included Bootstrap Paginator), but, since this is my first Laravel improvement I was wondering if this code can be improved: please, can you take a look? Thanks a lot in advance.
<?php namespace Illuminate\Pagination;
class DreamweaverPresenter extends BootstrapPresenter {
protected $TFM_LimitLinksEndCount = 5; // links' range to show
// Some useful properties, taken from the old DW extension and adapted to OOP
// This is the part that maybe can be improved: I had no other ideas to adapt those old procedural variables
// and put them in a costructor, but is there a better / more elegant way?
function __construct(Paginator $paginator){
parent::__construct($paginator);
$this->TFM_startLink = max(1, $this->currentPage - intval($this->TFM_LimitLinksEndCount/2));
$this->TFM_temp = $this->TFM_startLink + $this->TFM_LimitLinksEndCount - 1;
$this->TFM_endLink = min($this->TFM_temp, $this->lastPage);
if ($this->TFM_endLink != $this->TFM_temp){
$this->TFM_startLink = max(1, $this->TFM_endLink - $this->TFM_LimitLinksEndCount + 1);
}
}
// The following methods are simply adapted from Laravel Paginator code:
// I copied/pasted/changed some other existent methods
/**
* Render the Pagination contents.
*
* @return string
*/
public function render() // adapted from Presenter/render()
{
$content = $this->getPageRange($this->TFM_startLink, $this->TFM_endLink);
return $this->getFirst().$this->getPrevious('‹').$content.$this->getNext('›').$this->getLast();
}
/**
* Get the first page pagination element.
*
* @param string $text
* @return string
*/
public function getFirst($text = '«') // adapted from Presenter/GetFirst()
{
// If the current page is less than or equal to one, it means we can't go any
// further back in the pages, so we will render a disabled first button
// when that is the case. Otherwise, we will give it an active "status".
if ($this->currentPage <= 1)
{
return $this->getDisabledTextWrapper($text);
}
$url = $this->paginator->getUrl(1);
return $this->getPageLinkWrapper($url, $text, 'first');
}
/**
* Get the last page pagination element.
*
* @param string $text
* @return string
*/
public function getLast($text = '»') // adapted from Presenter/GetNext()
{
// If the current page is greater than or equal to the last page, it means we
// can't go any further into the pages, as we're already on this last page
// that is available, so we will make it the "last" link style disabled.
if ($this->currentPage >= $this->lastPage)
{
return $this->getDisabledTextWrapper($text);
}
$url = $this->paginator->getUrl($this->lastPage);
return $this->getPageLinkWrapper($url, $text, 'last');
}
}
Please or to participate in this conversation.