diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index 377eb5c..0a4a2af 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -30,7 +30,7 @@ class ClientController extends Controller public function post(Post $post) { - if ($post->status = 0 && !auth()->check()){ + if ($post->status = 0 && !auth()->check()) { return abort(403); } $area = 'post'; @@ -39,14 +39,15 @@ class ClientController extends Controller $post->increment('view'); return view('client.post', compact('area', 'post', 'title', 'subtitle')); } + public function gallery(Gallery $gallery) { - if ($gallery->status = 0 && !auth()->check()){ + if ($gallery->status = 0 && !auth()->check()) { return abort(403); } $area = 'gallery'; $title = $gallery->title; - $subtitle = \Str::limit(strip_tags($gallery->description),15); + $subtitle = \Str::limit(strip_tags($gallery->description), 15); $gallery->increment('view'); return view('client.gallery', compact('area', 'gallery', 'title', 'subtitle')); } @@ -60,6 +61,7 @@ class ClientController extends Controller ->orderByDesc('id')->paginate($this->paginate); return view('client.default-list', compact('area', 'posts', 'title', 'subtitle')); } + public function products() { $area = 'products-list'; @@ -133,23 +135,91 @@ class ClientController extends Controller } - public function group(Group $group){ + public function group(Group $group) + { $area = 'group'; $title = $group->name; $subtitle = $group->subtitle; $posts = $group->posts()->orderByDesc('id')->paginate($this->paginate); - return view('client.group', compact('area', 'posts', 'title', 'subtitle','group')); + return view('client.group', compact('area', 'posts', 'title', 'subtitle', 'group')); } - public function attachDl(Attachment $attachment){ + public function attachDl(Attachment $attachment) + { $attachment->increment('downloads'); - $file = (storage_path().'/app/public/attachments/'. $attachment->file); + $file = (storage_path() . '/app/public/attachments/' . $attachment->file); if (file_exists($file)) { return response()->download($file); } } + public function productCardToggle(Product $product) + { + + $quantity = \request()->input('quantity', null); + if (\Cookie::has('card')) { + $cards = json_decode(\Cookie::get('card'),true); + $qs = json_decode(\Cookie::get('q'),true); + if (in_array($product->id, $cards)) { + $msg = "Product removed from card"; + $i = array_search($product->id, $cards); + unset($cards[$i]); + unset($qs[$i]); + } else { + $cards[] = $product->id; + $qs[] = $quantity; + $msg = "Product added to card"; + } + $count = count($cards); + \Cookie::queue('card', json_encode($cards), 2000); + \Cookie::queue('q', json_encode($qs), 2000); + } else { + $count = 1; + $msg = "Product added to card"; + \Cookie::queue('card', "[$product->id]", 2000); + \Cookie::queue('q', "[$quantity]", 2000); + $qs = [$quantity]; + $cards = [$product->id]; + } + + if ($count > 0 && auth('customer')->check()) { + $customer = auth('customer')->user(); + $customer->card = json_encode(['cards' => $cards, 'quantities' => $qs]); + $customer->save(); + } + + if (\request()->ajax()) { + return success(['count' => $count], $msg); + } else { + return redirect()->back()->with(['message' => $msg]); + } + } + + public function productCompareToggle(Product $product) + { + 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(Product $product) { if (!auth('customer')->check()) { @@ -169,7 +239,7 @@ class ClientController extends Controller } if (\request()->ajax()) { - return success($fav, $message); + return success($fav, $message); } else { return redirect()->back()->with(['message' => $message]); } diff --git a/database/migrations/2024_05_07_125920_create_customers_table.php b/database/migrations/2024_05_07_125920_create_customers_table.php index 262035c..911485a 100644 --- a/database/migrations/2024_05_07_125920_create_customers_table.php +++ b/database/migrations/2024_05_07_125920_create_customers_table.php @@ -25,6 +25,7 @@ return new class extends Migration $table->boolean('colleague')->default(false); $table->text('description')->default(null)->nullable(); $table->bigInteger('credit')->default(0); + $table->json('card')->default(null)->nullable(); $table->rememberToken(); $table->timestamps(); $table->softDeletes(); diff --git a/resources/js/client-custom/customerActions.js b/resources/js/client-custom/customerActions.js index 6d4beb7..7a3fb60 100644 --- a/resources/js/client-custom/customerActions.js +++ b/resources/js/client-custom/customerActions.js @@ -1,11 +1,39 @@ window.addEventListener('load', function () { const favUrl = document.querySelector('#api-fav-toggle').value; + const compUrl = document.querySelector('#api-compare-toggle').value; document.querySelectorAll('.fav-btn')?.forEach(function (el) { el.addEventListener('click', async function () { let resp = await axios.get(favUrl+this.getAttribute('data-slug')); if (resp.data.success){ this.setAttribute('data-is-fav',resp.data.data); window.$toast.success(resp.data.message); + }else { + window.$toast.error("Error!"); + } + }); + }); + document.querySelectorAll('.compare-btn')?.forEach(function (el) { + el.addEventListener('click', async function () { + let resp = await axios.get(compUrl+this.getAttribute('data-slug')); + if (resp.data.success){ + window.$toast.success(resp.data.message); + }else { + window.$toast.error("Error!"); + } + }); + }); + + document.querySelectorAll('.add-to-card')?.forEach(function (el) { + el.addEventListener('click', async function (e) { + e.preventDefault(); + let resp = await axios.get(this.getAttribute('href')); + if (resp.data.success){ + window.$toast.success(resp.data.message); + document.querySelectorAll('.card-count')?.forEach(function (el2) { + el2.innerText = resp.data.data.count; + }); + }else { + window.$toast.error("Error!"); } }); }); diff --git a/resources/views/segments/products/TreeGridProducts/TreeGridProducts.blade.php b/resources/views/segments/products/TreeGridProducts/TreeGridProducts.blade.php index 8999ca4..c314f5f 100644 --- a/resources/views/segments/products/TreeGridProducts/TreeGridProducts.blade.php +++ b/resources/views/segments/products/TreeGridProducts/TreeGridProducts.blade.php @@ -33,7 +33,7 @@ {{$product->getPrice()}} - + Add to card diff --git a/resources/views/website/inc/website-foot.blade.php b/resources/views/website/inc/website-foot.blade.php index 1cb6d75..d27c793 100644 --- a/resources/views/website/inc/website-foot.blade.php +++ b/resources/views/website/inc/website-foot.blade.php @@ -1,7 +1,8 @@ @yield('custom-foot') - + + {{--@if(auth()->check() && auth()->user()->hasRole('developer') && !request()->has('ediable'))--}} {{----}} diff --git a/routes/web.php b/routes/web.php index 2b6cee0..6e5e712 100644 --- a/routes/web.php +++ b/routes/web.php @@ -368,7 +368,9 @@ Route::name('client.')->group(function (){ Route::get('attach/download/{attachment}', [\App\Http\Controllers\ClientController::class,'attachDl'])->name('attach-dl'); Route::get('/post/{post}', [\App\Http\Controllers\ClientController::class,'post'])->name('post'); - Route::get('product/fav/toggle/{product}', [\App\Http\Controllers\ClientController::class, 'ProductFavToggle'])->name('client.product-fav-toggle'); + Route::get('product/fav/toggle/{product}', [\App\Http\Controllers\ClientController::class, 'ProductFavToggle'])->name('product-fav-toggle'); + Route::get('product/compare/toggle/{product}', [\App\Http\Controllers\ClientController::class, 'productCompareToggle'])->name('product-compare-toggle'); + Route::get('card/toggle/{product}', [\App\Http\Controllers\ClientController::class, 'productCardToggle'])->name('product-card-toggle'); Route::post('/comment/submit', [\App\Http\Controllers\ClientController::class,'submitComment'])->name('comment.submit'); })->middleware([\App\Http\Middleware\VisitorCounter::class]);