From 4f3490367f2d48ea1fe37af81743472aa2cf180d Mon Sep 17 00:00:00 2001 From: Mischa Spelt Date: Wed, 2 Aug 2017 23:58:49 +0200 Subject: [PATCH] Calendar added --- api/app/Http/Controllers/AgendaController.php | 119 ------------------ api/app/Http/Controllers/NewsController.php | 67 +++++++--- api/routes/web.php | 11 +- common/classes/CalendarEvent.php | 15 ++- common/classes/Model.php | 2 +- common/classes/NewsItem.php | 4 - 6 files changed, 70 insertions(+), 148 deletions(-) delete mode 100644 api/app/Http/Controllers/AgendaController.php diff --git a/api/app/Http/Controllers/AgendaController.php b/api/app/Http/Controllers/AgendaController.php deleted file mode 100644 index cca3894..0000000 --- a/api/app/Http/Controllers/AgendaController.php +++ /dev/null @@ -1,119 +0,0 @@ - 0) { - $until = 'CURRENT_DATE() + INTERVAL ' . $daysAhead . ' DAY'; - $sql .= ' AND (`startdt` <= ' . $until . ' AND `enddt` >= CURRENT_DATE())'; - } else { - $sql .= ' AND `enddt` >= CURRENT_DATE()'; - } - - $calendarItems = app('db')->select($sql - . ' ORDER BY `startdt` ASC'); - - $result = array(); - foreach($calendarItems as $calendarItem) { - $pictures = app('db')->select(self::$LOAD_IMAGES, ['newsId' => $calendarItem->id]); - $result[] = new \Model\CalendarEvent($calendarItem, $pictures); - } - - return response()->json($result); - } - - /** - * Lijst van alle agendaberichten in de komende 7 dagen - */ - public function overviewWeek() { - return $this->overview(7); - } - - /** - * Lijst van alle agendaberichten in de komende 30 dagen - */ - public function overviewMonth() { - return $this->overview(30); - } - - /** - * Lijst van items per kalendermaand gegroepeerd op datum - */ - public function calendar(int $year = 0, int $month = 0) { - if($year == 0 && $month == 0) { - $year = date('Y'); - $month = date('m'); - } - - if($year < 2000 || $year > 2080 || $month < 1 || $month > 12) { - return abort(400); - } - - $firstOfMonth = "$year-$month-01"; // Parameters are validated by here - $calendarItems = app('db')->select(self::$BASE_SQL - . ' AND (`startdt` <= ? + INTERVAL 1 MONTH) AND `enddt` >= ?' - . ' ORDER BY `startdt` ASC', - [$firstOfMonth, $firstOfMonth]); - - $items = array(); - $days = array(); - $ONE_DAY = new \DateInterval('P1D'); - foreach($calendarItems as $calendarItem) { - $item = new \Model\CalendarEvent($calendarItem); - foreach(new \DatePeriod($item->starts, $ONE_DAY, (clone $item->ends)->add($ONE_DAY)) as $day) { - $days[$day->format('Y-m-d')][] = $item->id; - } - - $items[] = $item; - } - - return array("items" => $items, "days" => $days); - } - - /** - * Specifiek bericht ophalen - */ - public function item(int $id) { - $calendarItem = app('db')->select(self::$BASE_SQL - . ' AND `news`.`id` = :id LIMIT 0, 1', - ['id' => $id]); - - if(count($calendarItem) != 1) { - return abort(404); - } else { - $pictures = app('db')->select(self::$LOAD_IMAGES, ['newsId' => $id]); - return response()->json(new \Model\CalendarEvent($calendarItem[0])); - } - } -} diff --git a/api/app/Http/Controllers/NewsController.php b/api/app/Http/Controllers/NewsController.php index 65e85d9..75d5035 100644 --- a/api/app/Http/Controllers/NewsController.php +++ b/api/app/Http/Controllers/NewsController.php @@ -10,9 +10,12 @@ ini_set('display_errors', true); class NewsController extends Controller { // TODO: Include podcast + private static $NEWS_CATEGORY = 1; + private static $CALENDAR_CATEGORY = 42; private static $BASE_SQL = <<get('aantal', 15); $page = (int)$request->get('pagina', 1); if($count <= 0 || $page <= 0) { @@ -49,34 +52,66 @@ QUERY; $start = ($page - 1) * $count; $newsItems = app('db')->select(self::$BASE_SQL . ' ORDER BY `published` DESC' - . ' LIMIT ' . (int)$start . ', ' . (int)$count); + . ' LIMIT ' . (int)$start . ', ' . (int)$count, ['category' => self::$NEWS_CATEGORY]); - $result = array(); - foreach($newsItems as $newsItem) { - // Note: content is stored in the database with an additional addslashes() - don't ask why, just remove it :) - $newsItem->content = stripslashes($newsItem->content); - $pictures = app('db')->select(self::$LOAD_IMAGES, ['newsId' => $newsItem->id]); - $result[] = new \Model\NewsItem($newsItem, $pictures); - } + $result = array(); + foreach($newsItems as $newsItem) { + // Note: content is stored in the database with an additional addslashes() - don't ask why, just remove it :) + $newsItem->content = stripslashes($newsItem->content); + $pictures = app('db')->select(self::$LOAD_IMAGES, ['newsId' => $newsItem->id]); + $result[] = new \Model\NewsItem($newsItem, $pictures); + } - return response()->json(['page' => $page, 'count' => $count, 'news' => $result]); + return response()->json(['page' => $page, 'count' => $count, 'news' => $result]); } /** - * Specifiek bericht ophalen + * Agendaberichten ophalen */ - public function item($id) { + public function calendarlist(Request $request) { + $agendaItems = app('db')->select(self::$BASE_SQL + . ' AND `news`.`enddt` >= CURRENT_DATE() ' + . ' ORDER BY `news`.`startdt` ASC, `news`.`enddt` ASC', ['category' => self::$CALENDAR_CATEGORY]); + + $result = array(); + foreach($agendaItems as $agendaItem) { + // Note: content is stored in the database with an additional addslashes() - don't ask why, just remove it :) + $agendaItem->content = stripslashes($agendaItem->content); + $pictures = app('db')->select(self::$LOAD_IMAGES, ['newsId' => $agendaItem->id]); + $result[] = new \Model\CalendarEvent($agendaItem, $pictures); + } + + return response()->json($result); + } + + /** + * 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'])) : abort(404); + } + + /** + * Specifiek agendaitem ophalen + */ + public function calendaritem($id) { + $item = $this->itemFromCategory(self::$CALENDAR_CATEGORY, $id); + return $item ? response()->json(new \Model\CalendarEvent($item['data'], $item['images'])) : abort(404); + } + + private function itemFromCategory($category, $id) { $newsItem = app('db')->select(self::$BASE_SQL . ' AND `news`.`id` = :id LIMIT 0, 1', - ['id' => (int)$id]); + ['category' => $category, 'id' => (int)$id]); if(count($newsItem) != 1) { - return abort(404); + return null; } else { // Note: content is stored in the database with an additional addslashes() - don't ask why, just remove it :) $newsItem[0]->content = stripslashes($newsItem[0]->content); $images = app('db')->select(self::$LOAD_IMAGES, ['newsId' => $id]); - return response()->json(new \Model\NewsItem($newsItem[0], $images)); + return ['data' => $newsItem[0], 'images' => $images]; } } } diff --git a/api/routes/web.php b/api/routes/web.php index 4919dde..fda8c46 100644 --- a/api/routes/web.php +++ b/api/routes/web.php @@ -15,14 +15,11 @@ $app->get('/', function () use ($app) { return redirect('docs'); }); -$app->get('nieuws/overzicht', 'NewsController@overview' ); -$app->get('nieuws/bericht/{id:\d+}', 'NewsController@item' ); +$app->get('nieuws/overzicht', 'NewsController@newslist' ); +$app->get('nieuws/bericht/{id:\d+}', 'NewsController@newsitem' ); -$app->get('agenda/overzicht[/week]', 'AgendaController@overviewWeek' ); -$app->get('agenda/overzicht/maand', 'AgendaController@overviewMonth' ); -$app->get('agenda/overzicht/alles', 'AgendaController@overview' ); -$app->get('agenda/kalender[/{year:\d\d\d\d}/{month:\d\d?}]', 'AgendaController@calendar' ); -$app->get('agenda/details/{id:\d+}', 'AgendaController@item' ); +$app->get('agenda/overzicht[/week]', 'NewsController@calendarlist' ); +$app->get('agenda/item/{id:\d+}', 'NewsController@calendaritem' ); $app->get('podcast/overzicht', 'PodcastController@overview' ); $app->get('podcast/details/{id:\d+}', 'PodcastController@details' ); diff --git a/common/classes/CalendarEvent.php b/common/classes/CalendarEvent.php index bbafc34..960356d 100644 --- a/common/classes/CalendarEvent.php +++ b/common/classes/CalendarEvent.php @@ -10,13 +10,26 @@ class CalendarEvent extends Model { public $starts; public $ends; public $images; + public $podcast; + public $keywords; public $url; public function __construct($data, $images = null) { parent::__construct($data); parent::ConvertToDateTime($this->starts); parent::ConvertToDateTime($this->ends); - + + $this->keywords = null; + if(isset($data->keywords)) { + if(is_array($data->keywords)) { + $this->keywords = $data->keywords; + } else if(trim($data->keywords)) { + $this->keywords = explode(' ', $data->keywords); + } + } + + $this->podcast = isset($data->podcast) && $data->podcast ? $data->podcast : null; + $images = ($images != null) ? $images : (isset($data->images) ? $data->images : null); if($images) { diff --git a/common/classes/Model.php b/common/classes/Model.php index ff13e62..60f2fb3 100644 --- a/common/classes/Model.php +++ b/common/classes/Model.php @@ -16,7 +16,7 @@ class Model { $field = new \DateTime($field); } - if($field === false || $field->getTimestamp() == 0) { + if($field === false || $field->getTimestamp() <= 0) { // If field had data but is invalid DateTime or is 0000-00-00 00:00, set it to null. $field = null; } else { diff --git a/common/classes/NewsItem.php b/common/classes/NewsItem.php index 778db72..e7056fc 100644 --- a/common/classes/NewsItem.php +++ b/common/classes/NewsItem.php @@ -63,10 +63,6 @@ class NewsItem extends Model { $this->url = "/nieuws/{$this->id}/" . parent::url_slug($this->title); } - public function detailsUrl() { - return "/{$this->id}/" . urlencode($this->title); - } - public function excerpt() { $hasImages = count($this->images) > 0; $maxLength = $hasImages ? 200 : 500;