2019-03-18 08:53:00 -03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-09-08 19:45:49 -03:00
|
|
|
use App\Models\Contact;
|
2019-03-18 08:53:00 -03:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2019-04-15 22:18:29 -03:00
|
|
|
use Illuminate\Support\Facades\Redirect;
|
2020-07-29 11:58:25 -03:00
|
|
|
use Illuminate\Support\Facades\Request;
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
use Inertia\Inertia;
|
2019-03-18 08:53:00 -03:00
|
|
|
|
|
|
|
class ContactsController extends Controller
|
|
|
|
{
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
return Inertia::render('Contacts/Index', [
|
|
|
|
'filters' => Request::all('search', 'trashed'),
|
|
|
|
'contacts' => Auth::user()->account->contacts()
|
2019-04-02 16:08:52 -03:00
|
|
|
->with('organization')
|
2019-03-18 08:53:00 -03:00
|
|
|
->orderByName()
|
|
|
|
->filter(Request::only('search', 'trashed'))
|
2021-05-10 16:27:31 -03:00
|
|
|
->paginate(10)
|
2021-03-01 12:19:46 -03:00
|
|
|
->withQueryString()
|
2021-05-10 16:27:31 -03:00
|
|
|
->through(fn ($contact) => [
|
|
|
|
'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,
|
|
|
|
]),
|
2019-03-18 08:53:00 -03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
return Inertia::render('Contacts/Create', [
|
|
|
|
'organizations' => Auth::user()->account
|
|
|
|
->organizations()
|
|
|
|
->orderBy('name')
|
|
|
|
->get()
|
|
|
|
->map
|
|
|
|
->only('id', 'name'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function store()
|
|
|
|
{
|
2019-04-15 22:18:29 -03:00
|
|
|
Auth::user()->account->contacts()->create(
|
2019-03-18 08:53:00 -03:00
|
|
|
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'],
|
|
|
|
])
|
2019-04-15 22:18:29 -03:00
|
|
|
);
|
|
|
|
|
2019-05-21 20:12:49 -03:00
|
|
|
return Redirect::route('contacts')->with('success', 'Contact created.');
|
2019-03-18 08:53:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
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'],
|
2021-05-10 16:27:31 -03:00
|
|
|
'organization_id' => [
|
|
|
|
'nullable',
|
|
|
|
Rule::exists('organizations', 'id')->where(fn ($query) => $query->where('account_id', Auth::user()->account_id)),
|
|
|
|
],
|
2019-03-18 08:53:00 -03:00
|
|
|
'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'],
|
|
|
|
])
|
|
|
|
);
|
2019-04-15 22:18:29 -03:00
|
|
|
|
2019-12-18 19:34:14 -03:00
|
|
|
return Redirect::back()->with('success', 'Contact updated.');
|
2019-03-18 08:53:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function destroy(Contact $contact)
|
|
|
|
{
|
|
|
|
$contact->delete();
|
2019-04-15 22:18:29 -03:00
|
|
|
|
2019-12-18 19:34:14 -03:00
|
|
|
return Redirect::back()->with('success', 'Contact deleted.');
|
2019-03-18 08:53:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function restore(Contact $contact)
|
|
|
|
{
|
|
|
|
$contact->restore();
|
2019-04-15 22:18:29 -03:00
|
|
|
|
2019-12-18 19:34:14 -03:00
|
|
|
return Redirect::back()->with('success', 'Contact restored.');
|
2019-03-18 08:53:00 -03:00
|
|
|
}
|
|
|
|
}
|