File: D:/HostingSpaces/SBogers10/vebon.komma.pro/app/KommaApp/Kms/Core/SearchIndex/SearchIndexService.php
<?php namespace KommaApp\Kms\Core\SearchIndex;
use Illuminate\Database\DatabaseManager;
use KommaApp\Categories\Models\Category;
use KommaApp\Languages\Models\Language;
use KommaApp\Products\ProductRepository;
use KommaApp\Pages\Models\PageTranslation;
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma Mediadesign
*/
class SearchIndexService
{
/**
* @var DatabaseManager
*/
protected $db;
/**
* @var SearchIndexRepository
*/
protected $searchIndexRepository;
/**
* @var ProductRepository
*/
protected $productRepository;
function __construct(
DatabaseManager $db,
SearchIndexRepository $searchIndexRepository,
ProductRepository $productRepository
)
{
$this->db = $db;
$this->searchIndexRepository = $searchIndexRepository;
$this->productRepository = $productRepository;
}
public function reindexAllCategoriesAndProducts()
{
$this->reindex('all');
}
/***
* Rebuild the search index based in what.
*
* @param string $what , eg.all, products, pages, categories
*/
public function reindex($what = 'all')
{
/**Empty the current index*/
$this->what = $what;
//If what is all or products index products, remove current index
if ($this->what == 'all' || $this->what == 'products') \Search::index('products')->deleteIndex();
//If what is all or pages index pages, remove current index
if ($this->what == 'all' || $this->what == 'pages') \Search::index('pages')->deleteIndex();
//If what is all or categories index catetgories, remove current index
if ($this->what == 'all' || $this->what == 'categories') \Search::index('categories')->deleteIndex();
/** Create new index*/
//index the categories and his products of what is all, pages or products
if ($this->what == 'all' || $this->what == 'categories'||$this->what == 'products') $this->indexCategoriesAndItsProducts();
//index the pages if what is all or pages
if ($this->what == 'all' || $this->what == 'pages') $this->indexPages();
}
/**
* Index categories and the products from a category;
*/
public function indexCategoriesAndItsProducts()
{
//Load the root categorie (lft =1)
$rootCategories = Category::where('lft', '=', '1')->get();
//Loop trough the rootCategories (equqals sites)
foreach ($rootCategories as $category) {
$this->addCategoryAndChildren($category);
}
}
/***
* This will remove the index of a specific category
* This is called by the categoryRepository on destroy
*
* @param Category $category
*/
public function removeCategory(Category $category)
{
// Get the ids of the translations in the category
$categoryTranslations = $this->searchIndexRepository
->getCategoryTranslations($category, ['category_translations.id as id']);
// Remove the selected category from the search index
foreach ($categoryTranslations as $categoryTranslation)
\Search::index('categories')->delete($categoryTranslation->id);
// Get the ids of all product translations of the category and its children
$productTranslations = $this->searchIndexRepository
->getProductTranslationsInCategory($category, ['product_translations.id as id']);
// Remove the selected category from the search index
foreach ($productTranslations as $productTranslation)
\Search::index('products')->delete($productTranslation->id);
}
/***
* This will remove the index of a specific category
* This is not called because we will reindex all
*
* @param Category $category
*/
public function removeCategoryAndChildren(Category $category)
{
// Get the ids of the translations in the category
$categoryTranslations = $this->searchIndexRepository
->getCategoryTranslationsHierarchically($category, ['category_translations.id as id']);
// Remove the selected categories from the search index
foreach ($categoryTranslations as $categoryTranslation)
\Search::index('categories')->delete($categoryTranslation->id);
// Get the ids of all product translations of the category and its children
$productTranslations = $this->searchIndexRepository
->getProductTranslationsInCategoryHierarchically($category, ['product_translations.id as id']);
// Remove the selected products from the search index
foreach ($productTranslations as $productTranslation)
\Search::index('products')->delete($productTranslation->id);
}
/***
* Reindex all categories and categorie children
* All the products from a categorie will be indexed
*
* @param Category $category
*/
public function addCategoryAndChildren(Category $category)
{
// Get the categories to insert in the search index
$categoryTranslations = $this->searchIndexRepository
->getActiveCategoryTranslationsHierarchically($category, [
'categories.id as id',
'category_translations.id as translation_id',
'category_translations.name as title',
'category_translations.description as content',
'categories.site_id as site_id',
'category_translations.language_id as language_id',
'routes.route as route'
]);
// Put the categories in the search index
$count = 0;
foreach ($categoryTranslations as $categoryTranslation) {
$this->insertCategory($categoryTranslation);
$count++;
if ($count == 1000) {
$count = 0;
\Log::info("Indexed 1000 categorie translations");
}
}
\Log::info("Done: " . count($categoryTranslations) . " categorie translations indexed");
//index the products from the current $category
$this->indexCategoryProducts($category);
}
/***
* Index the products of the given category
*
* @param $category
*/
private function indexCategoryProducts($category)
{
// Get the products to insert in the search index
$productTranslations = $this->searchIndexRepository
->getProductTranslationsInActiveCategoriesHierarchically($category, [
'products.id as id',
'product_translations.id as translation_id',
'categories.id as category_id',
'product_translations.name as title',
'product_translations.description as content',
'products_sites.site_id as site_id',
'product_translations.language_id as language_id',
'products.name as name',
'products.brand_name as brand_name',
'products.supplier_name as supplier_name',
'products.article_number as article_number',
'products.internal_article_number as internal_article_number',
'products.bypass as bypass',
'products.fine_dust_filter as fine_dust_filter',
'products.filter_class as filter_class',
'products.size as size',
'products.is_filter_set as is_filter_set',
'product_translations.special_1 as special_1',
'product_translations.special_2 as special_2',
'product_translations.composition as composition',
'products_sites.price as price',
'routes.route as route'
]);
// Put the products in the search index
$count = 0;
foreach ($productTranslations as $productTranslation) {
$this->insertProduct($productTranslation);
$count++;
if ($count == 1000) {
$count = 0;
\Log::info("Indexed 1000 product translations");
}
}
\Log::info("Done: " . count($productTranslations) . " product translations indexed");
}
/***
* Index all the pages
*/
public function indexPages()
{
//Load all the pages
$pageTranslations = PageTranslation::with('page')->get();
$count = 0;
foreach($pageTranslations as $pageTranslation){
$this->insertPage($pageTranslation);
if ($count == 1000) {
$count = 0;
\Log::info("Indexed 1000 pages translations");
}
}
\Log::info("Done: " . count($pageTranslations) . " pages translations indexed");
}
/***
* Insert One category in the index
*
* @param $category
*/
public function insertCategory($category)
{
\Search::index('categories')->insert($category->translation_id, [
'title' => \Str::ascii($category->title),
'content' => \Str::ascii($category->content),
'siteId' => $category->site_id,
'languageId' => $category->language_id
], [
'title' => $category->title,
'id' => $category->id,
'type' => 'category',
'route' => $category->route,
]);
}
/**
* Index one given product in the search index
* @param $product
*/
public function insertProduct($product)
{
// $discounts = $this->productRepository->getProductDiscountsByCategoryId($product->category_id);
$titleArray = [];
if ($product->brand_name) $titleArray[] = $product->brand_name;
if ($product->title) $titleArray[] = $product->title;
if ($product->special_1) $titleArray[] = $product->special_1;
if ($product->special_2) $titleArray[] = $product->special_2;
if ($product->is_filter_set) $titleArray[] = \Lang::get('pages/products.is_filter_set_' . $product->is_filter_set, [], Language::find($product->language_id)->first()->iso_2);
if ($product->filter_class) $titleArray[] = $product->filter_class;
if ($product->internal_article_number) $titleArray[] = $product->internal_article_number;
$fullTitle = implode(' ', $titleArray);
if ($product->article_number) $titleArray[] = $product->article_number;
$title = implode(' ', $titleArray);
\Search::index('products')->insert($product->translation_id, [
'title' => $title,
'content' => \Str::ascii($product->content),
'siteId' => $product->site_id,
'languageId' => $product->language_id,
], [
'id' => $product->id,
'title' => $fullTitle,
'type' => 'product',
'route' => $product->route
]);
}
/***
* Insert One page in the index
*
* @param $page
*/
public function insertPage($pageTranslation)
{
\Search::index('pages')->insert($pageTranslation->id, [
'title' => $pageTranslation->name,
'content' => $pageTranslation->description . ' ' . $pageTranslation->description_two,
'siteId' => $pageTranslation->page->site_id,
'languageId' => $pageTranslation->language_id,
], [
'id' => $pageTranslation->page->id,
]);
}
}