File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Sitemap/SitemapService.php
<?php
namespace App\Komma\Sitemap;
use App\Komma\Base\Service;
use App\Komma\Locations\Models\Location;
use App\Komma\Pages\Models\Page;
use App\Komma\Posts\Models\Post;
use App\Komma\ProductCategories\Models\ProductCategory;
use App\Komma\Vacancies\Models\Vacancy;
use Carbon\Carbon;
use Laravelium\Sitemap\Sitemap;
final class SitemapService extends Service
{
public function makeSitemap(): Sitemap
{
$sitemap = app(Sitemap::class);
$this->addPagesToSitemap($sitemap);
$this->addWildcardModelToSitemap($sitemap, 'products', ProductCategory::class);
$this->addWildcardModelToSitemap($sitemap, 'posts', Post::class);
$this->addWildcardModelToSitemap($sitemap, 'vacancies', Vacancy::class);
$this->addWildcardModelToSitemap($sitemap, 'locations', Location::class);
$this->addAvailabilityToSitemap($sitemap, 'products');
// $this->addWildcardModelToSitemap($sitemap, 'updates', Update::class);
return $sitemap;
}
/**
* 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('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);
}
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')
->where('active', 1)
->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')
->where('active', 1)
->get();
// Prepare xml array for appending to the sitemap
$xmlModels = [];
foreach ($models as $model) {
if (is_a($model, Location::class)) {
$overviewPage = Page::where('id', $model->province_page_id)
->with('translations')
->with('translations.route')
->where('active', 1)
->first();
}
// 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) {
$overviewPageTranslation = $overviewPage->translations->where('language_id', '=', $translation->language_id)->first();
if (! isset($translation->slug) || ! isset($overviewPageTranslation) || $translation->slug === '') {
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);
}
private function addAvailabilityToSitemap(Sitemap &$sitemap, string $code_name)
{
// Get the product route
$overviewPage = Page::where('code_name', $code_name)
->with('translations')
->with('translations.route')
->where('active', 1)
->first();
// If the post index isn't found or inactive skip the posts from adding to the sitemap
if (! isset($overviewPage)) {
return null;
}
$locations = Location::where('active', 1)
->with('boundProducts')
->with('boundProducts.translation')
->with('translation')
->has('boundProducts')
->has('translation')
->get();
// Prepare xml array for appending to the sitemap
$xmlModels = [];
foreach ($locations as $location) {
foreach ($location->boundProducts as $product) {
if ($product->hide_on_site === 1) continue;
$xmlModels[] = (object) [
'loc' => \URL::to($overviewPage->translations->first()->route->alias.'/'.$product->translation->slug),
];
}
}
$this->addXmlArrayToSitemap($xmlModels, $sitemap);
}
}