NathanGeerinck /
laravel-newsletter
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Http\Controllers; |
||
| 4 | |||
| 5 | use App\Models\MailingList; |
||
| 6 | use App\Models\Subscription; |
||
| 7 | use Illuminate\Http\Request; |
||
| 8 | use Maatwebsite\Excel\Facades\Excel; |
||
| 9 | use App\Http\Requests\SubscriptionRequest; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Class SubscriptionController. |
||
| 13 | */ |
||
| 14 | class SubscriptionController extends Controller |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @param Request $request |
||
| 18 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 19 | */ |
||
| 20 | public function index(Request $request) |
||
| 21 | { |
||
| 22 | $subscriptions = Subscription::filter($request->all()) |
||
| 23 | ->with('mailingList') |
||
| 24 | ->paginateFilter(15, ['id', 'email', 'name', 'country', 'language', 'mailing_list_id']); |
||
| 25 | |||
| 26 | $lists = MailingList::get(['name', 'id'])->pluck('name', 'id'); |
||
| 27 | |||
| 28 | return view('subscriptions.index', compact('subscriptions', 'lists')); |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param Subscription $subscription |
||
| 33 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 34 | * @throws \Illuminate\Auth\Access\AuthorizationException |
||
| 35 | */ |
||
| 36 | public function show(Subscription $subscription) |
||
| 37 | { |
||
| 38 | $this->authorize('view', $subscription); |
||
| 39 | |||
| 40 | $subscription->load('mailingList'); |
||
| 41 | |||
| 42 | return view('subscriptions.show', compact('subscription')); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param Subscription $subscription |
||
| 47 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 48 | */ |
||
| 49 | public function new(Subscription $subscription) |
||
| 50 | { |
||
| 51 | $lists = MailingList::get(['name', 'id'])->pluck('name', 'id'); |
||
| 52 | |||
| 53 | return view('subscriptions.new', compact('subscription', 'lists')); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param Subscription $subscription |
||
| 58 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 59 | * @throws \Illuminate\Auth\Access\AuthorizationException |
||
| 60 | */ |
||
| 61 | public function edit(Subscription $subscription) |
||
| 62 | { |
||
| 63 | $this->authorize('update', $subscription); |
||
| 64 | |||
| 65 | $lists = MailingList::get(['name', 'id'])->pluck('name', 'id'); |
||
| 66 | |||
| 67 | return view('subscriptions.edit', compact('subscription', 'lists')); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param SubscriptionRequest $request |
||
| 72 | * @return \Illuminate\Http\RedirectResponse |
||
| 73 | */ |
||
| 74 | public function create(SubscriptionRequest $request) |
||
| 75 | { |
||
| 76 | $subscription = $request->user()->subscription()->create($request->all()); |
||
| 77 | |||
| 78 | notify()->flash($subscription->email, 'success', [ |
||
| 79 | 'timer' => 2000, |
||
| 80 | 'text' => trans('general.success.created'), |
||
| 81 | ]); |
||
| 82 | |||
| 83 | return redirect()->route('subscriptions.index'); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param SubscriptionRequest $request |
||
| 88 | * @param Subscription $subscription |
||
| 89 | * @return \Illuminate\Http\RedirectResponse |
||
| 90 | * @throws \Illuminate\Auth\Access\AuthorizationException |
||
| 91 | */ |
||
| 92 | public function update(SubscriptionRequest $request, Subscription $subscription) |
||
| 93 | { |
||
| 94 | $this->authorize('update', $subscription); |
||
| 95 | |||
| 96 | $subscription->update($request->all()); |
||
| 97 | |||
| 98 | notify()->flash($subscription->email, 'success', [ |
||
| 99 | 'timer' => 2000, |
||
| 100 | 'text' => trans('general.success.update'), |
||
| 101 | ]); |
||
| 102 | |||
| 103 | return redirect()->route('subscriptions.show', $subscription); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param Subscription $subscription |
||
| 108 | * @return \Illuminate\Http\RedirectResponse |
||
| 109 | * @throws \Exception |
||
| 110 | */ |
||
| 111 | public function delete(Subscription $subscription) |
||
| 112 | { |
||
| 113 | $this->authorize('delete', $subscription); |
||
| 114 | |||
| 115 | $subscription->delete(); |
||
| 116 | |||
| 117 | notify()->flash($subscription->email, 'success', [ |
||
| 118 | 'timer' => 2000, |
||
| 119 | 'text' => trans('general.success.delete'), |
||
| 120 | ]); |
||
| 121 | |||
| 122 | return redirect()->route('subscriptions.index'); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param $method |
||
| 127 | * @throws \Illuminate\Auth\Access\AuthorizationException |
||
| 128 | */ |
||
| 129 | public function export($method) |
||
| 130 | { |
||
| 131 | $subscriptions = Subscription::all(); |
||
| 132 | |||
| 133 | $this->authorize('view', $subscriptions->first()); |
||
| 134 | |||
| 135 | Excel::create('Newsletter Subscriptions', function ($excel) use ($subscriptions) { |
||
| 136 | $excel->sheet('Subscriptions', function ($sheet) use ($subscriptions) { |
||
| 137 | $sheet->fromArray($subscriptions); |
||
| 138 | }); |
||
| 139 | })->download($method); |
||
| 140 | } |
||
| 141 | } |
||
| 142 |