1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Models\Campaign; |
6
|
|
|
use App\Models\Template; |
7
|
|
|
use App\Jobs\SendCampaign; |
8
|
|
|
use App\Models\MailingList; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Maatwebsite\Excel\Facades\Excel; |
11
|
|
|
use App\Http\Requests\CampaignRequest; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class CampaignController. |
15
|
|
|
*/ |
16
|
|
|
class CampaignController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param Request $request |
20
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
21
|
|
|
*/ |
22
|
|
|
public function index(Request $request) |
23
|
|
|
{ |
24
|
|
|
$campaigns = auth()->user()->campaigns() |
25
|
|
|
->filter($request->all()) |
26
|
|
|
->with('mailingLists') |
27
|
|
|
->paginateFilter(15, ['id', 'name', 'send']); |
28
|
|
|
|
29
|
|
|
return view('campaigns.index', compact('campaigns')); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Campaign $campaign |
34
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
35
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
36
|
|
|
*/ |
37
|
|
|
public function show(Campaign $campaign) |
38
|
|
|
{ |
39
|
|
|
$this->authorize('view', $campaign); |
40
|
|
|
|
41
|
|
|
$campaign->load('template', 'mailingLists.subscriptions'); |
42
|
|
|
|
43
|
|
|
$subscriptions = $campaign->getSubscriptions(); |
44
|
|
|
|
45
|
|
|
return view('campaigns.show', compact('campaign', 'mailingLists', 'subscriptions')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Campaign $campaign |
50
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
51
|
|
|
*/ |
52
|
|
|
public function new(Campaign $campaign) |
53
|
|
|
{ |
54
|
|
|
if (request()->is('campaigns/clone*')) { |
|
|
|
|
55
|
|
|
$campaign->load('mailingLists'); |
56
|
|
|
|
57
|
|
|
$mailingLists = $campaign->getMailingList()->pluck('id')->toArray(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$lists = MailingList::get(['name', 'id'])->pluck('name', 'id'); |
61
|
|
|
$templates = Template::get(['name', 'id'])->pluck('name', 'id'); |
62
|
|
|
|
63
|
|
|
return view('campaigns.new', compact('campaign', 'lists', 'templates', 'mailingLists')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Campaign $campaign |
68
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
69
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
70
|
|
|
*/ |
71
|
|
|
public function edit(Campaign $campaign) |
72
|
|
|
{ |
73
|
|
|
$this->authorize('edit', $campaign); |
74
|
|
|
|
75
|
|
|
abort_if($campaign->send, 404); |
|
|
|
|
76
|
|
|
|
77
|
|
|
//$campaign->load('mailingLists'); |
78
|
|
|
|
79
|
|
|
$lists = MailingList::get(['name', 'id'])->pluck('name', 'id'); |
80
|
|
|
$templates = Template::get(['name', 'id'])->pluck('name', 'id'); |
81
|
|
|
|
82
|
|
|
$mailingLists = $campaign->getMailingList()->pluck('id')->toArray(); |
83
|
|
|
|
84
|
|
|
return view('campaigns.edit', compact('campaign', 'lists', 'templates', 'mailingLists')); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param Campaign $campaign |
89
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
90
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
91
|
|
|
*/ |
92
|
|
|
public function preSend(Campaign $campaign) |
93
|
|
|
{ |
94
|
|
|
$this->authorize('send', $campaign); |
95
|
|
|
|
96
|
|
|
abort_if($campaign->send, 404); |
|
|
|
|
97
|
|
|
|
98
|
|
|
$campaign->load('template', 'mailingLists.subscriptions'); |
99
|
|
|
|
100
|
|
|
$subscriptions = $campaign->getSubscriptions(); |
101
|
|
|
|
102
|
|
|
return view('campaigns.send', compact('campaign', 'mailingLists', 'subscriptions')); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param CampaignRequest $request |
107
|
|
|
* @return \Illuminate\Http\RedirectResponse |
108
|
|
|
*/ |
109
|
|
|
public function create(CampaignRequest $request) |
110
|
|
|
{ |
111
|
|
|
$campaign = auth()->user()->campaigns()->create($request->all()); |
112
|
|
|
|
113
|
|
|
if ($request->get('mailing_lists')) { |
114
|
|
|
$campaign->mailingLists()->sync($request->input('mailing_lists')); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
notify()->flash($campaign->name, 'success', [ |
118
|
|
|
'timer' => 2000, |
119
|
|
|
'text' => trans('general.success.create'), |
120
|
|
|
]); |
121
|
|
|
|
122
|
|
|
return redirect()->route('campaigns.show', $campaign); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param CampaignRequest $request |
127
|
|
|
* @param Campaign $campaign |
128
|
|
|
* @return \Illuminate\Http\RedirectResponse |
129
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
130
|
|
|
*/ |
131
|
|
|
public function update(CampaignRequest $request, Campaign $campaign) |
132
|
|
|
{ |
133
|
|
|
$this->authorize('update', $campaign); |
134
|
|
|
|
135
|
|
|
$campaign->update($request->except('mailing_lists')); |
136
|
|
|
|
137
|
|
|
if ($request->get('mailing_lists')) { |
138
|
|
|
$campaign->mailingLists()->sync($request->input('mailing_lists')); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
notify()->flash($campaign->name, 'success', [ |
142
|
|
|
'timer' => 2000, |
143
|
|
|
'text' => trans('general.success.update'), |
144
|
|
|
]); |
145
|
|
|
|
146
|
|
|
return redirect()->route('campaigns.show', $campaign); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param Campaign $campaign |
151
|
|
|
* @return \Illuminate\Http\RedirectResponse |
152
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
153
|
|
|
*/ |
154
|
|
|
public function send(Campaign $campaign) |
155
|
|
|
{ |
156
|
|
|
$this->authorize('send', $campaign); |
157
|
|
|
|
158
|
|
|
$campaign->load('template', 'mailingLists.subscriptions'); |
159
|
|
|
|
160
|
|
|
$this->dispatch(new SendCampaign(auth()->user(), $campaign, $campaign->template)); |
161
|
|
|
|
162
|
|
|
notify()->flash(trans('general.woohoo'), 'success', [ |
|
|
|
|
163
|
|
|
'timer' => 3500, |
164
|
|
|
'text' => trans('campaigns.send.success', ['name' => $campaign->name, 'subscribers' => $campaign->getSubscriptions()->count()]), |
165
|
|
|
]); |
166
|
|
|
|
167
|
|
|
return redirect()->route('campaigns.index'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param Campaign $campaign |
172
|
|
|
* @return \Illuminate\Http\RedirectResponse |
173
|
|
|
* @throws \Exception |
174
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
175
|
|
|
*/ |
176
|
|
|
public function delete(Campaign $campaign) |
177
|
|
|
{ |
178
|
|
|
$this->authorize('delete', $campaign); |
179
|
|
|
|
180
|
|
|
$campaign->delete(); |
181
|
|
|
|
182
|
|
|
notify()->flash($campaign->name, 'success', [ |
183
|
|
|
'timer' => 2000, |
184
|
|
|
'text' => trans('general.success.delete'), |
185
|
|
|
]); |
186
|
|
|
|
187
|
|
|
return redirect()->route('campaigns.index'); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param Campaign $campaign |
192
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
193
|
|
|
*/ |
194
|
|
|
public function export(Campaign $campaign) |
195
|
|
|
{ |
196
|
|
|
$this->authorize('export', $campaign); |
197
|
|
|
|
198
|
|
|
$subscriptions = $campaign->getSubscriptions(); |
199
|
|
|
|
200
|
|
|
Excel::create('Subscriptions-'.$campaign->name, function ($excel) use ($subscriptions) { |
201
|
|
|
$excel->sheet('Subscriptions', function ($sheet) use ($subscriptions) { |
202
|
|
|
$sheet->fromArray($subscriptions); |
203
|
|
|
}); |
204
|
|
|
})->export('csv'); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|