- Add package.json dependencies: jquery, jquery-ui, moment, daterangepicker, wavesurfer.js, air-datepicker - Create scripts/setup-vendor.js to copy npm packages to public/assets/vendor - Update view templates to use vendor paths instead of old /js/ or CDN URLs - Clean up legacy commented-out scripts in master layout
239 lines
8.3 KiB
PHP
239 lines
8.3 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\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
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;
|
|
protected $API_HOST;
|
|
|
|
|
|
private function getDataFromFileAndConvert($file, $path, $class, $maxItems = 0)
|
|
{
|
|
$data = json_decode(Storage::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', '/'));
|
|
$this->API_HOST = env('API_HOST', $this->API_URL);
|
|
|
|
//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::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::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::composer('widgets.menu', function($view) {
|
|
try {
|
|
$menuData = json_decode(Storage::get('static/menu.json'));
|
|
$view->with('items', $menuData ?? []);
|
|
} catch (\Exception $e) {
|
|
$view->with('items', []);
|
|
}
|
|
});
|
|
|
|
View::share('disableBanners', env('DISABLE_BANNERS', false));
|
|
}
|
|
|
|
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)
|
|
{
|
|
try {
|
|
$response = Http::connectTimeout(5)
|
|
->timeout(15)
|
|
->withHeaders([
|
|
'Host' => $this->API_HOST,
|
|
'X-Api-Key' => sha1(request()->server('REMOTE_ADDR')),
|
|
'X-User-Agent' => request()->server('HTTP_USER_AGENT')
|
|
])
|
|
->get($this->API_URL . $url);
|
|
|
|
if ($response->successful()) {
|
|
$body = $response->body();
|
|
return $body ? json_decode($body) : null;
|
|
}
|
|
Log::warning('API request failed', [
|
|
'url' => $this->API_URL . $url,
|
|
'status' => $response->status()
|
|
]);
|
|
return null;
|
|
} catch (\Exception $e) {
|
|
Log::error('API request error', [
|
|
'url' => $this->API_URL . $url,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
protected function checkAPI($url)
|
|
{
|
|
return $this->getHttpResponseCode($url);
|
|
}
|
|
|
|
protected function getHttpResponseCode($url)
|
|
{
|
|
try {
|
|
$response = Http::connectTimeout(5)
|
|
->timeout(15)
|
|
->withHeaders([
|
|
'Host' => $this->API_HOST,
|
|
'X-Api-Key' => sha1(request()->server('REMOTE_ADDR')),
|
|
'X-User-Agent' => request()->server('HTTP_USER_AGENT')
|
|
])
|
|
->head($this->API_URL . $url);
|
|
|
|
return $response->status();
|
|
} catch (\Exception $e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
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 = (object) $this->API('nieuws/populair?aantal=5');
|
|
foreach ($apiResult->news as $_newsItem) {
|
|
$populair[] = new \Model\NewsItem($_newsItem);
|
|
}
|
|
|
|
$newsItems = [];
|
|
$apiResult = (object) $this->API('nieuws/overzicht?aantal=5');
|
|
foreach ($apiResult->news as $_newsItem) {
|
|
$newsItems[] = new \Model\NewsItem($_newsItem);
|
|
}
|
|
|
|
return ['newsItems' => $newsItems, 'populair' => $populair];
|
|
}
|
|
|
|
public function static_page($slug)
|
|
{
|
|
if (view()->exists($slug)) {
|
|
return view($slug);
|
|
}
|
|
|
|
$page = (object) $this->API('statisch/' . $slug);
|
|
if ($page == null || !isset($page->title)) {
|
|
return abort(404);
|
|
}
|
|
|
|
if(isset($page->published)) {
|
|
$page->published = Controller::JsonToDateTime($page->published);
|
|
}
|
|
if (isset($page->edited) && $page->edited) {
|
|
$page->edited = Controller::JsonToDateTime($page->edited);
|
|
}
|
|
|
|
return view('static', compact('page'));
|
|
}
|
|
}
|