Issues (435)

app/Http/Controllers/CartController.php (1 issue)

Severity
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Release;
6
use App\Models\UsersRelease;
7
use Illuminate\Http\RedirectResponse;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Auth;
10
11
class CartController extends BasePageController
12
{
13
    /**
14
     * @throws \Exception
15
     */
16
    public function index()
17
    {
18
        $results = UsersRelease::getCart(Auth::id());
19
20
        $this->viewData = array_merge($this->viewData, [
21
            'results' => $results,
22
            'meta_title' => 'My Download Basket',
23
            'meta_keywords' => 'search,add,to,cart,download,basket,nzb,description,details',
24
            'meta_description' => 'Manage Your Download Basket',
25
        ]);
26
27
        return view('cart.index', $this->viewData);
28
    }
29
30
    /**
31
     * @throws \Exception
32
     */
33
    public function store(Request $request): RedirectResponse|\Illuminate\Http\JsonResponse
34
    {
35
        $guids = explode(',', $request->input('id'));
36
37
        $data = Release::query()->whereIn('guid', $guids)->select(['id'])->get();
38
39
        if ($data->isEmpty()) {
40
            return $request->ajax() || $request->wantsJson()
41
                ? response()->json(['success' => false, 'message' => 'No releases found'], 404)
42
                : redirect()->to('/cart/index');
43
        }
44
45
        $addedCount = 0;
46
        foreach ($data as $d) {
47
            $check = UsersRelease::whereReleasesId($d['id'])->first();
48
            if (empty($check)) {
49
                UsersRelease::addCart($this->userdata->id, $d['id']);
50
                $addedCount++;
51
            }
52
        }
53
54
        if ($request->ajax() || $request->wantsJson()) {
55
            return response()->json([
56
                'success' => true,
57
                'message' => $addedCount > 0 ? "{$addedCount} item(s) added to cart" : 'Items already in cart',
58
                'cartCount' => UsersRelease::where('users_id', $this->userdata->id)->count(),
59
            ]);
60
        }
61
62
        return redirect()->to('/cart/index');
63
    }
64
65
    /**
66
     * @throws \Exception
67
     */
68
    public function destroy(array|string $id): RedirectResponse
69
    {
70
        $ids = null;
71
        if (! empty($id) && ! \is_array($id)) {
0 ignored issues
show
The condition is_array($id) is always true.
Loading history...
72
            $ids = explode(',', $id);
73
        } elseif (\is_array($id)) {
74
            $ids = $id;
75
        }
76
77
        if (! empty($ids) && UsersRelease::delCartByGuid($ids, $this->userdata->id)) {
78
            return redirect()->to('/cart/index');
79
        }
80
81
        if (! $id) {
82
            return redirect()->to('/cart/index');
83
        }
84
85
        return redirect()->to('/cart/index');
86
    }
87
}
88