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/SBogers10/vebon.komma.pro/app/KommaApp/Kms/Core/SiteSectionController.php
<?php
/**
 *
 *
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2016, Komma Mediadesign
 */

namespace KommaApp\Kms\Core;

use KommaApp\Kms\Core\Sections\KmsSection;

class SiteSectionController 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.
     *
     * @param $site | Site name
     * @return mixed
     */
    public function index($site)
    {
        //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.
     *
     * @param $site | Site name
     * @param $id | Model id
     * @return mixed
     */
    public function show($site, $id)
    {
        return $this->edit($id);
    }

    /**
     * This method is called when we want to edit an item.
     * It is called from the $this->show method.
     *
     * @param $site | Site name
     * @param $id | Model id
     * @return mixed
     */
    public function edit($site, $id = null)
    {
        // Get input
        if (!$id) $id = \Route::current()->getParameter(str_replace('-', '_', $this->slug));

        $this->section->loadModel($id);
        $this->section->setErrors(\Session::get('errors'));
        return $this->section->render();
    }

    /**
     * This method will generate a new item form
     *
     * @param $site | Site name
     * @return mixed
     */
    public function create($site)
    {
        //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($site, $model = null)
    {
        $this->section->setModel($model);

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

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

        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.
     *
     * @param $site | Site name
     * @param $id | Model id
     * @return mixed
     */
    public function update($site, $id)
    {
        //Get the id from the uri
        if(!$id)$id = \Route::current()->getParameter(str_replace('-', '_', $this->slug));

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

    /**
     * This method is called when a item will be deleted
     *
     * @param $site | Site name
     * @param $id | Model id
     * @return mixed
     */
    public function destroy($site, $id)
    {
        // Get input
        if(!$id)$id = \Route::current()->getParameter(str_replace('-', '_', $this->slug));

        // Action
        $this->section->destroy($id);

        // Create output
        return \Redirect::action('\\'.get_class($this) . '@index', [
            'site' => $this->kms->getCurrentSiteSlug()
        ]);
    }
}