-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAddressController.php
More file actions
137 lines (118 loc) · 5.44 KB
/
Copy pathAddressController.php
File metadata and controls
137 lines (118 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreAddressRequest;
use App\Http\Requests\UpdateAddressRequest;
use App\Models\Address;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Attributes\Controllers\Authorize;
use Illuminate\Routing\Attributes\Controllers\Middleware;
use Illuminate\Support\Facades\Redirect;
use Illuminate\View\View;
/**
* In production, addresses are primarily managed by the person or contact controller.
* In testing, the address controller uses CRUD-style permissions which are theoretical rather than the contact CRUD permissions used in production
* In other words, in production, the create-contact permission is used rather than create-address.
*/
#[Middleware('auth')]
class AddressController extends Controller
{
/**
* Display a listing of the resource.
*/
#[Authorize('show-address')]
public function index(): View
{
$addresses = \App\Models\Address::orderBy('postal_code', 'asc')->with('addressee')->paginate(25, ['*'], 'addresses');
return view('addresses.index', compact('addresses'));
}
/**
* Show the form for creating a new resource.
*/
#[Authorize('create-address')]
public function create(): View
{
$countries = \App\Models\Country::orderBy('iso_code')->pluck('iso_code', 'id');
$countries->prepend('N/A', '');
$states = \App\Models\StateProvince::orderBy('name')->whereCountryId(config('polanco.country_id_usa'))->pluck('name', 'id');
$states->prepend('N/A', '');
$contacts = \App\Models\Contact::orderBy('sort_name')->pluck('sort_name', 'id');
$location_types = \App\Models\LocationType::whereIsActive(1)->orderBy('name')->pluck('name', 'id');
return view('addresses.create', compact('countries', 'states', 'contacts', 'location_types'));
}
/**
* Store a newly created resource in storage.
*/
#[Authorize('create-address')]
public function store(StoreAddressRequest $request): RedirectResponse
{
$address = new \App\Models\Address;
$address->contact_id = $request->input('contact_id');
$address->location_type_id = $request->input('location_type_id');
$address->is_primary = $request->input('is_primary');
$address->street_address = $request->input('street_address');
$address->supplemental_address_1 = $request->input('supplemental_address_1');
$address->city = $request->input('city');
$address->state_province_id = $request->input('state_province_id');
$address->postal_code = $request->input('postal_code');
$address->country_id = $request->input('country_id');
$address->save();
flash('Address ID#: <a href="'.url('/address/'.$address->id).'">'.$address->id.'</a> added')->success();
return Redirect::action([self::class, 'index']);
}
/**
* Display the specified resource.
*/
#[Authorize('show-address')]
public function show(int $id): View
{
$address = \App\Models\Address::with('addressee')->findOrFail($id);
return view('addresses.show', compact('address'));
}
/**
* Show the form for editing the specified resource.
*/
#[Authorize('update-address')]
public function edit(int $id): View
{
$countries = \App\Models\Country::orderBy('iso_code')->pluck('iso_code', 'id');
$countries->prepend('N/A', '');
$states = \App\Models\StateProvince::orderBy('name')->whereCountryId(config('polanco.country_id_usa'))->pluck('name', 'id');
$states->prepend('N/A', '');
$contacts = \App\Models\Contact::orderBy('sort_name')->pluck('sort_name', 'id');
$location_types = \App\Models\LocationType::whereIsActive(1)->orderBy('name')->pluck('name', 'id');
$address = \App\Models\Address::findOrFail($id);
return view('addresses.edit', compact('address', 'countries', 'states', 'contacts', 'location_types'));
}
/**
* Update the specified resource in storage.
*/
#[Authorize('update-address')]
public function update(UpdateAddressRequest $request, int $id): RedirectResponse
{
$address = \App\Models\Address::findOrFail($id);
$address->contact_id = $request->input('contact_id');
$address->location_type_id = $request->input('location_type_id');
$address->is_primary = $request->input('is_primary');
$address->street_address = $request->input('street_address');
$address->supplemental_address_1 = $request->input('supplemental_address_1');
$address->city = $request->input('city');
$address->state_province_id = $request->input('state_province_id');
$address->postal_code = $request->input('postal_code');
$address->country_id = $request->input('country_id');
$address->save();
flash('Address ID#: <a href="'.url('/address/'.$address->id).'">'.$address->id.'</a> updated')->success();
return Redirect::action([self::class, 'show'], $address->id);
}
/**
* Remove the specified resource from storage.
*/
#[Authorize('delete-address')]
public function destroy(int $id): RedirectResponse
{
$address = \App\Models\Address::findOrFail($id);
$contact_id = $address->contact_id;
\App\Models\Address::destroy($id);
flash('Address ID#: '.$address->id.' deleted')->warning()->important();
return Redirect::action([\App\Http\Controllers\PersonController::class, 'show'], $contact_id);
}
}