205 lines
4.8 KiB
Plaintext
205 lines
4.8 KiB
Plaintext
<?php
|
|
|
|
namespace Trinity\Bundle\ContentManagerBundle\Model;
|
|
|
|
use Trinity\Bundle\ContentManagerBundle\Model\om\BaseNav;
|
|
use Trinity\Bundle\ContentManagerBundle\Navigation\NavigationInterface;
|
|
|
|
class Nav extends BaseNav implements NavigationInterface
|
|
{
|
|
protected $root_node = null;
|
|
|
|
protected $host = null;
|
|
|
|
protected $host_requirements = null;
|
|
|
|
public function __toString()
|
|
{
|
|
return $this->getTitle();
|
|
}
|
|
|
|
public function getRootNode()
|
|
{
|
|
return null !== $this->root_node ? $this->root_node : $this->root_node = NodeQuery::create()->filterByTreeLeft('1')->filterByNavId($this->id)->findOne();
|
|
}
|
|
|
|
public function createRootNode()
|
|
{
|
|
if ($this->getRootNode()) {
|
|
return false;
|
|
}
|
|
|
|
$node = new Node();
|
|
$node->setNavId($this->getId())
|
|
->setTitle('Homepage')
|
|
->makeRoot()
|
|
->save();
|
|
|
|
return true;
|
|
}
|
|
|
|
public function isDomainAndCultureValid()
|
|
{
|
|
$nav = NavQuery::create()
|
|
->filterByDomain($this->getDomain())
|
|
->filterByCulture($this->getCulture())
|
|
->findOne();
|
|
|
|
if (count($nav) === 0) {
|
|
return true;
|
|
}
|
|
|
|
if ($nav->getId() === $this->getId()) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function isRegexDomain()
|
|
{
|
|
return preg_match(sprintf('/^%s/', preg_quote(NavPeer::REGEX_DOMAIN_TAG)), $this->getDomain()) !== 0;
|
|
}
|
|
|
|
public function getDomainRegex()
|
|
{
|
|
if (!$this->isRegexDomain()) {
|
|
return null;
|
|
}
|
|
|
|
return trim(str_replace(NavPeer::REGEX_DOMAIN_TAG, '', $this->getDomain()));
|
|
}
|
|
|
|
public function transformDomainRegex()
|
|
{
|
|
$host = $this->getDomainRegex();
|
|
$requirements = array();
|
|
|
|
preg_match('`(.{1}).*\1([a-zA-Z]*)`', $host, $regexParams);
|
|
|
|
if (!empty($regexParams)) {
|
|
$host = preg_replace('`(.{1})(.*)\1[a-zA-Z]*`', '$2', $host);
|
|
}
|
|
|
|
preg_replace_callback(
|
|
'`\((.+)\)`sU',
|
|
function ($m) use (&$requirements) {
|
|
if (isset($requirements[$m[0]])) {
|
|
return $requirements[$m[0]];
|
|
}
|
|
|
|
$identifier = 'param';
|
|
|
|
$requirements[$m[0]] = $identifier;
|
|
},
|
|
$host
|
|
);
|
|
|
|
preg_replace_callback(
|
|
'`.(\+|\*)`U',
|
|
function ($m) use (&$requirements) {
|
|
$m[1] = '.' . $m[1];
|
|
if (isset($requirements[$m[1]])) {
|
|
return $requirements[$m[1]];
|
|
}
|
|
|
|
$identifier = 'param';
|
|
|
|
$requirements[$m[1]] = $identifier;
|
|
},
|
|
$host
|
|
);
|
|
|
|
$i = 0;
|
|
foreach ($requirements as $k => $requirement) {
|
|
$host = str_replace($k, sprintf('{%s_%d}', $requirement, $i), $host);
|
|
$requirements[$k] = $requirement . '_' . $i;
|
|
$i++;
|
|
}
|
|
|
|
$this->host = $host;
|
|
$this->host_requirements = array_flip($requirements);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function transformHost($host)
|
|
{
|
|
$regex = $this->getDomainRegex();
|
|
|
|
$default_params = array();
|
|
|
|
if (preg_match($regex, $host, $matches)) {
|
|
array_shift($matches);
|
|
|
|
foreach ($matches as $k => $param) {
|
|
$default_params['param_' . $k] = $param;
|
|
}
|
|
}
|
|
|
|
return $default_params;
|
|
|
|
}
|
|
|
|
public function getHost()
|
|
{
|
|
return $this->host;
|
|
}
|
|
|
|
public function getHostRequirements()
|
|
{
|
|
return $this->host_requirements;
|
|
}
|
|
|
|
public function getVarnishDomainRegex()
|
|
{
|
|
if (!$this->isRegexDomain()) {
|
|
return null;
|
|
}
|
|
|
|
$regex = $this->getDomainRegex();
|
|
|
|
if (0 === strlen(trim($regex))) {
|
|
return null;
|
|
}
|
|
|
|
$delimiter = $regex[0] !== '/' ? preg_quote($regex[0]) : '\/';
|
|
|
|
return preg_replace(sprintf('/%s(.*)%s[a-zA-Z]*$/s', $delimiter, $delimiter), '$1', $regex);
|
|
}
|
|
|
|
public function regexDomainMatch($domain)
|
|
{
|
|
return preg_match($this->getDomainRegex(), $domain);
|
|
}
|
|
|
|
public function getSitemapNodes()
|
|
{
|
|
return NodeQuery::create()
|
|
->innerJoinPageRelatedByNodeId()
|
|
->where(sprintf('%s = %s', PagePeer::SITEMAP_STATUS, 1))
|
|
->_and()
|
|
->filterByNavId($this->getId())
|
|
->find();
|
|
}
|
|
|
|
public function getMinimisedCulture()
|
|
{
|
|
if (strlen($this->getCulture()) < 2) {
|
|
return $this->getCulture();
|
|
}
|
|
|
|
return strtolower(substr($this->getCulture(), 0, 2));
|
|
}
|
|
|
|
public function getLocale()
|
|
{
|
|
return $this->getCulture();
|
|
}
|
|
|
|
public function setLocale($locale)
|
|
{
|
|
return $this->setCulture($locale);
|
|
}
|
|
}
|