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

165 lines
4.8 KiB
PHP
Executable file

<?php
namespace App\Http\Controllers\CMS;
use App\Http\Controllers\Controller;
use App\Models\CMSContent;
use App\Models\CMSInfo;
use App\Models\CMSInfoAvailable;
use App\Models\CMSInfoHoliday;
use App\Helper\HTMLHelper;
use Request;
use Validator;
class CMSContentInfoController extends Controller
{
/*
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware(['admin', '2fa']);
}
public function index()
{
$data = [
'days' => CMSInfoAvailable::getWeekWithDate(),
'specials_local' => CMSInfoAvailable::getSpecialsBy('local'),
'specials_phone' => CMSInfoAvailable::getSpecialsBy('phone'),
'cms_holidays' => CMSInfoHoliday::all(),
];
return view('cms.content.info.index', $data);
}
public function store()
{
$data = Request::all();
if($data['action'] === "saveAll"){
//save in CMSInfoAvailable
if(isset($data['info_availables']) && is_array($data['info_availables'])){
foreach ($data['info_availables'] as $type=>$values){
foreach ($values as $wday=>$val){
CMSInfoAvailable::setContentBySlug($type, $wday, $val);
}
}
}
//save in CMSInfoAvailable SPECIALS
if(isset($data['special_availables']) && is_array($data['special_availables'])){
foreach ($data['special_availables'] as $id=>$values){
$special_available = CMSInfoAvailable::findOrFail($id);
$cdate = \Carbon::parse($values['date']);
$values['wday'] = $cdate->dayOfWeekIso;
$special_available->fill($values)->save();
}
}
//save in CMSinfoHolidays
if(isset($data['info_holidays']) && is_array($data['info_holidays'])){
foreach ($data['info_holidays'] as $id=>$val){
$content = CMSInfoHoliday::findOrFail($id);
$content->fill($val);
$content->save();
}
}
//save in CMSinfo
if(isset($data['infos']) && is_array($data['infos'])){
foreach ($data['infos'] as $slug=>$info){
$type = isset($info['type']) ? $info['type'] : "text";
$val = isset($info['val']) ? $info['val'] : "";
CMSInfo::setContentBySlug($slug, $val, $type);
}
}
}
if($data['action'] === "add_holiday"){
CMSInfoHoliday::create();
}
if($data['action'] === "add_special_available_local"){
$date = CMSInfoAvailable::getNextSpecialDateBy('local', 6);
CMSInfoAvailable::create([
'type' => 'local',
'wday' => $date->dayOfWeekIso,
'special' => true,
'active' => true,
'from' => '10:00',
'to' => '13:00',
'date' => $date->format('d.m.Y'),
]);
}
if($data['action'] === "add_special_available_phone"){
$date = CMSInfoAvailable::getNextSpecialDateBy('phone', 6);
CMSInfoAvailable::create([
'type' => 'phone',
'wday' => $date->dayOfWeekIso,
'special' => true,
'active' => true,
'from' => '10:00',
'to' => '13:00',
'date' => $date->format('d.m.Y'),
]);
}
\Session()->flash('alert-save', '1');
return redirect(route('cms_content_infos'));
}
private function createWeekWithDate(){
$days = HTMLHelper::getDeDays();
$now = \Carbon::now();
$day_of = $now->dayOfWeekIso;
//days after now
for ($i = $day_of; $i<=7; $i++){
$days[$i] = $days[$i].' <span class="text-muted">('.$now->format("d.m.Y").')</span>';
$now->modify('+1 day');
}
//days before now
for ($i = 1; $i<$day_of; $i++) {
$days[$i] = $days[$i] . ' <span class="text-muted">(' . $now->format("d.m.Y") . ')</span>';
$now->modify('+1 day');
}
//add
return $days;
}
public function delete($model, $id){
if($model === 'holiday'){
$content = CMSInfoHoliday::findOrFail($id);
$content->delete();
\Session()->flash('alert-success', __('Content gelöscht'));
}
if($model === 'special_available'){
$content = CMSInfoAvailable::findOrFail($id);
$content->delete();
\Session()->flash('alert-success', __('Content gelöscht'));
}
return redirect(route('cms_content_infos'));
}
}