Improve Board::Index() and avoid useless HTTP redirects

This commit is contained in:
Frédéric Guillot
2014-09-16 13:25:44 +02:00
parent a7f3cd87fb
commit 12a688347c
4 changed files with 64 additions and 47 deletions

View File

@@ -280,11 +280,12 @@ abstract class Base
* Common method to get a project * Common method to get a project
* *
* @access protected * @access protected
* @param integer $project_id Default project id
* @return array * @return array
*/ */
protected function getProject() protected function getProject($project_id = 0)
{ {
$project_id = $this->request->getIntegerParam('project_id'); $project_id = $this->request->getIntegerParam('project_id', $project_id);
$project = $this->project->getById($project_id); $project = $this->project->getById($project_id);
if (! $project) { if (! $project) {

View File

@@ -188,71 +188,54 @@ class Board extends Base
*/ */
public function index() public function index()
{ {
$projects = $this->project->getListByStatus(ProjectModel::ACTIVE); $last_seen_project_id = $this->user->getLastSeenProject();
$project_id = 0; $favorite_project_id = $this->user->getFavoriteProjectId();
$project_name = ''; $project_id = $last_seen_project_id ?: $favorite_project_id;
if ($this->acl->isRegularUser()) { if (! $project_id) {
$projects = $this->project->filterListByAccess($projects, $this->acl->getUserId()); $projects = $this->project->getAvailableList($this->acl->getUserId());
}
if (empty($projects)) { if (empty($projects)) {
if ($this->acl->isAdminUser()) { if ($this->acl->isAdminUser()) {
$this->redirectNoProject(); $this->redirectNoProject();
}
$this->forbidden();
} }
else {
$this->response->redirect('?controller=project&action=forbidden'); $project_id = key($projects);
}
}
else if (! empty($_SESSION['user']['last_show_project_id']) && isset($projects[$_SESSION['user']['last_show_project_id']])) {
$project_id = $_SESSION['user']['last_show_project_id'];
$project_name = $projects[$_SESSION['user']['last_show_project_id']];
}
else if (! empty($_SESSION['user']['default_project_id']) && isset($projects[$_SESSION['user']['default_project_id']])) {
$project_id = $_SESSION['user']['default_project_id'];
$project_name = $projects[$_SESSION['user']['default_project_id']];
}
else {
list($project_id, $project_name) = each($projects);
} }
$this->response->redirect('?controller=board&action=show&project_id='.$project_id); $this->show($project_id);
} }
/** /**
* Show a board for a given project * Show a board for a given project
* *
* @access public * @access public
* @param integer $project_id Default project id
*/ */
public function show() public function show($project_id = 0)
{ {
$project_id = $this->request->getIntegerParam('project_id'); $project = $this->getProject($project_id);
$user_id = $this->request->getIntegerParam('user_id', UserModel::EVERYBODY_ID);
// Stored last seen in the project dashboard
$_SESSION['user']['last_show_project_id'] = $project_id ;
$this->checkProjectPermissions($project_id);
$projects = $this->project->getAvailableList($this->acl->getUserId()); $projects = $this->project->getAvailableList($this->acl->getUserId());
if (! isset($projects[$project_id])) {
$this->notfound();
}
$board_selector = $projects; $board_selector = $projects;
unset($board_selector[$project_id]); unset($board_selector[$project['id']]);
$this->user->storeLastSeenProject($project['id']);
$this->response->html($this->template->layout('board_index', array( $this->response->html($this->template->layout('board_index', array(
'users' => $this->project->getUsersList($project_id, true, true), 'users' => $this->project->getUsersList($project['id'], true, true),
'filters' => array('user_id' => $user_id), 'filters' => array('user_id' => UserModel::EVERYBODY_ID),
'projects' => $projects, 'projects' => $projects,
'current_project_id' => $project_id, 'current_project_id' => $project['id'],
'current_project_name' => $projects[$project_id], 'current_project_name' => $projects[$project['id']],
'board' => $this->board->get($project_id), 'board' => $this->board->get($project['id']),
'categories' => $this->category->getList($project_id, true, true), 'categories' => $this->category->getList($project['id'], true, true),
'menu' => 'boards', 'menu' => 'boards',
'title' => $projects[$project_id], 'title' => $projects[$project['id']],
'board_selector' => $board_selector, 'board_selector' => $board_selector,
))); )));
} }

View File

@@ -53,7 +53,7 @@ class User extends Base
list($valid, $errors) = $this->authentication->validateForm($values); list($valid, $errors) = $this->authentication->validateForm($values);
if ($valid) { if ($valid) {
$this->response->redirect('?controller=app'); $this->response->redirect('?controller=board');
} }
$this->response->html($this->template->layout('user_login', array( $this->response->html($this->template->layout('user_login', array(

View File

@@ -27,6 +27,39 @@ class User extends Base
*/ */
const EVERYBODY_ID = -1; const EVERYBODY_ID = -1;
/**
* Get the default project from the session
*
* @access public
* @return integer
*/
public function getFavoriteProjectId()
{
return isset($_SESSION['user']['default_project_id']) ? $_SESSION['user']['default_project_id'] : 0;
}
/**
* Get the last seen project from the session
*
* @access public
* @return integer
*/
public function getLastSeenProject()
{
return empty($_SESSION['user']['last_show_project_id']) ? 0 : $_SESSION['user']['last_show_project_id'];
}
/**
* Set the last seen project from the session
*
* @access public
* @@param integer $project_id Project id
*/
public function storeLastSeenProject($project_id)
{
$_SESSION['user']['last_show_project_id'] = (int) $project_id;
}
/** /**
* Get a specific user by id * Get a specific user by id
* *