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
{
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 = <<<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`,
`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`,
`themes`.`title` AS `theme`, `themes`.`thumbnail` AS `theme_thumbnail`, `themes`.`slug` AS `theme_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_sources` AS `sources` ON `sources`.`id` = `news`.`source`
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;
private static $LOAD_IMAGES =
@@ -35,7 +38,7 @@ QUERY;
*/
public function __construct()
{
//
$this->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(<<<QUERY
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 `news_target_content` AS `content` ON `content`.`news` = `news`.`id`
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
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;

View File

@@ -10,7 +10,7 @@ ini_set('display_errors', true);
class ProgramController extends Controller
{
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`.`shortnamesuffix` AS `suffix`, `schedule`.`state` AS `state`
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 {
public $id;
public $title;
public $author;
public $url;
public function __construct($data, $urlPrefix = '/') {

View File

@@ -8,6 +8,7 @@ require "NewsSource.php";
class NewsItem extends Model {
public $id;
public $title;
public $author;
public $content;
public $published;
public $edited;
@@ -29,10 +30,10 @@ class NewsItem extends Model {
parent::ConvertToDateTime($this->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,31 +46,36 @@ 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)) {
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(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(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

View File

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