From 096b282a473fa2b27c3bfe3061f54b5fd83c75e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Sun, 27 Apr 2014 15:14:13 -0400 Subject: [PATCH] Add a basic task search --- controllers/project.php | 51 ++++++++++++++++++++++ core/request.php | 5 +++ core/translator.php | 4 ++ locales/es_ES/translations.php | 6 +++ locales/fr_FR/translations.php | 6 +++ locales/pl_PL/translations.php | 6 +++ locales/pt_BR/translations.php | 6 +++ models/task.php | 17 +++++++- templates/board_index.php | 1 + templates/project_search.php | 80 ++++++++++++++++++++++++++++++++++ templates/project_tasks.php | 14 ++++-- tests/TaskTest.php | 43 +++++++++++++++++- 12 files changed, 231 insertions(+), 8 deletions(-) create mode 100644 templates/project_search.php diff --git a/controllers/project.php b/controllers/project.php index 6085caec0..a9ce86340 100644 --- a/controllers/project.php +++ b/controllers/project.php @@ -25,6 +25,57 @@ class Project extends Base ))); } + /** + * Task search for a given project + * + * @access public + */ + public function search() + { + $project_id = $this->request->getIntegerParam('project_id'); + $search = $this->request->getStringParam('search'); + + $project = $this->project->getById($project_id); + $tasks = array(); + $nb_tasks = 0; + + if (! $project) { + $this->session->flashError(t('Project not found.')); + $this->response->redirect('?controller=project'); + } + + $this->checkProjectPermissions($project['id']); + + if ($search !== '') { + + $filters = array( + array('column' => 'project_id', 'operator' => 'eq', 'value' => $project_id), + 'or' => array( + array('column' => 'title', 'operator' => 'like', 'value' => '%'.$search.'%'), + array('column' => 'description', 'operator' => 'like', 'value' => '%'.$search.'%'), + ) + ); + + $tasks = $this->task->find($filters); + $nb_tasks = count($tasks); + } + + $this->response->html($this->template->layout('project_search', array( + 'tasks' => $tasks, + 'nb_tasks' => $nb_tasks, + 'values' => array( + 'search' => $search, + 'controller' => 'project', + 'action' => 'search', + 'project_id' => $project['id'], + ), + 'menu' => 'projects', + 'project' => $project, + 'columns' => $this->board->getColumnsList($project_id), + 'title' => $project['name'].($nb_tasks > 0 ? ' ('.$nb_tasks.')' : '') + ))); + } + /** * List of completed tasks for a given project * diff --git a/core/request.php b/core/request.php index b2c3e12e9..f2fa4d9f4 100644 --- a/core/request.php +++ b/core/request.php @@ -43,4 +43,9 @@ class Request return ''; } + + public function isPost() + { + return isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST'; + } } diff --git a/core/translator.php b/core/translator.php index 0f8c3d89c..094801117 100644 --- a/core/translator.php +++ b/core/translator.php @@ -52,6 +52,10 @@ namespace Translator { function datetime($format, $timestamp) { + if (! $timestamp) { + return ''; + } + return strftime(get($format, $format), (int) $timestamp); } diff --git a/locales/es_ES/translations.php b/locales/es_ES/translations.php index 8b331c491..bf6f80e6d 100644 --- a/locales/es_ES/translations.php +++ b/locales/es_ES/translations.php @@ -283,4 +283,10 @@ return array( // 'Filter by user' => '', // 'Filter by due date' => ', // 'Everybody' => '', + // 'Open' => '', + // 'Closed' => '', + // 'Search' => '', + // 'Nothing found.' => '', + // 'Search in the project "%s"' => '', + // 'Due date' => '', ); diff --git a/locales/fr_FR/translations.php b/locales/fr_FR/translations.php index 563ccc91c..54659276f 100644 --- a/locales/fr_FR/translations.php +++ b/locales/fr_FR/translations.php @@ -283,4 +283,10 @@ return array( 'Filter by user' => 'Filtrer par utilisateur', 'Filter by due date' => 'Filtrer par date d\'échéance', 'Everybody' => 'Tout le monde', + 'Open' => 'Ouvert', + 'Closed' => 'Fermé', + 'Search' => 'Rechercher', + 'Nothing found.' => 'Rien trouvé.', + 'Search in the project "%s"' => 'Rechercher dans le projet « %s »', + 'Due date' => 'Date d\'échéance', ); diff --git a/locales/pl_PL/translations.php b/locales/pl_PL/translations.php index 3ecbc4718..ed16779fe 100644 --- a/locales/pl_PL/translations.php +++ b/locales/pl_PL/translations.php @@ -288,4 +288,10 @@ return array( // 'Filter by user' => '', // 'Filter by due date' => ', // 'Everybody' => '', + // 'Open' => '', + // 'Closed' => '', + // 'Search' => '', + // 'Nothing found.' => '', + // 'Search in the project "%s"' => '', + // 'Due date' => '', ); diff --git a/locales/pt_BR/translations.php b/locales/pt_BR/translations.php index 53ff4b15d..54b357db9 100644 --- a/locales/pt_BR/translations.php +++ b/locales/pt_BR/translations.php @@ -284,4 +284,10 @@ return array( // 'Filter by user' => '', // 'Filter by due date' => ', // 'Everybody' => '', + // 'Open' => '', + // 'Closed' => '', + // 'Search' => '', + // 'Nothing found.' => '', + // 'Search in the project "%s"' => '', + // 'Due date' => '', ); diff --git a/models/task.php b/models/task.php index b61fb13f3..bef92f207 100644 --- a/models/task.php +++ b/models/task.php @@ -152,8 +152,21 @@ class Task extends Base ) ->join('users', 'id', 'owner_id'); - foreach ($filters as $filter) { - $table->$filter['operator']($filter['column'], $filter['value']); + foreach ($filters as $key => $filter) { + + if ($key === 'or') { + + $table->beginOr(); + + foreach ($filter as $subfilter) { + $table->$subfilter['operator']($subfilter['column'], $subfilter['value']); + } + + $table->closeOr(); + } + else if (isset($filter['operator']) && isset($filter['column']) && isset($filter['value'])) { + $table->$filter['operator']($filter['column'], $filter['value']); + } } if (empty($sorting)) { diff --git a/templates/board_index.php b/templates/board_index.php index fc7a49322..f7fa4bdd3 100644 --- a/templates/board_index.php +++ b/templates/board_index.php @@ -22,6 +22,7 @@
  • +
  • diff --git a/templates/project_search.php b/templates/project_search.php new file mode 100644 index 000000000..d7d7748e5 --- /dev/null +++ b/templates/project_search.php @@ -0,0 +1,80 @@ +
    + +
    +
    + + + + + +
    + + +

    + + + + + + + + + + + + + + + + + + + + + + + + +
    + + + + + + + + + + + + + + + + + + + + + + + + + +
    + + +
    +
    \ No newline at end of file diff --git a/templates/project_tasks.php b/templates/project_tasks.php index d54565f35..1ffd52acb 100644 --- a/templates/project_tasks.php +++ b/templates/project_tasks.php @@ -3,10 +3,8 @@

    ()

    @@ -19,6 +17,7 @@ + @@ -34,7 +33,14 @@ - + + + + + + + + diff --git a/tests/TaskTest.php b/tests/TaskTest.php index 0c5e24962..701ba6117 100644 --- a/tests/TaskTest.php +++ b/tests/TaskTest.php @@ -13,8 +13,8 @@ class TaskTest extends Base $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))); + $this->assertEquals(1, $t->create(array('title' => 'test a', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1, 'description' => 'biloute'))); + $this->assertEquals(2, $t->create(array('title' => 'test b', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 2, 'description' => 'toto et titi sont dans un bateau'))); $tasks = $t->find(array(array('column' => 'project_id', 'operator' => 'eq', 'value' => '1'))); $this->assertEquals(2, count($tasks)); @@ -34,6 +34,45 @@ class TaskTest extends Base )); $this->assertEquals(1, count($tasks)); $this->assertEquals(2, $tasks[0]['id']); + + // Condition with OR + $search = 'bateau'; + $filters = array( + array('column' => 'project_id', 'operator' => 'eq', 'value' => 1), + 'or' => array( + array('column' => 'title', 'operator' => 'like', 'value' => '%'.$search.'%'), + array('column' => 'description', 'operator' => 'like', 'value' => '%'.$search.'%'), + ) + ); + + $tasks = $t->find($filters); + $this->assertEquals(1, count($tasks)); + $this->assertEquals(2, $tasks[0]['id']); + + $search = 'toto et titi'; + $filters = array( + array('column' => 'project_id', 'operator' => 'eq', 'value' => 1), + 'or' => array( + array('column' => 'title', 'operator' => 'like', 'value' => '%'.$search.'%'), + array('column' => 'description', 'operator' => 'like', 'value' => '%'.$search.'%'), + ) + ); + + $tasks = $t->find($filters); + $this->assertEquals(1, count($tasks)); + $this->assertEquals(2, $tasks[0]['id']); + + $search = 'john'; + $filters = array( + array('column' => 'project_id', 'operator' => 'eq', 'value' => 1), + 'or' => array( + array('column' => 'title', 'operator' => 'like', 'value' => '%'.$search.'%'), + array('column' => 'description', 'operator' => 'like', 'value' => '%'.$search.'%'), + ) + ); + + $tasks = $t->find($filters); + $this->assertEquals(0, count($tasks)); } public function testDateFormat()