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/Neopoints/momsecurity.be/app/Komma/Kms/Core/SectionController.php
<?php
namespace App\Komma\Kms\Core;

use App\Komma\Forms\Models\Request;
use App\Komma\Kms\Core\Sections\Section;
use App\Komma\Sites\SiteServiceInterface;
use Illuminate\Database\Eloquent\Model;
use App\Komma\Users\Models\User;

class SectionController extends KmsController
{
    /** @var Section $section */
    protected $section;
    protected $slug = null;

    /**
     * This method is called on the overview page.
     * It will render the section and view.
     *
     * @return mixed
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function index()
    {
        $this->authorize('index', $this->forModelName);

        //Create a root model if the forModelName instance implements the treeInterface
        $this->createRootTreeModelIfNeeded();

        //Disable the right pane
        $this->section->showEntity = false;

        //Render the page
        $this->section->loadModels(); //we need to do this here because this won't work in section constructors since there the authenticated user is not available in laravel 5.3

        $this->section->setForModelName($this->forModelName);
        return $this->section->render();
    }

    /**
     * This method is called when a item is selected.
     * By default it will generate an edit form.
     *
     * @param Model $model
     * @return mixed
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function show($model)
    {
        $this->authorize('show', $model);

        //Create a root model if the forModelName instance implements the treeInterface
        $this->createRootTreeModelIfNeeded();

        //Call the edit method
        $this->section->loadModels(); //we need to do this here because this won't work in section constructors since there the authenticated user is not available in laravel 5.3
        return $this->edit($model);
    }

    /**
     * This method is called when we want to edit an item.
     * It is called from the $this->show method.
     *
     * @param $model
     * @return mixed
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function edit($model)
    {
        $this->authorize('edit', $model);

        //Create a root model if the forModelName instance implements the treeInterface
        $this->createRootTreeModelIfNeeded();

        if (is_string($model) || is_int($model)) {
            //Get the model via route model binding
            $model = \Route::getCurrentRoute()->parameter($this->slug);
        }

        // Build section tabs
        $this->section->getSectionTabDirector()->buildTabs();

        //Load the model
        $this->section->loadModel($model);

        $this->section->setForModelName($this->forModelName);
        return $this->section->render();
    }

    /**
     * This method will generate a new item form
     *
     * @return mixed
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function create()
    {
        $this->authorize('create', $this->forModelName);

        //Create a root model if the forModelName instance implements the treeInterface
        $this->createRootTreeModelIfNeeded();

        // Build section tabs
        $this->section->getSectionTabDirector()->buildTabs();

        //Load an empty model for the form
        $this->section->loadModel();

        $this->section->loadModels();

        //Render the form
        $this->section->setForModelName($this->forModelName);
        return $this->section->render();
    }

    /**
     * This method will validate and save the model
     * It is called on create form submit,
     * and it is called on the form edit.
     *
     * @param null|Model $model
     * @param bool $updateExistingModel If we are updating an model that exists (true) or a new one (false)
     * @return mixed
     * @throws \Exception
     */
    public function store($model = null, $updateExistingModel = false)
    {
//        dd('herere', $updateExistingModel, \Auth::user()->can('store', $this->forModelName), \Auth::user()->can('update', $model), $model, $this->forModelName);
        if($updateExistingModel == false) {
            $this->authorize('store', $this->forModelName);
        } else {
            $this->authorize('update', $model);
        }

        //Create a root model if the forModelName instance implements the treeInterface
        $this->createRootTreeModelIfNeeded();

        $model = $this->section->setOrCreateModel($model);

        $this->section->generateAttributesAndAddThemToTabs();

        //Validate the form data
        $validator = $this->section->validateInputAndReturnValidator();

        //Check if the validator fails
        if ($validator->fails()) {
            //It fails, redirect back with errors
            return \Redirect::back()->withInput()->withErrors($validator->messages());
        }

        $model = $this->section->save($model);

        //Set the siteSlug variable for routes that need the siteSlug variable in their route
        $routeParameters = [];
        $site = $this->siteService->getCurrentSite();
        if($site->exists)
            $routeParameters['siteSlug'] = $site->slug;
            $routeParameters[$this->slug] = $model;

        $sessionData = [
            'tabslug' => \Input::get('tabslug'),
            'success' => __('kms/global.saved')
        ];

        return \Redirect::action('\\'.get_class($this) . '@show', $routeParameters)->with($sessionData);
    }


    /**
     * This method handles the update functionality.
     * And is called by the edit form.
     * The method is route model binded via the RouteServiceProvider to the user class
     *
     * @param $idOrModel User|int
     * @return mixed
     * @throws \Exception
     */
    public function update($idOrModel)
    {
        //Create a root model if the forModelName instance implements the treeInterface
        $this->createRootTreeModelIfNeeded();

        if(!is_a($idOrModel, Model::class)) throw new \InvalidArgumentException("Please make sure that you've set route model binding for the section '".get_class($this)."'");

        //Handle the rest in the store method
        return $this->store($idOrModel, true);
    }

    /**
     * This method is called when a item will be deleted
     *
     * @param Model $model
     * @return mixed
     * @throws \Exception
     */
    public function destroy(Model $model)
    {
        $this->authorize('store', $model);

        $this->createRootTreeModelIfNeeded();

        //Set the siteSlug variable for routes that need the siteSlug variable in their route
        $routeParameters = [];
        $site = $this->siteService->getCurrentSite();
        if($site->exists)
            $routeParameters['siteSlug'] = $site->slug;
        $routeParameters[$this->slug] = $model;

        //Call the destroy method
        $this->section->destroy($model);

        //Return to the item index
        return \Redirect::action('\\' . get_class($this) . '@index', $routeParameters)
            ->with('message', __('kms/global.removed'));
    }

    /**
     * Get the models a a tree for api calls
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function getStructureAsJson()
    {
        $this->authorize('index', $this->forModelName);

        $tree = $this->treeService->getStructure();
        return \Response::json($tree);
    }

    /**
     * Change the model tree
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function setStructureAsJson()
    {
        // Special way to get POST data send by ajax on IIS servers
        $data = json_decode(\Request::instance()->getContent());
        $tree = json_decode($data->tree, true);

        $this->treeService->setStructure($tree);

        return \Response::json($tree);
    }
}