Changes to dev api for new site

This commit is contained in:
2020-01-24 23:37:47 +01:00
parent 1b4eabb75f
commit deb8deb40d
5 changed files with 72 additions and 36 deletions

View File

@@ -6,14 +6,17 @@ use Illuminate\Http\Request;
class NewsController extends Controller class NewsController extends Controller
{ {
private $EXTERNAL_NEWS_API;
private static $NEWS_CATEGORY = 1; private static $NEWS_CATEGORY = 1;
private static $CALENDAR_CATEGORY = 42; private static $CALENDAR_CATEGORY = 42;
private static $EXTERNAL_NEWS_CATEGORY = 44;
private static $BLOG_CATEGORY = 45; private static $BLOG_CATEGORY = 45;
private static $BASE_SQL = <<<QUERY private static $BASE_SQL = <<<QUERY
SELECT `news`.`id`, `content`.`title`, `content`.`content`, `news`.`podcast` AS `podcast_id`, `news`.`video`, `news`.`keywords`, `news`.`titlewithdate`, 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`, `news`.`creationdt` AS `published`, `content`.`creator`, `news`.`pubupdatedt` AS `edited`,
`news`.`startdt` AS `starts`, `news`.`enddt` AS `ends`, `news`.`startdt` AS `starts`, `news`.`enddt` AS `ends`, `content`.`extref` AS `external_id`,
`content`.`showsource` AS `showsource`, `sources`.`title` AS `source`, `sources`.`url` AS `source_url`, `content`.`showsource` AS `showsource`, `sources`.`title` AS `source`, `sources`.`url` AS `source_url`,
`themes`.`title` AS `theme`, `themes`.`thumbnail` AS `theme_thumbnail`, `themes`.`slug` AS `theme_slug`, `themes`.`title` AS `theme`, `themes`.`thumbnail` AS `theme_thumbnail`, `themes`.`slug` AS `theme_slug`,
`regions`.`title` AS `region`, `regions`.`slug` as `region_slug` `regions`.`title` AS `region`, `regions`.`slug` as `region_slug`
@@ -22,7 +25,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_regions` AS `regions` ON `regions`.`id` = `news`.`region`
LEFT JOIN `news_sources` AS `sources` ON `sources`.`id` = `news`.`source` LEFT JOIN `news_sources` AS `sources` ON `sources`.`id` = `news`.`source`
LEFT JOIN `news_themes` AS `themes` ON `themes`.`id` = `news`.`theme` LEFT JOIN `news_themes` AS `themes` ON `themes`.`id` = `news`.`theme`
WHERE `news`.`category` = :category 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` = 1 AND `news`.`active` = 1 AND `content`.`active` = 1 AND `content`.`published` = 1
QUERY; QUERY;
private static $LOAD_IMAGES = private static $LOAD_IMAGES =
@@ -35,7 +38,7 @@ QUERY;
*/ */
public function __construct() public function __construct()
{ {
// $this->EXTERNAL_NEWS_API = env('EXTERNAL_NEWS_API', '//');
} }
/** /**
@@ -49,7 +52,7 @@ QUERY;
} }
$filterSql = ""; $filterSql = "";
$params = ['category' => self::$NEWS_CATEGORY]; $params = ['category' => self::$NEWS_CATEGORY, 'secondarycategory' => self::$EXTERNAL_NEWS_CATEGORY];
if($filter) { if($filter) {
foreach($filter as $field => $value) { foreach($filter as $field => $value) {
$paramName = preg_replace('/[^A-Za-z0-9]/', '', $field); $paramName = preg_replace('/[^A-Za-z0-9]/', '', $field);
@@ -100,6 +103,7 @@ QUERY;
. ' ORDER BY COALESCE(`news`.`pubupdatedt`, `news`.`creationdt`) DESC' . ' ORDER BY COALESCE(`news`.`pubupdatedt`, `news`.`creationdt`) DESC'
. ' LIMIT 0, 20', [ . ' LIMIT 0, 20', [
'category' => self::$NEWS_CATEGORY, 'category' => self::$NEWS_CATEGORY,
'secondarycategory' => self::$EXTERNAL_NEWS_CATEGORY,
'startRange' => $searchRange[0]->format('Y-m-d'), 'startRange' => $searchRange[0]->format('Y-m-d'),
'endRange' => $searchRange[1]->format('Y-m-d'), 'endRange' => $searchRange[1]->format('Y-m-d'),
'query' => $query 'query' => $query
@@ -126,7 +130,7 @@ QUERY;
$today = new \DateTime('today'); $today = new \DateTime('today');
$agendaItems = app('db')->select(self::$BASE_SQL $agendaItems = app('db')->select(self::$BASE_SQL
. ' AND `news`.`enddt` >= :today ' . ' 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(); $result = array();
foreach($agendaItems as $agendaItem) { foreach($agendaItems as $agendaItem) {
@@ -156,7 +160,7 @@ QUERY;
. ' AND `news`.`startdt` >= :start AND `news`.`startdt` <= :end ' . ' AND `news`.`startdt` >= :start AND `news`.`startdt` <= :end '
. ' ORDER BY `news`.`startdt` DESC' . ' ORDER BY `news`.`startdt` DESC'
. ' LIMIT ' . (int)$startIndex . ', ' . (int)$count, . ' 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(); $result = array();
foreach($blogItems as $blogItem) { foreach($blogItems as $blogItem) {
@@ -184,14 +188,14 @@ QUERY;
* Populaire berichten ophalen * Populaire berichten ophalen
*/ */
public function popularNews(Request $request) { 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) { public function popularCalendar(Request $request) {
return $this->popularInCategory($request, self::$CALENDAR_CATEGORY); 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); $count = (int)$request->get('aantal', 5);
$recent = app('db')->select(<<<QUERY $recent = app('db')->select(<<<QUERY
SELECT `news`.`id`, COUNT(DISTINCT pagestats.visitor_ip) AS visitors SELECT `news`.`id`, COUNT(DISTINCT pagestats.visitor_ip) AS visitors
@@ -199,25 +203,28 @@ QUERY;
INNER JOIN pagestats ON pagestats.type = :type AND pagestats.item_id = news.id INNER JOIN pagestats ON pagestats.type = :type AND pagestats.item_id = news.id
INNER JOIN `news_target_content` AS `content` ON `content`.`news` = `news`.`id` INNER JOIN `news_target_content` AS `content` ON `content`.`news` = `news`.`id`
WHERE `news`.`startdt` >= DATE_SUB(CURRENT_TIMESTAMP, INTERVAL :interval DAY) WHERE `news`.`startdt` >= 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 GROUP BY news.id ORDER BY visitors DESC
LIMIT 0, :count LIMIT 0, :count
QUERY QUERY
, [ , [
'type' => $category == self::$NEWS_CATEGORY ? 'nieuws' 'type' => $category == self::$NEWS_CATEGORY ? 'nieuws'
: ( $category == self::$EXTERNAL_NEWS_CATEGORY ? 'nieuws'
: ($category == self::$CALENDAR_CATEGORY ? 'agenda' : ($category == self::$CALENDAR_CATEGORY ? 'agenda'
: ( "" ) ), : ( "" ) ) ),
'interval' => $category == self::$NEWS_CATEGORY ? 30 'interval' => $category == self::$NEWS_CATEGORY ? 30
: ($category == self::$EXTERNAL_NEWS_CATEGORY ? 30
: ($category == self::$CALENDAR_CATEGORY ? 0 : ($category == self::$CALENDAR_CATEGORY ? 0
: 30 ), : 30 ) ),
'category' => $category, 'category' => $category,
'secondarycategory' => $secondarycategory,
'count' => $count 'count' => $count
]); ]);
$result = array(); $result = array();
foreach($recent as $item) 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]); $pictures = app('db')->select(self::$LOAD_IMAGES, ['newsId' => $newsItem->id]);
$result[] = new \Model\NewsItem($newsItem, $pictures); $result[] = new \Model\NewsItem($newsItem, $pictures);
} }
@@ -229,8 +236,22 @@ QUERY
* Specifiek nieuwsbericht ophalen * Specifiek nieuwsbericht ophalen
*/ */
public function newsitem($id) { public function newsitem($id) {
$item = $this->itemFromCategory(self::$NEWS_CATEGORY, $id); $item = $this->itemFromCategory(array(self::$NEWS_CATEGORY, self::$EXTERNAL_NEWS_CATEGORY), $id);
return $item ? response()->json(new \Model\NewsItem($item['data'], $item['images'], $item['podcast'])) : abort(404); 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) { 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 $newsItem = app('db')->select(self::$BASE_SQL
. ' AND `news`.`id` = :id LIMIT 0, 1', . ' AND `news`.`id` = :id LIMIT 0, 1', $params);
['category' => $category, 'id' => (int)$id]);
if(count($newsItem) != 1) { if(count($newsItem) != 1) {
return null; return null;

View File

@@ -10,7 +10,7 @@ ini_set('display_errors', true);
class ProgramController extends Controller class ProgramController extends Controller
{ {
private static $SCHEDULE_SQL = <<<QUERY private static $SCHEDULE_SQL = <<<QUERY
SELECT `schedule`.`id` AS `scheduleid`, `programs`.`id` AS `id`, `programs`.`longname` AS `name`, `programs`.`description` AS `description`, `programs`.`email`, `schedule`.`priority`, SELECT `schedule`.`id` AS `scheduleid`, `programs`.`id` AS `id`, `programs`.`longname` AS `name`, `programs`.`tagline` AS `tagline`, `programs`.`description` AS `description`, `programs`.`email`, `schedule`.`priority`,
`schedule`.`startdate`, `schedule`.`startday`, `schedule`.`starttime`, `schedule`.`enddate`, `schedule`.`endday`, `schedule`.`endtime`, `schedule`.`startdate`, `schedule`.`startday`, `schedule`.`starttime`, `schedule`.`enddate`, `schedule`.`endday`, `schedule`.`endtime`,
`schedule`.`shortnamesuffix` AS `suffix`, `schedule`.`state` AS `state` `schedule`.`shortnamesuffix` AS `suffix`, `schedule`.`state` AS `state`
FROM `programs_schedule` AS `schedule` LEFT JOIN `programs` ON `schedule`.`program` = `programs`.`id` FROM `programs_schedule` AS `schedule` LEFT JOIN `programs` ON `schedule`.`program` = `programs`.`id`

View File

@@ -5,6 +5,7 @@ namespace Model;
class NewsImage extends Model { class NewsImage extends Model {
public $id; public $id;
public $title; public $title;
public $author;
public $url; public $url;
public function __construct($data, $urlPrefix = '/') { public function __construct($data, $urlPrefix = '/') {

View File

@@ -8,6 +8,7 @@ require "NewsSource.php";
class NewsItem extends Model { class NewsItem extends Model {
public $id; public $id;
public $title; public $title;
public $author;
public $content; public $content;
public $published; public $published;
public $edited; public $edited;
@@ -29,10 +30,10 @@ class NewsItem extends Model {
parent::ConvertToDateTime($this->published); parent::ConvertToDateTime($this->published);
parent::ConvertToDateTime($this->edited); parent::ConvertToDateTime($this->edited);
//if($this->edited && ($this->edited->getTimestamp() - $this->published->getTimestamp() < 1800 /* == 30 minutes */)) { 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) // If last edit was within grace period, consider it unedited (note: currently RES always saves edited == published on creation)
// $this->edited = null; $this->edited = null;
//} }
$this->source = null; $this->source = null;
if(isset($data->source)) if(isset($data->source))
@@ -45,30 +46,35 @@ class NewsItem extends Model {
} }
} }
if($podcast) if($podcast) {
{
$this->podcast = new \Model\Podcast($podcast); $this->podcast = new \Model\Podcast($podcast);
} else if(isset($data->podcast) && $data->podcast) { } else if(isset($data->podcast) && $data->podcast) {
$this->podcast = new \Model\Podcast($data->podcast); $this->podcast = new \Model\Podcast($data->podcast);
} }
$this->keywords = null; $this->keywords = null;
if(is_array($data->keywords)) { if(isset($data->keywords)) {
$this->keywords = $data->keywords; if(is_array($data->keywords) || is_object($data->keywords)) {
} else if(trim($data->keywords)) { $this->keywords = $data->keywords;
$this->keywords = explode(' ', $data->keywords); } else if(trim($data->keywords)) {
$this->keywords = explode(' ', $data->keywords);
}
} }
if(is_object($data->region)) { if(isset($data->region)) {
$this->region = new \Model\NewsRegion($data->region->title, $data->region->slug); if(is_object($data->region)) {
} else { $this->region = new \Model\NewsRegion($data->region->title, $data->region->slug);
$this->region = new \Model\NewsRegion($data->region, $data->region_slug); } else {
$this->region = new \Model\NewsRegion($data->region, $data->region_slug);
}
} }
if(is_object($data->theme)) { if(isset($data->theme)) {
$this->theme = new \Model\NewsRegion($data->theme->title, $data->theme->slug); if(is_object($data->theme)) {
} else { $this->theme = new \Model\NewsRegion($data->theme->title, $data->theme->slug);
$this->theme = new \Model\NewsRegion($data->theme, $data->theme_slug); } else {
$this->theme = new \Model\NewsRegion($data->theme, $data->theme_slug);
}
} }

View File

@@ -5,6 +5,7 @@ namespace Model;
class Program extends Model { class Program extends Model {
public $id; public $id;
public $name; public $name;
public $tagline;
public $description; public $description;
public $email; public $email;
public $nonstop; public $nonstop;