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/SBogers85/equichecker.com/app/KommaApp/Kms/Core/SectionController.php
<?php
/**
 *
 *
 * @author      Tim Van Samang <timvansamang@komma.pro>
 * @copyright   (c) 2012-2016, Komma Mediadesign
 */

namespace KommaApp\Kms\Core;

use KommaApp\Kms\Core\Sections\KmsSection;

class SectionController extends KmsController
{
    protected $section;
    protected $slug = null;

    function __construct(Kms $kms, KmsSection $section)
    {

        parent::__construct($kms);
        $this->section = $section;
    }


    /**
     * This method is called on the overview page.
     * It will render the section and view.
     *
     * @return mixed
     */
    public function index()
    {
        //Disable the right pane
        $this->section->showEntity = false;

        //Render the page
        return $this->section->render();
    }

    /**
     * This method is called when a item is selected.
     * By default it will generate an edit form.
     *
     * @return mixed
     */
    public function show($id)
    {
        //Call the edit method
        return $this->edit();
    }

    /**
     * This method is called when we want to edit an item.
     * It is called from the $this->show method.
     *
     * @return mixed
     */
    public function edit()
    {
        //Get the id from the uri
        $id = \Route::current()->getParameter($this->slug);
        //Load the model
        $this->section->loadModel($id);
        //Set error when available
        $this->section->setErrors(\Session::get('errors'));
        //Render the form
        return $this->section->render();
    }

    /**
     * This method will generate a new item form
     *
     * @return mixed
     */
    public function create()
    {
        //Set errors
        $this->section->setErrors(\Session::get('errors'));
        //Load an empty model for the form
        $this->section->loadModel();
        //Render the form
        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 $id
     * @return mixed
     */
    public function store($model = null)
    {
        //Set the model
        $this->section->setModel($model);

        $modelId = null;
        if (isset($model->id)) $modelId = $model->id;

        //Get the vallidation rules, messages, with id and true|false if id is set
        $validation = $this->section->getValidation(($model ? $modelId : null), isset($model), $model);


        //Validate the form data
        $validator = $this->section->validateAttributes($validation);
        //Check if the validator fails
        if ($validator->fails()) {
            //It fails, redirect with errors
            return \Redirect::action('\\' . get_class($this) . '@show',
                [
                    'site' => $this->kms->getCurrentSiteSlug(),
                    $this->slug => ($model ? $model->id : 'create')
                ])->withInput()->withErrors($validator->messages());
        }
        //Save the model and set the return data as id
        $id = $this->section->save($model);


        //Redirect to the form page
        return \Redirect::action('\\' . get_class($this) . '@show', [
            'site' => $this->kms->getCurrentSiteSlug(),
            $this->slug => $id
        ])
            ->with('success', 'Entity Saved')
            ->with('current_tab' , \Input::get('tab-slug')); // Pass the opened tab
    }


    /**
     * This method handles the update functionality.
     * And is called by the edit form.
     *
     * @return mixed
     */
    public function update()
    {
        //Get the id from the uri
        $id = \Route::current()->getParameter(str_replace('-', '_', $this->slug));

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

    /**
     * This method is called when a item will be deleted
     *
     * @return mixed
     */
    public function destroy()
    {
        //Get the id from the uri
        $id = \Route::current()->getParameter($this->slug);

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

        //Return to the item index
        return \Redirect::action('\\' . get_class($this) . '@index')
            ->with('message', 'Entity Removed');
    }
}