/**
  * MenuItemDetails constructor.
  * @param MenuItem $item
  * @param bool $active
  * @param int $parentPageId
  * @param bool $canonicals
  */
 public function __construct(MenuItem $item, $active = false, $parentPageId = 0, $canonicals = false)
 {
     $pageId = Path::unParsePageId($item->page_id);
     if (!$item->id && !$canonicals) {
         $item->page_id = Path::parsePageId($pageId, $parentPageId);
     }
     $this->item = $item;
     $this->page = Page::preload($pageId);
     $this->parentPageId = $parentPageId;
     $this->active = $active;
     $this->name = $item->custom_name ?: PageLang::getName($pageId);
     $this->url = $this->page && $this->page->link ? PageLang::getUrl($pageId) : Path::getFullUrl($item->page_id);
 }
 /**
  * Display pages selected with view
  * Also reverse lookup option, find pages with the current page selected
  * @param string $content
  * @param array $options
  * @return string
  */
 public function display($content, $options = [])
 {
     $pages = [];
     $page_ids = [];
     if (isset($options['reverse'])) {
         // get page_ids on which current page is selected in this block
         $currentPageId = PageBuilder::pageId(true);
         if ($currentPageId) {
             $same_blocks = PageBlock::where('block_id', '=', $this->_block->id)->get();
             foreach ($same_blocks as $same_block) {
                 $block_page_ids = @unserialize($same_block->content);
                 if (!empty($block_page_ids)) {
                     foreach ($block_page_ids as $k => $block_page_id) {
                         $block_page_id = Path::unParsePageId($block_page_id);
                         if ($currentPageId == $block_page_id) {
                             $page_ids[] = $same_block->page_id;
                             break;
                         }
                     }
                 }
             }
         }
     } elseif (!empty($content)) {
         $page_ids = unserialize($content);
     }
     if (!empty($page_ids)) {
         foreach ($page_ids as $page_id) {
             $parsedPageId = Path::unParsePageId($page_id, false);
             $pages[$page_id] = new PageDetails($parsedPageId[0], !empty($parsedPageId[1]) ? $parsedPageId[1] : 0);
         }
     }
     $template = !empty($options['view']) ? $options['view'] : $this->_block->name;
     $selectPageViews = 'themes.' . PageBuilder::getData('theme') . '.blocks.selectpages.';
     if (View::exists($selectPageViews . $template)) {
         return View::make($selectPageViews . $template, array('pages' => $pages))->render();
     } else {
         return 'Select pages template not found';
     }
 }
 /**
  * @param Page[]|MenuItem[] $items
  * @param int $parentPageId
  * @param int $level
  * @param int $subLevels
  * @return string
  */
 protected static function _buildMenu($items, $parentPageId, $level = 1, $subLevels = 0)
 {
     // convert page models to menu items and remove non-live pages
     foreach ($items as $k => $item) {
         if (is_a($item, Page::class)) {
             $pageId = $item->id;
             $items[$k] = new MenuItem();
             $items[$k]->page_id = $item->id;
             $items[$k]->sub_levels = 0;
             $items[$k]->custom_name = '';
         } else {
             $pageId = $item->page_id;
         }
         $pageId = Path::unParsePageId($pageId);
         $page = Page::preload($pageId);
         if (!$page->exists || !$page->is_live()) {
             unset($items[$k]);
         }
     }
     $pageParents = [];
     $pageLevels = PageBuilder::getData('pageLevels') ?: [];
     foreach ($pageLevels as $k => $parentPage) {
         if ($k > 0) {
             $pageParents[] = $parentPage->page_id;
         }
     }
     $currentPage = PageBuilder::getData('page') ?: new Page();
     $total = count($items);
     $menuItems = '';
     $defaultSubLevels = $subLevels;
     if (is_a($items, Collection::class)) {
         $items = $items->all();
     }
     $items = array_values($items);
     foreach ($items as $count => $item) {
         $isFirst = $count == 0;
         $isLast = $count == $total - 1;
         $pageId = Path::unParsePageId($item->page_id);
         $active = $currentPage->id == $pageId || in_array($pageId, $pageParents);
         $itemData = new MenuItemDetails($item, $active, $parentPageId, self::$_canonicals);
         $subMenu = '';
         $subLevels = $item->sub_levels > 0 ? $item->sub_levels : $defaultSubLevels;
         if ($subLevels > 0) {
             if ($subPages = Page::category_pages($pageId)) {
                 $subMenu = self::_buildMenu($subPages, $pageId, $level + 1, $subLevels - 1);
             }
         }
         if (!empty($subMenu)) {
             $menuItems .= self::_getRenderedView('submenu_' . $level, ['item' => $itemData, 'items' => $subMenu, 'is_first' => $isFirst, 'is_last' => $isLast, 'count' => $count + 1, 'total' => $total, 'level' => $level, 'further_levels' => $subLevels]);
         } else {
             $menuItems .= self::_getRenderedView('item', ['item' => $itemData, 'is_first' => $isFirst, 'is_last' => $isLast, 'count' => $count, 'total' => $total, 'level' => $level]);
         }
     }
     if ($level == 1) {
         return self::_getRenderedView('menu', ['items' => $menuItems]);
     } else {
         return $menuItems;
     }
 }
 /**
  * @param string $blockName
  * @param array $options
  * @return mixed|string
  */
 public function block($blockName, $options = [])
 {
     // force query available if block details changed in current request
     $block = Block::preloadClone($blockName, isset($options['force_query']));
     $pageId = !empty($options['page_id']) ? Path::unParsePageId($options['page_id']) : $this->pageId();
     $usingGlobalContent = false;
     $blockData = null;
     if (($customBlockData = $this->_getCustomBlockData($blockName)) !== null) {
         // load custom block data for (is also used for repeater content)
         $blockData = $customBlockData;
     } elseif ($block->exists) {
         // load block data
         $globalBlockData = PageBlockDefault::preload($block->id);
         $pageBlockData = PageBlock::preloadPageBlock($pageId, $block->id, $this->pageVersion($pageId));
         // get languages
         $loadForLanguages = [Language::current()];
         if (config('coaster::frontend.language_fallback') == 1 && !in_array(config('coaster::frontend.language'), $loadForLanguages)) {
             $loadForLanguages[] = config('coaster::frontend.language');
         }
         // run through languages until block data found
         foreach ($loadForLanguages as $language) {
             if (!empty($pageBlockData[$language])) {
                 // if custom page block for selected language exists
                 $blockData = $pageBlockData[$language]->content;
             } elseif (!empty($globalBlockData[$language])) {
                 // if default block for selected language exists
                 $blockData = $globalBlockData[$language]->content;
                 $usingGlobalContent = true;
                 break;
             }
         }
         // return raw data
         if (isset($options['raw']) && $options['raw']) {
             return $blockData;
         }
     } else {
         return 'block not found';
     }
     // set version that data has been grabbed for (0 = latest)
     if (empty($options['version'])) {
         $options['version'] = $usingGlobalContent ? 0 : $this->pageVersion($pageId);
     }
     // pass block details and data to display class
     return $block->setPageId($pageId)->setVersionId($options['version'])->getTypeObject()->display($blockData, $options);
 }