Pagination Recall
Does it drive you nuts when you edit an item on page 4 of a list, then when you save you're dropped back to page 1 with the list back in the default order? The PaginationRecall component automatically remembers the page you were on and the sorting so you're returned to the same spot on the list.
I’ve been using the built in pagination in CakePHP 1.2-beta a lot lately and I must say it’s freaking awesome. My one oh so minor grievance is that it doesn’t "remember" where you were if you navigate off the page. For example many times I'll be paging through a sorted list of records and want to edit a particular one. After I edit and save I'm dropped back to the index with the default sorting on page 1.
Enter the PaginationRecall component. It will automatically retain your pagination settings so that you can edit/delete/surf around and return to the same spot on list.
Just include the component in your controller:
Enter the PaginationRecall component. It will automatically retain your pagination settings so that you can edit/delete/surf around and return to the same spot on list.
Just include the component in your controller:
var $components = array('PaginationRecall');
Component Class:
<?php
/*
* Pagination Recall CakePHP Component
* Copyright (c) 2008 Matt Curry
* www.PseudoCoder.com
*
* @author mattc <matt@pseudocoder.com>
* @version 1.0
* @license MIT
*
*/
class PaginationRecallComponent extends Object {
var $components = array('Session');
var $Controller = null;
function startup(&$controller) {
$this->Controller = & $controller;
$options = array_merge($this->Controller->params,
$this->Controller->params['url'],
$this->Controller->passedArgs
);
$vars = array('page', 'sort', 'direction');
$keys = array_keys($options);
$count = count($keys);
for ($i = 0; $i < $count; $i++) {
if (!in_array($keys[$i], $vars)) {
unset($options[$keys[$i]]);
}
}
//save the options into the session
if ($options) {
if ($this->Session->check("Pagination.{$this->Controller->modelClass}.options")) {
$options = array_merge($this->Session->read("Pagination.{$this->Controller->modelClass}.options"), $options);
}
$this->Session->write("Pagination.{$this->Controller->modelClass}.options", $options);
}
//recall previous options
if ($this->Session->check("Pagination.{$this->Controller->modelClass}.options")) {
$options = $this->Session->read("Pagination.{$this->Controller->modelClass}.options");
$this->Controller->passedArgs = array_merge($this->Controller->passedArgs, $options);
}
}
}
?>

$paginator->current()
Then in your controller, when you have completed what you need to, do this:
where $page is the extra parameter you passed in above.$this->redirect($this->referer()."/page:".$page);
(After some research, I found that my friend was using a Firefox extension that purposely mangles his referrer data and does several other annoying things in the name of "security", but it does make one wonder how many other folk are using similar tools and blame their troubles with websites on the web designer rather than on their own choice of "security" tools. Something to think about...)
Doh! I knew as soon as I published this someone would show me an easier way. Thanks for the tip Andy.
The only time what i propose wouldn't work is if you are storing settings in the session.