Files
nhgooi.nl/app/Http/Controllers/Controller.php
Jorit Tijsen ecfbb47f1e Disable banner by env file
Favicon aanpassen
Logo aanpassen + subtitel toevoegd
Tab ‘Nieuws’ uit het menu gehaalt
Menu item ‘Gemist’ in menu plaaten
App uit menu balk
Adverteren toevoegen aan blauwe balk
Wat draait in sidebar NH Gooi Radio Live
Footer tekst
NAW toevoegen aan contact pagina
Op contactpagina: Chef redactie
Delen bericht
2024-05-06 15:08:19 +02:00

183 lines
6.7 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', [], '\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 = new \DateTimeImmutable($item_data->start->date,
new \DateTimeZone($item_data->start->timezone));
$program->end = new \DateTimeImmutable($item_data->end->date,
new \DateTimeZone($item_data->end->timezone));
}
// 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 = new \DateTimeImmutable($item_data->start->date,
new \DateTimeZone($item_data->start->timezone));
$recent->end = new \DateTimeImmutable($item_data->end->date,
new \DateTimeZone($item_data->end->timezone));
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', [], '\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)
{
// if (strpos($url, 'nieuws/overzicht') !== false) {
// return json_decode(file_get_contents(__DIR__ . '/../../../storage/app/laatste_nieuws.json'));
// }
// return [];
$arrContextOptions = [
'ssl' => [
"verify_peer" => false,
"verify_peer_name" => false,
],
];
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 new \DateTime($obj->date, new \DateTimeZone($obj->timezone));
}
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 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];
}
}