diff options
author | Frederic Guillot <fred@kanboard.net> | 2016-08-13 14:23:53 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2016-08-13 14:23:53 -0400 |
commit | 4ffaba2ba0dd6b5810adea1916080c3b645f3d29 (patch) | |
tree | 7c216830af0f97b0c0908d63be83698cabba3922 | |
parent | 29820bf83b7ee18b5f48990ce0188a9c041c3473 (diff) |
Add reference hooks
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | app/Core/Filter/LexerBuilder.php | 2 | ||||
-rw-r--r-- | app/Core/Plugin/Hook.php | 17 | ||||
-rw-r--r-- | app/Formatter/BoardFormatter.php | 11 | ||||
-rw-r--r-- | app/Pagination/SubtaskPagination.php | 5 | ||||
-rw-r--r-- | app/Pagination/TaskPagination.php | 5 | ||||
-rw-r--r-- | doc/plugin-hooks.markdown | 25 | ||||
-rw-r--r-- | tests/units/Core/Plugin/HookTest.php | 62 |
8 files changed, 97 insertions, 31 deletions
@@ -3,6 +3,7 @@ Version 1.0.33 (unreleased) Improvements: +* Add "reference" hooks * Show project name in task forms * Convert vanilla CSS to SASS diff --git a/app/Core/Filter/LexerBuilder.php b/app/Core/Filter/LexerBuilder.php index 626d7614..e3ab725b 100644 --- a/app/Core/Filter/LexerBuilder.php +++ b/app/Core/Filter/LexerBuilder.php @@ -51,7 +51,7 @@ class LexerBuilder */ public function __construct() { - $this->lexer = new Lexer; + $this->lexer = new Lexer(); $this->queryBuilder = new QueryBuilder(); } diff --git a/app/Core/Plugin/Hook.php b/app/Core/Plugin/Hook.php index ade69150..ca197937 100644 --- a/app/Core/Plugin/Hook.php +++ b/app/Core/Plugin/Hook.php @@ -96,4 +96,21 @@ class Hook return null; } + + /** + * Hook with reference + * + * @access public + * @param string $hook + * @param mixed $param + * @return mixed + */ + public function reference($hook, &$param) + { + foreach ($this->getListeners($hook) as $listener) { + $listener($param); + } + + return $param; + } } diff --git a/app/Formatter/BoardFormatter.php b/app/Formatter/BoardFormatter.php index 350dde6c..df443a52 100644 --- a/app/Formatter/BoardFormatter.php +++ b/app/Formatter/BoardFormatter.php @@ -44,6 +44,13 @@ class BoardFormatter extends BaseFormatter implements FormatterInterface { $swimlanes = $this->swimlaneModel->getSwimlanes($this->projectId); $columns = $this->columnModel->getAll($this->projectId); + + if (empty($swimlanes) || empty($columns)) { + return array(); + } + + $this->hook->reference('formatter:board:query', $this->query); + $tasks = $this->query ->eq(TaskModel::TABLE.'.project_id', $this->projectId) ->asc(TaskModel::TABLE.'.position') @@ -52,10 +59,6 @@ class BoardFormatter extends BaseFormatter implements FormatterInterface $task_ids = array_column($tasks, 'id'); $tags = $this->taskTagModel->getTagsByTasks($task_ids); - if (empty($swimlanes) || empty($columns)) { - return array(); - } - return BoardSwimlaneFormatter::getInstance($this->container) ->withSwimlanes($swimlanes) ->withColumns($columns) diff --git a/app/Pagination/SubtaskPagination.php b/app/Pagination/SubtaskPagination.php index f0cd6148..c55d0fb4 100644 --- a/app/Pagination/SubtaskPagination.php +++ b/app/Pagination/SubtaskPagination.php @@ -26,11 +26,14 @@ class SubtaskPagination extends Base */ public function getDashboardPaginator($user_id, $method, $max) { + $query = $this->subtaskModel->getUserQuery($user_id, array(SubtaskModel::STATUS_TODO, SubtaskModel::STATUS_INPROGRESS)); + $this->hook->reference('pagination:dashboard:subtask:query', $query); + return $this->paginator ->setUrl('DashboardController', $method, array('pagination' => 'subtasks', 'user_id' => $user_id)) ->setMax($max) ->setOrder(TaskModel::TABLE.'.id') - ->setQuery($this->subtaskModel->getUserQuery($user_id, array(SubtaskModel::STATUS_TODO, SubtaskModel::STATUS_INPROGRESS))) + ->setQuery($query) ->calculateOnlyIf($this->request->getStringParam('pagination') === 'subtasks'); } } diff --git a/app/Pagination/TaskPagination.php b/app/Pagination/TaskPagination.php index a395ab84..5fe986e7 100644 --- a/app/Pagination/TaskPagination.php +++ b/app/Pagination/TaskPagination.php @@ -25,11 +25,14 @@ class TaskPagination extends Base */ public function getDashboardPaginator($user_id, $method, $max) { + $query = $this->taskFinderModel->getUserQuery($user_id); + $this->hook->reference('pagination:dashboard:task:query', $query); + return $this->paginator ->setUrl('DashboardController', $method, array('pagination' => 'tasks', 'user_id' => $user_id)) ->setMax($max) ->setOrder(TaskModel::TABLE.'.id') - ->setQuery($this->taskFinderModel->getUserQuery($user_id)) + ->setQuery($query) ->calculateOnlyIf($this->request->getStringParam('pagination') === 'tasks'); } } diff --git a/doc/plugin-hooks.markdown b/doc/plugin-hooks.markdown index 787c62df..5e01e93a 100644 --- a/doc/plugin-hooks.markdown +++ b/doc/plugin-hooks.markdown @@ -115,6 +115,31 @@ List of asset Hooks: - `template:layout:css` - `template:layout:js` + +Reference hooks +--------------- + +Reference hooks are passing a variable by reference. + +Example: + +```php +$this->hook->on('formatter:board:query', function (\PicoDb\Table &query) { + $query->eq('color_id', 'red'); +}); +``` + +The code above will show only tasks in red on the board. + +List of reference hooks: + +| Hook | Description | +|--------------------------------------------|---------------------------------------------------------------| +| `formatter:board:query` | Alter database query before rendering board | +| `pagination:dashboard:task:query` | Alter database query for tasks pagination on the dashboard | +| `pagination:dashboard:subtask:query` | Alter database query for subtasks pagination on the dashboard | + + Template Hooks -------------- diff --git a/tests/units/Core/Plugin/HookTest.php b/tests/units/Core/Plugin/HookTest.php index d1c139b3..acadede0 100644 --- a/tests/units/Core/Plugin/HookTest.php +++ b/tests/units/Core/Plugin/HookTest.php @@ -8,89 +8,103 @@ class HookTest extends Base { public function testGetListeners() { - $h = new Hook; - $this->assertEmpty($h->getListeners('myhook')); + $hook = new Hook; + $this->assertEmpty($hook->getListeners('myhook')); - $h->on('myhook', 'A'); - $h->on('myhook', 'B'); + $hook->on('myhook', 'A'); + $hook->on('myhook', 'B'); - $this->assertEquals(array('A', 'B'), $h->getListeners('myhook')); + $this->assertEquals(array('A', 'B'), $hook->getListeners('myhook')); } public function testExists() { - $h = new Hook; - $this->assertFalse($h->exists('myhook')); + $hook = new Hook; + $this->assertFalse($hook->exists('myhook')); - $h->on('myhook', 'A'); + $hook->on('myhook', 'A'); - $this->assertTrue($h->exists('myhook')); + $this->assertTrue($hook->exists('myhook')); } public function testMergeWithNoBinding() { - $h = new Hook; + $hook = new Hook; $values = array('A', 'B'); - $result = $h->merge('myhook', $values, array('p' => 'c')); + $result = $hook->merge('myhook', $values, array('p' => 'c')); $this->assertEquals($values, $result); } public function testMergeWithBindings() { - $h = new Hook; + $hook = new Hook; $values = array('A', 'B'); $expected = array('A', 'B', 'c', 'D'); - $h->on('myhook', function ($p) { + $hook->on('myhook', function ($p) { return array($p); }); - $h->on('myhook', function () { + $hook->on('myhook', function () { return array('D'); }); - $result = $h->merge('myhook', $values, array('p' => 'c')); + $result = $hook->merge('myhook', $values, array('p' => 'c')); $this->assertEquals($expected, $result); $this->assertEquals($expected, $values); } public function testMergeWithBindingButReturningBadData() { - $h = new Hook; + $hook = new Hook; $values = array('A', 'B'); $expected = array('A', 'B'); - $h->on('myhook', function () { + $hook->on('myhook', function () { return 'string'; }); - $result = $h->merge('myhook', $values); + $result = $hook->merge('myhook', $values); $this->assertEquals($expected, $result); $this->assertEquals($expected, $values); } public function testFirstWithNoBinding() { - $h = new Hook; + $hook = new Hook; - $result = $h->first('myhook', array('p' => 2)); + $result = $hook->first('myhook', array('p' => 2)); $this->assertEquals(null, $result); } public function testFirstWithMultipleBindings() { - $h = new Hook; + $hook = new Hook; - $h->on('myhook', function ($p) { + $hook->on('myhook', function ($p) { return $p + 1; }); - $h->on('myhook', function ($p) { + $hook->on('myhook', function ($p) { return $p; }); - $result = $h->first('myhook', array('p' => 3)); + $result = $hook->first('myhook', array('p' => 3)); $this->assertEquals(4, $result); } + + public function testHookWithReference() + { + $hook = new Hook(); + + $hook->on('myhook', function (&$p) { + $p = 2; + }); + + $param = 123; + $result = $hook->reference('myhook', $param); + $this->assertSame(2, $result); + $this->assertSame(2, $param); + } } |