diff options
author | Frédéric Guillot <fguillot@users.noreply.github.com> | 2014-04-21 22:44:25 -0400 |
---|---|---|
committer | Frédéric Guillot <fguillot@users.noreply.github.com> | 2014-04-21 22:44:25 -0400 |
commit | 919e5d51a4cc4a2928cd34c8c575d4c003bb9a74 (patch) | |
tree | acc8c0312f8efe6d32dd97355754b663c7b383e7 | |
parent | 9bfd824ab724dc2eb356e06b1722f843dbd8d96d (diff) |
Add a method to apply filters for tasks
-rw-r--r-- | controllers/project.php | 8 | ||||
-rw-r--r-- | models/board.php | 16 | ||||
-rw-r--r-- | models/task.php | 79 | ||||
-rw-r--r-- | templates/project_tasks.php | 2 | ||||
-rw-r--r-- | tests/TaskTest.php | 29 |
5 files changed, 76 insertions, 58 deletions
diff --git a/controllers/project.php b/controllers/project.php index a89c7879..6085caec 100644 --- a/controllers/project.php +++ b/controllers/project.php @@ -42,12 +42,18 @@ class Project extends Base $this->checkProjectPermissions($project['id']); - $tasks = $this->task->getAllByProjectId($project_id, array(0)); + $filters = array( + array('column' => 'project_id', 'operator' => 'eq', 'value' => $project_id), + array('column' => 'is_active', 'operator' => 'eq', 'value' => \Model\Task::STATUS_CLOSED), + ); + + $tasks = $this->task->find($filters); $nb_tasks = count($tasks); $this->response->html($this->template->layout('project_tasks', array( 'menu' => 'projects', 'project' => $project, + 'columns' => $this->board->getColumnsList($project_id), 'tasks' => $tasks, 'nb_tasks' => $nb_tasks, 'title' => $project['name'].' ('.$nb_tasks.')' diff --git a/models/board.php b/models/board.php index 3bff8d56..af1f4f7a 100644 --- a/models/board.php +++ b/models/board.php @@ -181,10 +181,24 @@ class Board extends Base $this->db->startTransaction(); $columns = $this->getColumns($project_id); + + $filters = array( + array('column' => 'project_id', 'operator' => 'eq', 'value' => $project_id), + array('column' => 'is_active', 'operator' => 'eq', 'value' => Task::STATUS_OPEN), + ); + $taskModel = new Task($this->db, $this->event); + $tasks = $taskModel->find($filters); foreach ($columns as &$column) { - $column['tasks'] = $taskModel->getAllByColumnId($project_id, $column['id'], array(1)); + + $column['tasks'] = array(); + + foreach ($tasks as &$task) { + if ($task['column_id'] == $column['id']) { + $column['tasks'][] = $task; + } + } } $this->db->closeTransaction(); diff --git a/models/task.php b/models/task.php index e327e8c2..c54e0cbc 100644 --- a/models/task.php +++ b/models/task.php @@ -3,7 +3,6 @@ namespace Model; require_once __DIR__.'/base.php'; -require_once __DIR__.'/comment.php'; use \SimpleValidator\Validator; use \SimpleValidator\Validators; @@ -107,42 +106,6 @@ class Task extends Base } /** - * Get all tasks for a given project - * - * @access public - * @param integer $project_id Project id - * @param array $status List of status id - * @return array - */ - public function getAllByProjectId($project_id, array $status = array(self::STATUS_OPEN, self::STATUS_CLOSED)) - { - return $this->db->table(self::TABLE) - ->columns( - self::TABLE.'.id', - self::TABLE.'.title', - self::TABLE.'.description', - self::TABLE.'.date_creation', - self::TABLE.'.date_completed', - self::TABLE.'.date_due', - self::TABLE.'.color_id', - self::TABLE.'.project_id', - self::TABLE.'.column_id', - self::TABLE.'.owner_id', - self::TABLE.'.position', - self::TABLE.'.is_active', - self::TABLE.'.score', - Board::TABLE.'.title AS column_title', - User::TABLE.'.username' - ) - ->join(Board::TABLE, 'id', 'column_id') - ->join(User::TABLE, 'id', 'owner_id') - ->eq(self::TABLE.'.project_id', $project_id) - ->in('is_active', $status) - ->desc('date_completed') - ->findAll(); - } - - /** * Count all tasks for a given project and status * * @access public @@ -160,33 +123,39 @@ class Task extends Base } /** - * Get all tasks for a given column + * Get tasks that match defined filters * * @access public - * @param integer $project_id Project id - * @param integer $column_id Column id - * @param array $status List of status id + * @param array $filters Filters: [ ['column' => '...', 'operator' => '...', 'value' => '...'], ... ] * @return array */ - public function getAllByColumnId($project_id, $column_id, array $status = array(self::STATUS_OPEN)) + public function find(array $filters) { - $tasks = $this->db + $table = $this->db ->table(self::TABLE) - ->columns('tasks.id', 'title', 'color_id', 'project_id', 'owner_id', 'column_id', 'position', 'score', 'date_due', 'users.username') - ->join('users', 'id', 'owner_id') - ->eq('project_id', $project_id) - ->eq('column_id', $column_id) - ->in('is_active', $status) - ->asc('position') - ->findAll(); - - $commentModel = new Comment($this->db, $this->event); + ->columns( + '(SELECT count(*) FROM comments WHERE task_id=tasks.id) AS nb_comments', + 'tasks.id', + 'tasks.title', + 'tasks.date_creation', + 'tasks.date_completed', + 'tasks.date_due', + 'tasks.color_id', + 'tasks.project_id', + 'tasks.column_id', + 'tasks.owner_id', + 'tasks.position', + 'tasks.is_active', + 'tasks.score', + 'users.username' + ) + ->join('users', 'id', 'owner_id'); - foreach ($tasks as &$task) { - $task['nb_comments'] = $commentModel->count($task['id']); + foreach ($filters as $filter) { + $table->$filter['operator']($filter['column'], $filter['value']); } - return $tasks; + return $table->findAll(); } /** diff --git a/templates/project_tasks.php b/templates/project_tasks.php index 77b4db62..d54565f3 100644 --- a/templates/project_tasks.php +++ b/templates/project_tasks.php @@ -28,7 +28,7 @@ <a href="?controller=task&action=show&task_id=<?= $task['id'] ?>" title="<?= t('View this task') ?>"><?= Helper\escape($task['id']) ?></a> </td> <td> - <?= Helper\escape($task['column_title']) ?> + <?= Helper\in_list($task['column_id'], $columns) ?> </td> <td> <a href="?controller=task&action=show&task_id=<?= $task['id'] ?>" title="<?= t('View this task') ?>"><?= Helper\escape($task['title']) ?></a> diff --git a/tests/TaskTest.php b/tests/TaskTest.php index 5ddb1860..8218a5cf 100644 --- a/tests/TaskTest.php +++ b/tests/TaskTest.php @@ -7,6 +7,35 @@ use Model\Project; class TaskTest extends Base { + public function testFilter() + { + $t = new Task($this->db, $this->event); + $p = new Project($this->db, $this->event); + + $this->assertEquals(1, $p->create(array('name' => 'test1'))); + $this->assertEquals(1, $t->create(array('title' => 'test a', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1))); + $this->assertEquals(2, $t->create(array('title' => 'test b', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 2))); + + $tasks = $t->find(array(array('column' => 'project_id', 'operator' => 'eq', 'value' => '1'))); + $this->assertEquals(2, count($tasks)); + $this->assertEquals(1, $tasks[0]['id']); + $this->assertEquals(2, $tasks[1]['id']); + + $tasks = $t->find(array( + array('column' => 'project_id', 'operator' => 'eq', 'value' => '1'), + array('column' => 'owner_id', 'operator' => 'eq', 'value' => '2'), + )); + $this->assertEquals(1, count($tasks)); + $this->assertEquals(2, $tasks[0]['id']); + + $tasks = $t->find(array( + array('column' => 'project_id', 'operator' => 'eq', 'value' => '1'), + array('column' => 'title', 'operator' => 'like', 'value' => '%b%'), + )); + $this->assertEquals(1, count($tasks)); + $this->assertEquals(2, $tasks[0]['id']); + } + public function testDateFormat() { $t = new Task($this->db, $this->event); |