Feuerwehr-eppingen/app/Http/Controllers/DashboardController.php

357 lines
9.9 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: Marco Glietsch
* Date: 22.11.2018
* Time: 09:28
*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Models\Tag;
use App\Models\Post;
use App\Models\SubscribeChild;
use App\Models\Publication;
use App\Helpers\AccessHelper as Access;
use \Carbon\Carbon;
use Response;
//use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Request as Input;
use App\Helpers\StringHelper;
class DashboardController extends ExtendedController
{
public function __construct()
{
$this->middleware(['auth' => 'dashboardPermissions']);
parent::__construct();
}
public function indexLatestPublicationsAjax(Request $request)
{
return $this->indexPublicationsAjax('latest');
}
public function indexPendingPublicationsAjax(Request $request)
{
return $this->indexPublicationsAjax('pending');
}
private function indexPublicationsAjax($type)
{
if(Input::get('page'))
{
$page = Input::get('page');
}
else
{
$page = 1;
}
$page--;
$publicationDates = null;
if($type == 'pending')
{
/*********************************************/
/* Suche nach anstehenden Veröffentlichungen */
/*********************************************/
// Suche zuerst die nächsten 5 Veröffentlichungstermine
$publicationDates = Publication::select('date_to_publish', 'publications.post_id')
->whereState("PENDING")
->where('date_to_publish', '>=', Carbon::now()->startOfDay())
// ->where('date_to_publish', '<', Carbon::now()->addWeeks(2)->endOfDay())
->whereNotNull('date_to_publish')
->justValidPosts()
->groupBy('publications.date_to_publish')
->orderBy('publications.date_to_publish')
->limit(14);
}
else if($type == 'latest')
{
/*********************************************/
/* Suche nach den letzten Veröffentlichungen */
/*********************************************/
// Suche zuerst die letzten 14 Veröffentlichungstermine (2 Wochen)
$publicationDates = Publication::select(DB::raw("date(published_at) as published_at"), 'publications.post_id')
->whereNotNull('state')
->where('state', '!=', 'PENDING')
->where('published_at', '<=', Carbon::now())
->justValidPosts()
->groupBy('publications.published_at')
->orderBy('publications.published_at', 'DESC')
->limit(14);
}
$publicationDates = $this->prepareModelWithDepartments($publicationDates);
$publicationDates = $publicationDates->get();
$publications = array();
// Schleife über alle Termine
foreach($publicationDates as $date)
{
if($type == 'pending')
{
$date->date = $date->date_to_publish;
$p = Publication::select('*', DB::raw("group_concat(publications.platform) as platforms"))
->whereState("PENDING")
->whereDateToPublish($date->date_to_publish)
->groupBy('publications.post_id')
->justValidPosts()
->with('post');
}
else if($type == 'latest')
{
$date->date = $date->published_at;
$dayStart = Carbon::parse($date->published_at)->startOfDay();
$dayEnd = Carbon::parse($date->published_at)->endOfDay();
$p = Publication::select('*', DB::raw("group_concat(publications.platform) as platforms"))
->where('state', '!=', 'PENDING')
->where('published_at', '>=', $dayStart)
->where('published_at', '<', $dayEnd)
// ->wherePublishedAt($date->published_at)
->groupBy('publications.post_id')
->justValidPosts()
->with('post');
}
$p = $this->prepareModelWithDepartments($p);
// Anfrage ausführen
$p = $p->get();
$p->each(function($m) use ($type){
if($type == 'pending')
{
$m->date = $m->date_to_publish;
}
else
{
$m->date = $m->published_at;
}
});
$publications[$date->date]= $p;
}
$view = view('inc.admin.dashboard.posts');
$view->with('page', $page);
$view->with('posts', $publications);
$response = [
'status' => [
'error' => false,
'code' => 200,
'message' => 'ajax entities',
'title' => ''
],
'content' => [
'html' => $view->render(),
],
];
return Response::json($response);
}
public function indexLatestPostsAjax()
{
if(Input::get('page'))
{
$page = Input::get('page');
}
else
{
$page = 1;
}
$page--;
/*********************************************/
/* Suche nach den letzten Veröffentlichungen */
/*********************************************/
// Suche zuerst die nächsten 5 Veröffentlichungstermine
$dates = Post::select(DB::raw('DATE(`posts`.`updated_at`) as date'))
->where('posts.updated_at', '<=', Carbon::now())
// ->where('updated_at', '>', Carbon::now()->subWeeks(40)->startOfDay())
->groupBy(DB::raw('DATE(`posts`.`updated_at`)'))
->orderBy('posts.updated_at', 'DESC')
->limit(14);
$dates = $this->prepareModelWithDepartments($dates);
$dates = $dates->get();
$posts = array();
// Schleife über alle Termine
foreach($dates as $date)
{
$p = Post::select('posts.*')
->where(DB::raw('DATE(`posts`.`updated_at`)'), $date->date)
->orderBy('posts.updated_at');
// Nur solche Veröffentlichungen anzeigen, die für den Benutzer auch relevant sind
// => Der Benutzer muss die entsprechende Abteilung zugeordnet bekommen haben, oder Admin sein
$p = $this->prepareModelWithDepartments($p);
// Anfrage ausführen
$p = $p->get();
foreach($p as $temp)
{
$temp->date = $temp->updated_at;
}
$posts[$date->date] = $p;
}
$view = view('inc.admin.dashboard.posts');
$view->with('page', $page);
$view->with('posts', $posts);
$response = [
'status' => [
'error' => false,
'code' => 200,
'message' => 'ajax entities',
'title' => ''
],
'content' => [
'html' => $view->render(),
],
];
return Response::json($response);
}
public function indexPendingPublications()
{
$view = view('inc.admin.dashboard');
$view->with('title', __('admin.pending publications'));
$view->with('type', 'pending-publications');
return $view->render();
}
public function indexLatestPublications()
{
$view = view('inc.admin.dashboard');
$view->with('title', __('admin.last publications'));
$view->with('type', 'latest-publications');
return $view->render();
}
public function indexLatestPosts()
{
$view = view('inc.admin.dashboard');
$view->with('title', __('admin.last activities'));
$view->with('type', 'latest-posts');
return $view->render();
}
public function index(Request $request)
{
$view = view('inc.admin.dashboard');
return $view->render();
}
private function prepareModelWithDepartments($model)
{
// Nur solche Posts anzeigen, die für den Benutzer auch relevant sind
// => Der Benutzer muss die entsprechende Abteilung zugeordnet bekommen haben, oder Admin sein
if(!Access::isAdmin())
{
foreach(\Auth::user()->departments as $department)
{
$departments[] = $department->id;
}
if(count($departments) > 0)
{
$model = $model->join('post_tag as post_tag', function ($join) use ($model, $departments)
{
switch($model->getQuery()->from)
{
case 'posts':
$join->on('post_tag.post_id', 'posts.id');
break;
case 'publications':
$join->on('post_tag.post_id', 'publications.post_id');
break;
default:
break;
}
})
->join('tags as tags', function ($join) use ($departments)
{
$join->on('tags.id', 'post_tag.tag_id')
->where('tags.type', 'abteilung')
->where('tags.id', $departments);
});
}
}
return $model;
}
public function subscriptions()
{
// Suche zuerst alle Orte
$cities = SubscribeChild::select('city')
->groupBy('city')
->orderBy('city')
->get();
$statistics = array();
$totals = (object)[];
$totals->totalOverall = 0;
$totals->totalAddresses = 0;
$totals->totalLastHour = 0;
$totals->totalToday = 0;
$totals->totalToday = 0;
$totals->totalSevenDays = 0;
// Zähle alle Datenbankeinträge nach Orten
foreach($cities as $city)
{
$obj = (object)[];
$obj->city = $city->city;
$obj->subscriptions = SubscribeChild::where('city', $city->city)->count();
$totals->totalOverall += $obj->subscriptions;
$obj->addresses = SubscribeChild::where('city', $city->city)->groupBy('lastnameParent')->get()->count();
$totals->totalAddresses += $obj->addresses;
$obj->lastHour = SubscribeChild::where('city', $city->city)
->where('created_at', '>', Carbon::now()->subHours(1)->toDateTimeString())
->count();
$totals->totalLastHour += $obj->lastHour;
$obj->today = SubscribeChild::where('city', $city->city)
->where('created_at', '>', Carbon::today()->toDateTimeString())
->count();
$totals->totalToday += $obj->today;
$obj->sevenDays = SubscribeChild::where('city', $city->city)
->where('created_at', '>', Carbon::today()->subDays(7)->toDateTimeString())
->count();
$totals->totalSevenDays += $obj->sevenDays;
$obj->duplicates = SubscribeChild::where('city', $city->city)
->groupBy('lastnameParent', 'firstnameParent', 'firstnameChild', 'city')
->orderBy('lastnameParent', 'firstnameParent', 'firstnameChild')
->havingRaw('COUNT(*) > 1')
->get();
$statistics[] = $obj;
}
// Gegencheck: Zähle noch einmal alle Datenbankeinträge
$count = SubscribeChild::count();
$view = view('inc.admin.subscriptions');
$view->with('title', __('admin.subscriptions'));
$view->with('type', 'subscriptions');
$view->with('statistics', $statistics);
$view->with('totals', $totals);
return $view->render();
}
}