<?php namespace Roche\Trainings\Controllers\Admin;
use App, Datatables, Lang, View;
use Training;
/**
* Trainings controller
* @author Tomica Korać <[email protected]>
*/
class AdminTrainingsController extends \AdminController {
/**
* Training Model
* @var Training
*/
protected $training;
/**
* Inject the models.
* @param Training $training
*/
public function __construct(Training $training)
{
parent::__construct();
$this->training = $training;
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function getCreate()
{
// Title
$title = Lang::get('trainings::admin/title.create_a_new_training');
// Show the page
return View::make('trainings::admin/trainings/create_edit', compact('title'));
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function postCreate()
{
// Declare the rules for the form validation
$rules = array(
'title' => 'required|min:3',
'description' => 'required|min:3'
);
// Validate the inputs
$validator = \Validator::make(\Input::all(), $rules);
// Check if the form validates with success
if ($validator->passes())
{
// Create a new training
// $user = \Auth::user();
// Insert the training data
$this->training->title = \Input::get('title');
$this->training->description = \Input::get('description');
$this->training->starts_at = \Input::get('starts_at');
$this->training->ends_at = \Input::get('ends_at');
$this->training->is_newcomer = \Input::get('is_newcomer');
$this->training->area = \Input::get('area');
// $this->training->user_id = $user->id;
// Was the training created?
if($this->training->save())
{
// Redirect to the new training page
return \Redirect::to('trainings/' . $this->training->id . '/edit')->with('success', Lang::get('trainings::admin/messages.create.success'));
}
// Redirect to the training create page
return \Redirect::to('trainings/create')->with('error', Lang::get('trainings::admin/messages.create.error'));
}
// Form validation failed
return \Redirect::to('trainings/create')->withInput()->withErrors($validator);
}
}