Powerful and easy to use cms to build websites, blogs or ecommerce stores.
HTML backendblogblog-engineblog-platformbloggingcmscontent-managementcontent-management-systemecommerceecommerce-platformno-codepage-builderphpphp-cmsphp7php8self-hostedshopping-cartweb-applicationwebsite-builder
User Wishlist
givanz6dfd4c6a5ee1
diff --git a/app/component/user/wishlist.php b/app/component/user/wishlist.phpindex 133f00c..e550740 100644--- a/app/component/user/wishlist.php+++ b/app/component/user/wishlist.php@@ -25,12 +25,15 @@ namespace Vvveb\Component\User;use Vvveb\Sql\User_WishlistSQL;use Vvveb\System\Component\ComponentBase;use Vvveb\System\Event;+use Vvveb\System\Images;use function Vvveb\url;class Wishlist extends ComponentBase {public static $defaultOptions = [- 'start' => 0,- 'limit' => 10,+ 'user_id' => null,+ 'start' => 0,+ 'limit' => 10,+ 'image_size' => 'thumb',];//don't cache@@ -45,10 +48,15 @@ class Wishlist extends ComponentBase {$wishlist = new User_WishlistSQL();$results = $wishlist->getAll($this->options);- if ($results && isset($results['wishlist'])) {- foreach ($results['wishlist'] as $id => &$wishlist) {- $wishlist['url'] = url('user/wishlist/edit', $wishlist);- $wishlist['delete-url'] = url('user/wishlist/delete', $wishlist);+ if ($results && isset($results['user_wishlist'])) {+ foreach ($results['user_wishlist'] as $id => &$product) {+ if (isset($product['image']) && $product['image']) {+ $product['image'] = Images::image($product['image'], 'product', $this->options['image_size']);+ }++ $url = ['slug' => $product['slug'], 'product_id' => $product['product_id']];+ $product['url'] = url('product/product/index', $url);+ $product['delete-url'] = url('user/wishlist/delete', $product);}}}diff --git a/app/component/wishlist.php b/app/component/wishlist.phpnew file mode 100644index 0000000..98ceafb--- /dev/null+++ b/app/component/wishlist.php@@ -0,0 +1,146 @@+<?php++/**+ * Vvveb+ *+ * Copyright (C) 2022 Ziadin Givan+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ */++namespace Vvveb\Component;++use Vvveb\System\Cart\Currency;+use Vvveb\System\Cart\Tax;+use Vvveb\System\Component\ComponentBase;+use Vvveb\System\Event;+use Vvveb\System\Images;+use function Vvveb\url;++class Wishlist extends ComponentBase {+ public static $defaultOptions = [+ 'start' => 0,+ 'page' => 1,+ 'limit' => 4,+ 'status' => 1,+ 'language_id' => null,+ 'site_id' => null,+ 'type' => 'product',+ 'user_id' => NULL,+ 'taxonomy_item_id' => NULL,+ 'image_size' => 'medium',+ 'filter' => null,+ ];++ public $options = [];++ function results() {+ $products = new \Vvveb\Sql\User_wishlistSQL();++ if ($page = $this->options['page']) {+ $this->options['start'] = ($page - 1) * $this->options['limit'];+ }++ if ($this->options['filter']) {+ foreach ($this->options['filter'] as $name => $values) {+ if ($name == 'manufacturer_id' || $name == 'vendor_id') {+ $this->options[$name] = $values;+ }+ }+ }++ if (isset($this->options['product_id']) &&+ ($this->options['related'] || $this->options['variant'] || $this->options['source'] == 'autocomplete')) {+ if (! is_array($this->options['product_id'])) {+ $this->options['product_id'] = [$this->options['product_id'] => 1];+ }+ $this->options['product_id'] = array_keys($this->options['product_id']);+ } else {+ $this->options['product_id'] = [];+ }++ if (isset($this->options['order_by']) &&+ ! in_array($this->options['order_by'], ['product_id', 'price', 'created_at', 'updated_at'])) {+ unset($this->options['order_by']);+ }++ if (isset($this->options['direction']) &&+ ! in_array($this->options['direction'], ['asc', 'desc'])) {+ unset($this->options['direction']);+ }++ //if only one slug is provided then add it to array+ if (isset($this->options['slug']) && ! is_array($this->options['slug'])) {+ $this->options['slug'] = [$this->options['slug']];+ }++ //if only one manufacturer_id is provided then add it to array+ if (isset($this->options['manufacturer_id']) && ! is_array($this->options['manufacturer_id'])) {+ $this->options['manufacturer_id'] = [$this->options['manufacturer_id']];+ }++ //if only one vendor_id is provided then add it to array+ if (isset($this->options['vendor_id']) && ! is_array($this->options['vendor_id'])) {+ $this->options['vendor_id'] = [$this->options['vendor_id']];+ }++ $results = $products->getAll($this->options) + $this->options;++ if ($results && isset($results['products'])) {+ $tax = Tax::getInstance($this->options);+ $currency = Currency::getInstance($this->options);++ foreach ($results['products'] as $id => &$product) {+ $language = [];++ if ($product['language_id'] != $this->options['default_language_id']) {+ $language = ['language' => $this->options['language']];+ }++ if (isset($product['images'])) {+ $product['images'] = json_decode($product['images'], true);++ foreach ($product['images'] as &$image) {+ $image['image'] = Images::image($image['image'], 'product', $this->options['image_size']);+ }+ }++ if (isset($product['image']) && $product['image']) {+ $product['image'] = Images::image($product['image'], 'product', $this->options['image_size']);+ //$product['images'][] = ['image' => Images::image($product['image'], 'product')];+ }++ //rfc+ $product['pubDate'] = date('r', strtotime($product['created_at']));++ $url = ['slug' => $product['slug'], 'product_id' => $product['product_id']] + $language;+ $product['url'] = url('product/product/index', $url);+ $product['add_cart_url'] = url('cart/cart/add', ['product_id' => $product['product_id']]);+ $product['buy_url'] = url('checkout/checkout/index', ['product_id' => $product['product_id']]);+ $product['add_wishlist_url'] = url('user/wishlist/add', ['product_id' => $product['product_id']]);+ $product['add_compare_url'] = url('cart/compare/add', ['product_id' => $product['product_id']]);+ $product['full-url'] = url('product/product/index', $url + ['host' => SITE_URL, 'scheme' => $_SERVER['REQUEST_SCHEME'] ?? 'http']);++ $product['price_tax'] = $tax->addTaxes($product['price'], $product['tax_type_id']);+ $product['price_tax_formatted'] = $currency->format($product['price_tax']);+ $product['price_formatted'] = $currency->format($product['price']);+ }+ }++ list($results) = Event :: trigger(__CLASS__,__FUNCTION__, $results);++ return $results;+ }+}diff --git a/app/controller/user/wishlist.php b/app/controller/user/wishlist.phpindex eb8e1ca..0a265e2 100755--- a/app/controller/user/wishlist.php+++ b/app/controller/user/wishlist.php@@ -22,13 +22,39 @@namespace Vvveb\Controller\User;-use Vvveb\System\User\User;+use Vvveb\Sql\User_wishlistSQL;class Wishlist extends Base {function index() {}+ private function action($action) {+ $productId = (int) ($this->request->request['product_id'] ?? false);++ if ($productId) {+ $wishlist = new User_wishlistSQL();++ switch ($action) {+ case 'add':+ $result = $wishlist->add(['user_id' => $this->global['user_id'], 'product_id' => $productId]);++ break;++ case 'remove':+ $result = $wishlist->delete(['user_id' => $this->global['user_id'], 'product_id' => [$productId]]);++ break;+ }++ return $this->index();+ }+ }+function add() {- $productId = $this->request->request['product_id'];+ return $this->action('add');+ }++ function remove() {+ return $this->action('remove');}}diff --git a/app/template/components/user/wishlist.tpl b/app/template/components/user/wishlist.tplnew file mode 100644index 0000000..cd22322--- /dev/null+++ b/app/template/components/user/wishlist.tpl@@ -0,0 +1,128 @@+//set selector prefix to have shorter and easier to read selectors for rules+@wishlist = [data-v-component-user-wishlist]+@product = [data-v-component-user-wishlist] [data-v-product]+@product-image = [data-v-product-images] [data-v-product-image]++//we only need the first product element to iterate+@product|deleteAllButFirstChild++//set component variables+@wishlist|prepend = <?php+ $vvveb_is_page_edit = Vvveb\isEditor();++ //use a counter to know which component instance we need to use if there are more than one component on page+ if (isset($wishlist_idx)) $wishlist_idx++; else $wishlist_idx = 0;+ $previous_component = isset($current_component)?$current_component:null;+ $wishlist = $current_component = $this->_component['user_wishlist'][$wishlist_idx] ?? [];++ $index = 0;+ $count = $wishlist['count'] ?? 0;+ $limit = isset($wishlist['limit'])? $wishlist['limit'] : 5;++ //if page loaded in editor then set a fist empty product if there are no wishlist+ //to render an empty product to avoid losing the html on edit+ $_default = (isset($vvveb_is_page_edit) && $vvveb_is_page_edit ) ? [0 => []] : false;+ //$_default = [0 => []];+ $_wishlist = empty($wishlist['user_wishlist']) ? $_default : $wishlist['user_wishlist'];+?>++@wishlist [data-v-wishlist-category] = <?php $_category = current($wishlist);echo $_category['category'];?>+@wishlist [data-v-wishlist-count] = <?php echo $wishlist['count'] ?? ''?>+@wishlist [data-v-wishlist-manufacturer] = <?php $_manufacturer = current($wishlist);echo $_manufacturer['manufacturer'];?>+++@product|before = <?php++if ($_wishlist) {+ foreach ($_wishlist as $index => $_product) { $index++;?>++ @product [data-v-product-*]|innerText = $_product['@@__data-v-product-(*)__@@']+ @product a[data-v-product-*]|href = $_product['@@__data-v-product-(*)__@@']++ //editor attributes+ @product|data-v-id = $_product['product_id']+ @product|data-v-type = 'product'++ @product [name="product_id"]|value = $_product['product_id']++ //title attributes+ @product [data-v-product-alt]|alt = $_product['name']++ @product [data-product_id]|data-product_id = $_product['product_id']++ //url+ @product [data-v-product-url]|title = $_product['name']+ @product a[data-v-product-url]|href = $_product['url']++ //image+ //@product [data-v-product-image]|src = $_product['images'][0]['image']+ //@product [data-v-product-image]|data-v-id = $_product['images'][0]['id']+ //@product [data-v-product-image]|data-v-type = 'product_image'++ @product [data-v-product-image]|src = <?php+ if (isset($_product['image'])) {+ $image = $_product['image'] ?? '';+ //$size = '@@__data-v-product-image__@@';+ $size = '@@__data-v-size__@@';+ if ($size) {+ //$image = Vvveb\System\Images::size($image, $size);+ echo $image;+ } else {+ echo $image;+ }+ }+ ?>++ @product [data-v-product-image-url] = $_product['image']+ //@product [data-v-product-image]|data-v-id = 'image'+ //@product [data-v-product-image]|data-v-type = 'product_image'++ //usually used for second image to show hover [data-v-product-image-0] [data-v-product-image-1]+ //@product [data-v-product-image-*]|src = $_product['images']['@@__data-v-product-image-(\d+)__@@']['image']++ @product [data-v-product-image-*]|src = <?php+ //$size = '@@__data-v-product-image-\d+__@@';+ $size = '@@__data-v-size__@@';+ $nr = '@@__data-v-product-image-(\d+)__@@';+ if (isset($_product['images'][$nr]['image'])) {+ $image = $_product['images'][$nr]['image'];+ if ($size) {+ //echo imageSize($_product['image'], $size);+ //$image = Vvveb\System\Images::size($image, $size);+ echo $image;+ } else {+ echo $image;+ }+ }+ ?>++++ @product [data-v-product-image-*]|data-v-id = $_product['images']['@@__data-v-product-image-(\d+)__@@']['id']+ @product [data-v-product-image-*]|data-v-type = 'product_image'++ //image gallery+ @product [data-v-product-images] [data-v-product-image]|before = <?php+ if(isset($_product['images']) && is_array($_product['images']))+ foreach ($_product['images'] as $_product_image_id => $image) {+ ?>++ @product [data-v-product-images] img[data-v-product-image-src]|src = $image['image']+ @product [data-v-product-images] img[data-v-product-image-src]|data-v-id = $_product_image_id+ @product [data-v-product-images] img[data-v-product-image-src]|data-v-type = 'product_image'+ @product [data-v-product-images] a[data-v-product-image-src]|href = $image['image']++ @product [data-v-product-images] [data-v-product-image]|after = <?php+ } ?>++ //catch all data attributes+ @product [data-v-product-*]|innerText = $_product['@@__data-v-product-(*)__@@']+ //echo description directly to avoid htmlentities escape+ @product [data-v-product-content] = <?php echo $_product['content'];?>++ @product|after = <?php+ }+}++$current_component = $previous_component;+?>