mein-sterntours/app/Http/Controllers/Settings/TravelCountryController.php

164 lines
5.9 KiB
PHP
Executable file

<?php
namespace App\Http\Controllers\Settings;
use App\Http\Controllers\Controller;
use App\Models\GeneralFile;
use App\Models\TravelCountry;
use App\Models\TravelCountryService;
use App\Models\TravelNationality;
use App\Repositories\CustomerFileRepository;
use App\Repositories\GeneralFileRepository;
use App\Services\Util;
use IqContent\LaravelFilemanager\Lfm;
use Request;
class TravelCountryController extends Controller
{
/*
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware(['superadmin', '2fa']);
}
public function index($step = false)
{
$data = [
'travel_countries' => TravelCountry::all(),
];
return view('settings.travel_country.index', $data);
}
public function detail($id, $step = false)
{
if ($id == "new") {
$model = new TravelCountry();
$id = 'new';
$model->active_backend = 1;
} else {
$model = TravelCountry::findOrFail($id);
$id = $model->id;
}
$data = [
'model' => $model,
'id' => $id,
'step' => $step,
'travel_nationalities' => TravelNationality::where('active', true)->get(),
'lfm_helper' => app(Lfm::class),
];
return view('settings.travel_country.detail', $data);
}
public function update($id)
{
$data = Request::all();
if (isset($data['update-action'])) {
if ($data['update-action'] === 'save-travel-county-service') {
$data['active'] = true; //isset($data['active']) ? true : false;
$travel_country = TravelCountry::findOrFail($id);
$data['travel_country_id'] = $travel_country->id;
$data['crm_travel_country_id'] = $travel_country->crm_id;
if ($data['travel_county_service_id'] === 'new') {
$model = TravelCountryService::create($data);
} else {
$model = TravelCountryService::find($data['travel_county_service_id']);
$model->fill($data);
$model->save();
}
\Session()->flash('alert-save', '1');
return redirect($data['back']);
}
}
$data['contact_emails'] = isset($data['contact_emails']) ? Util::_explodeLines($data['contact_emails']) : null;
if (!isset($data['contact_lands'])) {
$data['contact_lands'] = null;
}
$data['action'] = isset($data['action']) ? $data['action'] : false;
/*
* $data['is_customer_country'] = isset($data['is_customer_country']) ? true : false;
$data['active_frontend'] = isset($data['active_frontend']) ? true : false;
$data['active_backend'] = isset($data['active_backend']) ? true : false;
*/
if ($id === "new") {
$model = TravelCountry::create($data);
} else {
$model = TravelCountry::find($id);
$model->fill($data);
$model->save();
}
//travel_nationality_requirement
if (isset($data['travel_nationality_requirement'])) {
foreach ($data['travel_nationality_requirement'] as $travel_nationality_id => $text) {
$model->setNationalityRequirement($travel_nationality_id, $text);
}
}
//TODO for this time
$tc = \App\Models\Sym\TravelCountry::find($model->crm_id);
if (!$tc) {
$tc = \App\Models\Sym\TravelCountry::create($data);
$model->crm_id = $tc->id;
$model->save();
} else {
$tc->fill($data);
$tc->save();
}
\Session()->flash('alert-save', '1');
return redirect(route('admin_settings_travel_country_detail', [$model->id, $data['action']]));
}
public function delete($id, $del = "travel_country")
{
if ($del === 'travel_country') {
$model = TravelCountry::findOrFail($id);
if ($model->travel_nationality_requirements) {
foreach ($model->travel_nationality_requirements as $travel_nationality_requirement) {
$travel_nationality_requirement->delete();
}
}
$tc = \App\Models\Sym\TravelCountry::find($model->crm_id);
if ($tc) {
$tc->delete();
}
$model->delete();
\Session()->flash('alert-success', 'Eintrag gelöscht');
return redirect()->back();
}
if ($del === 'general_file') {
$general_file = GeneralFile::findOrFail($id);
$travel_country = $general_file->travel_country;
$fileRepo = new GeneralFileRepository($general_file);
$fileRepo->_set('disk', 'general');
$fileRepo->delete();
$general_file->delete();
\Session()->flash('alert-success', 'Datei gelöscht');
return redirect(route('admin_settings_travel_country_detail', [$travel_country->id, 'data']));
}
if ($del === 'country_service') {
$travel_country_service = TravelCountryService::findOrFail($id);
$travel_country = $travel_country_service->travel_country;
//check as entry
if ($travel_country_service->booking_country_services->count() > 0) {
\Session()->flash('alert-error', 'Die Leistung kann nicht gelöscht werden, diese hat Einträge bei den Buchungen');
return redirect(route('admin_settings_travel_country_detail', [$travel_country->id, 'services']));
}
$travel_country_service->delete();
\Session()->flash('alert-success', 'Eintrag gelöscht');
return redirect(route('admin_settings_travel_country_detail', [$travel_country->id, 'services']));
}
}
}