179 lines
6.5 KiB
PHP
179 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller as BaseController;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\View;
|
|
|
|
class Controller extends BaseController
|
|
{
|
|
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
|
|
|
protected $API_URL;
|
|
|
|
private function getDataFromFileAndConvert($file, $path, $class, $maxItems = 0)
|
|
{
|
|
$data = json_decode(Storage::disk('local')->get($file));
|
|
foreach ($path as $subobject) {
|
|
$data = $data->$subobject;
|
|
}
|
|
$items = [];
|
|
foreach ($data as $item_data) {
|
|
$items[] = new $class($item_data);
|
|
if ($maxItems && count($items) == $maxItems) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
|
|
public function __construct()
|
|
{
|
|
View::share('apiUrl', $this->API_URL = env('API_URL', 'http://api.6fm.nl/'));
|
|
View::share('imgBase', env('IMAGE_BASE_URL', '/'));
|
|
|
|
$blogs = $this->getDataFromFileAndConvert('blogs.json', [], '\Model\Blog', 1);
|
|
$activeBlog = count($blogs) && $blogs[0]->is_active ? $blogs[0] : null;
|
|
View::share('activeBlog', $activeBlog);
|
|
|
|
|
|
//View::share('onAir', file_get_contents(url('onair')));
|
|
View::composer('widgets.laatstenieuws', function ($view) {
|
|
$view->with('data', $this->getDataFromFileAndConvert('laatste_nieuws.json', ['news'], '\Model\NewsItem'));
|
|
});
|
|
View::composer('widgets.populairnieuws', function ($view) {
|
|
$view->with('data', $this->getDataFromFileAndConvert('populair_nieuws.json', ['news'], '\Model\NewsItem'));
|
|
});
|
|
View::composer('widgets.nustraks', function ($view) {
|
|
$data = json_decode(Storage::disk('local')->get('nu_straks.json'))->schedule;
|
|
$programs = [];
|
|
foreach ($data as $item_data) {
|
|
$programs[] = $program = new \Model\Program($item_data->program);
|
|
$program->start = self::JsonToDateTime($item_data->start);
|
|
$program->end = self::JsonToDateTime($item_data->end);
|
|
}
|
|
|
|
// Need a bit of slack here, otherwise the current program may show up
|
|
$now = new \DateTimeImmutable('2 minutes ago');
|
|
$data = json_decode(Storage::disk('local')->get('zojuist.json'))->schedule;
|
|
$i = 0;
|
|
foreach (array_reverse($data) as $item_data) {
|
|
$recent = $program = new \Model\Program($item_data->program);
|
|
$recent->start = self::JsonToDateTime($item_data->start);
|
|
$recent->end = self::JsonToDateTime($item_data->end);
|
|
if (($recent->end < $now) && (!$recent->nonstop) && (!$recent->rerun)) {
|
|
$view->with('recent', $recent);
|
|
break;
|
|
}
|
|
}
|
|
|
|
$view->with('data', $programs);
|
|
});
|
|
View::composer('widgets.laatstepodcasts', function ($view) {
|
|
$view->with('data',
|
|
$this->getDataFromFileAndConvert('laatste_podcasts.json', ['podcasts'], '\Model\Podcast'));
|
|
});
|
|
View::composer('widgets.regioagenda', function ($view) {
|
|
$view->with('data', $this->getDataFromFileAndConvert('regioagenda.json', [], '\Model\CalendarEvent'));
|
|
});
|
|
View::composer('widgets.beelden', function ($view) {
|
|
$view->with('data', $this->getDataFromFileAndConvert('beelden.json', ['items'], '\Model\NewsItem'));
|
|
});
|
|
View::composer('widgets.menu', function ($view) {
|
|
$view->with('news', $this->getDataFromFileAndConvert('laatste_nieuws.json', ['news'], '\Model\NewsItem'))
|
|
->with('popular', $this->getDataFromFileAndConvert('populair_nieuws.json', ['news'], '\Model\NewsItem', 3))
|
|
->with('podcasts',
|
|
$this->getDataFromFileAndConvert('laatste_podcasts.json', ['podcasts'], '\Model\Podcast'));
|
|
});
|
|
|
|
View::share('disableBanners', env('DISABLE_BANNERS', true));
|
|
}
|
|
|
|
protected function registerView(Request $request, $type, $id)
|
|
{
|
|
if (config('app.env') == 'local') {
|
|
return;
|
|
}
|
|
|
|
app('db')->insert('INSERT INTO `pagestats`(`type`, `item_id`, `visitor_ip`, `session`, `referer`) VALUES(:type, :id, :ip, :session, :referer)',
|
|
[
|
|
'type' => $type,
|
|
'id' => $id,
|
|
'ip' => $request->server('REMOTE_ADDR'),
|
|
'session' => md5(Session::getId()),
|
|
'referer' => $request->server('HTTP_REFERRER')
|
|
]);
|
|
}
|
|
|
|
protected function API($url)
|
|
{
|
|
$arrContextOptions = [
|
|
'ssl' => [
|
|
"verify_peer" => false,
|
|
"verify_peer_name" => false,
|
|
],
|
|
'http' => [
|
|
'method' => 'GET',
|
|
'header' => 'X-Api-Key: ' . sha1(request()->server('REMOTE_ADDR')) . "\r\n"
|
|
. 'X-User-Agent: ' . request()->server('HTTP_USER_AGENT') . "\r\n"
|
|
]
|
|
];
|
|
|
|
return json_decode(file_get_contents($this->API_URL . $url, false, stream_context_create($arrContextOptions)));
|
|
}
|
|
|
|
protected function checkAPI($url)
|
|
{
|
|
return $this->get_http_response_code($this->API_URL . $url);
|
|
}
|
|
|
|
protected function get_http_response_code($url)
|
|
{
|
|
$headers = get_headers($url);
|
|
return substr($headers[0], 9, 3);
|
|
}
|
|
|
|
|
|
|
|
protected static function JsonToDateTime($obj)
|
|
{
|
|
return is_object($obj) ? new \DateTime($obj->date, new \DateTimeZone($obj->timezone)) : \Carbon\Carbon::parse($obj)->setTimezone(date_default_timezone_get());
|
|
}
|
|
|
|
public function __call($method, $arguments)
|
|
{
|
|
if (substr($method, 0, 5) == 'view_') {
|
|
$view = substr($method, 5);
|
|
if (view()->exists($view)) {
|
|
return view($view);
|
|
}
|
|
}
|
|
|
|
return abort(404);
|
|
}
|
|
|
|
public function getSidebareData()
|
|
{
|
|
$populair = [];
|
|
$apiResult = $this->API('nieuws/populair?aantal=5');
|
|
foreach ($apiResult->news as $_newsItem) {
|
|
$populair[] = new \Model\NewsItem($_newsItem);
|
|
}
|
|
|
|
$newsItems = [];
|
|
$apiResult = $this->API('nieuws/overzicht?aantal=5');
|
|
foreach ($apiResult->news as $_newsItem) {
|
|
$newsItems[] = new \Model\NewsItem($_newsItem);
|
|
}
|
|
|
|
return ['newsItems' => $newsItems, 'populair' => $populair];
|
|
}
|
|
}
|