File: D:/HostingSpaces/SBogers10/anvil.komma.pro/app/KommaApp/Sitemap/SitemapService.php
<?php
namespace App\KommaApp\Sitemap;
use App\KommaApp\Pages\Models\Page;
use App\KommaApp\Posts\Models\Post;
use Carbon\Carbon;
use Roumen\Sitemap\Sitemap;
class SitemapService
{
/**
* Make the sitemap
* @return mixed
*/
public function makeSitemap()
{
$sitemap = \App::make('sitemap');
// $sitemap = new Sitemap(\Config::get('sitemap'));
// dd($sitemap);
$this->addPagesToSitemap($sitemap);
// $this->addPostsToSitemap($sitemap);
$this->addSpecialismsToSitemap($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');
}
}
}
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') && 1 == 2){
$pageTranslations = [];
foreach ($page->translations as $translation) {
if(!isset($translation->route->alias)) continue;
//TODO build this ;)
$alias = $translation->route->alias;
if($alias === '/') $alias = '';
$pageTranslations[] = (object)[
'url' => \URL::to($alias),
'language' => $translation->getLanguageIso()
];
}
foreach ($page->translations as $translation) {
if(!isset($translation->route->alias)) continue;
$alias = $translation->route->alias;
if($alias === '/') $alias = '';
$xmlPages[] = (object)[
'loc' => \URL::to($alias),
'translation' => $pageTranslations
];
}
}
// Generate for single language
else{
$alias = $page->translations->first()->route->alias;
if($alias === '/') $alias = '';
$xmlPages[] = (object)[
'loc' => \URL::to($alias),
];
}
}
// dd($xmlPages);
$this->addXmlArrayToSitemap($xmlPages, $sitemap);
}
private function addPostsToSitemap(Sitemap &$sitemap)
{
// Get the post index
$postOverviewPage = Page::where('code_name', 'blog')
->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 $sitemap;
// 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')){
// TODO build this :p
}
// 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);
}
private function addSpecialismsToSitemap(Sitemap &$sitemap)
{
// Get the post index
$specialismOverviewPage = \App::getSite()
->pages()
->where('code_name', 'specialisms')
->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($specialismOverviewPage) || ! $specialismOverviewPage->active) return $sitemap;
// Load the posts
$specialisms = \App::getSite()
->specialisms()
->where('active', 1)
->where('lft', '!=', 1)
->with('translations')
->get();
// Prepare xml array for appending to the sitemap
$xmlSpecialisms = [];
foreach ($specialisms as $specialism) {
// Generate for multiple languages
if(\Config::get('app.multipleLanguages') && 1 == 2){
// TODO build this :p
}
// Generate for single language
else{
$alias = $specialismOverviewPage->translations->first()->route->alias.'/'.$specialism->translations->first()->slug;
if($alias === '/') $alias = '';
$xmlSpecialisms[] = (object)[
'loc' => \URL::to($alias),
];
}
}
$this->addXmlArrayToSitemap($xmlSpecialisms, $sitemap);
}
}