Pagination refactoring

This commit is contained in:
Frederic Guillot
2015-01-18 17:59:41 -05:00
parent 74e4a7b064
commit 7c1c14cf64
21 changed files with 772 additions and 656 deletions

View File

@@ -29,157 +29,44 @@ class App extends Base
*/
public function index()
{
$paginate = $this->request->getStringParam('paginate', 'userTasks');
$offset = $this->request->getIntegerParam('offset', 0);
$direction = $this->request->getStringParam('direction');
$order = $this->request->getStringParam('order');
$status = array(SubTaskModel::STATUS_TODO, SubTaskModel::STATUS_INPROGRESS);
$user_id = $this->userSession->getId();
$projects = $this->projectPermission->getMemberProjects($user_id);
$project_ids = array_keys($projects);
$params = array(
$task_paginator = $this->paginator
->setUrl('app', 'index', array('pagination' => 'tasks'))
->setMax(10)
->setOrder('tasks.id')
->setQuery($this->taskFinder->getUserQuery($user_id))
->calculateOnlyIf($this->request->getStringParam('pagination') === 'tasks');
$subtask_paginator = $this->paginator
->setUrl('app', 'index', array('pagination' => 'subtasks'))
->setMax(10)
->setOrder('tasks.id')
->setQuery($this->subTask->getUserQuery($user_id, $status))
->calculateOnlyIf($this->request->getStringParam('pagination') === 'subtasks');
$project_paginator = $this->paginator
->setUrl('app', 'index', array('pagination' => 'projects'))
->setMax(10)
->setOrder('name')
->setQuery($this->project->getQueryColumnStats($project_ids))
->calculateOnlyIf($this->request->getStringParam('pagination') === 'projects');
$this->response->html($this->template->layout('app/dashboard', array(
'title' => t('Dashboard'),
'board_selector' => $this->projectPermission->getAllowedProjects($user_id),
'events' => $this->projectActivity->getProjects($project_ids, 10),
);
$params += $this->getTaskPagination($user_id, $paginate, $offset, $order, $direction);
$params += $this->getSubtaskPagination($user_id, $paginate, $offset, $order, $direction);
$params += $this->getProjectPagination($project_ids, $paginate, $offset, $order, $direction);
$this->response->html($this->template->layout('app/dashboard', $params));
'task_paginator' => $task_paginator,
'subtask_paginator' => $subtask_paginator,
'project_paginator' => $project_paginator,
)));
}
/**
* Get tasks pagination
*
* @access public
* @param integer $user_id
* @param string $paginate
* @param integer $offset
* @param string $order
* @param string $direction
*/
private function getTaskPagination($user_id, $paginate, $offset, $order, $direction)
{
$limit = 10;
if (! in_array($order, array('tasks.id', 'project_name', 'title', 'date_due'))) {
$order = 'tasks.id';
$direction = 'ASC';
}
if ($paginate === 'userTasks') {
$tasks = $this->taskPaginator->userTasks($user_id, $offset, $limit, $order, $direction);
}
else {
$offset = 0;
$tasks = $this->taskPaginator->userTasks($user_id, $offset, $limit);
}
return array(
'tasks' => $tasks,
'task_pagination' => array(
'controller' => 'app',
'action' => 'index',
'params' => array('paginate' => 'userTasks'),
'direction' => $direction,
'order' => $order,
'total' => $this->taskPaginator->countUserTasks($user_id),
'offset' => $offset,
'limit' => $limit,
)
);
}
/**
* Get subtasks pagination
*
* @access public
* @param integer $user_id
* @param string $paginate
* @param integer $offset
* @param string $order
* @param string $direction
*/
private function getSubtaskPagination($user_id, $paginate, $offset, $order, $direction)
{
$status = array(SubTaskModel::STATUS_TODO, SubTaskModel::STATUS_INPROGRESS);
$limit = 10;
if (! in_array($order, array('tasks.id', 'project_name', 'status', 'title'))) {
$order = 'tasks.id';
$direction = 'ASC';
}
if ($paginate === 'userSubtasks') {
$subtasks = $this->subtaskPaginator->userSubtasks($user_id, $status, $offset, $limit, $order, $direction);
}
else {
$offset = 0;
$subtasks = $this->subtaskPaginator->userSubtasks($user_id, $status, $offset, $limit);
}
return array(
'subtasks' => $subtasks,
'subtask_pagination' => array(
'controller' => 'app',
'action' => 'index',
'params' => array('paginate' => 'userSubtasks'),
'direction' => $direction,
'order' => $order,
'total' => $this->subtaskPaginator->countUserSubtasks($user_id, $status),
'offset' => $offset,
'limit' => $limit,
)
);
}
/**
* Get projects pagination
*
* @access public
* @param array $project_ids
* @param string $paginate
* @param integer $offset
* @param string $order
* @param string $direction
*/
private function getProjectPagination(array $project_ids, $paginate, $offset, $order, $direction)
{
$limit = 10;
if (! in_array($order, array('id', 'name'))) {
$order = 'name';
$direction = 'ASC';
}
if ($paginate === 'projectSummaries') {
$projects = $this->projectPaginator->projectSummaries($project_ids, $offset, $limit, $order, $direction);
}
else {
$offset = 0;
$projects = $this->projectPaginator->projectSummaries($project_ids, $offset, $limit);
}
return array(
'projects' => $projects,
'project_pagination' => array(
'controller' => 'app',
'action' => 'index',
'params' => array('paginate' => 'projectSummaries'),
'direction' => $direction,
'order' => $order,
'total' => count($project_ids),
'offset' => $offset,
'limit' => $limit,
)
);
}
/**
* Render Markdown Text and reply with the HTML Code
* Render Markdown text and reply with the HTML Code
*
* @access public
*/

