diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/Controller/Board.php | 2 | ||||
-rw-r--r-- | app/Core/Event.php | 15 | ||||
-rw-r--r-- | app/Model/Task.php | 93 | ||||
-rw-r--r-- | app/Model/TaskCreation.php | 13 | ||||
-rw-r--r-- | app/Model/TaskPosition.php | 128 |
5 files changed, 149 insertions, 102 deletions
diff --git a/app/Controller/Board.php b/app/Controller/Board.php index f5bb4d80..e14b94c7 100644 --- a/app/Controller/Board.php +++ b/app/Controller/Board.php @@ -347,7 +347,7 @@ class Board extends Base $values = $this->request->getJson(); - if ($this->task->movePosition($project_id, $values['task_id'], $values['column_id'], $values['position'])) { + if ($this->taskPosition->movePosition($project_id, $values['task_id'], $values['column_id'], $values['position'])) { $this->response->html( $this->template->load('board/show', array( diff --git a/app/Core/Event.php b/app/Core/Event.php index a32499d8..935f8b9c 100644 --- a/app/Core/Event.php +++ b/app/Core/Event.php @@ -69,7 +69,7 @@ class Event { if (! $this->isEventTriggered($eventName)) { - $this->events[] = $eventName; + $this->events[$eventName] = $data; if (isset($this->listeners[$eventName])) { @@ -119,6 +119,17 @@ class Event } /** + * Get a list of triggered events + * + * @access public + * @return array + */ + public function getEventData($eventName) + { + return isset($this->events[$eventName]) ? $this->events[$eventName] : array(); + } + + /** * Check if an event have been triggered * * @access public @@ -127,7 +138,7 @@ class Event */ public function isEventTriggered($eventName) { - return in_array($eventName, $this->events); + return isset($this->events[$eventName]); } /** diff --git a/app/Model/Task.php b/app/Model/Task.php index 93914fba..4fed25f6 100644 --- a/app/Model/Task.php +++ b/app/Model/Task.php @@ -109,12 +109,6 @@ class Task extends Base if (isset($updated_task['owner_id']) && $original_task['owner_id'] != $updated_task['owner_id']) { $events[] = self::EVENT_ASSIGNEE_CHANGE; } - else if (isset($updated_task['column_id']) && $original_task['column_id'] != $updated_task['column_id']) { - $events[] = self::EVENT_MOVE_COLUMN; - } - else if (isset($updated_task['position']) && $original_task['position'] != $updated_task['position']) { - $events[] = self::EVENT_MOVE_POSITION; - } else { $events[] = self::EVENT_CREATE_UPDATE; $events[] = self::EVENT_UPDATE; @@ -147,93 +141,6 @@ class Task extends Base } /** - * Move a task to another column or to another position - * - * @access public - * @param integer $project_id Project id - * @param integer $task_id Task id - * @param integer $column_id Column id - * @param integer $position Position (must be >= 1) - * @return boolean - */ - public function movePosition($project_id, $task_id, $column_id, $position) - { - // The position can't be lower than 1 - if ($position < 1) { - return false; - } - - $board = $this->db->table(Board::TABLE)->eq('project_id', $project_id)->asc('position')->findAllByColumn('id'); - $columns = array(); - - // Prepare the columns - foreach ($board as $board_column_id) { - - $columns[$board_column_id] = $this->db->table(self::TABLE) - ->eq('is_active', 1) - ->eq('project_id', $project_id) - ->eq('column_id', $board_column_id) - ->neq('id', $task_id) - ->asc('position') - ->findAllByColumn('id'); - } - - // The column must exists - if (! isset($columns[$column_id])) { - return false; - } - - // We put our task to the new position - array_splice($columns[$column_id], $position - 1, 0, $task_id); // print_r($columns); - - // We save the new positions for all tasks - return $this->savePositions($task_id, $columns); - } - - /** - * Save task positions - * - * @access private - * @param integer $moved_task_id Id of the moved task - * @param array $columns Sorted tasks - * @return boolean - */ - private function savePositions($moved_task_id, array $columns) - { - foreach ($columns as $column_id => $column) { - - $position = 1; - - foreach ($column as $task_id) { - - if ($task_id == $moved_task_id) { - - // Events will be triggered only for that task - $result = $this->update(array( - 'id' => $task_id, - 'position' => $position, - 'column_id' => $column_id - )); - } - else { - $result = $this->db->table(self::TABLE)->eq('id', $task_id)->update(array( - 'position' => $position, - 'column_id' => $column_id - )); - } - - $position++; - - if (! $result) { - return false; - } - } - } - - return true; - } - - /** * Move a task to another project * * @access public diff --git a/app/Model/TaskCreation.php b/app/Model/TaskCreation.php index c101f659..320bcb93 100644 --- a/app/Model/TaskCreation.php +++ b/app/Model/TaskCreation.php @@ -21,7 +21,10 @@ class TaskCreation extends Base { $this->prepare($values); $task_id = $this->persist(Task::TABLE, $values); - $this->fireEvents($task_id, $values); + + if ($task_id) { + $this->fireEvents($task_id, $values); + } return (int) $task_id; } @@ -60,10 +63,8 @@ class TaskCreation extends Base */ private function fireEvents($task_id, array $values) { - if ($task_id) { - $values['task_id'] = $task_id; - $this->event->trigger(Task::EVENT_CREATE_UPDATE, $values); - $this->event->trigger(Task::EVENT_CREATE, $values); - } + $values['task_id'] = $task_id; + $this->event->trigger(Task::EVENT_CREATE_UPDATE, $values); + $this->event->trigger(Task::EVENT_CREATE, $values); } } diff --git a/app/Model/TaskPosition.php b/app/Model/TaskPosition.php new file mode 100644 index 00000000..06170509 --- /dev/null +++ b/app/Model/TaskPosition.php @@ -0,0 +1,128 @@ +<?php + +namespace Model; + +/** + * Task Position + * + * @package model + * @author Frederic Guillot + */ +class TaskPosition extends Base +{ + /** + * Move a task to another column or to another position + * + * @access public + * @param integer $project_id Project id + * @param integer $task_id Task id + * @param integer $column_id Column id + * @param integer $position Position (must be >= 1) + * @return boolean + */ + public function movePosition($project_id, $task_id, $column_id, $position) + { + $original_task = $this->taskFinder->getById($task_id); + $positions = $this->calculatePositions($project_id, $task_id, $column_id, $position); + + if ($positions === false || ! $this->savePositions($positions)) { + return false; + } + + $this->fireEvents($original_task, $column_id, $position); + + return true; + } + + /** + * Calculate the new position of all tasks + * + * @access public + * @param integer $project_id Project id + * @param integer $task_id Task id + * @param integer $column_id Column id + * @param integer $position Position (must be >= 1) + * @return array|boolean + */ + public function calculatePositions($project_id, $task_id, $column_id, $position) + { + // The position can't be lower than 1 + if ($position < 1) { + return false; + } + + $board = $this->db->table(Board::TABLE)->eq('project_id', $project_id)->asc('position')->findAllByColumn('id'); + $columns = array(); + + // For each column fetch all tasks ordered by position + foreach ($board as $board_column_id) { + + $columns[$board_column_id] = $this->db->table(Task::TABLE) + ->eq('is_active', 1) + ->eq('project_id', $project_id) + ->eq('column_id', $board_column_id) + ->neq('id', $task_id) + ->asc('position') + ->findAllByColumn('id'); + } + + // The column must exists + if (! isset($columns[$column_id])) { + return false; + } + + // We put our task to the new position + array_splice($columns[$column_id], $position - 1, 0, $task_id); + + return $columns; + } + + /** + * Save task positions + * + * @access private + * @param array $columns Sorted tasks + * @return boolean + */ + private function savePositions(array $columns) + { + return $this->db->transaction(function ($db) use ($columns) { + + foreach ($columns as $column_id => $column) { + + $position = 1; + + foreach ($column as $task_id) { + + $result = $this->db->table(Task::TABLE)->eq('id', $task_id)->update(array( + 'position' => $position, + 'column_id' => $column_id + )); + + if (! $result) { + return false; + } + + $position++; + } + } + }); + } + + public function fireEvents(array $task, $new_column_id, $new_position) + { + $event_data = array( + 'task_id' => $task['id'], + 'project_id' => $task['project_id'], + 'position' => $new_position, + 'column_id' => $new_column_id, + ); + + if ($task['column_id'] != $new_column_id) { + $this->event->trigger(Task::EVENT_MOVE_COLUMN, $event_data); + } + else if ($task['position'] != $new_position) { + $this->event->trigger(Task::EVENT_MOVE_POSITION, $event_data); + } + } +} |