dienstplan/app/Http/Controllers/VeranstaltungController.php
2017-07-08 10:26:50 +02:00

138 lines
3.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Veranstaltung;
use App\Eintragung;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class VeranstaltungController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('veranstaltung.index', ['veranstaltungen' => Veranstaltung::get()->sortBy('beginn')]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$v = new Veranstaltung();
$v->beginn = \Carbon\Carbon::today();
$v->gaeste = 0;
return view ('veranstaltung.create',
$v);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$input = $request->input();
$v = new Veranstaltung();
$v->name = $input['name'];
$v->gaeste = (int)$input['gaeste'];
$v->beginn = new \Carbon\Carbon($input['beginn']);
if ($input['ende']) {
$v->ende = new \Carbon\Carbon($input['ende']);
}
$v->hinweise = $input['hinweise'] ?: "";
if ($v->save()) {
return redirect()->route('veranstaltung.show', [$v] );
}
return response()->json($v);
}
/**
* Display the specified resource.
*
* @param \App\Veranstaltung $veranstaltung
* @return \Illuminate\Http\Response
*/
public function show(Veranstaltung $veranstaltung)
{
if ($veranstaltung->has('eintragungen')) {
$veranstaltung = $veranstaltung->with('eintragungen.user')->find($veranstaltung->id);
}
return view('veranstaltung.show', $veranstaltung);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Veranstaltung $veranstaltung
* @return \Illuminate\Http\Response
*/
public function edit(Veranstaltung $veranstaltung)
{
return view('veranstaltung.edit', $veranstaltung);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Veranstaltung $veranstaltung
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Veranstaltung $veranstaltung)
{
if ($request->isMethod('put')) {
$i = $request->input();
$veranstaltung->gaeste = $i['gaeste'];
$veranstaltung->name = $i['name'];
$veranstaltung->hinweise = $i['hinweise'];
$veranstaltung->beginn = new \Carbon\Carbon($i['beginn']);
$veranstaltung->ende = $i['ende'] ? new \Carbon\Carbon($i['ende']) : null;
if($veranstaltung->save()){
return redirect()->route('veranstaltung.show', [$veranstaltung]);
}
}
return response()->json($veranstaltung);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Veranstaltung $veranstaltung
* @return \Illuminate\Http\Response
*/
public function destroy(Veranstaltung $veranstaltung)
{
$veranstaltung->delete();
return redirect()->route('veranstaltung.index');
}
public function enter(Request $request, Veranstaltung $veranstaltung)
{
$eintragung = new Eintragung();
$eintragung->veranstaltung()->associate($veranstaltung);
$eintragung->user()->associate(\Auth::user());
if($eintragung->save()){
return redirect()->route('veranstaltung.index');
}
return response()->json($eintragung);
}
}