File: D:/HostingSpaces/SBogers93/fitale.nl/workbench/komma/kms/src/Komma/Kms/Core/SectionController.php
<?php
/**
* Short description for the file.
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign
*/
namespace Komma\Kms\Core;
use Illuminate\Http\Request;
use Komma\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;
}
public function index()
{
$this->section->showEntity = false;
return $this->section->render();
}
public function show()
{
return $this->edit();
}
/**
* Show add new form
*
* @return mixed
*/
public function create()
{
// Load entity in the section
$this->section->loadEntity();
// Push errors in the section
$this->section->setErrors(\Session::get('errors'));
// Render the view
return $this->section->render();
}
/**
* Save a new item
*
* @return mixed
*/
public function store()
{
$data = \Input::all();
// Load entity in the section
$this->section->loadEntity();
// Populate attributes
$this->section->setAttributesData($data);
// Create a validator object
$validator = $this->section->validateAttributes();
// Validate data
if($validator->fails())
{
return \Redirect::action(get_class($this).'@create')
->withInput()
->withErrors($validator->messages());
}
// Save entity
$entity = $this->section->save();
return \Redirect::action(get_class($this).'@show',[
$this->slug => $entity->id
])
->with('success', 'Entity Saved');
}
/**
* Show edit form
*
* @return mixed
*/
public function edit()
{
// Get id from slug as snake case
// Directly to snake_case did'nt work
// First as camel did work
$camelSlug = camel_case($this->slug);
$snakeSlug = snake_case($camelSlug);
$id = \Route::current()->getParameter($snakeSlug);
// Load entity in the section
$this->section->loadEntity($id);
// Parse data into attributes
$this->section->populateAttributes();
$this->section->setErrors(\Session::get('errors'));
return $this->section->render();
}
/**
* Save an existing item
*
* @return mixed
*/
public function update()
{
// Get id from slug as snake case
// Directly to snake_case did'nt work
// First as camel did work
$camelSlug = camel_case($this->slug);
$snakeSlug = snake_case($camelSlug);
$id = \Route::current()->getParameter($snakeSlug);
// Get all input data
$data = \Input::all();
// Load entity in section
$this->section->loadEntity($id);
// Push data into attributes
$this->section->setAttributesData($data);
// Create a validator
$validator = $this->section->validateAttributes();
// Validate
if($validator->fails())
{
return \Redirect::action(get_class($this).'@show', [$this->slug => $id])
->withInput()
->withErrors($validator->messages());
}
// Save the entity
$this->section->update($id);
return \Redirect::action(get_class($this).'@show', [$this->slug => $id])
->with('success', 'Entity Saved')
->withInput(compact(\Input::get('tab-slug'))); // Pass the opened tab*/
}
public function destroy()
{
// Get id from slug as snake case
// Directly to snake_case did'nt work
// First as camel did work
$camelSlug = camel_case($this->slug);
$snakeSlug = snake_case($camelSlug);
$id = \Route::current()->getParameter($snakeSlug);
// Action
$this->section->destroy($id);
// Create output
return \Redirect::action(get_class($this).'@index')
->with('message', 'Entity Removed');
}
}