From 5c5c9e1eeffbe9582a410c83edc4313c5017d733 Mon Sep 17 00:00:00 2001 From: denfil Date: Thu, 24 Dec 2015 15:07:06 +0300 Subject: [PATCH] Include groups into API --- models/feed.php | 21 +++++++++++++++++++-- models/group.php | 26 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/models/feed.php b/models/feed.php index 7f84d3c..8c91c0b 100644 --- a/models/feed.php +++ b/models/feed.php @@ -342,10 +342,18 @@ function count_failed_feeds() // Get all feeds function get_all() { - return Database::getInstance('db') + $result = Database::getInstance('db') ->table('feeds') ->asc('title') ->findAll(); + $groups = Group\get_all_grouped_by_feeds(); + foreach ($result as &$feed) { + $feed['groups'] = isset($groups[$feed['id']]) + ? $groups[$feed['id']] + : array(); + } + + return $result; } // Get all feeds with the number unread/total items in the order failed, working, disabled @@ -397,10 +405,19 @@ function count_items($feed_id) // Get one feed function get($feed_id) { - return Database::getInstance('db') + $result = Database::getInstance('db') ->table('feeds') ->eq('id', $feed_id) ->findOne(); + if (!$result) { + return $result; + } + $groups = Group\get_all_grouped_by_feeds(); + $result['groups'] = isset($groups[$result['id']]) + ? $groups[$result['id']] + : array(); + + return $result; } // Update parsing error column diff --git a/models/group.php b/models/group.php index 31d254a..b9e69a5 100644 --- a/models/group.php +++ b/models/group.php @@ -17,6 +17,32 @@ function get_all() ->findAll(); } +/** + * Get all groups grouped by feed_id + * + * @return array + */ +function get_all_grouped_by_feeds() +{ + $groups = Database::getInstance('db') + ->table('groups') + ->join('feeds_groups', 'group_id', 'id') + ->columns('feed_id', 'group_id', 'title') + ->findAll(); + if (!$groups) { + return array(); + } + $result = array(); + foreach ($groups as $group) { + $result[$group['feed_id']][] = [ + 'group_id' => $group['group_id'], + 'title' => $group['title'] + ]; + } + + return $result; +} + /** * Get assoc array of group ids with assigned feeds ids *