View File

@@ -51,7 +51,7 @@ class Project extends Base
$this->response->html($this->projectLayout('project/show', array(
'project' => $project,
'stats' => $this->project->getStats($project['id']),
'stats' => $this->project->getTaskStats($project['id']),
'title' => $project['name'],
)));
}
@@ -425,38 +425,32 @@ class Project extends Base
{
$project = $this->getProject();
$search = $this->request->getStringParam('search');
$direction = $this->request->getStringParam('direction', 'DESC');
$order = $this->request->getStringParam('order', 'tasks.id');
$offset = $this->request->getIntegerParam('offset', 0);
$tasks = array();
$nb_tasks = 0;
$limit = 25;
$paginator = $this->paginator
->setUrl('project', 'search', array('search' => $search, 'project_id' => $project['id']))
->setMax(30)
->setOrder('tasks.id')
->setDirection('DESC');
if ($search !== '') {
$tasks = $this->taskPaginator->searchTasks($project['id'], $search, $offset, $limit, $order, $direction);
$nb_tasks = $this->taskPaginator->countSearchTasks($project['id'], $search);
$paginator
->setQuery($this->taskFinder->getSearchQuery($project['id'], $search))
->calculate();
$nb_tasks = $paginator->getTotal();
}
$this->response->html($this->template->layout('project/search', array(
'board_selector' => $this->projectPermission->getAllowedProjects($this->userSession->getId()),
'tasks' => $tasks,
'nb_tasks' => $nb_tasks,
'pagination' => array(
'controller' => 'project',
'action' => 'search',
'params' => array('search' => $search, 'project_id' => $project['id']),
'direction' => $direction,
'order' => $order,
'total' => $nb_tasks,
'offset' => $offset,
'limit' => $limit,
),
'values' => array(
'search' => $search,
'controller' => 'project',
'action' => 'search',
'project_id' => $project['id'],
),
'paginator' => $paginator,
'project' => $project,
'columns' => $this->board->getColumnsList($project['id']),
'categories' => $this->category->getList($project['id'], false),
@@ -472,32 +466,21 @@ class Project extends Base
public function tasks()
{
$project = $this->getProject();
$direction = $this->request->getStringParam('direction', 'DESC');
$order = $this->request->getStringParam('order', 'tasks.date_completed');
$offset = $this->request->getIntegerParam('offset', 0);
$limit = 25;
$tasks = $this->taskPaginator->closedTasks($project['id'], $offset, $limit, $order, $direction);
$nb_tasks = $this->taskPaginator->countClosedTasks($project['id']);
$paginator = $this->paginator
->setUrl('project', 'tasks', array('project_id' => $project['id']))
->setMax(30)
->setOrder('tasks.id')
->setDirection('DESC')
->setQuery($this->taskFinder->getClosedTaskQuery($project['id']))
->calculate();
$this->response->html($this->template->layout('project/tasks', array(
'board_selector' => $this->projectPermission->getAllowedProjects($this->userSession->getId()),
'pagination' => array(
'controller' => 'project',
'action' => 'tasks',
'params' => array('project_id' => $project['id']),
'direction' => $direction,
'order' => $order,
'total' => $nb_tasks,
'offset' => $offset,
'limit' => $limit,
),
'project' => $project,
'columns' => $this->board->getColumnsList($project['id']),
'categories' => $this->category->getList($project['id'], false),
'tasks' => $tasks,
'nb_tasks' => $nb_tasks,
'title' => t('Completed tasks for "%s"', $project['name']).' ('.$nb_tasks.')'
'paginator' => $paginator,
'title' => t('Completed tasks for "%s"', $project['name']).' ('.$paginator->getTotal().')'
)));
}

View File

@@ -115,31 +115,19 @@ class User extends Base
*/
public function index()
{
$direction = $this->request->getStringParam('direction', 'ASC');
$order = $this->request->getStringParam('order', 'username');
$offset = $this->request->getIntegerParam('offset', 0);
$limit = 25;
$users = $this->user->paginate($offset, $limit, $order, $direction);
$nb_users = $this->user->count();
$paginator = $this->paginator
->setUrl('user', 'index')
->setMax(30)
->setOrder('username')
->setQuery($this->user->getQuery())
->calculate();
$this->response->html(
$this->template->layout('user/index', array(
'board_selector' => $this->projectPermission->getAllowedProjects($this->userSession->getId()),
'projects' => $this->project->getList(),
'nb_users' => $nb_users,
'users' => $users,
'title' => t('Users').' ('.$nb_users.')',
'pagination' => array(
'controller' => 'user',
'action' => 'index',
'direction' => $direction,
'order' => $order,
'total' => $nb_users,
'offset' => $offset,
'limit' => $limit,
'params' => array(),
),
'title' => t('Users').' ('.$paginator->getTotal().')',
'paginator' => $paginator,
)));
}