|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Campaign; |
| 4 | + |
| 5 | +use App\Facades\Datagrid; |
| 6 | +use App\Http\Controllers\Controller; |
| 7 | +use App\Http\Requests\StoreWebhook; |
| 8 | +use App\Models\Campaign; |
| 9 | +use App\Models\Webhook; |
| 10 | +use App\Jobs\TestWebhookJob; |
| 11 | + |
| 12 | +class WebhookController extends Controller |
| 13 | +{ |
| 14 | + protected string $view = 'webhooks'; |
| 15 | + |
| 16 | + /** |
| 17 | + * Create a new controller instance. |
| 18 | + * @return void |
| 19 | + */ |
| 20 | + public function __construct() |
| 21 | + { |
| 22 | + $this->middleware('auth'); |
| 23 | + $this->middleware('campaign.member'); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse |
| 28 | + * @throws \Illuminate\Auth\Access\AuthorizationException |
| 29 | + */ |
| 30 | + public function index(Campaign $campaign) |
| 31 | + { |
| 32 | + Datagrid::layout(\App\Renderers\Layouts\Campaign\Webhook::class); |
| 33 | + |
| 34 | + $this->authorize('webhooks', $campaign); |
| 35 | + |
| 36 | + $webhooks = $campaign->webhooks() |
| 37 | + ->sort(request()->only(['o', 'k'])) |
| 38 | + //->with(['users', 'permissions', 'campaign']) |
| 39 | + ->orderBy('updated_at', 'DESC') |
| 40 | + //->orderBy('name') |
| 41 | + ->paginate(); |
| 42 | + |
| 43 | + $rows = $webhooks; |
| 44 | + |
| 45 | + // Ajax Datagrid |
| 46 | + if (request()->ajax()) { |
| 47 | + $html = view('layouts.datagrid._table')->with('rows', $rows)->with('campaign', $campaign)->render(); |
| 48 | + $deletes = view('layouts.datagrid.delete-forms')->with('models', Datagrid::deleteForms())->with('campaign', $campaign)->render(); |
| 49 | + return response()->json([ |
| 50 | + 'success' => true, |
| 51 | + 'html' => $html, |
| 52 | + 'deletes' => $deletes, |
| 53 | + ]); |
| 54 | + } |
| 55 | + |
| 56 | + return view('campaigns.webhooks', compact('campaign', 'rows', 'webhooks')); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View |
| 61 | + * @throws \Illuminate\Auth\Access\AuthorizationException |
| 62 | + */ |
| 63 | + public function create(Campaign $campaign) |
| 64 | + { |
| 65 | + $this->authorize('webhooks', $campaign); |
| 66 | + |
| 67 | + return view('campaigns.webhooks.create', ['campaign' => $campaign, 'model' => $campaign]); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + public function store(StoreWebhook $request, Campaign $campaign) |
| 72 | + { |
| 73 | + $this->authorize('webhooks', $campaign); |
| 74 | + if ($request->ajax()) { |
| 75 | + return response()->json(); |
| 76 | + } |
| 77 | + |
| 78 | + $data = $request->all() + ['campaign_id' => $campaign->id]; |
| 79 | + Webhook::create($data); |
| 80 | + |
| 81 | + return redirect()->route('webhooks.index', $campaign) |
| 82 | + ->with('success_raw', __('campaigns.webhooks.create.success')); |
| 83 | + } |
| 84 | + |
| 85 | + public function edit(Campaign $campaign, Webhook $webhook) |
| 86 | + { |
| 87 | + $this->authorize('webhooks', $campaign); |
| 88 | + |
| 89 | + return view('campaigns.webhooks.edit', [ |
| 90 | + 'campaign' => $campaign, |
| 91 | + 'model' => $campaign, |
| 92 | + 'webhook' => $webhook, |
| 93 | + ]); |
| 94 | + } |
| 95 | + |
| 96 | + public function update(StoreWebhook $request, Campaign $campaign, Webhook $webhook) |
| 97 | + { |
| 98 | + $this->authorize('webhooks', $campaign); |
| 99 | + |
| 100 | + $webhook->update($request->all()); |
| 101 | + return redirect()->route('webhooks.index', $campaign) |
| 102 | + ->with('success_raw', __('campaigns.webhooks.edit.success')); |
| 103 | + } |
| 104 | + |
| 105 | + public function destroy(Campaign $campaign, Webhook $webhook) |
| 106 | + { |
| 107 | + $this->authorize('webhooks', $campaign); |
| 108 | + |
| 109 | + $webhook->delete(); |
| 110 | + |
| 111 | + return redirect()->route('webhooks.index', $campaign) |
| 112 | + ->with('success_raw', __('campaigns.webhooks.destroy.success')); |
| 113 | + } |
| 114 | + |
| 115 | + public function status(Campaign $campaign, Webhook $webhook) |
| 116 | + { |
| 117 | + $this->authorize('webhooks', $campaign); |
| 118 | + |
| 119 | + return view('campaigns.webhooks.status', [ |
| 120 | + 'campaign' => $campaign, |
| 121 | + 'model' => $campaign, |
| 122 | + 'webhook' => $webhook, |
| 123 | + ]); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @return \Illuminate\Http\RedirectResponse |
| 128 | + * @throws \Illuminate\Auth\Access\AuthorizationException |
| 129 | + */ |
| 130 | + public function toggle(Campaign $campaign, Webhook $webhook) |
| 131 | + { |
| 132 | + $this->authorize('webhooks', $campaign); |
| 133 | + |
| 134 | + $webhook->update(['status' => !$webhook->status]); |
| 135 | + |
| 136 | + return redirect()->route('webhooks.index', $campaign) |
| 137 | + ->with( |
| 138 | + 'success', |
| 139 | + __('campaigns.webhooks.toggle.success') |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + */ |
| 145 | + public function bulk(Campaign $campaign) |
| 146 | + { |
| 147 | + $this->authorize('webhooks', $campaign); |
| 148 | + |
| 149 | + $action = request()->get('action'); |
| 150 | + $models = request()->get('model'); |
| 151 | + if (!in_array($action, ['enable', 'disable', 'delete']) || empty($models)) { |
| 152 | + return redirect() |
| 153 | + ->route('webhooks.index', $campaign); |
| 154 | + } |
| 155 | + $count = 0; |
| 156 | + foreach ($models as $id) { |
| 157 | + /** @var Webhook|null $webhook */ |
| 158 | + $webhook = Webhook::find($id); |
| 159 | + if ($webhook === null) { |
| 160 | + continue; |
| 161 | + } |
| 162 | + |
| 163 | + if ($action === 'delete') { |
| 164 | + $webhook->delete(); |
| 165 | + $count++; |
| 166 | + } |
| 167 | + |
| 168 | + if ($action === 'disable' && $webhook->status) { |
| 169 | + $webhook->update(['status' => 0]); |
| 170 | + $count++; |
| 171 | + } |
| 172 | + |
| 173 | + if ($action === 'enable' && !$webhook->status) { |
| 174 | + $webhook->update(['status' => 1]); |
| 175 | + $count++; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return redirect() |
| 180 | + ->route('webhooks.index', $campaign) |
| 181 | + ->with('success', trans_choice('campaigns.webhooks.actions.bulks.' . $action . '_success', $count, ['count' => $count])); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * @return \Illuminate\Http\RedirectResponse |
| 186 | + * @throws \Illuminate\Auth\Access\AuthorizationException |
| 187 | + */ |
| 188 | + public function test(Campaign $campaign, Webhook $webhook) |
| 189 | + { |
| 190 | + $this->authorize('webhooks', $campaign); |
| 191 | + |
| 192 | + TestWebhookJob::dispatch($campaign, auth()->user(), $webhook, $webhook->action); |
| 193 | + |
| 194 | + return redirect()->route('webhooks.index', $campaign) |
| 195 | + ->with( |
| 196 | + 'success', |
| 197 | + __('campaigns.webhooks.test.success') |
| 198 | + ); |
| 199 | + } |
| 200 | +} |
0 commit comments