File: D:/HostingSpaces/SBogers10/deensekroon.komma-mediadesign.nl/wwwroot/App/Routes/RouteService.php
<?php
namespace App\Routes;
include_once($_SERVER['DOCUMENT_ROOT'] . '/App/Categories/CategoryRepository.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/App/Categories/CategoryTree.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/App/Routes/RouteRepository.php');
use App\Categories\CategoryRepository;
use App\Categories\CategoryTree;
class RouteService
{
private $categoryTree;
private $categoryRepo;
private $routeRepo;
public function __construct()
{
$this->categoryTree = new CategoryTree();
$this->categoryRepo = new CategoryRepository();
$this->routeRepo = new RoutesRepository();
}
/**
* @return RoutesRepository
*/
public function repo()
{
return $this->routeRepo;
}
/**
* @param $category
* @return \StdClass
*/
public function createCategoryRoute($treeNode,$category)
{
// Create route
$route = new \StdClass();
$route->route_nl = $this->createCategoryRouteForLanguage($treeNode, $category, 'nl');
$route->route_en = $this->createCategoryRouteForLanguage($treeNode, $category, 'en');
return $route;
}
/**
* @param $treeNode
* @param $category
* @param $lang
* @return string
*/
private function createCategoryRouteForLanguage($treeNode, $category, $lang)
{
$route = '';
// If grandparent, start with grandparent
if($treeNode->{'grandparent_title_' . $lang} != null)
$route .= '/' . linkname($treeNode->{'grandparent_title_' . $lang});
// If parent, add with parent
if($treeNode->{'parent_title_' . $lang } != null)
$route .= '/' . linkname($treeNode->{'parent_title_' . $lang });
// Add title
if($category->{'title_' . $lang } != null)
$route .= '/' . linkname($category->{'title_' . $lang });
// Strip the first slash
return substr($route,1);
}
/**
* Create route for products
*
* @param $titleNL
* @param $titleEN
* @return \StdClass
*/
public function createProductRoute($productId, $titleNl, $titleEn, $categoryId)
{
// Get route prefix
$routePrefixNl = $routePrefixEn = '/';
if($categoryRoute = $this->routeRepo->findByRouteable($categoryId,'category'))
{
$routePrefixNl = $categoryRoute->route_nl . '/';
$routePrefixEn = $categoryRoute->route_en . '/';
}
// Create route
$route = new \StdClass();
$route->route_nl = $routePrefixNl . linkname($titleNl) . '-' . $productId;
$route->route_en = $routePrefixEn . linkname($titleEn) . '-' . $productId;
return $route;
}
/**
* Create route for brands
*
* @param $brand
* @return string
*/
public function createBrandRoute($brand)
{
return linkname($brand);
}
/**
* Validate route
*
* @param $route
* @param $routeable_id
* @param $routeable_type
* @return bool
*/
public function isValid($route, $routeable_id, $routeable_type)
{
if( ! $this->routeRepo->unique($route,$routeable_id,$routeable_type)) return false;
return true;
}
/**
* @param $route
* @param $routeable_id
* @param $routeable_type
* @return bool
*/
public function update($route, $routeable_id, $routeable_type)
{
// Get the old route
$oldRoute = $this->routeRepo->findByRouteable($routeable_id,$routeable_type);
if( ! $oldRoute)
{
// Store new if no old route is found
if( ! $this->routeRepo->store($route,$routeable_id,$routeable_type)) return false;
}
else
{
// Update routable route
if( ! $this->routeRepo->update($route,$routeable_id,$routeable_type)) return false;
}
return true;
}
/**
* @param $oldRoute
* @param $newRoute
*/
public function updateChildren($oldRoute, $newRoute)
{
// Get route children
$children = $this->routeRepo->childrenByRoute($oldRoute);
// Loop through each child
foreach($children as $child)
{
// Replace old route with new route in child route
$child->route_nl = str_replace($oldRoute->route_nl . '/',$newRoute->route_nl . '/',$child->route_nl);
$child->route_en = str_replace($oldRoute->route_en . '/',$newRoute->route_en . '/',$child->route_en);
// Update child route
$this->routeRepo->update($child,
$child->routeable_id,
$child->routeable_type);
}
}
}