From deb8deb40d55cb207080b2a0de91996315e1b514 Mon Sep 17 00:00:00 2001 From: Mischa Spelt Date: Fri, 24 Jan 2020 23:37:47 +0100 Subject: [PATCH] Changes to dev api for new site --- api/app/Http/Controllers/NewsController.php | 60 ++++++++++++++----- .../Http/Controllers/ProgramController.php | 2 +- common/classes/NewsImage.php | 1 + common/classes/NewsItem.php | 44 ++++++++------ common/classes/Program.php | 1 + 5 files changed, 72 insertions(+), 36 deletions(-) diff --git a/api/app/Http/Controllers/NewsController.php b/api/app/Http/Controllers/NewsController.php index 947d12b..2ef9a59 100644 --- a/api/app/Http/Controllers/NewsController.php +++ b/api/app/Http/Controllers/NewsController.php @@ -6,14 +6,17 @@ use Illuminate\Http\Request; class NewsController extends Controller { + private $EXTERNAL_NEWS_API; + private static $NEWS_CATEGORY = 1; private static $CALENDAR_CATEGORY = 42; + private static $EXTERNAL_NEWS_CATEGORY = 44; private static $BLOG_CATEGORY = 45; private static $BASE_SQL = <<EXTERNAL_NEWS_API = env('EXTERNAL_NEWS_API', '//'); } /** @@ -49,7 +52,7 @@ QUERY; } $filterSql = ""; - $params = ['category' => self::$NEWS_CATEGORY]; + $params = ['category' => self::$NEWS_CATEGORY, 'secondarycategory' => self::$EXTERNAL_NEWS_CATEGORY]; if($filter) { foreach($filter as $field => $value) { $paramName = preg_replace('/[^A-Za-z0-9]/', '', $field); @@ -100,6 +103,7 @@ QUERY; . ' ORDER BY COALESCE(`news`.`pubupdatedt`, `news`.`creationdt`) DESC' . ' LIMIT 0, 20', [ 'category' => self::$NEWS_CATEGORY, + 'secondarycategory' => self::$EXTERNAL_NEWS_CATEGORY, 'startRange' => $searchRange[0]->format('Y-m-d'), 'endRange' => $searchRange[1]->format('Y-m-d'), 'query' => $query @@ -126,7 +130,7 @@ QUERY; $today = new \DateTime('today'); $agendaItems = app('db')->select(self::$BASE_SQL . ' AND `news`.`enddt` >= :today ' - . ' ORDER BY `news`.`startdt` ASC, `news`.`enddt` ASC', ['category' => self::$CALENDAR_CATEGORY, 'today' => $today]); + . ' ORDER BY `news`.`startdt` ASC, `news`.`enddt` ASC', ['category' => self::$CALENDAR_CATEGORY, 'secondarycategory' => 0, 'today' => $today]); $result = array(); foreach($agendaItems as $agendaItem) { @@ -156,7 +160,7 @@ QUERY; . ' AND `news`.`startdt` >= :start AND `news`.`startdt` <= :end ' . ' ORDER BY `news`.`startdt` DESC' . ' LIMIT ' . (int)$startIndex . ', ' . (int)$count, - ['category' => self::$BLOG_CATEGORY, 'start' => $from, 'end' => $to]); + ['category' => self::$BLOG_CATEGORY, 'secondarycategory' => 0, 'start' => $from, 'end' => $to]); $result = array(); foreach($blogItems as $blogItem) { @@ -184,14 +188,14 @@ QUERY; * Populaire berichten ophalen */ public function popularNews(Request $request) { - return $this->popularInCategory($request, self::$NEWS_CATEGORY); + return $this->popularInCategory($request, self::$NEWS_CATEGORY, self::$EXTERNAL_NEWS_CATEGORY); } public function popularCalendar(Request $request) { return $this->popularInCategory($request, self::$CALENDAR_CATEGORY); } - protected function popularInCategory(Request $request, $category) { + protected function popularInCategory(Request $request, $category, $secondarycategory = -1) { $count = (int)$request->get('aantal', 5); $recent = app('db')->select(<<= DATE_SUB(CURRENT_TIMESTAMP, INTERVAL :interval DAY) - AND `news`.`category` = :category AND `content`.`target` = 1 AND `news`.`active` = 1 AND `content`.`active` = 1 + AND (`news`.`category` = :category OR `news`.`category` = :secondarycategory) AND `content`.`target` = 1 AND `news`.`active` = 1 AND `content`.`active` = 1 GROUP BY news.id ORDER BY visitors DESC LIMIT 0, :count QUERY , [ 'type' => $category == self::$NEWS_CATEGORY ? 'nieuws' + : ( $category == self::$EXTERNAL_NEWS_CATEGORY ? 'nieuws' : ($category == self::$CALENDAR_CATEGORY ? 'agenda' - : ( "" ) ), + : ( "" ) ) ), 'interval' => $category == self::$NEWS_CATEGORY ? 30 + : ($category == self::$EXTERNAL_NEWS_CATEGORY ? 30 : ($category == self::$CALENDAR_CATEGORY ? 0 - : 30 ), + : 30 ) ), 'category' => $category, + 'secondarycategory' => $secondarycategory, 'count' => $count ]); $result = array(); foreach($recent as $item) { - $newsItem = app('db')->select(self::$BASE_SQL . ' AND `news`.`id` = :newsId', ['category' => $category, 'newsId' => $item->id])[0]; + $newsItem = app('db')->select(self::$BASE_SQL . ' AND `news`.`id` = :newsId', ['category' => $category, 'secondarycategory' => $secondarycategory, 'newsId' => $item->id])[0]; $pictures = app('db')->select(self::$LOAD_IMAGES, ['newsId' => $newsItem->id]); $result[] = new \Model\NewsItem($newsItem, $pictures); } @@ -229,8 +236,22 @@ QUERY * Specifiek nieuwsbericht ophalen */ public function newsitem($id) { - $item = $this->itemFromCategory(self::$NEWS_CATEGORY, $id); - return $item ? response()->json(new \Model\NewsItem($item['data'], $item['images'], $item['podcast'])) : abort(404); + $item = $this->itemFromCategory(array(self::$NEWS_CATEGORY, self::$EXTERNAL_NEWS_CATEGORY), $id); + if(!$item) { + return abort(404); + } + + $data = array('version' => 1, + 'news' => new \Model\NewsItem($item['data'], $item['images'], $item['podcast'])); + if($externalId = (int)$item['data']->external_id) { + $externalSource = json_decode(file_get_contents($this->EXTERNAL_NEWS_API . 'news?source=api&externalid=' . (int)$externalId)); + if($externalSource) { + $data['version'] = 2; + $data['source'] = $externalSource->news; + } + } + + return response()->json($data); } /** @@ -242,9 +263,16 @@ QUERY } private function itemFromCategory($category, $id) { + $params = ['id' => (int)$id]; + if(is_array($category)) { + $params['category'] = $category[0]; + $params['secondarycategory'] = $category[1]; + } else { + $params['category'] = $category; + $params['secondarycategory'] = 0; + } $newsItem = app('db')->select(self::$BASE_SQL - . ' AND `news`.`id` = :id LIMIT 0, 1', - ['category' => $category, 'id' => (int)$id]); + . ' AND `news`.`id` = :id LIMIT 0, 1', $params); if(count($newsItem) != 1) { return null; diff --git a/api/app/Http/Controllers/ProgramController.php b/api/app/Http/Controllers/ProgramController.php index a93b400..706c718 100644 --- a/api/app/Http/Controllers/ProgramController.php +++ b/api/app/Http/Controllers/ProgramController.php @@ -10,7 +10,7 @@ ini_set('display_errors', true); class ProgramController extends Controller { private static $SCHEDULE_SQL = <<published); parent::ConvertToDateTime($this->edited); - //if($this->edited && ($this->edited->getTimestamp() - $this->published->getTimestamp() < 1800 /* == 30 minutes */)) { - // // If last edit was within grace period, consider it unedited (note: currently RES always saves edited == published on creation) - // $this->edited = null; - //} + if($this->edited && ($this->edited->getTimestamp() - $this->published->getTimestamp() < 1800 /* == 30 minutes */)) { + // If last edit was within grace period, consider it unedited (note: currently RES always saves edited == published on creation) + $this->edited = null; + } $this->source = null; if(isset($data->source)) @@ -45,32 +46,37 @@ class NewsItem extends Model { } } - if($podcast) - { + if($podcast) { $this->podcast = new \Model\Podcast($podcast); } else if(isset($data->podcast) && $data->podcast) { $this->podcast = new \Model\Podcast($data->podcast); } $this->keywords = null; - if(is_array($data->keywords)) { - $this->keywords = $data->keywords; - } else if(trim($data->keywords)) { - $this->keywords = explode(' ', $data->keywords); + if(isset($data->keywords)) { + if(is_array($data->keywords) || is_object($data->keywords)) { + $this->keywords = $data->keywords; + } else if(trim($data->keywords)) { + $this->keywords = explode(' ', $data->keywords); + } } - if(is_object($data->region)) { - $this->region = new \Model\NewsRegion($data->region->title, $data->region->slug); - } else { - $this->region = new \Model\NewsRegion($data->region, $data->region_slug); + if(isset($data->region)) { + if(is_object($data->region)) { + $this->region = new \Model\NewsRegion($data->region->title, $data->region->slug); + } else { + $this->region = new \Model\NewsRegion($data->region, $data->region_slug); + } } - if(is_object($data->theme)) { - $this->theme = new \Model\NewsRegion($data->theme->title, $data->theme->slug); - } else { - $this->theme = new \Model\NewsRegion($data->theme, $data->theme_slug); + if(isset($data->theme)) { + if(is_object($data->theme)) { + $this->theme = new \Model\NewsRegion($data->theme->title, $data->theme->slug); + } else { + $this->theme = new \Model\NewsRegion($data->theme, $data->theme_slug); + } } - + $images = ($images != null) ? $images : (isset($data->images) ? $data->images : null); diff --git a/common/classes/Program.php b/common/classes/Program.php index 1f70686..845f0fc 100644 --- a/common/classes/Program.php +++ b/common/classes/Program.php @@ -5,6 +5,7 @@ namespace Model; class Program extends Model { public $id; public $name; + public $tagline; public $description; public $email; public $nonstop;