123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Http\Controllers\Controller;
- use App\Models\Product;
- use Exception;
- use Illuminate\Contracts\Foundation\Application;
- use Illuminate\Contracts\View\Factory;
- use Illuminate\Contracts\View\View;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Http\Response;
- class ProductController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return Application|Factory|View
- */
- public function index()
- {
- return view('admin.products.index');
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return Application|Factory|View
- */
- public function create()
- {
- return view('admin.products.create');
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param Request $request
- * @return RedirectResponse
- */
- public function store(Request $request)
- {
- $request->validate([
- "name" => "required|max:30",
- "price" => "required|numeric|max:1000000|min:0",
- "memory" => "required|numeric|max:1000000|min:5",
- "cpu" => "required|numeric|max:1000000|min:5",
- "swap" => "required|numeric|max:1000000|min:0",
- "description" => "required",
- "disk" => "required|numeric|max:1000000|min:5",
- "io" => "required|numeric|max:1000000|min:0",
- "databases" => "required|numeric|max:1000000|min:0",
- "backups" => "required|numeric|max:1000000|min:0",
- "allocations" => "required|numeric|max:1000000|min:0",
- "disabled" => "nullable",
- ]);
- $disabled = !is_null($request->input('disabled'));
- Product::create(array_merge($request->all(), ['disabled' => $disabled]));
- return redirect()->route('admin.products.index')->with('success', 'product has been created!');
- }
- /**
- * Display the specified resource.
- *
- * @param Product $product
- * @return Application|Factory|View
- */
- public function show(Product $product)
- {
- return view('admin.products.show', [
- 'product' => $product
- ]);
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param Product $product
- * @return Application|Factory|View
- */
- public function edit(Product $product)
- {
- return view('admin.products.edit', [
- 'product' => $product
- ]);
- }
- /**
- * Update the specified resource in storage.
- *
- * @param Request $request
- * @param Product $product
- * @return RedirectResponse
- */
- public function update(Request $request, Product $product): RedirectResponse
- {
- $request->validate([
- "name" => "required|max:30",
- "price" => "required|numeric|max:1000000|min:0",
- "memory" => "required|numeric|max:1000000|min:5",
- "cpu" => "required|numeric|max:1000000|min:5",
- "swap" => "required|numeric|max:1000000|min:0",
- "description" => "required",
- "disk" => "required|numeric|max:1000000|min:5",
- "io" => "required|numeric|max:1000000|min:0",
- "databases" => "required|numeric|max:1000000|min:0",
- "backups" => "required|numeric|max:1000000|min:0",
- "allocations" => "required|numeric|max:1000000|min:0",
- "disabled" => "nullable",
- ]);
- $disabled = !is_null($request->input('disabled'));
- $product->update(array_merge($request->all(), ['disabled' => $disabled]));
- return redirect()->route('admin.products.index')->with('success', 'product has been updated!');
- }
- /**
- * @param Request $request
- * @param Product $product
- * @return RedirectResponse
- */
- public function disable(Request $request, Product $product) {
- $product->update(['disabled' => !$product->disabled]);
- return redirect()->route('admin.products.index')->with('success', 'product has been updated!');
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param Product $product
- * @return RedirectResponse
- */
- public function destroy(Product $product)
- {
- $servers = $product->servers()->count();
- if ($servers > 0) {
- return redirect()->back()->with('error', "Product cannot be removed while it's linked to {$servers} servers");
- }
- $product->delete();
- return redirect()->back()->with('success', 'product has been removed!');
- }
- /**
- * @return JsonResponse|mixed
- * @throws Exception|Exception
- */
- public function dataTable()
- {
- $query = Product::with(['servers']);
- return datatables($query)
- ->addColumn('actions', function (Product $product) {
- return '
- <a data-content="Show" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('admin.products.show', $product->id) . '" class="btn btn-sm text-white btn-warning mr-1"><i class="fas fa-eye"></i></a>
- <a data-content="Edit" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('admin.products.edit', $product->id) . '" class="btn btn-sm btn-info mr-1"><i class="fas fa-pen"></i></a>
- <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.products.destroy', $product->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>
- </form>
- ';
- })
- ->addColumn('servers', function (Product $product) {
- return $product->servers()->count();
- })
- ->addColumn('disabled', function (Product $product) {
- $checked = $product->disabled == false ? "checked" : "";
- return '
- <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.products.disable', $product->id) . '">
- ' . csrf_field() . '
- ' . method_field("PATCH") . '
- <div class="custom-control custom-switch">
- <input '.$checked.' name="disabled" onchange="this.form.submit()" type="checkbox" class="custom-control-input" id="switch'.$product->id.'">
- <label class="custom-control-label" for="switch'.$product->id.'"></label>
- </div>
- </form>
- ';
- })
- ->editColumn('created_at', function (Product $product) {
- return $product->created_at ? $product->created_at->diffForHumans() : '';
- })
- ->rawColumns(['actions', 'disabled'])
- ->make();
- }
- }
|