113 lines
3.8 KiB
Plaintext
113 lines
3.8 KiB
Plaintext
<?php
|
|
|
|
namespace Trinity\Bundle\ContentManagerBundle\Routing;
|
|
|
|
use Symfony\Component\Config\Loader\Loader;
|
|
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
|
use Symfony\Component\Routing\RouteCollection;
|
|
use Symfony\Component\Routing\Route;
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
|
|
use Trinity\Bundle\ContentManagerBundle\Model\NavQuery;
|
|
use Trinity\Bundle\ContentManagerBundle\Model\PageableInterface;
|
|
|
|
class NodeLoader extends Loader
|
|
{
|
|
/**
|
|
* Loads a resource.
|
|
*
|
|
* @param mixed $resource The resource
|
|
* @param string $type The resource type
|
|
* Deprecated method
|
|
*/
|
|
public function load($resource, $type = null)
|
|
{
|
|
try {
|
|
$routes = new RouteCollection();
|
|
|
|
$nodes = \PropelQuery::from( $resource )
|
|
->orderByTreeLeft()
|
|
->find();
|
|
|
|
$navCount = NavQuery::create()->count();
|
|
|
|
if ($navCount !== 1) {
|
|
$locales = NavQuery::create()->select( 'culture' )->find();
|
|
$locales = implode( '|', $locales->toArray() );
|
|
}
|
|
|
|
foreach ($nodes as $node) {
|
|
$defaults = $node->getCleanedDefaultParams();
|
|
$defaults[ '_controller' ] = $node->getController() ? $node->getController() : 'TrinityContentManagerBundle:Page:catchAll';
|
|
$defaults[ '_node' ] = $node->getId();
|
|
$requirements = $node->getCleanedRequirements();
|
|
|
|
$id = $node->getId();
|
|
$routeName = $node->getRouteName() ? $node->getRouteName() : sprintf('cms_node_%d', $id);
|
|
|
|
if ($node->getNodeAlias()) {
|
|
$node = $node->getNodeRelatedByNodeAlias();
|
|
}
|
|
|
|
$url = $node->getUrl();
|
|
$page = $node->getPage();
|
|
$controller = $node->getController() ? $node->getController() : 'TrinityContentManagerBundle:Page:catchAll';
|
|
|
|
if ($node->getNodeAlias() && !$node->getNodeAlias()->getPage()) {
|
|
continue;
|
|
}
|
|
|
|
if ($page && !$node->getController()) {
|
|
$configuration = $page->getConfiguration();
|
|
$controller = $configuration->getDefaultController() ? $configuration->getDefaultController() : $controller;
|
|
}
|
|
|
|
$route = new NodeRoute($url, $defaults, $requirements);
|
|
$route->addNode($id);
|
|
$route->setController($controller);
|
|
|
|
if (!empty($page) && $page instanceof PageableInterface) {
|
|
$route->addPage();
|
|
}
|
|
|
|
if ($nav = $node->getNav()) {
|
|
$route->addDefaults( array( '_locale' => $nav->getCulture() ) );
|
|
|
|
if ($navCount === 1) {
|
|
$route->removeLocale();
|
|
} else {
|
|
$route->addLocale($locales);
|
|
}
|
|
}
|
|
|
|
// $node->setUrl( $route->getPath() )->save();
|
|
|
|
if ($exist = $routes->get($routeName)) {
|
|
if ($exist instanceof NodeRoute) {
|
|
$exist->addNode($id);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
$routes->add( $routeName, $route );
|
|
}
|
|
} catch (\PropelException $e) {
|
|
throw new ResourceNotFoundException('Unable to find or load nodes. Please check your database access.');
|
|
}
|
|
|
|
return $routes;
|
|
}
|
|
|
|
/**
|
|
* Returns true if this class supports the given resource.
|
|
*
|
|
* @param mixed $resource A resource
|
|
* @param string $type The resource type
|
|
*
|
|
* @return Boolean true if this class supports the given resource, false otherwise
|
|
*/
|
|
public function supports($resource, $type = null)
|
|
{
|
|
return 'cms_node' === $type;
|
|
}
|
|
}
|