File: D:/HostingSpaces/SBogers10/immoginis.komma.pro/app/Komma/Seo/SitemapService.php
<?php
namespace Komma\Seo;
use Komma\Kms\Properties\Models\Property;
use Komma\Properties\PropertyService;
use Komma\Routes\RouteRepository;
use Roumen\Sitemap\Sitemap;
use Carbon\Carbon;
class SitemapService
{
/**
* @var RouteRepository
*/
private $routeRepository;
private $propertyService;
/**
* SitemapService constructor.
* @param RouteRepository $routeRepository
*/
public function __construct(RouteRepository $routeRepository, PropertyService $propertyService)
{
$this->routeRepository = $routeRepository;
$this->propertyService = $propertyService;
}
/***
* 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(
$groups = ['pages'],
$weight_from = 0.1)
{
//Create the sitemap class
$sitemap = \App::make('sitemap');
//Loop trough the routes based on the given parameters
foreach ($this->routes($groups, $weight_from) as $type => $route)
{
$sitemap->add(
\URL::to($route['route']),
Carbon::now(),
$route['weight'],
'monthly',
[],
$route['name']);
}
return $sitemap;
}
public function getCitiesSitemap(){
$cities = Property::select('city','goal')
->where('active', '=', 1)
->whereNotNull('goal')
->where('goal','!=','')
->orderBy('city')
->groupBy('city')
->groupBy('goal')
->get();
return $cities;
}
/**
* Return all (cleaned up) routes
*
* @param array $groups
* @return array
*/
public function routes($groups = ['pages'], $minWeight = 0.1)
{
// Get all routes from database
$routes = $this->routeRepository->routesForSitemap($minWeight);
// Clean up route structure
$cleanRoutes = [];
foreach ($routes as $route)
{
//dd($route);
// Get pages
if ($route->routable_type == 'Komma\Kms\Pages\Models\PageTranslation' && in_array('pages', $groups))
{
$cleanRoutes['page-' . $route->page_id] = $this->cleanRoute($route, 'page');
}
//get offer/city
// Get projects
if ($route->routable_type == 'Komma\Properties\Models\Property' && in_array('properties', $groups))
{
$property = $this->createPropertyName($this->cleanRoute($route, 'property'));
if(!$property) continue;
$cleanRoutes['aanbod-' . $route->id] = $property;
}
}
if(in_array('city', $groups)){
$cities = $this->getCitiesSitemap();
foreach ($cities as $city){
$cleanRoutes['aanbod-sub-'.strtolower($city->city)] = [
'route' => '/aanbod/'.strtolower($city->city),
'weight' => 0.5,
'name' => 'Aanbod: ' . $city->city
];
}
}
return $cleanRoutes;
}
private function createPropertyName($propertyArray){
if(!$property = $this->propertyService->decodeProperties($this->propertyService->getProperties($propertyArray['name']))->first()) return;
if(isset($property->decoded_info->Ident)){
$propertyArray['name'] = ucfirst(strtolower($property->decoded_info->Ident));
}
else{
$nameTemp = str_replace('"', "", $property->decoded_info->Name ) ;
$nameTemp = str_replace("'", "", $nameTemp);
$propertyArray['name'] = 'Project: '.ucfirst(strtolower($nameTemp)).' - '.ucfirst(strtolower($property->decoded_info->City));
}
return $propertyArray;
}
/**
* Return only necessary properties
*
* @param $route
* @param $prefix
* @return array
*/
private function cleanRoute($route, $prefix)
{
return [
'route' => $route->route,
'weight' => $route->sitemap_weight,
'name' => $route->{$prefix . '_name'}
];
}
}