File: D:/HostingSpaces/Eurotools/euro-tools.nl/app/KommaApp/Sitemap/SitemapService.php
<?php
namespace App\KommaApp\Sitemap;
use App\KommaApp\Pages\Models\Page;
use App\KommaApp\Shop\Categories\Models\Category;
use App\KommaApp\Shop\Products\Product\Product;
use Carbon\Carbon;
use Roumen\Sitemap\Sitemap;
class SitemapService
{
/**
* Generate a sitemap xml
*
* @return Sitemap
*/
public function makeSitemap()
{
$sitemap = \App::make('sitemap');
$this->addPagesToSitemap($sitemap);
$this->addCategoriesToSitemap($sitemap);
$this->addProductsToSitemap($sitemap);
return $sitemap->render();
}
/**
* Append a prepared xml array to the sitemap
*
* @param array $xmlArray
* @param Sitemap $sitemap
*/
private function addXmlArrayToSitemap(Array $xmlArray, Sitemap &$sitemap)
{
foreach ($xmlArray as $xmlObject){
if(isset($xmlObject->translation)){
$sitemap->add($xmlObject->loc, Carbon::now(), 0.5,'monthly',[],[],$xmlObject->translation);
}
else{
$sitemap->add($xmlObject->loc, Carbon::now(), 0.5,'monthly');
}
}
}
/**
* Add pages to the sitemap
*
* @param Sitemap $sitemap
*/
private function addPagesToSitemap(Sitemap &$sitemap)
{
$pages = \App::getSite()
->pages()
->where('lft', '!=', 1)
->where('active', 1)
->with('translations')
->with('translations.route')
->get();
// Prepare xml array for appending to the sitemap
$xmlPages = [];
foreach ($pages as $page) {
// Generate for multiple languages
if(\Config::get('app.multipleLanguages')){
$pageTranslations = [];
// First we need to create an array of the available translations of this page
foreach ($page->translations as $translation) {
if(!isset($translation->route->alias)) continue;
$alias = $translation->route->alias;
if($alias === '/') $alias = '';
$pageTranslations[] = [
'url' => \URL::to($alias),
'language' => $translation->getLanguageIso()
];
}
// Then append for each individual page the translation alias with all available translations of this page to the xml array
foreach ($pageTranslations as $translation) {
$xmlPages[] = (object)[
'loc' => \URL::to($translation['url']),
'translation' => $pageTranslations
];
}
}
// Generate for single language
else{
$alias = $page->translations->first()->route->alias;
if($alias === '/') $alias = '';
$xmlPages[] = (object)[
'loc' => \URL::to($alias),
];
}
}
$this->addXmlArrayToSitemap($xmlPages, $sitemap);
}
/**
* Add the products to the sitemap
*
* @param Sitemap $sitemap
* @return null
*/
private function addProductsToSitemap(Sitemap &$sitemap)
{
// Get the product index
$productOverviewPage = Page::where('code_name', 'products')
->with('translations')
->with('translations.route')
->first();
// If the product index isn't found or inactive skip the products from adding to the sitemap
if(!isset($productOverviewPage) || ! $productOverviewPage->active) return null;
// Load the products
$products = Product::where('active', 1)
->with('translations')
->get();
// Prepare xml array for appending to the sitemap
$xmlProducts = [];
foreach ($products as $product) {
// Generate for multiple languages
if(\Config::get('app.multipleLanguages')){
$productTranslations = [];
// First we need to create an array of the available translations of this product
foreach ($product->translations as $translation) {
$productOverviewPageTranslation = $productOverviewPage->translations->where('language_id', '=', $translation->language_id)->first();
if(!isset($translation->slug) || !isset($productOverviewPageTranslation) || $translation->slug === '') continue;
$alias = $productOverviewPageTranslation->route->alias . '/' . $translation->slug;
$productTranslations[] = [
'url' => \URL::to($alias),
'language' => $translation->getLanguageIso()
];
}
// Then append for each individual product the translation alias with all available translations of this page to the xml array
foreach ($productTranslations as $translation) {
$xmlProducts[] = (object)[
'loc' => \URL::to($translation['url']),
'translation' => $productTranslations
];
}
}
// Generate for single language
else{
$alias = $productOverviewPage->translations->first()->route->alias.'/'.$product->translations->first()->slug;
if($alias === '/') $alias = '';
$xmlProducts[] = (object)[
'loc' => \URL::to($alias),
];
}
}
$this->addXmlArrayToSitemap($xmlProducts, $sitemap);
}
/**
* Add the categories to the sitemap
*
* @param Sitemap $sitemap
* @return null
*/
private function addCategoriesToSitemap(Sitemap &$sitemap)
{
// Get the category index
$categoryOverviewPage = Page::where('code_name', 'categories')
->with('translations')
->with('translations.route')
->first();
// If the category index isn't found or inactive skip the categories from adding to the sitemap
if(!isset($categoryOverviewPage) || ! $categoryOverviewPage->active) return null;
// Load the categories
$categories = Category::where('active', 1)
->with('translations')
->get();
// Prepare xml array for appending to the sitemap
$xmlCategories = [];
foreach ($categories as $category) {
// Generate for multiple languages
if(\Config::get('app.multipleLanguages')){
$categoryTranslations = [];
// First we need to create an array of the available translations of this category
foreach ($category->translations as $translation) {
$categoryOverviewPageTranslation = $categoryOverviewPage->translations->where('language_id', '=', $translation->language_id)->first();
if(!isset($translation->slug) || !isset($categoryOverviewPageTranslation) || $translation->slug === '') continue;
$alias = $categoryOverviewPageTranslation->route->alias . '/' . $translation->slug;
$categoryTranslations[] = [
'url' => \URL::to($alias),
'language' => $translation->getLanguageIso()
];
}
// Then append for each individual category the translation alias with all available translations of this page to the xml array
foreach ($categoryTranslations as $translation) {
$xmlCategories[] = (object)[
'loc' => \URL::to($translation['url']),
'translation' => $categoryTranslations
];
}
}
// Generate for single language
else{
$alias = $categoryOverviewPage->translations->first()->route->alias.'/'.$category->translations->first()->slug;
if($alias === '/') $alias = '';
$xmlCategories[] = (object)[
'loc' => \URL::to($alias),
];
}
}
$this->addXmlArrayToSitemap($xmlCategories, $sitemap);
}
}