ctrlpanel/app/Http/Controllers/Admin/ShopProductController.php

181 lines
6.5 KiB
PHP
Raw Normal View History

2021-06-05 09:26:32 +00:00
<?php
namespace App\Http\Controllers\Admin;
2022-05-30 09:07:10 +00:00
use App\Models\ShopProduct;
2021-06-05 09:26:32 +00:00
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Validation\Rule;
2022-05-30 09:07:10 +00:00
class ShopProductController extends Controller
2021-06-05 09:26:32 +00:00
{
2023-02-15 20:22:16 +00:00
2021-06-05 09:26:32 +00:00
/**
* Display a listing of the resource.
*
* @return Application|Factory|View|Response
*/
public function index(Request $request)
{
$isPaymentSetup = false;
if (
env('APP_ENV') == 'local' ||
config('SETTINGS::PAYMENTS:PAYPAL:SECRET') && config('SETTINGS::PAYMENTS:PAYPAL:CLIENT_ID') ||
config('SETTINGS::PAYMENTS:STRIPE:SECRET') && config('SETTINGS::PAYMENTS:STRIPE:ENDPOINT_SECRET') && config('SETTINGS::PAYMENTS:STRIPE:METHODS')
) {
$isPaymentSetup = true;
}
2021-06-05 09:26:32 +00:00
return view('admin.store.index', [
'isPaymentSetup' => $isPaymentSetup,
2021-06-05 09:26:32 +00:00
]);
}
/**
* Show the form for creating a new resource.
*
* @return Application|Factory|View|Response
*/
public function create()
{
return view('admin.store.create', [
'currencyCodes' => config('currency_codes'),
2021-06-05 09:26:32 +00:00
]);
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
2021-06-05 09:26:32 +00:00
* @return RedirectResponse
*/
public function store(Request $request)
{
$request->validate([
'disabled' => 'nullable',
'type' => 'required|string',
'currency_code' => ['required', 'string', 'max:3', Rule::in(config('currency_codes'))],
'price' => "required|regex:/^\d+(\.\d{1,2})?$/",
'quantity' => 'required|numeric',
'description' => 'required|string|max:60',
'display' => 'required|string|max:60',
2021-06-05 09:26:32 +00:00
]);
2023-02-15 20:22:16 +00:00
$disabled = !is_null($request->input('disabled'));
2022-05-30 09:07:10 +00:00
ShopProduct::create(array_merge($request->all(), ['disabled' => $disabled]));
2021-06-05 09:26:32 +00:00
2021-12-13 10:47:35 +00:00
return redirect()->route('admin.store.index')->with('success', __('Store item has been created!'));
2021-06-05 09:26:32 +00:00
}
/**
* Show the form for editing the specified resource.
*
* @param ShopProduct $shopProduct
2021-06-05 09:26:32 +00:00
* @return Application|Factory|View|Response
*/
2022-05-30 09:07:10 +00:00
public function edit(ShopProduct $shopProduct)
2021-06-05 09:26:32 +00:00
{
return view('admin.store.edit', [
'currencyCodes' => config('currency_codes'),
'shopProduct' => $shopProduct,
2021-06-05 09:26:32 +00:00
]);
}
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param ShopProduct $shopProduct
2021-06-05 09:26:32 +00:00
* @return RedirectResponse
*/
2022-05-30 09:07:10 +00:00
public function update(Request $request, ShopProduct $shopProduct)
2021-06-05 09:26:32 +00:00
{
$request->validate([
'disabled' => 'nullable',
'type' => 'required|string',
'currency_code' => ['required', 'string', 'max:3', Rule::in(config('currency_codes'))],
'price' => "required|regex:/^\d+(\.\d{1,2})?$/",
'quantity' => 'required|numeric|max:100000000',
'description' => 'required|string|max:60',
'display' => 'required|string|max:60',
2021-06-05 09:26:32 +00:00
]);
2023-02-15 20:22:16 +00:00
$disabled = !is_null($request->input('disabled'));
2022-05-30 09:07:10 +00:00
$shopProduct->update(array_merge($request->all(), ['disabled' => $disabled]));
2021-06-05 09:26:32 +00:00
2021-12-13 10:47:35 +00:00
return redirect()->route('admin.store.index')->with('success', __('Store item has been updated!'));
2021-06-05 09:26:32 +00:00
}
/**
* @param Request $request
* @param ShopProduct $shopProduct
2021-06-05 09:26:32 +00:00
* @return RedirectResponse
*/
2023-02-04 16:40:42 +00:00
public function disable(ShopProduct $shopProduct)
2021-06-05 09:26:32 +00:00
{
2023-02-15 20:22:16 +00:00
$shopProduct->update(['disabled' => !$shopProduct->disabled]);
2021-06-05 09:26:32 +00:00
2021-12-13 10:47:35 +00:00
return redirect()->route('admin.store.index')->with('success', __('Product has been updated!'));
2021-06-05 09:26:32 +00:00
}
/**
* Remove the specified resource from storage.
*
* @param ShopProduct $shopProduct
2021-06-05 09:26:32 +00:00
* @return RedirectResponse
*/
2022-05-30 09:07:10 +00:00
public function destroy(ShopProduct $shopProduct)
2021-06-05 09:26:32 +00:00
{
2022-05-30 09:07:10 +00:00
$shopProduct->delete();
2021-12-13 10:47:35 +00:00
return redirect()->back()->with('success', __('Store item has been removed!'));
2021-06-05 09:26:32 +00:00
}
2023-02-15 20:22:16 +00:00
public function dataTable(Request $request)
2021-06-05 09:26:32 +00:00
{
2022-05-30 09:07:10 +00:00
$query = ShopProduct::query();
2021-06-05 09:26:32 +00:00
2023-02-15 20:22:16 +00:00
2021-06-05 09:26:32 +00:00
return datatables($query)
2022-05-30 09:07:10 +00:00
->addColumn('actions', function (ShopProduct $shopProduct) {
2021-06-05 09:26:32 +00:00
return '
2023-02-15 20:22:16 +00:00
<a data-content="' . __('Edit') . '" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('admin.store.edit', $shopProduct->id) . '" class="btn btn-sm btn-info mr-1"><i class="fas fa-pen"></i></a>
2021-06-05 09:26:32 +00:00
2023-02-15 20:22:16 +00:00
<form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.store.destroy', $shopProduct->id) . '">
' . csrf_field() . '
' . method_field('DELETE') . '
<button data-content="' . __('Delete') . '" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm btn-danger mr-1"><i class="fas fa-trash"></i></button>
2021-06-05 09:26:32 +00:00
</form>
';
})
2022-05-30 09:07:10 +00:00
->addColumn('disabled', function (ShopProduct $shopProduct) {
$checked = $shopProduct->disabled == false ? 'checked' : '';
2021-06-05 09:26:32 +00:00
return '
2023-02-15 20:22:16 +00:00
<form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.store.disable', $shopProduct->id) . '">
' . csrf_field() . '
' . method_field('PATCH') . '
2021-06-05 09:26:32 +00:00
<div class="custom-control custom-switch">
2023-02-15 20:22:16 +00:00
<input ' . $checked . ' name="disabled" onchange="this.form.submit()" type="checkbox" class="custom-control-input" id="switch' . $shopProduct->id . '">
<label class="custom-control-label" for="switch' . $shopProduct->id . '"></label>
2021-06-05 09:26:32 +00:00
</div>
</form>
';
})
2022-05-30 09:07:10 +00:00
->editColumn('created_at', function (ShopProduct $shopProduct) {
return $shopProduct->created_at ? $shopProduct->created_at->diffForHumans() : '';
2021-06-05 09:26:32 +00:00
})
2022-05-30 09:07:10 +00:00
->editColumn('price', function (ShopProduct $shopProduct) {
return $shopProduct->formatToCurrency($shopProduct->price);
2021-06-05 09:26:32 +00:00
})
->rawColumns(['actions', 'disabled'])
->make();
}
}