From cd9bc86fbeea01fa492606b9c19ded36bf0ab366 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 22 Aug 2015 17:39:37 -0400 Subject: Add new automated action to change task color based on the task link --- app/Model/TaskLink.php | 62 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 10 deletions(-) (limited to 'app/Model/TaskLink.php') 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 @@ -21,6 +22,13 @@ class TaskLink extends Base */ const TABLE = 'task_has_links'; + /** + * Events + * + * @var string + */ + const EVENT_CREATE_UPDATE = 'tasklink.create_update'; + /** * Get a task link * @@ -113,6 +121,20 @@ class TaskLink extends Base return $result; } + /** + * 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 * @@ -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; } /** -- cgit v1.2.3