improved code quality

fixed some bugs
pull/49/head
A1Gard 3 months ago
parent 43a0202338
commit 1eda1fe3c6

@ -60,7 +60,7 @@ class CardController extends Controller
$count = 1;
$msg = "Product added to card";
\Cookie::queue('card', "[$product->id]", 2000);
\Cookie::queue('q', "[$quantity]", 2000);
\Cookie::queue('q', "[null]", 2000);
$qs = [$quantity];
$cards = [$product->id];
}
@ -184,4 +184,32 @@ class CardController extends Controller
];
}
}
public function productCompareToggle($slug)
{
$product = Product::where('slug', $slug)->firstOrFail();
if (\Cookie::has('compares')) {
$compares = json_decode(\Cookie::get('compares'), true);
if (in_array($product->id, $compares)) {
$msg = __("Product removed from compare");
unset($compares[array_search($product->id, $compares)]);
} else {
$compares[] = $product->id;
$msg = __( "Product added to compare");
}
\Cookie::queue('compares', json_encode($compares), 2000);
} else {
$msg = "Product added to compare";
\Cookie::queue('compares', "[$product->id]", 2000);
}
if (\request()->ajax()) {
return success(null, $msg);
} else {
return redirect()->back()->with(['message' => $msg]);
}
}
}

@ -398,58 +398,8 @@ class ClientController extends Controller
}
public function productCompareToggle($slug)
{
$product = Product::where('slug', $slug)->firstOrFail();
if (\Cookie::has('compares')) {
$compares = json_decode(\Cookie::get('compares'), true);
if (in_array($product->id, $compares)) {
$msg = "Product removed from compare";
unset($compares[array_search($product->id, $compares)]);
} else {
$compares[] = $product->id;
$msg = "Product added to compare";
}
\Cookie::queue('compares', json_encode($compares), 2000);
} else {
$msg = "Product added to compare";
\Cookie::queue('compares', "[$product->id]", 2000);
}
if (\request()->ajax()) {
return success(null, $msg);
} else {
return redirect()->back()->with(['message' => $msg]);
}
}
public function ProductFavToggle($slug)
{
$product = Product::where('slug', $slug)->firstOrFail();
if (!auth('customer')->check()) {
return errors([
__("You need to login first"),
], 403, __("You need to login first"));
}
if (auth('customer')->user()->favorites()->where('product_id', $product->id)->count() == 0) {
auth('customer')->user()->favorites()->attach($product->id);
$message = __('Product added to favorites');
$fav = '1';
} else {
auth('customer')->user()->favorites()->detach($product->id);
$message = __('Product removed from favorites');
$fav = '0';
}
if (\request()->ajax()) {
return success($fav, $message);
} else {
return redirect()->back()->with(['message' => $message]);
}
}
public function compare()

@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use App\Models\Invoice;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Validation\Rules\In;
@ -63,4 +64,34 @@ class CustomerController extends Controller
return $invoice;
}
public function ProductFavToggle($slug)
{
$product = Product::where('slug', $slug)->firstOrFail();
if (!auth('customer')->check()) {
return errors([
__("You need to login first"),
], 403, __("You need to login first"));
}
if (auth('customer')->user()->favorites()->where('product_id', $product->id)->count() == 0) {
auth('customer')->user()->favorites()->attach($product->id);
$message = __('Product added to favorites');
$fav = '1';
} else {
auth('customer')->user()->favorites()->detach($product->id);
$message = __('Product removed from favorites');
$fav = '0';
}
if (\request()->ajax()) {
return success($fav, $message);
} else {
return redirect()->back()->with(['message' => $message]);
}
}
}

@ -27,22 +27,22 @@ class LangControl
} else {
app()->setLocale(config('app.locale'));
}
array_shift($segments);
$url = \request()->path();
$url = str_replace(app()->getLocale(), '', $url);
// Modify the request
$newPath = '/' . implode('/', $segments);
$newUrl = $request->root() . $newPath . ($request->getQueryString() ? '?'.$request->getQueryString() : '');
$request->initialize(
$request->query->all(),
$request->request->all(),
$request->attributes->all(),
$request->cookies->all(),
$request->files->all(),
$request->server->all()
);
// array_shift($segments);
// $url = \request()->path();
// $url = str_replace(app()->getLocale(), '', $url);
// // Modify the request
// $newPath = '/' . implode('/', $segments);
// $newUrl = $request->root() . $newPath . ($request->getQueryString() ? '?'.$request->getQueryString() : '');
//
// $request->initialize(
// $request->query->all(),
// $request->request->all(),
// $request->attributes->all(),
// $request->cookies->all(),
// $request->files->all(),
// $request->server->all()
// );
//
return $next($request);

@ -390,8 +390,8 @@ Route::middleware([\App\Http\Middleware\VisitorCounter::class])
Route::get('attach/download/{attachment}', [ClientController::class, 'attachDl'])->name('attach-dl');
Route::get('/post/{post}', [ClientController::class, 'post'])->name('post');
Route::get('product/fav/toggle/{product}', [ClientController::class, 'ProductFavToggle'])->name('product-fav-toggle');
Route::get('product/compare/toggle/{product}', [ClientController::class, 'productCompareToggle'])->name('product-compare-toggle');
Route::get('product/fav/toggle/{product}', [\App\Http\Controllers\CustomerController::class, 'ProductFavToggle'])->name('product-fav-toggle');
Route::get('product/compare/toggle/{product}', [\App\Http\Controllers\CardController::class, 'productCompareToggle'])->name('product-compare-toggle');
Route::get('card/toggle/{product}', [\App\Http\Controllers\CardController::class, 'productCardToggle'])->name('product-card-toggle');
Route::post('/comment/submit', [ClientController::class, 'submitComment'])->name('comment.submit');

Loading…
Cancel
Save