236 lines
7.4 KiB
PHP
236 lines
7.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\IqImage;
|
|
use App\Models\IqSite;
|
|
use App\Models\DashboardNews;
|
|
use Request;
|
|
|
|
|
|
class SitesController extends Controller
|
|
{
|
|
|
|
public function __construct()
|
|
{
|
|
$this->middleware('admin');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
//
|
|
}
|
|
|
|
// Dashboard News Methods
|
|
public function dashboardNews()
|
|
{
|
|
$data = [
|
|
'news' => DashboardNews::orderBy('created_at', 'DESC')->get(),
|
|
'languages' => config('localization.supportedLocales'),
|
|
];
|
|
return view('admin.site.news.index', $data);
|
|
}
|
|
|
|
public function dashboardNewsEdit($id)
|
|
{
|
|
$news = $id === 'new' ? new DashboardNews() : DashboardNews::findOrFail($id);
|
|
$data = [
|
|
'news' => $news,
|
|
'languages' => config('localization.supportedLocales'),
|
|
];
|
|
return view('admin.site.news.edit', $data);
|
|
}
|
|
|
|
public function dashboardNewsStore($id)
|
|
{
|
|
$data = Request::all();
|
|
|
|
// Handle translations
|
|
$transTitle = [];
|
|
$transTeaser = [];
|
|
$transContent = [];
|
|
|
|
foreach (config('localization.supportedLocales') as $locale => $localeData) {
|
|
if ($locale !== 'de') {
|
|
$transTitle[$locale] = Request::get('trans_title_' . $locale, '');
|
|
$transTeaser[$locale] = Request::get('trans_teaser_' . $locale, '');
|
|
$transContent[$locale] = Request::get('trans_content_' . $locale, '');
|
|
}
|
|
}
|
|
|
|
$data['trans_title'] = $transTitle;
|
|
$data['trans_teaser'] = $transTeaser;
|
|
$data['trans_content'] = $transContent;
|
|
$data['active'] = Request::has('active') ? 1 : 0;
|
|
|
|
// Handle file links
|
|
$fileLinks = Request::get('file_links', []);
|
|
// Filter out empty entries (where neither label nor file_id is set)
|
|
$filteredFileLinks = [];
|
|
foreach ($fileLinks as $locale => $links) {
|
|
if (is_array($links)) {
|
|
$filteredFileLinks[$locale] = array_values(array_filter($links, function ($link) {
|
|
return !empty($link['file_id']) || !empty($link['label']);
|
|
}));
|
|
}
|
|
}
|
|
$data['file_links'] = $filteredFileLinks;
|
|
|
|
// Handle display_date
|
|
if (!empty($data['display_date'])) {
|
|
try {
|
|
$data['display_date'] = \Carbon\Carbon::createFromFormat('d.m.Y', $data['display_date'])->format('Y-m-d');
|
|
} catch (\Throwable $e) {
|
|
$data['display_date'] = now()->format('Y-m-d');
|
|
}
|
|
} else {
|
|
$data['display_date'] = now()->format('Y-m-d');
|
|
}
|
|
|
|
// Wenn diese News aktiv gesetzt wird, setze alle anderen auf inaktiv
|
|
if ($data['active']) {
|
|
DashboardNews::where('active', 1)->update(['active' => 0]);
|
|
}
|
|
|
|
if ($id === 'new') {
|
|
DashboardNews::create($data);
|
|
} else {
|
|
$news = DashboardNews::findOrFail($id);
|
|
$news->fill($data);
|
|
$news->save();
|
|
}
|
|
|
|
\Session()->flash('alert-success', __('msg.saved_successfully'));
|
|
return redirect(route('admin_dashboard_news'));
|
|
}
|
|
|
|
public function dashboardNewsDelete($id)
|
|
{
|
|
$news = DashboardNews::findOrFail($id);
|
|
$news->delete();
|
|
|
|
\Session()->flash('alert-success', __('msg.deleted_successfully'));
|
|
return redirect(route('admin_dashboard_news'));
|
|
}
|
|
|
|
public function show($site)
|
|
{
|
|
$data = [
|
|
'value' => IqSite::find(1),
|
|
'site' => $site,
|
|
];
|
|
return view('admin.site.edit', $data);
|
|
}
|
|
|
|
public function store($site)
|
|
{
|
|
$data = Request::all();
|
|
$data['products'] = isset($data['products']) ? $data['products'] : null;
|
|
$data['set_products'] = isset($data['set_products']) ? $data['set_products'] : null;
|
|
|
|
if ($site == "new") {
|
|
// $model = IqSite::create($data);
|
|
} else {
|
|
$model = IqSite::find(1);
|
|
$model->fill($data);
|
|
$model->save();
|
|
}
|
|
|
|
\Session()->flash('alert-save', '1');
|
|
return redirect(route('admin_sites', ['start']));
|
|
}
|
|
|
|
|
|
// Upload FILE -----------------------------------------------------------------------------------------------------------------------
|
|
|
|
public function imageUpload($site)
|
|
{
|
|
|
|
$model = IqSite::find(1);
|
|
|
|
try {
|
|
$image = \App\Services\Slim::getImages('images')[0];
|
|
|
|
if (isset($image['output']['data'])) {
|
|
|
|
// Base64 of the image
|
|
$data = $image['output']['data'];
|
|
$file_ex = array('image/jpeg' => 'jpg', 'image/png' => 'png');
|
|
|
|
if (!isset($file_ex[$image['output']['type']])) {
|
|
\Session()->flash('alert-danger', 'File is not jpg or png!');
|
|
return redirect(route('admin_sites', [$model->slug]));
|
|
}
|
|
|
|
$ext = $file_ex[$image['output']['type']];
|
|
// Original file name
|
|
$name = $image['output']['name'];
|
|
$name = \App\Services\Slim::sanitizeFileName($name);
|
|
$path = 'images/iq_images/';
|
|
|
|
$image_name = "";
|
|
do {
|
|
$image_name = uniqid('', false) . '_' . $name;
|
|
} while (\Storage::disk('public')->exists($path . $image_name));
|
|
|
|
$data = \Storage::disk('public')->put(
|
|
$path . $image_name,
|
|
$data
|
|
);
|
|
|
|
$iq_image = IqImage::create([
|
|
'filename' => $image_name,
|
|
'original_name' => $image['output']['name'],
|
|
'ext' => $ext,
|
|
'mine' => $image['output']['type'],
|
|
'size' => $image['input']['size']
|
|
]);
|
|
|
|
$model->iq_image_id = $iq_image->id;
|
|
$model->save();
|
|
|
|
\Session()->flash('alert-success', __('msg.file_uploaded'));
|
|
return redirect(route('admin_sites', [$model->slug]));
|
|
}
|
|
\Session()->flash('alert-danger', __('msg.file_empty'));
|
|
return redirect(route('admin_sites', [$model->slug]));
|
|
} catch (\Exception $e) {
|
|
\Session()->flash('alert-danger', "Error: " . $e);
|
|
return redirect(route('admin_sites', [$model->slug]));
|
|
}
|
|
}
|
|
|
|
public function imageDelete($site, $image_id)
|
|
{
|
|
|
|
$iq_image = IqImage::findOrFail($image_id);
|
|
$model = IqSite::find(1);
|
|
|
|
|
|
if ($iq_image->id == $model->iq_image->id) {
|
|
$file = 'images/iq_images/' . $iq_image->filename;
|
|
\Storage::disk('public')->delete($file);
|
|
$model->iq_image_id = NULL;
|
|
$model->save();
|
|
$iq_image->delete();
|
|
|
|
|
|
\Session()->flash('alert-success', __('msg.file_deleted'));
|
|
return redirect(route('admin_sites', [$model->slug]));
|
|
}
|
|
\Session()->flash('alert-danger', __('msg.file_not_found'));
|
|
return redirect(route('admin_sites', [$model->slug]));
|
|
}
|
|
|
|
public function imageAttribute($site, $image_id, $attr, $val = false)
|
|
{
|
|
|
|
$iq_image = IqImage::findOrFail($image_id);
|
|
|
|
$iq_image->{$attr} = $val;
|
|
$iq_image->save();
|
|
|
|
\Session()->flash('alert-success', "Wert gespeichert");
|
|
return redirect()->back();
|
|
}
|
|
}
|