File: D:/HostingSpaces/SBogers10/topswtw.komma.pro/app/KommaApp/Shop/Sitemap/SitemapService.php
<?php
namespace KommaApp\Shop\Sitemap;
/**
* Short description for the file.
*
* @author Tim Van Samang <timvansamang@komma.pro>
* @copyright (c) 2012-2015, Komma Mediadesign
*/
use Carbon\Carbon;
use \KommaApp\Shop\Routes\RouteService;
use \Roumen\Sitemap\Sitemap;
class SitemapService
{
private $routeService;
public function __construct(RouteService $routeService)
{
$this->routeService = $routeService;
}
/***
* This method will generate the sitemap based on the routes table
* It will collect the routes based on the given groups array.
* Then it will build the sitemap grouped by the linked id
*
* @param array $groups , wich routes we want in the sitemap
* @param float $weight_from / the minimum weight of the sitemap itens
* @return Sitemap object
*/
public function getSitemap($groups = ['pages', 'categories', 'products'], $weight_from = 0.1, $homePage = true, $translation = true)
{
//Create the sitemap class
$sitemap = new Sitemap([]);
//$sitemap = \App::make('sitemap');
//Is the homepage variale set
if ($homePage) {
// Ad the home page on road / to the sitemap
$hreflang = 'en_' . strtolower(\Shop::getDomainCountry());
$sitemap->add(\URL::to('/'), '2012-08-25T20:10:00+02:00', '1.0', 'monthly', [], 'Home', [
['language' => $hreflang, 'url' => \URL::to('/en')],
]);
}
//Check if there is already an current langauage (uri)
if (!$default_language = \Shop::getLanguageService()->getCurrentLanguageId()) {
//Load the default_language based on the shop
$default_language = \Shop::getShop()->default_language_id;
}
//Load the available languages
$avialable_languages = \Shop::getLanguageService()->getLanguagesByShop(\Shop::getShop());
//Loop trough the routes based on the given parameters
foreach ($this->routeService->getSitemapRoutes($groups, $weight_from) as $type => $routeGroup) {
//Default refresh is daily
$refresh = 'daily';
//Refresh for pageTranslation is monthly
if ($type == 'Komma\Kms\Pages\Models\PageTranslation') $refresh = 'monthly';
//Loop the routes per group (pages, products, categories)
foreach ($routeGroup as $routes) {
//Check if the translation is true and the route doesn't exist in the default language
if ($translation && !isset($routes[$default_language])) {
//Route without translation
//Loop trough the available languages
foreach ($avialable_languages as $iso_key => $lang) {
//Check if the lang exist as route, if not continue
if (!isset($routes[$lang])) continue;
//If exist, create a sitemap entry without translations
$sitemap->add(\URL::to($routes[$lang]['route']), Carbon::now(), $routes[$lang]['weight'], $refresh);
}
//go out of the loop
continue;
} else if (!isset($routes[$default_language])) continue;
//The default language exists
$translations = [];
//Is translation true
if ($translation) {
//Loop trough the available languages
foreach ($avialable_languages as $iso_key => $lang) {
//if it is the default lang, skip
// if ($lang == $default_language) continue;
//if the route in this lang doesnt exist, skip
if (!isset($routes[$lang])) continue;
//Add the route as translation
$hreflang = $iso_key . '_' . strtolower(\Shop::getDomainCountry());
$translations[] = ['language' => $hreflang, 'url' => \URL::to($routes[$lang]['route'])];
}
}
$sitemap->add(\URL::to($routes[$default_language]['route']), Carbon::now(), $routes[$default_language]['weight'], $refresh, [], $routes[$default_language]['name'], $translations);
}
}
return $sitemap;
}
}