216 lines
6.6 KiB
Plaintext
216 lines
6.6 KiB
Plaintext
<?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;
|
|
|
|
class UrlExtension extends \Twig_Extension
|
|
{
|
|
protected $environment;
|
|
|
|
protected $resources;
|
|
|
|
protected $router;
|
|
|
|
protected $pageSelector;
|
|
|
|
protected $url_resolver;
|
|
|
|
public function __construct(NodeUrlResolver $url_resolver, PageManagerSelectorInterface $pageSelector, RouterInterface $router)
|
|
{
|
|
$this->url_resolver = $url_resolver;
|
|
$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'),
|
|
);
|
|
}
|
|
|
|
public function isCurrentNode(Node $node)
|
|
{
|
|
return $this->pageSelector->retrieve()->getCurrentNode() === $node;
|
|
//getDescendants
|
|
}
|
|
|
|
public function hasCurrentNode(Node $node, $only_child = false)
|
|
{
|
|
if (!$only_child && $this->isCurrentNode($node)) {
|
|
return true;
|
|
}
|
|
|
|
foreach ($node->getDescendants() as $child) {
|
|
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->url_resolver->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->url_resolver->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->url_resolver->resolve(
|
|
$node,
|
|
$parameters,
|
|
$model,
|
|
$absolute
|
|
);
|
|
} catch (MissingMandatoryParametersException $e) {
|
|
$node = NodeQuery::create()->findRoot($node->getScopeValue());
|
|
$url = $this->url_resolver->resolve($node, $parameters, $model, $absolute);
|
|
|
|
} catch (RouteNotFoundException $e) {
|
|
$node = NodeQuery::create()->findRoot($node->getScopeValue());
|
|
$url = $this->url_resolver->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->url_resolver->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 {
|
|
return $this->router->generate($route, $params, $referenceType);
|
|
} catch (RouteNotFoundException $e) {
|
|
return '#404(route:'.$route.')';
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|