153 lines
4.5 KiB
PHP
153 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace Trinity\Bundle\ContentManagerBundle\Twig\Extension;
|
|
|
|
use Trinity\Bundle\ContentManagerBundle\Model\Node;
|
|
use \Trinity\Bundle\ContentManagerBundle\Block\BlockManager;
|
|
use \Trinity\Bundle\ContentManagerBundle\Page\PageManagerSelectorInterface;
|
|
use \Trinity\Bundle\ContentManagerBundle\Model\NodeQuery;
|
|
use \Trinity\Bundle\ContentManagerBundle\Model\NodePeer;
|
|
use Trinity\Bundle\ContentManagerBundle\Routing\NodeUrlResolver;
|
|
|
|
class PageExtension extends \Twig_Extension
|
|
{
|
|
private $environment;
|
|
private $resources;
|
|
private $blockManager;
|
|
|
|
protected $pageSelector;
|
|
protected $url_resolver;
|
|
|
|
public function __construct(NodeUrlResolver $url_resolver, PageManagerSelectorInterface $pageSelector, BlockManager $blockManager)
|
|
{
|
|
$this->url_resolver = $url_resolver;
|
|
$this->pageSelector = $pageSelector;
|
|
$this->blockManager = $blockManager;
|
|
}
|
|
|
|
public function initRuntime(\Twig_Environment $environment)
|
|
{
|
|
$this->environment = $environment;
|
|
}
|
|
|
|
public function getName()
|
|
{
|
|
return 'page_extension';
|
|
}
|
|
|
|
public function getGlobals()
|
|
{
|
|
return array(
|
|
'manager' => $this->pageSelector->retrieve(),
|
|
'currentNode' => $this->pageSelector->retrieve()->getCurrentNode(),
|
|
);
|
|
}
|
|
|
|
public function getFunctions()
|
|
{
|
|
return array(
|
|
'render_breadcrumb' => new \Twig_Function_Method($this, 'renderBreadcrumb', array('is_safe' => array('html'))),
|
|
'render_dictionary' => new \Twig_Function_Method($this, 'renderDictionary', array('is_safe' => array('html'))),
|
|
);
|
|
}
|
|
|
|
public function getFilters()
|
|
{
|
|
return array(
|
|
'transform_url' => new \Twig_Filter_Method($this, 'transformUrl', array('is_safe' => array('html'))),
|
|
'transform_link' => new \Twig_Filter_Method($this, 'transformLink', array('is_safe' => array('html')))
|
|
);
|
|
}
|
|
|
|
public function transformUrl($value)
|
|
{
|
|
return preg_replace_callback(
|
|
'`href=["\']{1}/\{.*\}`',
|
|
array(
|
|
$this->url_resolver,
|
|
'translate'
|
|
),
|
|
$value
|
|
);
|
|
}
|
|
|
|
public function transformLink($value)
|
|
{
|
|
return $this->url_resolver->translate($value, true);
|
|
}
|
|
|
|
public function renderBreadcrumb(array $addNodes = null, array $params = array(), $template = 'TrinityContentManagerBundle:Utils:breadcrumb.html.twig')
|
|
{
|
|
$cms = $this->pageSelector->retrieve();
|
|
|
|
if (!$cms) {
|
|
return null;
|
|
}
|
|
|
|
$currentNode = $cms->getCurrentNode();
|
|
|
|
$nodes = NodeQuery::create()
|
|
->ancestorsOf($currentNode)
|
|
->filterByStatus(NodePeer::IS_VISIBLE)
|
|
->_or()
|
|
->filterByStatus(NodePeer::IS_VISIBLE + NodePeer::IS_ACCESSIBLE)
|
|
->orderByLevel()
|
|
->find();
|
|
|
|
if ($currentNode->isVisible() && !$currentNode->layOnDataModels()) {
|
|
$nodes->append($currentNode);
|
|
}
|
|
|
|
if ($addNodes) {
|
|
foreach ($addNodes as $add) {
|
|
if (!array_key_exists('node', $add)) {
|
|
continue;
|
|
}
|
|
// use a copy of the node avoid conflict when node is re-used
|
|
$node = $add['node']->copy();
|
|
|
|
if (array_key_exists('params', $add)) {
|
|
$node->addDefaultParams($add['params']);
|
|
}
|
|
|
|
if (array_key_exists('title', $add)) {
|
|
$node->setTitle($add['title']);
|
|
}
|
|
|
|
if ($nodes->contains($node)) {
|
|
$aim = $nodes->search($node);
|
|
|
|
if ($aim) {
|
|
$nodes[$aim] = $node;
|
|
}
|
|
} else {
|
|
$nodes->append($node);
|
|
}
|
|
|
|
unset($node);
|
|
}
|
|
}
|
|
|
|
return $this->render($template, array('nodes' => $nodes, 'params' => $params));
|
|
}
|
|
|
|
public function renderDictionary($page)
|
|
{
|
|
return $this->render(
|
|
'TrinityContentManagerBundle:Utils:dictionary.html.twig',
|
|
array(
|
|
'dictionary' => $page->getMetaWords(),
|
|
)
|
|
);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|