126 lines
3.3 KiB
PHP
126 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
use App\Person;
|
|
use App\MemberState;
|
|
|
|
|
|
class PeopleController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$people = Person::orderBy('name_or_company')->get();
|
|
|
|
return view('people.index', compact('people'));
|
|
}
|
|
|
|
public function show(Person $person)
|
|
{
|
|
$memberStates = $person->memberStates;
|
|
$targets = $person->targets;
|
|
$actuals = $person->actuals;
|
|
|
|
return view('people.show', compact('person', 'memberStates', 'targets',
|
|
'actuals'));
|
|
}
|
|
|
|
// Show the form for creating a new person
|
|
public function create() {
|
|
$person = new Person();
|
|
return view('people.create', compact('person'));
|
|
}
|
|
|
|
// Store a new person in the database
|
|
public function store() {
|
|
// Create a new Person model and fill it with data from the request
|
|
$person = new Person();
|
|
|
|
$person->fill(request([
|
|
'title',
|
|
'name_or_company',
|
|
'surname',
|
|
'street',
|
|
'postal_code',
|
|
'city',
|
|
'email',
|
|
'mobile',
|
|
'phone',
|
|
'date_of_birth'
|
|
]));
|
|
|
|
$person->save();
|
|
|
|
// If the member states data is non-empty, save a record to the database.
|
|
if (!is_null(request('joining_date')[0])) {
|
|
$memberState = new MemberState();
|
|
|
|
$memberState->joining_date = request('joining_date')[0];
|
|
$memberState->leaving_date = request('leaving_date')[0];
|
|
|
|
$person->memberStates()->save($memberState);
|
|
}
|
|
|
|
// Redirect to the people index page
|
|
return redirect('/people');
|
|
}
|
|
|
|
// Show the form for editing a existing person
|
|
public function edit(Person $person)
|
|
{
|
|
return view('people.edit', compact('person'));
|
|
}
|
|
|
|
// Store the edited data of an existing person in the database
|
|
public function update(Person $person)
|
|
{
|
|
// Create a new Person model and fill it with data from the request
|
|
$person->fill(request([
|
|
'title',
|
|
'name_or_company',
|
|
'surname',
|
|
'street',
|
|
'postal_code',
|
|
'city',
|
|
'email',
|
|
'mobile',
|
|
'phone',
|
|
'date_of_birth'
|
|
]));
|
|
|
|
$person->save();
|
|
|
|
// If the member states data is non-empty, save a record to the database.
|
|
if ($person->title != 'Firma') {
|
|
for ($i = 0; $i < sizeof(request('joining_date')); $i++) {
|
|
$memberState = MemberState::find(request('memberStateId')[$i]);
|
|
|
|
$memberState->joining_date = request('joining_date')[$i];
|
|
$memberState->leaving_date = request('leaving_date')[$i];
|
|
|
|
$memberState->save();
|
|
}
|
|
}
|
|
|
|
// Redirect to the people index page
|
|
return redirect('/people');
|
|
}
|
|
|
|
// Show the delete dialog for a given person
|
|
public function delete(Person $person)
|
|
{
|
|
return view('people.delete', compact('person'));
|
|
}
|
|
|
|
// Actually delete a person from the database
|
|
public function destroy(Person $person)
|
|
{
|
|
$person->delete();
|
|
|
|
return redirect('/people');
|
|
}
|
|
}
|