HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/MdnDirecteur/hours.komma.cloud/app/Komma/Settings/Countries/CountryController.php
<?php

namespace App\Komma\Settings\Countries;

use App\Http\Controllers\Controller;
use App\Komma\Messages\MessageController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

class CountryController extends Controller
{
    private $messageController;

    public function __construct(MessageController $messageController)
    {
        $this->middleware('auth');
//        $this->middleware('permission:view_settings');
        $this->messageController = $messageController;
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        //get all countries
        $countries = Country::all();

        //return
        return view('settings.country.index', compact('countries'));
    }


    /**
     *
     */
    public function create()
    {
        //get reference
        $ref = \Request::filled('ref') ? \Request::get('ref') : '';

        //return
        return view('settings.country.create', compact('ref'));
    }


    /**
     *
     */
    public function edit($country)
    {
        //find country
        $country = Country::find($country);
        //get reference
        $ref = \Request::filled('ref') ? \Request::get('ref') : '';
        //make sure to update the country
        $update = true;

        //return
        return view('settings.country.edit', compact('country', 'ref', 'update'));
    }


    /**
     *
     */
    public function store(Request $request)
    {
        //check if deleted exist then restore
        if (!empty(Country::withTrashed()->where('name', $request->name)->get()->first()->deleted_at)) return $this->update($request, Country::withTrashed()->where('name', $request->name)->get()->first()->id);

        $this->validate($request, [
            'name' => 'required|unique:countries',
        ]);

        //store country
        $country = new Country();
        $country->name = $request->name;
        $country->save();

        //make message + activity
        $this->messageController->create("Nieuw land", $country);

        // Find redirect route
        $redirectRoute = 'instellingen/landen';
        if ($request->filled('ref')) {
            $ref = $request->get('ref');
            $redirectRoute = str_replace('-', '/', $ref);
        }

        //redirect
        return redirect('/' . $redirectRoute);
    }


    /**
     * @param Request $request
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required',
        ]);

        //update expense unit
        $country = Country::withTrashed()->find($id);
        $country->name = $request->name;
        $country->deleted_at = null;
        $country->save();

        //make message
        $this->messageController->changed("Land", $country);

        // Find redirect route
        $redirectRoute = 'instellingen/landen';
        if ($request->filled('ref')) {
            $ref = $request->get('ref');
            $redirectRoute = str_replace('-', '/', $ref);
        }

        //redirect
        return redirect('/' . $redirectRoute);
    }

    /**
     * @param $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function destroy($id)
    {
        //find country
        $country = Country::find($id);
        //if country has related items
        if (count($country->Companies)) {
            //message
            $value = "land";
            $underlying = "klanten";
            $this->messageController->failed("Land", "klanten");

            //return
            return redirect('/instellingen/landen');
        } else {
            //find
            $country->delete();

            //message + activity
            $this->messageController->destroyed("land", $country);
        }

        //return
        return redirect('/instellingen/landen');
    }


    /**
     * @param $country
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function restore($country)
    {
        \DB::transaction(function () use ($country) {
            //find country
            $country = Country::withTrashed()->find($country);
            $country->restore();
            //message + activity
            $this->messageController->recovered("Land", $country);
        });

        //return
        return redirect('/instellingen/landen');
    }
}