File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Sitemap/SitemapService.php
<?php
namespace App\Komma\Sitemap;
use App\Komma\Base\Service;
use App\Komma\CustomerStories\Models\CustomerStory;
use App\Komma\Jobs\Models\Job;
use App\Komma\Languages\Models\Language;
use App\Komma\Pages\Models\Page;
use App\Komma\Posts\Models\Post;
use App\Komma\Products\Models\Product;
use App\Komma\Solutions\Models\Solution;
use App\Komma\Trainings\Models\Training;
use App\Komma\Updates\Models\Update;
use Carbon\Carbon;
use Laravelium\Sitemap\Sitemap;
class SitemapService extends Service
{
private $availableLanguages;
private $availableLanguagesIds;
/**
* Generate a sitemap xml
*
* @return Sitemap
*/
public function makeSitemap()
{
$this->availableLanguages = Language::whereIn('iso_2', config('app.available_languages'))->get();
$this->availableLanguagesIds = $this->availableLanguages->pluck('id')->toArray();
$sitemap = \App::make('sitemap');
$this->addPagesToSitemap($sitemap);
$this->addWildcardModelToSitemap($sitemap, 'posts', Post::class);
$this->addWildcardModelToSitemap($sitemap, 'trainings', Training::class);
// $this->addWildcardModelToSitemap($sitemap, 'jobs', Job::class);
$this->addWildcardModelToSitemap($sitemap, 'products', Product::class);
$this->addWildcardModelToSitemap($sitemap, 'solutions', Solution::class);
$this->addWildcardModelToSitemap($sitemap, 'updates', Update::class);
$this->addCustomerStoriesToSitemap($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)
->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 (! in_array($translation->language_id, $this->availableLanguagesIds)) {
continue;
}
if (! isset($translation->route->alias) || ! $translation->active) {
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 customer stories to the sitemap
*
* @param Sitemap $sitemap
* @return null
*/
private function addCustomerStoriesToSitemap(Sitemap &$sitemap)
{
// Get the post index
$customerStoryOverviewPage = Page::where('code_name', 'customerStories')
->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($customerStoryOverviewPage)) {
return null;
}
// Load the posts
$customerStories = CustomerStory::with('translations')
->has('translations')
->get();
// Prepare xml array for appending to the sitemap
$xmlCustomerStories = [];
foreach ($customerStories as $customerStory) {
// Generate for multiple languages
if (\Config::get('app.multipleLanguages')) {
$customerStoryTranslations = [];
// First we need to create an array of the available translations of this post
foreach ($customerStory->translations as $translation) {
if (! in_array($translation->language_id, $this->availableLanguagesIds)) {
continue;
}
$customerStoryOverviewPageTranslation = $customerStoryOverviewPage->translations->where('language_id', '=', $translation->language_id)->first();
if (! isset($customerStory->slug) || ! isset($customerStoryOverviewPageTranslation) || $customerStory->slug === '' || ! $translation->active) {
continue;
}
$alias = $customerStoryOverviewPageTranslation->route->alias.'/'.$customerStory->slug;
$customerStoryTranslations[] = [
'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 ($customerStoryTranslations as $translation) {
$xmlCustomerStories[] = (object) [
'loc' => \URL::to($translation['url']),
'translation' => $customerStoryTranslations,
];
}
}
// Generate for single language
else {
$alias = $customerStoryOverviewPage->translations->first()->route->alias.'/'.$customerStory->slug;
if ($alias === '/') {
$alias = '';
}
$xmlCustomerStories[] = (object) [
'loc' => \URL::to($alias),
];
}
}
$this->addXmlArrayToSitemap($xmlCustomerStories, $sitemap);
}
private function addWildcardModelToSitemap(Sitemap &$sitemap, string $code_name, string $modelClass)
{
// Get the post index
$overviewPage = Page::where('code_name', $code_name)
->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($overviewPage)) {
return null;
}
// Load the posts
$models = $modelClass::with('translations')
->has('translations')
->get();
// Prepare xml array for appending to the sitemap
$xmlModels = [];
foreach ($models as $model) {
// Generate for multiple languages
if (\Config::get('app.multipleLanguages')) {
$modelTranslations = [];
// First we need to create an array of the available translations of this post
foreach ($model->translations as $translation) {
if (! in_array($translation->language_id, $this->availableLanguagesIds)) {
continue;
}
$overviewPageTranslation = $overviewPage->translations->where('language_id', '=', $translation->language_id)->where('active', 1)->where('name', '!=', '')->first();
if (! isset($translation->slug) || ! isset($overviewPageTranslation) || $translation->slug === '' || ! $translation->active) {
continue;
}
$alias = $overviewPageTranslation->route->alias.'/'.$translation->slug;
$modelTranslations[] = [
'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 ($modelTranslations as $translation) {
$xmlModels[] = (object) [
'loc' => \URL::to($translation['url']),
'translation' => $modelTranslations,
];
}
}
// Generate for single language
else {
$alias = $overviewPage->translations->first()->route->alias.'/'.$model->translations->first()->slug;
if ($alias === '/') {
$alias = '';
}
$xmlModels[] = (object) [
'loc' => \URL::to($alias),
];
}
}
$this->addXmlArrayToSitemap($xmlModels, $sitemap);
}
}