File: D:/HostingSpaces/SBogers10/douven.komma.pro/app/KommaApp/Kms/Core/SectionController.php
<?php
/**
*
*
* @copyright (c) 2012-2016, Komma
*/
namespace App\KommaApp\Kms\Core;
use App\KommaApp\Kms\Core\Sections\Section;
use Illuminate\Database\Eloquent\Model;
use App\KommaApp\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
*/
public function index()
{
//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
*/
public function show($model)
{
//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
*/
public function edit($model)
{
if (is_string($model) || is_int($model)) {
//Get the id from the uri
// $id = \Route::current()->getParameter($this->slug);
$model = \Route::getCurrentRoute()->parameter($this->slug);
}
// Build section tabs
$this->section->getSectionTabDirector()->buildTabs();
//Load the model
$this->section->loadModel($model);
//Set error when available
$this->section->setErrors(\Session::get('errors'));
//Render the form
$this->section->setForModelName($this->forModelName); //TODO fixme
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'));
// 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); //TODO fixme
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)
{
$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());
}
$id = $this->section->save($model);
$routeParameters = [];
if($this->kms->getSiteSlug())
$routeParameters['site'] = $this->kms->getSiteSlug();
$routeParameters[$this->slug] = $id;
return \Redirect::action('\\'.get_class($this) . '@show', $routeParameters)
->with('success', __('kms/global.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.
* 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)
{
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)
{
//Call the destroy method
$this->section->destroy($model);
//Return to the item index
return \Redirect::action('\\' . get_class($this) . '@index')
->with('message', __('kms/global.removed'));
}
}