diff options
author | Frederic Guillot <fred@kanboard.net> | 2015-08-22 17:39:37 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2015-08-22 17:39:37 -0400 |
commit | cd9bc86fbeea01fa492606b9c19ded36bf0ab366 (patch) | |
tree | cd7a9a5bca2e661107cdec11a2678fd68f8c9df4 /app/Model/TaskLink.php | |
parent | fd60964c239627d2d55c6eca0888be84a8f6653f (diff) |
Add new automated action to change task color based on the task link
Diffstat (limited to 'app/Model/TaskLink.php')
-rw-r--r-- | app/Model/TaskLink.php | 62 |
1 files changed, 52 insertions, 10 deletions
diff --git a/app/Model/TaskLink.php b/app/Model/TaskLink.php index 3fdbd04b..466e44df 100644 --- a/app/Model/TaskLink.php +++ b/app/Model/TaskLink.php @@ -4,6 +4,7 @@ namespace Model; use SimpleValidator\Validator; use SimpleValidator\Validators; +use Event\TaskLinkEvent; /** * TaskLink model @@ -22,6 +23,13 @@ class TaskLink extends Base const TABLE = 'task_has_links'; /** + * Events + * + * @var string + */ + const EVENT_CREATE_UPDATE = 'tasklink.create_update'; + + /** * Get a task link * * @access public @@ -114,6 +122,20 @@ class TaskLink extends Base } /** + * Publish events + * + * @access private + * @param array $events + */ + private function fireEvents(array $events) + { + foreach ($events as $event) { + $event['project_id'] = $this->taskFinder->getProjectId($event['task_id']); + $this->container['dispatcher']->dispatch(self::EVENT_CREATE_UPDATE, new TaskLinkEvent($event)); + } + } + + /** * Create a new link * * @access public @@ -124,29 +146,37 @@ class TaskLink extends Base */ public function create($task_id, $opposite_task_id, $link_id) { + $events = array(); $this->db->startTransaction(); // Get opposite link $opposite_link_id = $this->link->getOppositeLinkId($link_id); - // Create the original task link - $this->db->table(self::TABLE)->insert(array( + $values = array( 'task_id' => $task_id, 'opposite_task_id' => $opposite_task_id, 'link_id' => $link_id, - )); + ); + // Create the original task link + $this->db->table(self::TABLE)->insert($values); $task_link_id = $this->db->getLastId(); + $events[] = $values; // Create the opposite task link - $this->db->table(self::TABLE)->insert(array( + $values = array( 'task_id' => $opposite_task_id, 'opposite_task_id' => $task_id, 'link_id' => $opposite_link_id, - )); + ); + + $this->db->table(self::TABLE)->insert($values); + $events[] = $values; $this->db->closeTransaction(); + $this->fireEvents($events); + return (int) $task_link_id; } @@ -162,6 +192,7 @@ class TaskLink extends Base */ public function update($task_link_id, $task_id, $opposite_task_id, $link_id) { + $events = array(); $this->db->startTransaction(); // Get original task link @@ -174,22 +205,33 @@ class TaskLink extends Base $opposite_link_id = $this->link->getOppositeLinkId($link_id); // Update the original task link - $rs1 = $this->db->table(self::TABLE)->eq('id', $task_link_id)->update(array( + $values = array( 'task_id' => $task_id, 'opposite_task_id' => $opposite_task_id, 'link_id' => $link_id, - )); + ); + + $rs1 = $this->db->table(self::TABLE)->eq('id', $task_link_id)->update($values); + $events[] = $values; // Update the opposite link - $rs2 = $this->db->table(self::TABLE)->eq('id', $opposite_task_link['id'])->update(array( + $values = array( 'task_id' => $opposite_task_id, 'opposite_task_id' => $task_id, 'link_id' => $opposite_link_id, - )); + ); + + $rs2 = $this->db->table(self::TABLE)->eq('id', $opposite_task_link['id'])->update($values); + $events[] = $values; $this->db->closeTransaction(); - return $rs1 && $rs2; + if ($rs1 && $rs2) { + $this->fireEvents($events); + return true; + } + + return false; } /** |