pingcrm/app/Http/Controllers/ContactsController.php

121 lines
4.1 KiB
PHP
Raw Normal View History

2019-03-18 08:53:00 -03:00
<?php
namespace App\Http\Controllers;
use App\Contact;
use Inertia\Inertia;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
class ContactsController extends Controller
{
public function index()
{
return Inertia::render('Contacts/Index', [
'filters' => Request::all('search', 'trashed'),
'contacts' => Auth::user()->account->contacts()
->orderByName()
->filter(Request::only('search', 'trashed'))
->paginate()
->transform(function ($contact) {
return [
'id' => $contact->id,
'name' => $contact->name,
'phone' => $contact->phone,
'city' => $contact->city,
'deleted_at' => $contact->deleted_at,
'organization' => $contact->organization ? $contact->organization->only('name') : null,
];
}),
]);
}
public function create()
{
return Inertia::render('Contacts/Create', [
'organizations' => Auth::user()->account
->organizations()
->orderBy('name')
->get()
->map
->only('id', 'name'),
]);
}
public function store()
{
return Auth::user()->account->contacts()->create(
Request::validate([
'first_name' => ['required', 'max:50'],
'last_name' => ['required', 'max:50'],
'organization_id' => ['nullable', Rule::exists('organizations', 'id')->where(function ($query) {
$query->where('account_id', Auth::user()->account_id);
})],
'email' => ['nullable', 'max:50', 'email'],
'phone' => ['nullable', 'max:50'],
'address' => ['nullable', 'max:150'],
'city' => ['nullable', 'max:50'],
'region' => ['nullable', 'max:50'],
'country' => ['nullable', 'max:2'],
'postal_code' => ['nullable', 'max:25'],
])
)->only('id');
}
public function edit(Contact $contact)
{
return Inertia::render('Contacts/Edit', [
'contact' => [
'id' => $contact->id,
'first_name' => $contact->first_name,
'last_name' => $contact->last_name,
'organization_id' => $contact->organization_id,
'email' => $contact->email,
'phone' => $contact->phone,
'address' => $contact->address,
'city' => $contact->city,
'region' => $contact->region,
'country' => $contact->country,
'postal_code' => $contact->postal_code,
'deleted_at' => $contact->deleted_at,
],
'organizations' => Auth::user()->account->organizations()
->orderBy('name')
->get()
->map
->only('id', 'name'),
]);
}
public function update(Contact $contact)
{
$contact->update(
Request::validate([
'first_name' => ['required', 'max:50'],
'last_name' => ['required', 'max:50'],
'organization_id' => ['nullable', Rule::exists('organizations', 'id')->where(function ($query) {
$query->where('account_id', Auth::user()->account_id);
})],
'email' => ['nullable', 'max:50', 'email'],
'phone' => ['nullable', 'max:50'],
'address' => ['nullable', 'max:150'],
'city' => ['nullable', 'max:50'],
'region' => ['nullable', 'max:50'],
'country' => ['nullable', 'max:2'],
'postal_code' => ['nullable', 'max:25'],
])
);
}
public function destroy(Contact $contact)
{
$contact->delete();
}
public function restore(Contact $contact)
{
$contact->restore();
}
}