views:

737

answers:

4

I have an index view that lists items, and it's a long list so I use Paginator to limit items to 50-to-a-view.

Each item has an "edit" link that goes to an edit view with inputs/validations/etc. When that form is submitted, I redirect the use back to the index view.

So far so good, but here's the rub:

If a user is on page N of the index and they click edit and edit an item, I want them to be redirected back to page N of the index. If I knew the page number, I could just stick "/page:N" on to end of the URL, but I have no idea how I can get the page number. (N could be any page number, but especially >=2)

Any ideas would be appreciated.

+1  A: 

The page number should be part of the $params var in the list view. Just tack it onto the end of the edit link and handle it from there. On the edit page you will need a way to take in the optional page number, store it during any form submission, and forward back to the list with the same page number.

Mike B
That's kinda what I figured, I was just hoping CakePHP had a better way already built in. Thanks
mgroves
+1  A: 

I created a component that saves the page in the session. Then in the app_controller.php, I check to see if there is anything in the session for the particular model being used and then add that to the url. If you are interested in the code for the component, message me. I also store the order, if the user changed the sort order in the index page before editing.

See here for the source: http://github.com/jimiyash/cake-pluggables/blob/a0c3774982c19d02cfdd19a2977eabe046a4b294/controllers/components/memory.php

Here is the gist of what I am doing.

//controller or component code
if(!empty($params['named']) && !empty($params['controller']) && $params['action'] == 'admin_index'){
    $this->Session->write("Pagem.{$params['controller']}", $params['named']);
}

//app_controller.php
    $redirectNew = "";
 if(is_array($redirectTo)){
  if(!empty($params['prefix']) && $params['prefix'] == 'admin'){
   $redirectNew .= '/admin';
  }
  if(!empty($params['controller'])){
   $redirectNew .= "/" . $params['controller'];
  }
  if(!empty($redirectTo['action'])){
   $redirectNew .= "/" . $redirectTo['action'];
  }
 } else {
  $redirectNew = $redirectTo;
 }

 $controller = $params['controller'];
 if($this->Session->check("Pagem.$controller")){
  $settings =  $this->Session->read("Pagem.$controller");
  $append = array();
  foreach($settings as $key=>$value){
   $append[] = "$key:$value";
  }
  return $redirectNew . "/" . join("/", $append);
 } else {
  return $redirectNew;
 }
jimiyash
Jason I didn't mention this, but I would prefer not to use session for this, however you're answer was very good and I will still look into it.
mgroves
I like the solution because it will work for all controllers, as this is a very common problem, and the user expects pagination to be persistent.
jimiyash
+2  A: 

If I understand correctly, the above is fine for editing, but not for adding. This solution should work for both situations:

In your controllers or your /app/app_controller.php, put in something like this for adding:

$insertID = $this->{$this->modelClass}->getLastInsertID();
$page = $this->{$this->modelClass}->getPageNumber($insertID, $this->paginate['limit']);
$this->redirect("/admin/{$controllerName}/index/page:{$page}");

...and something like this for editing:

$page = $this->{$this->modelClass}->getPageNumber($id, $this->paginate['limit']);
$this->redirect("/admin/{$controllerName}/index/page:{$page}");

In your /app/app_model.php, put in this:

/**
 * Work out which page a record is on, so the user can be redirected to
 * the correct page.  (Not necessarily the page she came from, as this
 * could be a new record.)
 */

  function getPageNumber($id, $rowsPerPage) {
    $result = $this->find('list'); // id => name
    $resultIDs = array_keys($result); // position - 1 => id
    $resultPositions = array_flip($resultIDs); // id => position - 1
    $position = $resultPositions[$id] + 1; // Find the row number of the record
    $page = ceil($position / $rowsPerPage); // Find the page of that row number
    return $page;
  }

Hope that helps!

Zoe Blade
A: 

Does a simple

$this->redirect($this->referer());

works?

Paolo