From 8d9b87bb6ee084d2ee71e8ca17cf2537dba87d7f Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Mon, 30 Mar 2015 21:58:16 -0400 Subject: Avoid code duplication in pull-request #746 --- app/Controller/Task.php | 7 ++- app/Model/ProjectActivity.php | 117 +++++++++++++++++++------------------- app/Template/app/dashboard.php | 2 +- app/Template/event/events.php | 21 +++++++ app/Template/project/activity.php | 2 +- app/Template/project/events.php | 21 ------- app/Template/task/activity.php | 5 ++ app/Template/task/events.php | 25 -------- 8 files changed, 90 insertions(+), 110 deletions(-) create mode 100644 app/Template/event/events.php delete mode 100644 app/Template/project/events.php create mode 100644 app/Template/task/activity.php delete mode 100644 app/Template/task/events.php diff --git a/app/Controller/Task.php b/app/Controller/Task.php index 9bd6e711..3c5b5c39 100644 --- a/app/Controller/Task.php +++ b/app/Controller/Task.php @@ -92,14 +92,15 @@ class Task extends Base public function activites() { $task = $this->getTask(); - $this->response->html($this->taskLayout('task/events', array( + + $this->response->html($this->taskLayout('task/activity', array( 'title' => $task['title'], 'task' => $task, 'ajax' => $this->request->isAjax(), - 'events' => $this->projectActivity->getTasks([$task['id']]), + 'events' => $this->projectActivity->getTask($task['id']), ))); } - + /** * Display a form to create a new task * diff --git a/app/Model/ProjectActivity.php b/app/Model/ProjectActivity.php index 05081a47..27f1cfcd 100644 --- a/app/Model/ProjectActivity.php +++ b/app/Model/ProjectActivity.php @@ -50,52 +50,6 @@ class ProjectActivity extends Base return $this->db->table(self::TABLE)->insert($values); } - /** - * Get all events for the given task - * - * @access public - * @param integer[] $task_ids Task ids - * @param integer $limit Maximum events number - * @param integer $start Timestamp of earliest activity - * @param integer $end Timestamp of latest activity - * @return array - */ - public function getTasks(array $task_ids, $limit = 50, $start = null, $end = null) - { - $query = $this->db->table(self::TABLE) - ->columns( - self::TABLE.'.*', - User::TABLE.'.username AS author_username', - User::TABLE.'.name AS author_name' - ) - ->in('task_id', $task_ids) - ->join(User::TABLE, 'id', 'creator_id') - ->desc(self::TABLE.'.id') - ->limit($limit); - - if(!is_null($start)){ - $query->gte('date_creation', $start); - } - - if(!is_null($end)){ - $query->lte('date_creation', $end); - } - - $events = $query->findAll(); - - foreach ($events as &$event) { - - $event += $this->decode($event['data']); - unset($event['data']); - - $event['author'] = $event['author_name'] ?: $event['author_username']; - $event['event_title'] = $this->getTitle($event); - $event['event_content'] = $this->getContent($event); - } - - return $events; - } - /** * Get all events for the given project * @@ -127,23 +81,68 @@ class ProjectActivity extends Base return array(); } - $query = $this->db->table(self::TABLE) - ->columns( - self::TABLE.'.*', - User::TABLE.'.username AS author_username', - User::TABLE.'.name AS author_name', - User::TABLE.'.email' - ) - ->in('project_id', $project_ids) - ->join(User::TABLE, 'id', 'creator_id') - ->desc(self::TABLE.'.id') - ->limit($limit); + $query = $this + ->db + ->table(self::TABLE) + ->columns( + self::TABLE.'.*', + User::TABLE.'.username AS author_username', + User::TABLE.'.name AS author_name', + User::TABLE.'.email' + ) + ->in('project_id', $project_ids) + ->join(User::TABLE, 'id', 'creator_id') + ->desc(self::TABLE.'.id') + ->limit($limit); + + return $this->getEvents($query, $start, $end); + } + + /** + * Get all events for the given task + * + * @access public + * @param integer $task_id Task id + * @param integer $limit Maximum events number + * @param integer $start Timestamp of earliest activity + * @param integer $end Timestamp of latest activity + * @return array + */ + public function getTask($task_id, $limit = 50, $start = null, $end = null) + { + $query = $this + ->db + ->table(self::TABLE) + ->columns( + self::TABLE.'.*', + User::TABLE.'.username AS author_username', + User::TABLE.'.name AS author_name', + User::TABLE.'.email' + ) + ->eq('task_id', $task_id) + ->join(User::TABLE, 'id', 'creator_id') + ->desc(self::TABLE.'.id') + ->limit($limit); + + return $this->getEvents($query, $start, $end); + } - if (!is_null($start)){ + /** + * Common function to return events + * + * @access public + * @param \PicoDb\Table $query PicoDb Query + * @param integer $start Timestamp of earliest activity + * @param integer $end Timestamp of latest activity + * @return array + */ + private function getEvents(\PicoDb\Table $query, $start, $end) + { + if (! is_null($start)){ $query->gte('date_creation', $start); } - if (!is_null($end)){ + if (! is_null($end)){ $query->lte('date_creation', $end); } diff --git a/app/Template/app/dashboard.php b/app/Template/app/dashboard.php index 5b83540c..a832345b 100644 --- a/app/Template/app/dashboard.php +++ b/app/Template/app/dashboard.php @@ -53,7 +53,7 @@

- render('project/events', array('events' => $events)) ?> + render('event/events', array('events' => $events)) ?>
diff --git a/app/Template/event/events.php b/app/Template/event/events.php new file mode 100644 index 00000000..93bc6584 --- /dev/null +++ b/app/Template/event/events.php @@ -0,0 +1,21 @@ + +

+ + + +
+

+ contains($event['event_name'], 'subtask')): ?> + + contains($event['event_name'], 'task')): ?> + + contains($event['event_name'], 'comment')): ?> + + +   +

+
+
+ + + \ No newline at end of file diff --git a/app/Template/project/activity.php b/app/Template/project/activity.php index bc4eb19c..7da6be5c 100644 --- a/app/Template/project/activity.php +++ b/app/Template/project/activity.php @@ -23,6 +23,6 @@
- render('project/events', array('events' => $events)) ?> + render('event/events', array('events' => $events)) ?>
\ No newline at end of file diff --git a/app/Template/project/events.php b/app/Template/project/events.php deleted file mode 100644 index 93bc6584..00000000 --- a/app/Template/project/events.php +++ /dev/null @@ -1,21 +0,0 @@ - -

- - - -
-

- contains($event['event_name'], 'subtask')): ?> - - contains($event['event_name'], 'task')): ?> - - contains($event['event_name'], 'comment')): ?> - - -   -

-
-
- - - \ No newline at end of file diff --git a/app/Template/task/activity.php b/app/Template/task/activity.php new file mode 100644 index 00000000..cc4aad03 --- /dev/null +++ b/app/Template/task/activity.php @@ -0,0 +1,5 @@ + + +render('event/events', array('events' => $events)) ?> \ No newline at end of file diff --git a/app/Template/task/events.php b/app/Template/task/events.php deleted file mode 100644 index 188f46fe..00000000 --- a/app/Template/task/events.php +++ /dev/null @@ -1,25 +0,0 @@ - - - -

- - - -
-

- contains($event['event_name'], 'subtask')): ?> - - contains($event['event_name'], 'task')): ?> - - contains($event['event_name'], 'comment')): ?> - - -   -

-
-
- - - \ No newline at end of file -- cgit v1.2.3