mein-sterntours/app/Http/Controllers/API/CMSContentInfoController.php
Kevin Adametz 881fc84207 08 2024
2024-08-05 11:58:09 +02:00

238 lines
No EOL
7.7 KiB
PHP
Executable file

<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Models\CMSInfo;
use App\Models\CMSInfoAvailable;
use App\Models\CMSInfoHoliday;
use App\Helper\HTMLHelper;
class CMSContentInfoController extends Controller
{
public $successStatus = 200;
public function headerInfo()
{
$request = \Request::all();
/* if(!isset($request['url']) || !isset($request['key'])){
return response()->json(['error' => "request no found"], $this->successStatus);
}
if($request['key'] !== 'f6077389c9ce710e554763a5de02c8ec'){
return response()->json(['error' => "key"], 401);
}*/
$ret = [];
$days = HTMLHelper::getDeDays();
$locals = CMSInfoAvailable::whereType('local')->whereSpecial(false)->get();
foreach ($locals as $value){
$date = CMSInfoAvailable::getWeekWithDate($value->wday);
$ret['local'][strtotime($date['date'])] = [
'day'=>$days[$value->wday],
'active' => $value->active,
'from' => $value->from,
'to' => $value->to,
'date' => $date['date'],
];
}
$phones = CMSInfoAvailable::whereType('phone')->whereSpecial(false)->get();
foreach ($phones as $value){
$date = CMSInfoAvailable::getWeekWithDate($value->wday);
$ret['phone'][strtotime($date['date'])] = [
'day'=>$days[$value->wday],
'active' => $value->active,
'from' => $value->from,
'to' => $value->to,
'date' => $date['date'],
];
}
$specials = CMSInfoAvailable::whereSpecial(true)->get();
foreach ($specials as $special){
$ret[$special->type][strtotime($special->date)] = [
'day'=>$days[$special->wday],
'active' => $special->active,
'from' => $special->from,
'to' => $special->to,
'date' => $special->date,
];
}
ksort($ret['local']);
ksort($ret['phone']);
$ret['info']['office_important_note_active'] = CMSInfo::getContentBySlug('office-important-note-active');
$ret['info']['office_important_note'] = CMSInfo::getContentBySlug('office-important-note');
$ret['info']['office_appointment'] = CMSInfo::getContentBySlug('office-appointment');
$now = \Carbon::now();
$wday = $now->dayOfWeekIso;
$ret['available']['local'] = $this->checkAvailable('local', $wday);
$ret['available']['phone'] = $this->checkAvailable('phone', $wday);
return response()->json(['success' => "content", "ret"=>$ret], $this->successStatus);
}
private function checkAvailable($type, $wday){
$contents = [];
$contents['local'] = [
1 => " keinen Eintrag",
2 => "öffnet am #day#",
3 => "schließt um #to# Uhr",
4 => "öffnet am #n-day# um #from# Uhr",
5 => "ab #from# Uhr",
];
$contents['phone'] = [
1 => " keinen Eintrag",
2 => "ab #day#",
3 => "bis #to# Uhr",
4 => "ab #n-day# um #from# Uhr",
5 => "ab #from# Uhr",
];
$now = \Carbon::now();
//holidays
if($next_date = $this->checkBetweenHoliday($type, $now)){
$content = str_replace('#day#', $next_date, $contents[$type][2]);
return ['active' => 0, 'content'=>$content];
}
$now = \Carbon::now();
$minutes = $now->hour*60 + $now->minute;
if($special = $this->checkDateforSpecial($type, $wday, $now->format("d.m.Y"))) {
if($special->active) {
$from = $special->fromMinutes();
$to = $special->toMinutes();
//open to
if($minutes >= $from && $minutes <= $to){
$content = str_replace('#to#', $special->to, $contents[$type][3]);
return ['active' => 1, 'content'=> $content];
}
//is close, but open today
if($minutes < $from && $minutes < $to) {
$content = str_replace('#from#', $special->from, $contents[$type][5]);
return ['active' => 0, 'content'=> $content];
}
}
}else{
$available = CMSInfoAvailable::getContentBySlug($type, $wday);
if(!$available){
return ['active' => 0, 'content'=> $contents[$type][1]];
}
if($available->active) {
$from = $available->fromMinutes();
$to = $available->toMinutes();
//open to
if($minutes >= $from && $minutes <= $to){
$content = str_replace('#to#', $available->to, $contents[$type][3]);
return ['active' => 1, 'content'=> $content];
}
//is close, but open today
if($minutes < $from && $minutes < $to) {
$content = str_replace('#from#', $available->from, $contents[$type][5]);
return ['active' => 0, 'content'=> $content];
}
}
}
//is next day
$next = $this->getNextActiveDay($type, $wday, $contents);
if($next){
return $next;
}
return ['active' => 0, 'content'=> $contents[$type][1]];
}
private function getNextActiveDay($type, $wday, $contents){
$days = HTMLHelper::getDeDays();
$counter = 0;
while ($counter <= 6) {
$counter++;
$wday = $wday < 7 ? $wday+1 : 1;
$w = CMSInfoAvailable::getWeekWithDate($wday);
$wDate = \Carbon::parse($w['date']);
if($next_date = $this->checkBetweenHoliday($type, $wDate)){
$content = str_replace('#day#', $next_date, $contents[$type][2]);
return ['active' => 0, 'content'=>$content];
}
if($special = $this->checkDateforSpecial($type, $wday, $wDate->format("d.m.Y"))){
if($special->active) {
$content = str_replace('#from#', $special->from, $contents[$type][4]);
$content = str_replace('#n-day#', $days[$special->wday], $content);
return ['active' => 0, 'content'=> $content];
}
}
$available = CMSInfoAvailable::getContentBySlug($type, $wday);
if(isset($available) && $available->active){
//is in holiday?
$content = str_replace('#from#', $available->from, $contents[$type][4]);
$content = str_replace('#n-day#', $days[$available->wday], $content);
return ['active' => 0, 'content'=> $content];
}
}
return false;
}
private function checkBetweenHoliday($type, \Carbon $cdate){
$holidays = CMSInfoHoliday::where($type, true)->get();
foreach ($holidays as $holiday){
//is in range?
$from = \Carbon::parse($holiday->from);
$to = \Carbon::parse($holiday->to);
if($cdate->between($from, $to)){
$to->modify('+1 day');
return $to->format("d.m.Y");
}
}
return false;
}
private function checkDateforSpecial($type, $wday, $date){
$specials = CMSInfoAvailable::whereType($type)->whereWday($wday)->whereSpecial(true)->get();
foreach ($specials as $special){
if($special->date == $date){
return $special;
}
}
}
}