*/
protected $_breadcrumbs = true;
+ /**
+ * @var array
+ */
+ protected $_urlAliases = [];
+
/**
* @param string $id
return $res;
}
+ public function findAllWithAlias($href)
+ {
+ $res = [];
+ if (in_array($href, $this->getUrlAliases())) {
+ $res[] = $this;
+ }
+ foreach ($this->getChildren() as $child) {
+ $res = array_merge($res, $child->findAllWithAlias($href));
+ }
+
+ return $res;
+ }
+
+ public function findOneWithAlias($href)
+ {
+ if (in_array($href, $this->getUrlAliases())) {
+ return $this;
+ }
+ if (!$this->hasChildren()) {
+ return null;
+ }
+ foreach ($this->getChildren() as $child) {
+ $res = $child->findOneWithAlias($href);
+ if (null !== $res) {
+ return $res;
+ }
+ }
+ return null;
+ }
+
public function findItemWithURL($href)
{
foreach ($this->findAllBy('href', $href) as $item) {
return $this->_breadcrumbs;
}
+ /**
+ * @return array
+ */
+ public function getUrlAliases(): array
+ {
+ return $this->_urlAliases;
+ }
+
+ /**
+ * @param array $urlAliases
+ */
+ public function setUrlAliases(array $urlAliases): void
+ {
+ $this->_urlAliases = $urlAliases;
+ }
+
/**
* @param $menu Menu
*/
use Cubist\Backpack\app\Magic\Models\CMSPage;
+use Cubist\Backpack\app\Magic\Models\CubistMagicPageModel;
class PageItem extends Item
{
/**
- * @var CMSPage
+ * @var CubistMagicPageModel
*/
protected $_page;
+
/**
- * @param $page CMSPage
- * @param $all CMSPage[]
+ * @param $page CubistMagicPageModel
+ * @param $all CubistMagicPageModel[]
*/
public function initFromPage($page, $all)
{
- $this->setPage($page);
- $this->setId($page->id);
- $this->setName($page->name);
- $this->setSlug($page->slug);
- $this->setTitle($page->title);
+ $this->initFromEntity($page);
$this->setChildrenFromData($all, $this->getId());
$this->setChildrenFromTemplate();
}
+ /**
+ * @param $entity CubistMagicPageModel
+ */
+ public function initFromEntity($entity)
+ {
+ $this->setPage($entity);
+ $this->setId($entity->id);
+ $this->setName($entity->name ?? get_class($entity) . '_' . $entity->id);
+ $this->setSlug($entity->slug);
+ $this->setTitle($entity->title);
+ $dbaliases = $entity->getPageData()->url_alias;
+ if (is_array($dbaliases)) {
+ $aliases = [];
+ foreach ($dbaliases as $dbalias) {
+ $aliases[] = ltrim($dbalias['url'],'/');
+ }
+ $this->setURLAliases(array_unique(array_values($aliases)));
+ }
+ }
+
public function setChildrenFromTemplate()
{
- $template = $this->getPage()->getUsedTemplate();
- $template->setMenuChildren($this);
+ if ($this->getPage() instanceof CMSPage) {
+ $template = $this->getPage()->getUsedTemplate();
+ $template->setMenuChildren($this);
+ }
+ }
+
+ public function getTemplate()
+ {
+ if ($this->getPage() instanceof CMSPage) {
+ return $this->getPage()->template;
+ }
+ return false;
}
public function getHref()
{
- if ($this->getPage()->template == 'first_redirection' && $this->hasChildren()) {
+ if ($this->getTemplate() == 'first_redirection' && $this->hasChildren()) {
return $this->getChildren()[0]->getHref();
- } else if ($this->getPage()->template == 'internal_redirection') {
+ } else if ($this->getTemplate() == 'internal_redirection') {
- } else if ($this->getPage()->template == 'redirection') {
+ } else if ($this->getTemplate() == 'redirection') {
return $this->navigation;
}
- if ($this->getPage()->getUsedTemplate()->isVirtual()) {
+ if ($this->isVirtual()) {
return '#';
}
return $this->getSlug();
}
+ public function isVirtual()
+ {
+ if ($this->getPage() instanceof CMSPage) {
+ return $this->getPage()->getUsedTemplate()->isVirtual();
+ }
+ return false;
+ }
+
public function getBreadcrumbHref()
{
- if ($this->getPage()->template == 'first_redirection' && $this->hasChildren()) {
+ if ($this->getTemplate() == 'first_redirection' && $this->hasChildren()) {
return '#';
- } else if ($this->getPage()->template == 'internal_redirection') {
+ } else if ($this->getTemplate() == 'internal_redirection') {
return '#';
- } else if ($this->getPage()->template == 'redirection') {
+ } else if ($this->getTemplate() == 'redirection') {
return '#';
- } else if ($this->getPage()->getUsedTemplate()->isVirtual()) {
+ } else if ($this->isVirtual()) {
return '#';
}
public function getClasses()
{
$classes = parent::getClasses();
- if ($this->getPage()->getUsedTemplate()->isVirtual()) {
+ if ($this->isVirtual()) {
$classes[] = 'nav-virtual';
}
return $classes;
}
/**
- * @param CMSPage $page
+ * @param CubistMagicPageModel $page
*/
- public function setPage(CMSPage $page): void
+ public function setPage(CubistMagicPageModel $page): void
{
$this->_page = $page;
}
/**
- * @return CMSPage
+ * @return CubistMagicPageModel|null
*/
- public function getPage(): CMSPage
+ public function getPage()
{
return $this->_page;
}
*/
public function getController(): array
{
- return ['controller' => 'PageController', 'action' => 'index', 'params' => ['slug' => $this->getSlug()]];
+ if (!$this->_controller) {
+ return ['controller' => 'PageController', 'action' => 'index', 'params' => ['slug' => $this->getSlug()]];
+ }
+ return parent::getController();
}
}