|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SiteBase |
| 5 | + * PHP Version 8.3 |
| 6 | + * |
| 7 | + * @category CMS / Framework |
| 8 | + * @package Degami\Sitebase |
| 9 | + * @author Mirko De Grandis <degami@github.com> |
| 10 | + * @license MIT https://opensource.org/licenses/mit-license.php |
| 11 | + * @link https://github.com/degami/sitebase |
| 12 | + */ |
| 13 | + |
| 14 | +namespace App\Base\Blocks; |
| 15 | + |
| 16 | +use App\Base\Abstracts\Blocks\BaseCodeBlock; |
| 17 | +use App\Base\Abstracts\Controllers\AdminPage; |
| 18 | +use App\Base\Abstracts\Controllers\BasePage; |
| 19 | +use Degami\Basics\Exceptions\BasicException; |
| 20 | +use DI\DependencyException; |
| 21 | +use DI\NotFoundException; |
| 22 | +use Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException; |
| 23 | +use Degami\Basics\Html\TagElement; |
| 24 | +use App\Base\Models\Cart; |
| 25 | +use App\App; |
| 26 | +use App\Base\Abstracts\Controllers\BaseHtmlPage; |
| 27 | +use App\Base\Models\CartItem; |
| 28 | +use App\Base\Interfaces\Model\ProductInterface; |
| 29 | +use App\Base\Abstracts\Models\FrontendModel; |
| 30 | +use App\Base\Controllers\Frontend\Commerce\Cart as CommerceCart; |
| 31 | +use App\Base\Tools\Search\AIManager as AISearchManager; |
| 32 | + |
| 33 | +/** |
| 34 | + * "You May Like" Block |
| 35 | + */ |
| 36 | +class YouMayLikeProducts extends BaseCodeBlock |
| 37 | +{ |
| 38 | + protected ?Cart $cart = null; |
| 39 | + protected ?AISearchManager $aiSearchManager = null; |
| 40 | + |
| 41 | + public function getCart(?BaseHtmlPage $current_page = null) : ?Cart |
| 42 | + { |
| 43 | + if ($this->cart instanceof Cart) { |
| 44 | + return $this->cart; |
| 45 | + } |
| 46 | + |
| 47 | + $cart = Cart::getCollection()->where([ |
| 48 | + 'user_id' => $current_page?->getCurrentUser()->getId(), |
| 49 | + 'website_id' => $current_page?->getCurrentWebsite()->getId(), |
| 50 | + 'is_active' => true, |
| 51 | + ])->getFirst(); |
| 52 | + |
| 53 | + if ($cart instanceof Cart) { |
| 54 | + $this->cart = $cart; |
| 55 | + return $this->cart; |
| 56 | + } |
| 57 | + |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * {@inheritdoc} |
| 63 | + * |
| 64 | + * @param BasePage|null $current_page |
| 65 | + * @param array $data |
| 66 | + * @return string |
| 67 | + * @throws BasicException |
| 68 | + * @throws PhpfastcacheSimpleCacheException |
| 69 | + * @throws DependencyException |
| 70 | + * @throws NotFoundException |
| 71 | + */ |
| 72 | + public function renderHTML(?BasePage $current_page = null, array $data = []): string |
| 73 | + { |
| 74 | + if (!App::getInstance()->getEnvironment()->getVariable('ENABLE_COMMERCE', false)) { |
| 75 | + return ''; |
| 76 | + } |
| 77 | + |
| 78 | + if (!($current_page instanceof CommerceCart)) { |
| 79 | + return ''; |
| 80 | + } |
| 81 | + |
| 82 | + $config = array_filter(json_decode($data['config'] ?? '{}', true)); |
| 83 | + |
| 84 | + $route_info = $current_page?->getRouteInfo(); |
| 85 | + |
| 86 | + // $current_page_handler = $route_info->getHandler(); |
| 87 | + if ($current_page?->getRouteGroup() == AdminPage::getRouteGroup() || $route_info?->isAdminRoute() || !$current_page?->getCurrentUser()) { |
| 88 | + return ''; |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + $youMayLike = []; |
| 93 | + foreach ($this->getCart($current_page)->getItems() as $item) { |
| 94 | + $youMayLike = array_unique_by( |
| 95 | + array_merge($youMayLike, $this->getProductSuggestions($item)), |
| 96 | + fn ($el) => $el['type'] . '::' . $el['id'] |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + $cartIdentifiers = array_map(function(CartItem $item) { |
| 101 | + return static::getClassBasename($item->getProduct()).'::'.$item->getProduct()->getId(); |
| 102 | + }, $this->getCart($current_page)->getItems()); |
| 103 | + |
| 104 | + $suggestions = []; |
| 105 | + foreach ($youMayLike as $elem) { |
| 106 | + if (is_subclass_of($elem['modelClass'], ProductInterface::class)) { |
| 107 | + $suggestion = $this->containerCall([$elem['modelClass'], 'load'], ['id' => $elem['id']]); |
| 108 | + |
| 109 | + if (in_array(static::getClassBasename($suggestion).'::'.$suggestion->getId(), $cartIdentifiers)) { |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + $suggestions[] = $suggestion; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + $list = []; |
| 118 | + foreach ($suggestions as $suggestion) { |
| 119 | + /** @var FrontendModel $suggestion */ |
| 120 | + $link = $this->containerMake(TagElement::class, ['options' => [ |
| 121 | + 'tag' => 'a', |
| 122 | + 'attributes' => [ |
| 123 | + 'href' => $suggestion->getFrontendUrl(), |
| 124 | + ], |
| 125 | + 'text' => $suggestion->getTitle(), |
| 126 | + ]]); |
| 127 | + |
| 128 | + $sku = $this->containerMake(TagElement::class, ['options' => [ |
| 129 | + 'tag' => 'span', |
| 130 | + 'attributes' => [ |
| 131 | + 'class' => 'sku', |
| 132 | + ], |
| 133 | + 'text' => $this->getUtils()->translate('SKU', locale: $current_page?->getCurrentLocale()) . ': ' . $suggestion->getSku(), |
| 134 | + ]]); |
| 135 | + |
| 136 | + $price = $this->containerMake(TagElement::class, ['options' => [ |
| 137 | + 'tag' => 'span', |
| 138 | + 'attributes' => [ |
| 139 | + 'class' => 'price', |
| 140 | + ], |
| 141 | + 'text' => $this->getUtils()->translate('Price', locale: $current_page?->getCurrentLocale()) . ': ' . $this->getUtils()->formatPrice($suggestion->getPrice(), $this->getCart($current_page)->getCurrencyCode()), |
| 142 | + ]]); |
| 143 | + |
| 144 | + $list[] = '<div class="row"><div class="col-12">'.$link . '</div><div class="col d-flex justify-content-between">' . $sku . $price . '</div></div>'; |
| 145 | + } |
| 146 | + |
| 147 | + if (empty($list)) { |
| 148 | + return ''; |
| 149 | + } |
| 150 | + |
| 151 | + return "".$this->containerMake(TagElement::class, ['options' => [ |
| 152 | + 'tag' => 'div', |
| 153 | + 'attributes' => [ |
| 154 | + 'class' => 'pt-5', |
| 155 | + ], |
| 156 | + 'text' => '<h2>'.$this->getUtils()->translate('You may also like', locale: $current_page?->getCurrentLocale()).'</h2><ul class="list-group"><li class="list-group-item">'.implode('</li><li class="list-group-item">', $list).'</li></ul>', |
| 157 | + ]]); |
| 158 | + } |
| 159 | + |
| 160 | + public function isCachable() : bool |
| 161 | + { |
| 162 | + return false; |
| 163 | + } |
| 164 | + |
| 165 | + protected function getProductSuggestions(CartItem $item, ?BasePage $current_page = null) : array |
| 166 | + { |
| 167 | + /** @var ProductInterface $product */ |
| 168 | + $product = $item->getProduct(); |
| 169 | + |
| 170 | + $locale = $current_page?->getCurrentLocale() ?? App::getInstance()->getCurrentLocale(); |
| 171 | + $website_id = App::getInstance()->getSiteData()->getCurrentWebsiteId(); |
| 172 | + |
| 173 | + $cacheKey = 'youmaylike.'.$product->getSku().'.'.$locale.'.'.$website_id; |
| 174 | + |
| 175 | + if (App::getInstance()->getCache()->has($cacheKey)) { |
| 176 | + $result = (array) App::getInstance()->getCache()->get($cacheKey); |
| 177 | + return array_map(fn ($el) => $el['data'], $result['docs']); |
| 178 | + } |
| 179 | + |
| 180 | + /** @var FrontendModel $product */ |
| 181 | + $embeddable = $this->getSearchManager()->getEmbeddableDataForFrontendModel($product); |
| 182 | + $result = $this->getSearchManager()->searchNearby(implode(' ', array_filter($embeddable))); |
| 183 | + |
| 184 | + App::getInstance()->getCache()->set($cacheKey, $result, 1800); |
| 185 | + |
| 186 | + return array_map(fn ($el) => $el['data'], $result['docs']); |
| 187 | + } |
| 188 | + |
| 189 | + protected function getSearchManager(string $llmCode = 'googlegemini') : AISearchManager |
| 190 | + { |
| 191 | + if (!is_null($this->aiSearchManager)) { |
| 192 | + return $this->aiSearchManager; |
| 193 | + } |
| 194 | + |
| 195 | + /** @var AISearchManager $embeddingManager */ |
| 196 | + return $this->aiSearchManager = $this->containerMake(AISearchManager::class, [ |
| 197 | + 'llm' => $this->getAI()->getAIModel($llmCode), |
| 198 | + 'model' => match ($llmCode) { |
| 199 | + 'googlegemini' => 'text-embedding-004', |
| 200 | + 'chatgpt' => 'text-embedding-3-small', |
| 201 | + 'claude' => 'claude-2.0-embedding', |
| 202 | + 'groq' => 'groq-vector-1', |
| 203 | + 'mistral' => 'mistral-embedding-001', |
| 204 | + 'perplexity' => 'perplexity-embedding-001', |
| 205 | + default => null, |
| 206 | + } |
| 207 | + ]); |
| 208 | + } |
| 209 | +} |
0 commit comments