File: D:/HostingSpaces/SBogers10/umans.komma.pro/app/Komma/Sitemap/SitemapService.php
<?php
namespace Komma\Sitemap;
use Komma\Pages\Models\Page;
use Komma\Posts\Models\Post;
use Komma\Projects\Models\Project;
use Roumen\Sitemap\Sitemap;
use Carbon\Carbon;
class SitemapService
{
/***
* 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 , which routes we want in the sitemap
* @param float $weight_from / the minimum weight of the sitemap items
* @return Sitemap object
*/
public function getSitemap($asXML = false)
{
if( ! $asXML) return $this->getActiveRoutesOneLanguage();
//Create the sitemap class
$sitemap = new Sitemap([]);
//Loop trough the routes based on the given parameters
foreach ($this->getActiveRoutesMultiLanguage() as $route)
{
$sitemap->add(
\URL::to($route['loc']),
Carbon::now(),
0.5,
'monthly',
[],
null,
$route['translation']
);
}
return $sitemap;
}
public function getActiveRoutesOneLanguage()
{
$urls = [];
$urls['pages'] = Page::with('translation')
->with('translation.route')
->where('pages.active', '=', 1)
->where('pages.lft', '!=', 1)
->orderBy('lft')
->get();
// $urls['projects'] = Project::with('translation')
// ->with('translation.route')
// ->where('projects.active', '=', 1)
// ->where('projects.code_name', '!=', 'null')
// ->where('projects.lft', '!=', 1)
// ->orderBy('lft')
// ->get();
// $urls['blog'] = Post::with('translation')
// ->with('translation.route')
// ->where('active', '=', 1)
// ->where('lft', '!=', 1)
// ->where('date', '<=', Carbon::now()->addHour())
// ->orderBy('date', 'desc')
// ->get();
$sitemap = [];
//dde($urls);
foreach ($urls as $key => $type)
{
foreach ($type as $link)
{
//check if translation isset else page not available in this language
if( ! $link->translation) continue;
//if blog or projects prepend text for visibility
if($key == 'blog') $name = trans('translations.sitemapPosts') . ': ' . $link->translation->name;
elseif($key == 'projects') $name = trans('translations.sitemapProjects') . ': ' . $link->translation->name;
else $name = $link->translation->name;
$sitemap[$link->lft] = [
'type' => $key,
'name' => $name,
'route' => $link->translation->route->route,
'lft' => $link->lft,
'rgt' => $link->rgt,
];
}
}
return $sitemap;
}
public function getActiveRoutesMultiLanguage()
{
$urls = [];
$urls['pages'] = Page::with('allTranslations')
->with('allTranslations.route')
->where('pages.active', '=', 1)
->where('pages.lft', '!=', 1)
->orderBy('lft')
->get();
$urls['projects'] = Project::with('allTranslations')
->with('allTranslations.route')
->where('projects.active', '=', 1)
->where('projects.code_name', '!=', 'null')
->where('projects.lft', '!=', 1)
->orderBy('lft')
->get();
$urls['blog'] = Post::with('allTranslations')
->with('allTranslations.route')
->where('active', '=', 1)
->where('lft', '!=', 1)
->where('date', '<=', Carbon::now()->addHour())
->orderBy('date', 'desc')
->get();
$sitemap = [];
foreach ($urls as $key => $type)
{
foreach ($type as $link)
{
if( ! isset($link->allTranslations)) continue;
$allTranslation = [];
foreach ($link->allTranslations as $translation)
{
$allTranslation[] = [
'url' => \URL::to($translation->route->route),
'language' => $translation->iso_2
];
}
foreach ($link->allTranslations as $translation)
{
$sitemap[] = [
'loc' => $translation->route->route,
'translation' => $allTranslation
];
}
}
}
return $sitemap;
}
}