deblan.tv/vendor/trinity/src/Trinity/Bundle/ContentManagerBundle/Model/Node.php
2016-06-20 11:37:14 +02:00

740 lines
17 KiB
PHP

<?php
namespace Trinity\Bundle\ContentManagerBundle\Model;
use PropelPDO;
use Trinity\Bundle\ContentManagerBundle\Model\om\BaseNode;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;
class Node extends BaseNode
{
protected $menu_id_changed = false;
protected $parser = null;
protected $externalUrl = null;
protected $oldUrl = null;
public function __construct()
{
$this->parser = new Parser();
parent::__construct();
}
public function __toString()
{
return (string)$this->getTitle();
}
public function getHttpMethod()
{
$data = parent::getHttpMethod();
if ($data !== null) {
return $data;
}
return $this->setHttpMethod('[GET, POST]')->getHttpMethod();
}
public function getFormat()
{
$data = parent::getFormat();
if ($data !== null) {
return $data;
}
return $this->setFormat('html')->getFormat();
}
public function getRealRouteName()
{
if ($navId = $this->getNavId()) {
return $this->getNavId() . '_' . $this->getRouteName();
}
return $this->getRouteName();
}
public function getLevelRender()
{
$prefix = '';
if ($this->getLevel() > 1) {
for ($i = 0; $i < $this->getLevel(); $i++) {
$prefix .= '&nbsp;&nbsp;&nbsp;';
}
$prefix .= '&raquo;';
}
return sprintf('%s %s', html_entity_decode($prefix), $this->getTitle());
}
public function insertAbove(Node $node)
{
$this->setMenuId($node->getMenuId());
return $this->insertAsLastChildOf($node);
}
public function insertAfter(Node $node)
{
$this->setMenuId($node->getMenuId());
return $this->insertAsNextSiblingOf($node);
}
public function insertBefore(Node $node)
{
$this->setMenuId($node->getMenuId());
return $this->insertAsPrevSiblingOf($node);
}
public function moveAbove(Node $node)
{
$this->setMenuId($node->getMenuId());
return $this->moveToLastChildOf($node);
}
public function moveAfter(Node $node)
{
$this->setMenuId($node->getMenuId());
return $this->moveToNextSiblingOf($node);
}
public function moveBefore(Node $node)
{
$this->setMenuId($node->getMenuId());
return $this->moveToPrevSiblingOf($node);
}
public function getPage()
{
if ($alias = $this->getNodeAlias()) {
return $alias->getPageRelatedByPageId();
}
return $this->getPageRelatedByPageId();
}
public function setVisible($visible)
{
if ($visible && !$this->getVisible()) {
$this->setStatus($this->getStatus() + NodePeer::IS_VISIBLE);
}
if (!$visible && $this->getVisible()) {
$this->setStatus($this->getStatus() - NodePeer::IS_VISIBLE);
}
return $this;
}
public function getVisible()
{
return in_array(
$this->getStatus(),
array(
NodePeer::IS_VISIBLE,
NodePeer::IS_VISIBLE + NodePeer::IS_ACCESSIBLE,
)
);
}
public function isVisible()
{
return $this->getVisible();
}
public function setAccessible($accessible)
{
if ($accessible && !$this->getAccessible()) {
$this->setStatus($this->getStatus() + NodePeer::IS_ACCESSIBLE);
}
if (!$accessible && $this->getAccessible()) {
$this->setStatus($this->getStatus() - NodePeer::IS_ACCESSIBLE);
}
return $this;
}
public function getAccessible()
{
return in_array(
$this->getStatus(),
array(
NodePeer::IS_ACCESSIBLE,
NodePeer::IS_ACCESSIBLE + NodePeer::IS_VISIBLE,
)
);
}
public function isAccessible()
{
return $this->getAccessible();
}
public function getPermissions()
{
$default = array();
try {
$value = $this->parser->parse(parent::getPermissions());
if (!$value || !is_array($value)) {
$value = $default;
} else {
}
} catch (ParseException $e) {
$value = $default;
}
return $value;
}
public function setPermissions($value)
{
if (is_array($value)) {
$value = Yaml::dump($value);
}
return parent::setPermissions($value);
}
public function setMenuId($id)
{
$this->menu_id_changed = $id !== $this->getMenuId();
return parent::setMenuId($id);
}
public function setPage(Page $page = null)
{
if ($page) {
$this->setPageId($page->getId());
}
}
public function setUrl($v)
{
if (null == $this->oldUrl) {
$this->oldUrl = $this->url;
}
return parent::setUrl($v);
}
public function save(\PropelPDO $con = null)
{
if ($this->menu_id_changed && !$this->isNew()) {
foreach ($this->getChildren() as $child) {
$child->setMenuId($this->getMenuId())->save();
}
}
// TODO : find better than this ugly hack
$this->setUrl($this->url !== '/' ? ltrim($this->url, '/') : $this->url);
if ($this->hasExternalUrl()) {
return parent::save($con);
}
$this->createUrl();
$this->loadUrlParameter();
if ($this->url) {
$this->setUrl(sprintf('/%s', ltrim($this->url, '/'))); // conserve le / devant toute les uri définies
}
if ($this->getPage() && $this->getPage()->getNodeId() != $this->getId()) {
//$this->getPage()->setNodeId($this->getId())->save();
}
return parent::save($con);
}
/**
* Create a default url based on parent URL and node SLUG
* @return bool
*/
public function createUrl()
{
if ($this->getUrl()) {
return false;
}
$node = $this->getParent();
if (!$node) {
return false;
}
$url = preg_replace('`\/{.*}`', '', $node->getUrl());
$this->setUrl(sprintf('%s/%s', $url, $this->createRawSlug()));
return true;
}
/**
* ajoute les paramètres du controleur si il ne sont ni présent dans les "DEFAULT_PARAMS" ni dans "URL"
* retire le param de l'url si il est présent dans les "DEFAULT_PARAMS"
* @return bool
*/
public function loadUrlParameter()
{
if (!$page = $this->getPage()) {
return false;
}
$configuration = $page->getConfiguration();
if (!$configuration || !$configuration->getDefaultControllerClass()) {
return false;
}
if (!$this->getController()) {
$this->setController($configuration->getDefaultControllerClass());
}
$injected_params = $configuration->getInjectParams();
$ignore_params = $configuration->getIgnoreParams();
$controller = explode('::', $configuration->getDefaultControllerClass());
try {
$reflection = new \ReflectionClass($controller[0]);
$method = $reflection->getMethod($controller[1]);
foreach ($method->getParameters() as $param) {
$defaults = $this->getCleanedDefaultParams();
if (in_array($param->getName(), $ignore_params)) {
continue;
}
if (in_array($param->getName(), $injected_params) && in_array(
$param->getName(),
array_keys($defaults)
)
) {
$this->setUrl(preg_replace('`\/{' . $param->getName() . '}`', '', $this->getUrl()));
} elseif (!preg_match('`{' . $param->getName() . '}`', $this->getUrl())) {
$this->setUrl(sprintf('%s/{%s}', $this->getUrl(), $param->getName()));
}
}
} catch (\ReflectionException $e) {
}
return true;
}
protected static function cleanupSlugPart($slug, $replacement = '-')
{
$slug = str_replace(
array(
'à',
'â',
'ä',
'á',
'ã',
'å',
'î',
'ï',
'ì',
'í',
'ô',
'ö',
'ò',
'ó',
'õ',
'ø',
'ù',
'û',
'ü',
'ú',
'é',
'è',
'ê',
'ë',
'ç',
'ÿ',
'ñ',
'À',
'Â',
'Ä',
'Á',
'Ã',
'Å',
'Î',
'Ï',
'Ì',
'Í',
'Ô',
'Ö',
'Ò',
'Ó',
'Õ',
'Ø',
'Ù',
'Û',
'Ü',
'Ú',
'É',
'È',
'Ê',
'Ë',
'Ç',
'Ÿ',
'Ñ',
'\'',
'"',
' ',
' ',
' ',
' ',
' '
),
array(
'a',
'a',
'a',
'a',
'a',
'a',
'i',
'i',
'i',
'i',
'o',
'o',
'o',
'o',
'o',
'o',
'u',
'u',
'u',
'u',
'e',
'e',
'e',
'e',
'c',
'y',
'n',
'A',
'A',
'A',
'A',
'A',
'A',
'I',
'I',
'I',
'I',
'O',
'O',
'O',
'O',
'O',
'O',
'U',
'U',
'U',
'U',
'E',
'E',
'E',
'E',
'C',
'Y',
'N',
' ',
' ',
' ',
' ',
' ',
' ',
' '
),
$slug
);
return parent::cleanupSlugPart($slug, $replacement);
}
public function setNodeAlias($node)
{
return is_object($node) && $node instanceof BaseNode ? parent::setNodeAlias(
$node->getId()
) : parent::setNodeAlias($node);
}
public function getNodeAlias()
{
if (parent::getNodeAlias()) {
return NodeQuery::create()->findPk(parent::getNodeAlias());
}
return null;
}
/**
* Hybrid nodes depends of symfony default router
* @return bool
*/
public function isHybrid()
{
if (0 === preg_match('`^cms_node.*`', $this->getRouteName()) || $this->getController(
) || $this->getDefaultParams() || $this->getRequirements()
) {
return true;
}
return false;
}
public function layOnDataModel()
{
if ($this->getPage() && $this->getPage()->getDataModel() && $this->getPage()->getDataModelId()) {
return true;
}
return false;
}
public function layOnDataModels()
{
if ($this->getPage() && $this->getPage()->getDataModel() && !$this->getPage()->getDataModelId()) {
return true;
}
return false;
}
private function getDataModelQuery()
{
return \PropelQuery::from($this->getPage()->getDataModel());
}
public function getDataModelsCollection($raw = false)
{
$method = method_exists(
$this->getDataModelQuery(),
'dataModelsCollectionFind'
) ? 'dataModelsCollectionFind' : 'find';
$method = ($raw) ? 'find' : $method;
return $this->layOnDataModels() ? call_user_func(array($this->getDataModelQuery(), $method)) : array();
}
public function getDataModelObject()
{
return $this->layOnDataModel() ? $this->getDataModelQuery()->findOneById(
$this->getPage()->getDataModelId()
) : null;
}
public function getUrl()
{
if ($this->getNodeAlias()) {
return $this->getNodeAlias()->getUrl();
}
return parent::getUrl();
}
public function setExternalUrl($externalUrl)
{
$this->externalUrl = $externalUrl;
return $this;
}
public function getExternalUrl($raw = false)
{
if ($raw) {
return $this->externalUrl;
}
return $this->hasExternalUrl() ? $this->getUrl() : $this->externalUrl;
}
public function getDefaultParams()
{
return '{' . preg_replace(
'/^\{{1}(.*)\}{1}$/is',
'$1',
str_replace(array("\r", "\n"), '', parent::getDefaultParams())
) . '}';
}
public function getRequirements()
{
return '{' . preg_replace(
'/^\{{1}(.*)\}{1}$/is',
'$1',
str_replace(array("\r", "\n"), '', parent::getRequirements())
) . '}';
}
public function addDefaultParams($params)
{
$current_params = $this->getCleanedDefaultParams();
foreach ($params as $key => $value) {
$current_params[$key] = $value;
}
$this->setCleanedDefaultParams($current_params);
return true;
}
public function setCleanedDefaultParams($value)
{
if (is_array($value)) {
if (!array_key_exists('value', $value)) {
$value = array('value' => $value);
}
$value = Yaml::dump($value, 1);
}
return parent::setDefaultParams($value);
}
public function getCleanedDefaultParams()
{
$default = array('value' => array());
if ($this->getNav() && $this->getNav()->getLocale()) {
$default['value']['_locale'] = $this->getNav()->getLocale();
}
try {
$value = $this->parser->parse($this->getDefaultParams());
if (!$value || !is_array($value)) {
$value = $default;
}
} catch (ParseException $e) {
$value = $default;
}
return isset($value['value']) ? $value['value'] : array('value' => $value);
}
public function setCleanedRequirements($value)
{
if (is_array($value)) {
if (!array_key_exists('value', $value)) {
$value = array('value' => $value);
}
$value = Yaml::dump($value);
}
return parent::setRequirements($value);
}
public function getCleanedRequirements()
{
$default = array('value' => array());
try {
$value = $this->parser->parse($this->getRequirements());
if (!$value || !is_array($value)) {
$value = $default;
}
} catch (ParseException $e) {
$value = $default;
}
return isset($value['value']) ? $value['value'] : array();
}
/*
* Return ancestors with root node and reverse order (from child node to root node)
*/
public function getReverseAncestors(PropelPDO $con = null)
{
if ($this->isRoot()) {
// save one query
return array($this);
} else {
$nodes = NodeQuery::create()
->ancestorsOf($this)
->orderByBranch(true)
->find($con);
$nodes->prepend($this);
return $nodes;
}
}
public function getBlockWithName($block_name)
{
$block = BlockQuery::create()
->filterByPageId($this->getPageId())
->filterByName($block_name)
->findOne();
return $block;
}
public function hasExternalUrl()
{
return 0 !== preg_match('`^(http|ftp)+s?://`', $this->getUrl());
}
public function hasAliases()
{
return count($this->getAliases());
}
public function getAliases()
{
return NodeQuery::create()->findByNodeAlias($this->getId());
}
public function getNodeAliasId()
{
if ($this->getNodeAlias()) {
return is_object($this->getNodeAlias()) ? $this->getNodeAlias()->getId() : $this->getNodeAlias();
}
return null;
}
public function setNodeAliasId($id)
{
$this->setNodeAlias($id);
}
public function getRouteName()
{
if ($this->route_name === null && !$this->isNew()) {
return sprintf('cms_node_%d', $this->id);
}
return parent::getRouteName();
}
}