File: D:/HostingSpaces/SBogers10/netwerkbrabant.komma.pro/app/KommaApp/Sitemap/SitemapService.php
<?php
namespace App\KommaApp\Sitemap;
use App\KommaApp\Base\Service;
use App\KommaApp\Pages\Models\Page;
use App\KommaApp\Posts\Models\Post;
use Carbon\Carbon;
use Roumen\Sitemap\Sitemap;
class SitemapService extends Service
{
/**
* Generate a sitemap xml
*
* @return Sitemap
*/
public function makeSitemap()
{
$sitemap = \App::make('sitemap');
$this->addPagesToSitemap($sitemap);
$this->addPostsToSitemap($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 = $this->site
->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 posts to the sitemap
*
* @param Sitemap $sitemap
* @return null
*/
private function addPostsToSitemap(Sitemap &$sitemap)
{
// Get the post index
$postOverviewPage = Page::where('code_name', 'posts')
->with('translations')
->with('translations.route')
->first();
// If the post index isn't found or inactive skip the posts from adding to the sitemap
if(!isset($postOverviewPage) || ! $postOverviewPage->active) return null;
// Load the posts
$posts = Post::where('active', 1)
->with('translations')
->get();
// Prepare xml array for appending to the sitemap
$xmlPosts = [];
foreach ($posts as $post) {
// Generate for multiple languages
if(\Config::get('app.multipleLanguages')){
$postTranslations = [];
// First we need to create an array of the available translations of this post
foreach ($post->translations as $translation) {
$postOverviewPageTranslation = $postOverviewPage->translations->where('language_id', '=', $translation->language_id)->first();
if(!isset($translation->slug) || !isset($postOverviewPageTranslation) || $translation->slug === '') continue;
$alias = $postOverviewPageTranslation->route->alias . '/' . $translation->slug;
$postTranslations[] = [
'url' => \URL::to($alias),
'language' => $translation->getLanguageIso()
];
}
// Then append for each individual post the translation alias with all available translations of this page to the xml array
foreach ($postTranslations as $translation) {
$xmlPosts[] = (object)[
'loc' => \URL::to($translation['url']),
'translation' => $postTranslations
];
}
}
// Generate for single language
else{
$alias = $postOverviewPage->translations->first()->route->alias.'/'.$post->translations->first()->slug;
if($alias === '/') $alias = '';
$xmlPosts[] = (object)[
'loc' => \URL::to($alias),
];
}
}
$this->addXmlArrayToSitemap($xmlPosts, $sitemap);
}
}