File: D:/HostingSpaces/SBogers10/vebon.komma.pro/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.
*
* @param $idOrModel
* @return mixed
*/
public function show($idOrModel)
{
//Call the edit method
return $this->edit($idOrModel);
}
/**
* This method is called when we want to edit an item.
* It is called from the $this->show method.
*
* @return mixed
*/
public function edit($idOrModel)
{
if (is_string($idOrModel) || is_int($idOrModel)) {
//Get the id from the uri
$idOrModel = \Route::current()->getParameter($this->slug);
}
//Load the model
$this->section->loadModel($idOrModel);
//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($idOrModel)
{
if (is_string($idOrModel) || is_int($idOrModel)) {
//Get the id from the uri
$idOrModel = \Route::current()->getParameter(str_replace('-', '_', $this->slug));
}
//Handle the rest in the store method
return $this->store($idOrModel);
}
/**
* This method is called when a item will be deleted
*
* @return mixed
*/
public function destroy($idOrModel)
{
if (is_string($idOrModel) || is_int($idOrModel)) {
//Get the id from the uri
$idOrModel = \Route::current()->getParameter($this->slug);
}
//Call the destroy method
$this->section->destroy($idOrModel);
//Return to the item index
return \Redirect::action('\\' . get_class($this) . '@index')
->with('message', 'Entity Removed');
}
}