<?php
namespace App\Manager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use App\Entity\Category;
use App\Entity\Product;
use App\Entity\Redirection;
class RedirectionManager {
private $em;
private $router;
public function __construct(EntityManagerInterface $em, \Symfony\Component\Routing\RouterInterface $router)
{
$this->em = $em;
$this->router = $router;
}
public function getRedirectUrl(Redirection $redirect) {
$toUrl = $redirect->getToUrl();
if(!empty($toUrl)){
return $this->translateUrl($toUrl);
}
$category = $redirect->getToCategory();
if($category){
$route = 'universe';
if($category->getParent()){
$route = 'category';
}
return $this->router->generate($route, ['id'=>$category->getId(), 'url'=>$category->getUrl()]);
}
$product = $redirect->getToProduct();
if($product){
return $this->router->generate('product', ['id'=>$product->getId(), 'url'=>$product->getUrl()]);
}
$targetPath = $redirect->getTargetPath();
if($targetPath){
$tmp = explode('_',$targetPath);
$category = $this->em->getRepository('App:Category')->find($tmp[count($tmp)-1]);
if($category){
$route = 'universe';
if($category->getParent()){
$route = 'category';
}
return $this->router->generate($route, ['id'=>$category->getId(), 'url'=>$category->getUrl()]);
}
}
return $this->router->generate('index');
}
public function redirectCategoryToCategory(Category $categoryFrom, Category $categoryTo) {
$this->removeByCategory($categoryFrom);
$redirect = new Redirection();
$redirect->setFromCategory($categoryFrom);
$redirect->setToCategory($categoryTo);
$this->em->persist($redirect);
$this->em->flush();
}
public function delete(Redirection $redirect) {
$this->em->remove($redirect);
$this->em->flush();
}
public function removeByCategory(Category $category) {
$categories = $this->em->getRepository('App:Redirection')->findByFromCategory($category);
foreach($categories as $cat){
$this->em->remove($cat);
}
$this->em->flush();
}
public function translateUrl($url) {
if(strpos($url, 'product_info.php?products_id=')!==false){
$urlInfos = parse_url($url);
$params = [];
parse_str($urlInfos['query'], $params);
$product = $this->em->getRepository('App:Product')->find($params['products_id']);
if($product){
$description = $product->getProductDescription($params['language']);
return $this->router->generate('product', [
'_locale' => $params['language'],
'id' => $product->getId(),
'url' => $description->getUrl()
]);
}
}
if(strpos($url, 'index.php?cPath=')!==false){
$urlInfos = parse_url($url);
$params = [];
parse_str($urlInfos['query'], $params);
$tmp = explode('_',$params['cPath']);
$category = $this->em->getRepository('App:Category')->find(array_pop($tmp));
if($category){
$description = $category->getCategoryDescription($params['language']);
return $this->router->generate('category', [
'_locale' => $params['language'],
'id' => $category->getId(),
'url' => $description->getUrl()
]);
}
}
if(strpos($url, '/professionnel/index.php')!==false)
return $this->router->generate('index');
return $url;
}
}