Calendar added
This commit is contained in:
@@ -1,119 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', true);
|
||||
|
||||
class AgendaController extends Controller
|
||||
{
|
||||
// TODO: Include podcast
|
||||
private static $BASE_SQL = <<<QUERY
|
||||
SELECT `news`.`id`, `content`.`title`, `content`.`content`, `news`.`startdt` AS `starts`, `news`.`enddt` AS `ends`, `regions`.`title` AS `region`
|
||||
FROM `news`
|
||||
LEFT JOIN `news_target_content` AS `content` ON `content`.`news` = `news`.`id`
|
||||
LEFT JOIN `news_regions` AS `regions` ON `regions`.`id` = `news`.`region`
|
||||
WHERE `news`.`category` = 42 AND `content`.`target` = 1 AND `news`.`active` = 1 AND `content`.`active` = 1
|
||||
QUERY;
|
||||
|
||||
private static $LOAD_IMAGES =
|
||||
'SELECT `id`, `file`, `description` AS `title` FROM `news_pictures` WHERE `news` = :newsId AND `active` = 1';
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Lijst van alle agendaberichten
|
||||
*/
|
||||
public function overview(int $daysAhead = 0) {
|
||||
$sql = self::$BASE_SQL;
|
||||
if($daysAhead > 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]));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 = <<<QUERY
|
||||
SELECT `news`.`id`, `content`.`title`, `content`.`content`, `news`.`podcast`, `news`.`video`, `news`.`keywords`, `news`.`titlewithdate`,
|
||||
`news`.`creationdt` AS `published`, `content`.`creator`, `content`.`editingdt` AS `edited`, `content`.`editor`,
|
||||
`news`.`startdt` AS `starts`, `news`.`enddt` AS `ends`,
|
||||
`content`.`showsource`, `sources`.`title` AS `source`, `sources`.`url` AS `source_url`,
|
||||
`themes`.`title` AS `theme`, `themes`.`thumbnail` AS `theme_thumbnail`, `regions`.`title` AS `region`
|
||||
FROM `news`
|
||||
@@ -20,7 +23,7 @@ SELECT `news`.`id`, `content`.`title`, `content`.`content`, `news`.`podcast`, `n
|
||||
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` = 1 AND `content`.`target` = 1 AND `news`.`active` = 1 AND `content`.`active` = 1
|
||||
WHERE `news`.`category` = :category AND `content`.`target` = 1 AND `news`.`active` = 1 AND `content`.`active` = 1
|
||||
QUERY;
|
||||
|
||||
private static $LOAD_IMAGES =
|
||||
@@ -39,7 +42,7 @@ QUERY;
|
||||
/**
|
||||
* Lijst van alle nieuwsberichten
|
||||
*/
|
||||
public function overview(Request $request) {
|
||||
public function newslist(Request $request) {
|
||||
$count = (int)$request->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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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' );
|
||||
|
||||
@@ -10,6 +10,8 @@ class CalendarEvent extends Model {
|
||||
public $starts;
|
||||
public $ends;
|
||||
public $images;
|
||||
public $podcast;
|
||||
public $keywords;
|
||||
public $url;
|
||||
|
||||
public function __construct($data, $images = null) {
|
||||
@@ -17,6 +19,17 @@ class CalendarEvent extends Model {
|
||||
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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user