Kabelkrant API gemaakt
This commit is contained in:
@@ -12,6 +12,9 @@ class NewsController extends Controller
|
||||
private static $EXTERNAL_NEWS_CATEGORY = 44;
|
||||
private static $BLOG_CATEGORY = 45;
|
||||
|
||||
private static $WEBSITE_TARGET = 1;
|
||||
private static $TV_TARGET = 2;
|
||||
|
||||
private static $BASE_SQL = <<<QUERY
|
||||
SELECT `news`.`id`, `content`.`title`, `content`.`content`, `news`.`podcast` AS `podcast_id`, `news`.`video`, `news`.`keywords`, `news`.`titlewithdate`,
|
||||
`news`.`creationdt` AS `published`, `content`.`creator`, `news`.`pubupdatedt` AS `edited`,
|
||||
@@ -24,7 +27,7 @@ SELECT `news`.`id`, `content`.`title`, `content`.`content`, `news`.`podcast` AS
|
||||
LEFT JOIN `news_regions` AS `regions` ON `regions`.`id` = `news`.`region`
|
||||
LEFT JOIN `news_sources` AS `sources` ON `sources`.`id` = `news`.`source`
|
||||
LEFT JOIN `news_themes` AS `themes` ON `themes`.`id` = `news`.`theme`
|
||||
WHERE (`news`.`category` = :category OR `news`.`category` = :secondarycategory) AND `content`.`target` = 1 AND `news`.`active` = 1 AND `content`.`active` = 1 AND `content`.`published` = 1
|
||||
WHERE (`news`.`category` = :category OR `news`.`category` = :secondarycategory) AND `content`.`target` = :target AND `news`.`active` = 1 AND `content`.`active` = 1 AND `content`.`published` = 1
|
||||
QUERY;
|
||||
|
||||
private static $LOAD_IMAGES =
|
||||
@@ -67,10 +70,38 @@ QUERY;
|
||||
$result = $this->retrieveNewsItems($page, $count, $filter, []);
|
||||
return response()->json(['page' => $page, 'count' => $count, 'news' => $result]);
|
||||
}
|
||||
/**
|
||||
* Lijst van alle nieuwsberichten voor de kabelkrant
|
||||
*/
|
||||
public function tvlist(Request $request) {
|
||||
$count = (int)$request->get('aantal', 15);
|
||||
if($count <= 0) {
|
||||
return abort(400);
|
||||
}
|
||||
|
||||
$params = ['target' => self::$TV_TARGET, 'category' => self::$NEWS_CATEGORY, 'secondarycategory' => self::$EXTERNAL_NEWS_CATEGORY];
|
||||
$newsItems = app('db')->select(self::$BASE_SQL
|
||||
. ' ORDER BY COALESCE(`news`.`pubupdatedt`, `news`.`creationdt`) DESC'
|
||||
. ' LIMIT 0, ' . (int)$count, $params);
|
||||
|
||||
$result = array();
|
||||
foreach($newsItems as $newsItem) {
|
||||
$pictures = app('db')->select(self::$LOAD_IMAGES, ['newsId' => $newsItem->id]);
|
||||
$newsItem = new \Model\NewsItem($newsItem, $pictures);
|
||||
$newsItem->content = strip_tags($newsItem->content);
|
||||
if($newsItem->images && count($newsItem->images) > 1) {
|
||||
$newsItem->images = [$newsItem->images[0]];
|
||||
}
|
||||
|
||||
$result[] = $newsItem;
|
||||
}
|
||||
|
||||
return response()->json(['news' => $result]);
|
||||
}
|
||||
|
||||
public function retrieveNewsItems($page, $count, $filter, $params) {
|
||||
$filterSql = "";
|
||||
$params = ['category' => self::$NEWS_CATEGORY, 'secondarycategory' => self::$EXTERNAL_NEWS_CATEGORY];
|
||||
$params = ['target' => self::$WEBSITE_TARGET, 'category' => self::$NEWS_CATEGORY, 'secondarycategory' => self::$EXTERNAL_NEWS_CATEGORY];
|
||||
if($filter) {
|
||||
foreach($filter as $field => $value) {
|
||||
$paramName = preg_replace('/[^A-Za-z0-9]/', '', $field);
|
||||
@@ -143,7 +174,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, 'secondarycategory' => 0, 'today' => $today]);
|
||||
. ' ORDER BY `news`.`startdt` ASC, `news`.`enddt` ASC', ['target' => self::$WEBSITE_TARGET, 'category' => self::$CALENDAR_CATEGORY, 'secondarycategory' => 0, 'today' => $today]);
|
||||
|
||||
$result = array();
|
||||
foreach($agendaItems as $agendaItem) {
|
||||
@@ -192,7 +223,7 @@ QUERY;
|
||||
. ' AND `news`.`startdt` >= :start AND `news`.`startdt` <= :end'
|
||||
. ' ORDER BY COALESCE(`news`.`pubupdatedt`, `news`.`creationdt`) DESC'
|
||||
. ' LIMIT ' . (int)$startIndex . ', ' . (int)$count,
|
||||
['category' => $blog->news_category, 'secondarycategory' => 0, 'start' => $blog->start_date, 'end' => $blog->end_date]);
|
||||
['target' => self::$WEBSITE_TARGET, 'category' => $blog->news_category, 'secondarycategory' => 0, 'start' => $blog->start_date, 'end' => $blog->end_date]);
|
||||
|
||||
$result = array();
|
||||
foreach($blogItems as $blogItem) {
|
||||
@@ -256,7 +287,7 @@ QUERY
|
||||
$result = array();
|
||||
foreach($recent as $item)
|
||||
{
|
||||
$newsItem = app('db')->select(self::$BASE_SQL . ' AND `news`.`id` = :newsId', ['category' => $category, 'secondarycategory' => $secondarycategory, 'newsId' => $item->id])[0];
|
||||
$newsItem = app('db')->select(self::$BASE_SQL . ' AND `news`.`id` = :newsId', ['target' => self::$WEBSITE_TARGET, 'category' => $category, 'secondarycategory' => $secondarycategory, 'newsId' => $item->id])[0];
|
||||
$pictures = app('db')->select(self::$LOAD_IMAGES, ['newsId' => $newsItem->id]);
|
||||
$result[] = new \Model\NewsItem($newsItem, $pictures);
|
||||
}
|
||||
@@ -296,7 +327,7 @@ QUERY
|
||||
}
|
||||
|
||||
private function itemFromCategory($category, $id) {
|
||||
$params = ['id' => (int)$id];
|
||||
$params = ['id' => (int)$id, 'target' => self::$WEBSITE_TARGET];
|
||||
if(is_array($category)) {
|
||||
$params['category'] = $category[0];
|
||||
$params['secondarycategory'] = $category[1];
|
||||
|
||||
@@ -22,6 +22,7 @@ $app->get( 'rss/nieuws', 'NewsController@rss' );
|
||||
$app->get( 'menu/special', 'MenuController@special' );
|
||||
|
||||
$app->get('nieuws/overzicht', 'NewsController@newslist' );
|
||||
$app->get('nieuws/kabelkrant', 'NewsController@tvlist' );
|
||||
$app->get('nieuws/regio/{region:[a-z0-9-]+}', 'NewsController@regionlist' );
|
||||
$app->get('nieuws/thema/{theme:[a-z0-9-]+}', 'NewsController@themelist' );
|
||||
$app->get('nieuws/zoeken/{query}', 'NewsController@findnews' );
|
||||
|
||||
Reference in New Issue
Block a user