HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/be.naniguide.com/app/Http/Controllers/Refactor/Admin/WebhookController.php
<?php

namespace App\Http\Controllers\Refactor\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\Webhook;

class WebhookController extends Controller
{
    public function index(Request $request)
    {
        $stats = [
            'total' => Webhook::backend()->count(),
            'active' => Webhook::backend()->active()->count(),
        ];

        return view('refactor.pages.admin.webhooks.index', compact('stats'));
    }

    public function listing(Request $request)
    {
        $query = Webhook::search($request->keyword)->backend();

        if ($request->status && $request->status !== 'all') {
            $query = $query->where('status', $request->status);
        }

        $items = $query->orderBy(
            $request->sort_order ?? 'created_at',
            $request->sort_direction ?? 'desc'
        )->paginate($request->per_page ?? 15);

        return view('refactor.pages.admin.webhooks._list', compact('items'));
    }

    public function create(Request $request)
    {
        $webhook = Webhook::newDefault();

        return view('refactor.pages.admin.webhooks.create', compact('webhook'));
    }

    public function store(Request $request)
    {
        $webhook = Webhook::newDefault();

        list($result, $errors) = $webhook->saveWebhook(
            $request->input('webhook.name'),
            $request->input('webhook.event'),
        );

        if (!$result) {
            return response()->json([
                'status' => 'error',
                'errors' => $errors,
            ], 422);
        }

        return response()->json([
            'status' => 'success',
            'message' => trans('refactor/admin_webhooks.flash.created'),
            'redirect' => route('refactor.admin.webhooks.setup', $webhook->uid),
        ]);
    }

    public function setup(Request $request, $uid)
    {
        $webhook = Webhook::findByUid($uid);

        if (!$webhook) {
            return response()->json(['status' => 'error', 'message' => 'Not found'], 404);
        }

        if ($request->isMethod('post')) {
            $webhook->saveHttpConfig($request->webhook);

            return response()->json([
                'status' => 'success',
                'message' => trans('refactor/admin_webhooks.flash.updated'),
            ]);
        }

        return view('refactor.pages.admin.webhooks.setup', compact('webhook'));
    }

    public function edit(Request $request, $uid)
    {
        $webhook = Webhook::findByUid($uid);

        if (!$webhook) {
            return response()->json(['status' => 'error', 'message' => 'Not found'], 404);
        }

        return view('refactor.pages.admin.webhooks.edit', compact('webhook'));
    }

    public function update(Request $request, $uid)
    {
        $webhook = Webhook::findByUid($uid);

        if (!$webhook) {
            return response()->json(['status' => 'error', 'message' => 'Not found'], 404);
        }

        list($result, $errors) = $webhook->saveWebhook(
            $request->input('webhook.name'),
            $request->input('webhook.event'),
        );

        if (!$result) {
            return response()->json([
                'status' => 'error',
                'errors' => $errors,
            ], 422);
        }

        return response()->json([
            'status' => 'success',
            'message' => trans('refactor/admin_webhooks.flash.updated'),
        ]);
    }

    public function delete(Request $request)
    {
        $webhooks = Webhook::whereIn(
            'uid',
            is_array($request->uids) ? $request->uids : explode(',', $request->uids)
        );

        foreach ($webhooks->get() as $webhook) {
            $webhook->delete();
        }

        return response()->json(['status' => 'success', 'message' => trans('refactor/admin_webhooks.flash.deleted')]);
    }

    public function enable(Request $request)
    {
        $webhooks = Webhook::whereIn(
            'uid',
            is_array($request->uids) ? $request->uids : explode(',', $request->uids)
        );

        foreach ($webhooks->get() as $webhook) {
            $webhook->enable();
        }

        return response()->json(['status' => 'success', 'message' => trans('refactor/admin_webhooks.flash.enabled')]);
    }

    public function disable(Request $request)
    {
        $webhooks = Webhook::whereIn(
            'uid',
            is_array($request->uids) ? $request->uids : explode(',', $request->uids)
        );

        foreach ($webhooks->get() as $webhook) {
            $webhook->disable();
        }

        return response()->json(['status' => 'success', 'message' => trans('refactor/admin_webhooks.flash.disabled')]);
    }
}