FleetCart/Themes/Storefront/Http/ViewComposers/LayoutComposer.php

236 lines
6.2 KiB
PHP
Raw Permalink Normal View History

2023-06-11 12:14:03 +00:00
<?php
namespace Themes\Storefront\Http\ViewComposers;
2023-12-03 14:07:47 +00:00
use Exception;
use Illuminate\View\View;
2023-06-11 12:14:03 +00:00
use Mexitek\PHPColors\Color;
use Modules\Compare\Compare;
use Spatie\SchemaOrg\Schema;
use Modules\Tag\Entities\Tag;
use Modules\Cart\Facades\Cart;
use Modules\Menu\Entities\Menu;
use Modules\Page\Entities\Page;
use Modules\Media\Entities\File;
use Modules\Menu\MegaMenu\MegaMenu;
use Illuminate\Support\Facades\Cache;
use Modules\Category\Entities\Category;
use Modules\Product\Entities\SearchTerm;
class LayoutComposer
{
/**
2023-12-03 14:07:47 +00:00
* @var Compare
2023-06-11 12:14:03 +00:00
*/
private $compare;
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
* Create a new view composer instance.
*
2023-12-03 14:07:47 +00:00
* @param Compare $compare
2023-06-11 12:14:03 +00:00
*/
public function __construct(Compare $compare)
{
$this->compare = $compare;
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
/**
* Bind data to the view.
*
2023-12-03 14:07:47 +00:00
* @param View $view
*
2023-06-11 12:14:03 +00:00
* @return void
*/
public function compose($view)
{
$view->with([
'themeColor' => $this->getThemeColor(),
'compareCount' => $this->compare->count(),
'favicon' => $this->getFavicon(),
'logo' => $this->getHeaderLogo(),
'newsletterBgImage' => $this->getNewsletterBgImage(),
'privacyPageUrl' => $this->getPrivacyPageUrl(),
'categories' => $this->getCategories(),
'mostSearchedKeywords' => $this->getMostSearchedKeywords(),
'primaryMenu' => $this->getPrimaryMenu(),
'categoryMenu' => $this->getCategoryMenu(),
'cart' => $this->getCart(),
'wishlist' => $this->getWishlist(),
'compareList' => $this->compare->list(),
'footerMenuOne' => $this->getFooterMenuOne(),
'footerMenuTwo' => $this->getFooterMenuTwo(),
'footerTags' => $this->getFooterTags(),
'copyrightText' => $this->getCopyrightText(),
'acceptedPaymentMethodsImage' => $this->getAcceptedPaymentMethodsImage(),
'schemaMarkup' => $this->getSchemaMarkup(),
]);
}
2023-12-03 14:07:47 +00:00
public function footerTagsCallback($tagIds)
{
return function () use ($tagIds) {
return Tag::whereIn('id', $tagIds)
->when(!empty($tagIds), function ($query) use ($tagIds) {
$tagIdsString = collect($tagIds)->filter()->implode(',');
$query->orderByRaw("FIELD(id, {$tagIdsString})");
})
->get();
};
}
2023-06-11 12:14:03 +00:00
private function getThemeColor()
{
try {
return new Color(storefront_theme_color());
2023-12-03 14:07:47 +00:00
} catch (Exception $e) {
2023-06-11 12:14:03 +00:00
return new Color('#0068e1');
}
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function getFavicon()
{
return $this->getMedia(setting('storefront_favicon'))->path;
}
2023-12-03 14:07:47 +00:00
private function getMedia($fileId)
{
return Cache::rememberForever(md5("files.{$fileId}"), function () use ($fileId) {
return File::findOrNew($fileId);
});
}
2023-06-11 12:14:03 +00:00
private function getHeaderLogo()
{
return $this->getMedia(setting('storefront_header_logo'))->path;
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function getNewsletterBgImage()
{
return $this->getMedia(setting('storefront_newsletter_bg_image'))->path;
}
private function getPrivacyPageUrl()
{
return Cache::tags('settings')->rememberForever('privacy_page_url', function () {
return Page::urlForPage(setting('storefront_privacy_page'));
});
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function getCategories()
{
return Category::searchable();
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function getMostSearchedKeywords()
{
return Cache::remember('most_searched_keywords', now()->addHour(), function () {
return SearchTerm::select('term')->orderByDesc('hits')->take(5)->pluck('term');
});
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function getPrimaryMenu()
{
return new MegaMenu(setting('storefront_primary_menu'));
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function getCategoryMenu()
{
return new MegaMenu(setting('storefront_category_menu'));
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function getCart()
{
return Cart::instance();
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function getWishlist()
{
if (auth()->guest()) {
return collect();
}
return auth()->user()->wishlist()->pluck('product_id');
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function getFooterMenuOne()
{
return $this->getFooterMenu(setting('storefront_footer_menu_one'));
}
private function getFooterMenu($menuId)
{
return Cache::tags(['menu_items', 'categories', 'pages', 'settings'])
->rememberForever(md5("storefront_footer_menu.{$menuId}:" . locale()), function () use ($menuId) {
return Menu::for($menuId);
});
}
2023-12-03 14:07:47 +00:00
private function getFooterMenuTwo()
{
return $this->getFooterMenu(setting('storefront_footer_menu_two'));
}
2023-06-11 12:14:03 +00:00
private function getFooterTags()
{
$tagIds = setting('storefront_footer_tags', []);
return Cache::tags(['tags', 'settings'])
->rememberForever(
md5('storefront_footer_tags:' . serialize($tagIds) . ':' . locale()),
$this->footerTagsCallback($tagIds)
);
}
private function getCopyrightText()
{
return strtr(setting('storefront_copyright_text'), [
'{{ store_url }}' => route('home'),
'{{ store_name }}' => setting('store_name'),
'{{ year }}' => date('Y'),
]);
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function getAcceptedPaymentMethodsImage()
{
return $this->getMedia(setting('storefront_accepted_payment_methods_image'));
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function getSchemaMarkup()
{
return Schema::webSite()
->url(route('home'))
->potentialAction($this->searchActionSchema());
}
2023-12-03 14:07:47 +00:00
2023-06-11 12:14:03 +00:00
private function searchActionSchema()
{
return Schema::searchAction()
->target(route('products.index') . '?query={search_term_string}')
->setProperty('query-input', 'required name=search_term_string');
}
}