deblan.tv/vendor/trinity/src/Trinity/Bundle/ContentManagerBundle/Twig/Extension/UrlExtension.php
2015-03-02 21:57:49 +01:00

275 lines
8.6 KiB
PHP

<?php
namespace Trinity\Bundle\ContentManagerBundle\Twig\Extension;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Trinity\Bundle\ContentManagerBundle\Model\Node;
use Trinity\Bundle\ContentManagerBundle\Model\Page;
use \Trinity\Bundle\ContentManagerBundle\Page\PageManagerSelectorInterface;
use \Trinity\Bundle\ContentManagerBundle\Model\NodeQuery;
use \Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use \Symfony\Component\Routing\Exception\RouteNotFoundException;
use Trinity\Bundle\ContentManagerBundle\Routing\NodeUrlResolver;
use Symfony\Component\Routing\RouterInterface;
use Trinity\Bundle\ContentManagerBundle\Model\Nav;
class UrlExtension extends \Twig_Extension
{
protected $environment;
protected $resources;
protected $router;
protected $pageSelector;
protected $urlResolver;
public function __construct(NodeUrlResolver $urlResolver, PageManagerSelectorInterface $pageSelector, RouterInterface $router)
{
$this->urlResolver = $urlResolver;
$this->pageSelector = $pageSelector;
$this->router = $router;
}
public function initRuntime(\Twig_Environment $environment)
{
$this->environment = $environment;
}
public function getName()
{
return 'url_extension';
}
public function getFunctions()
{
return array(
'page_url' => new \Twig_Function_Method($this, 'pageUrl', array('is_safe' => array('html'))),
'page_admin_url' => new \Twig_Function_Method($this, 'pageAdminUrl', array('is_safe' => array('html'))),
'text_url' => new \Twig_Function_Method($this, 'textUrl', array('is_safe' => array('html'))),
'page_link' => new \Twig_Function_Method($this, 'pageLink', array('is_safe' => array('html'))),
'is_current_node' => new \Twig_Function_Method($this, 'isCurrentNode'),
'has_current_node' => new \Twig_Function_Method($this, 'hasCurrentNode'),
'cms_path' => new \Twig_Function_Method($this, 'cmsPath'),
'get_node' => new \Twig_Function_Method($this, 'getNode'),
);
}
public function getNode($routeName, Nav $nav = null)
{
$query = NodeQuery::create()->filterByRouteName($routeName);
if ($nav !== null) {
$query->filterByNav($nav);
}
return $query->findOne();
}
public function isCurrentNode(Node $node)
{
$alias = $node->getNodeRelatedByNodeAlias();
if ($alias) {
if ($this->pageSelector->retrieve()->getCurrentNode() === $alias) {
return true;
}
}
return $this->pageSelector->retrieve()->getCurrentNode() === $node;
}
public function hasCurrentNode(Node $node, $only_child = false)
{
if (!$only_child && $this->isCurrentNode($node)) {
return true;
}
foreach ($node->getDescendants() as $child) {
if ($child !== null) {
if ($this->isCurrentNode($child)) {
return true;
}
}
}
return false;
}
public function pageUrl($node = null, $parameters = array(), $model = null, $absolute = false)
{
if (!$node) {
return '';
}
$url = ($node->hasExternalUrl()) ? $node->getUrl() : $this->urlResolver->resolve(
$node,
$parameters,
$model,
$absolute
);
return $url;
}
public function pageLink($text, Node $node = null, $parameters = array(), $model = null, $attributes = array(), $absolute = false)
{
$cms = $this->pageSelector->retrieve();
if (!$cms) {
throw new \Twig_Error('No active page manager.');
}
$current_node = $cms->getCurrentNode();
$url = ($node->hasExternalUrl()) ? $node->getUrl() : $this->urlResolver->resolve(
$node,
$parameters,
$model,
$absolute
);
$attributes = array_merge(
$attributes,
array(
'href' => $node->isAccessible() ? $url : '#',
)
);
if ($this->isCurrentNode($node) || ($current_node !== null && $node == $current_node->getParent())) {
if (!isset($attributes['class'])) {
$attributes['class'] = array();
} else {
if (!is_array($attributes['class'])) {
$attributes['class'] = explode(' ', $attributes['class']);
}
}
$attributes['class'][] = 'active';
$attributes['class'] = implode(' ', $attributes['class']);
}
foreach ($attributes as $k => $attribute) {
$html_attrs[] = sprintf('%s="%s"', $k, htmlspecialchars($attribute));
}
$pattern = '<a %s>%s</a>';
return sprintf($pattern, implode(' ', $html_attrs), $text);
}
public function pageAdminUrl($node = null, $parameters = array(), $model = null, $absolute = false)
{
if (!$node || (!$node->isHybrid() && !$node->getPage())) {
return '';
}
try {
$url = ($node->hasExternalUrl()) ? $node->getUrl() : $this->urlResolver->resolve(
$node,
$parameters,
$model,
$absolute
);
} catch (MissingMandatoryParametersException $e) {
$node = NodeQuery::create()->findRoot($node->getScopeValue());
$url = $this->urlResolver->resolve($node, $parameters, $model, $absolute);
} catch (RouteNotFoundException $e) {
$node = NodeQuery::create()->findRoot($node->getScopeValue());
$url = $this->urlResolver->resolve($node, $parameters, $model, $absolute);
}
return $url;
}
public function textUrl($node = null, $parameters = array(), $model = null)
{
if (!$node || (!$node->isHybrid() && !$node->getPage())) {
return '';
}
if ($node->getNodeAlias()) {
$node = $node->getNodeRelatedByNodeAlias();
}
$url = $node->getUrl();
try {
$this->urlResolver->resolve($node, $parameters, $model);
} catch (MissingMandatoryParametersException $e) {
$message = 'Vous devez configurer le modèle de donnée pour cette page.';
$url = sprintf(
'<div data-placement="bottom" data-toggle="tooltip" data-original-title="%s">%s</div>',
$message,
$url
);
}
return $url;
}
public function cmsPath($route, $params = array(), $referenceType = UrlGenerator::ABSOLUTE_PATH)
{
try {
try {
return $this->router->generate($route, $params, $referenceType);
} catch (MissingMandatoryParametersException $e) {
return $this->getMultiDomainCmsPath($e, $route, $params, $referenceType);
} catch (RouteNotFoundException $e) {
return $this->getCmsPathWhenException($e, $route, $params, $referenceType);
}
} catch (RouteNotFoundException $e) {
return '#404(route:' . $route . ')';
}
}
protected function getCmsPathWhenException(\Exception $e, $route, $params = array(), $referenceType = UrlGenerator::ABSOLUTE_PATH)
{
if ($pm = $this->pageSelector->retrieve()) {
if ($pm->getCurrentNode()) {
return $this->router->generate(
$pm->getCurrentNode()->getNavId() . '_' . $route,
$params,
$referenceType
);
}
}
throw $e;
}
protected function getMultiDomainCmsPath(\Exception $e, $route, $params = array(), $referenceType = UrlGenerator::ABSOLUTE_PATH)
{
if (!$pm = $this->pageSelector->retrieve()) {
throw $e;
}
if (!$pm->getCurrentNode()) {
throw $e;
}
if (!$nav = $pm->getCurrentNode()->getNav()) {
throw $e;
}
if (!$nav->isRegexDomain()) {
throw $e;
}
$params = array_merge($params, $nav->transformDomainRegex()->transformHost($_SERVER['SERVER_NAME']));
return $this->getCmsPathWhenException($e, $route, $params, $referenceType);
}
public function render($template, array $parameters = array())
{
if (!isset($this->resources[$template])) {
$this->resources[$template] = $this->environment->loadTemplate($template);
}
return $this->resources[$template]->render($parameters);
}
}