/**
* Calculates the page to redirect to.
*
* @param int $topic_id
* @param int $post_id
* @param bool $return
* @return mixed
*/
public function goToPage($topic_id = null, $post_id = null, $return = false)
{
$topic = ClassRegistry::init('Forum.Topic')->getById($topic_id);
$slug = !empty($topic['Topic']['slug']) ? $topic['Topic']['slug'] : null;
// Certain page
if ($topic_id && $post_id) {
$posts = ClassRegistry::init('Forum.Post')->getIdsForTopic($topic_id);
$perPage = Configure::read('Forum.settings.postsPerPage');
$totalPosts = count($posts);
if ($totalPosts > $perPage) {
$totalPages = ceil($totalPosts / $perPage);
} else {
$totalPages = 1;
}
if ($totalPages <= 1) {
$url = array('plugin' => 'forum', 'controller' => 'topics', 'action' => 'view', $slug, '#' => 'post-' . $post_id);
} else {
$posts = array_values($posts);
$flips = array_flip($posts);
$position = $flips[$post_id] + 1;
$goTo = ceil($position / $perPage);
$url = array('plugin' => 'forum', 'controller' => 'topics', 'action' => 'view', $slug, 'page' => $goTo, '#' => 'post-' . $post_id);
}
// First post
} else {
if ($topic_id && !$post_id) {
$url = array('plugin' => 'forum', 'controller' => 'topics', 'action' => 'view', $slug);
// None
} else {
$url = $this->Controller->referer();
if (!$url || strpos($url, 'delete') !== false) {
$url = array('plugin' => 'forum', 'controller' => 'forum', 'action' => 'index');
}
}
}
if ($return) {
return $url;
}
return $this->Controller->redirect($url);
}