public function init()
 {
     if ($this->languageId == null) {
         if (Yii::$app->getModule('simplecms_frontend') == null) {
             throw new \Exception('Could not find module with name simplecms_frontend. Make sure you included the moduel in your configuration');
         }
         $this->languageId = Frontend::getLanguageManagerStatic()->getLanguageIdForString(Yii::$app->language);
     }
     parent::init();
 }
 function __construct($cmsHierarchyItemDetailsArray, $displayExpanded, $levelDepth)
 {
     $this->levelDepth = $levelDepth;
     $this->id = $cmsHierarchyItemDetailsArray['id'];
     $this->key = $cmsHierarchyItemDetailsArray['id'];
     $this->parent_id = $cmsHierarchyItemDetailsArray['parent_id'];
     $this->position = $cmsHierarchyItemDetailsArray['position'];
     if (isset($cmsHierarchyItemDetailsArray['menu_item']) && $cmsHierarchyItemDetailsArray['menu_item'] != null) {
         $this->title = $cmsHierarchyItemDetailsArray['menu_item']['name'];
         $this->alias = $cmsHierarchyItemDetailsArray['menu_item']['alias'];
         $this->menu_id = $cmsHierarchyItemDetailsArray['menu_item']['id'];
         $this->linkTarget = $cmsHierarchyItemDetailsArray['menu_item']['link_target'];
         $this->linkCssClass = $cmsHierarchyItemDetailsArray['menu_item']['link_css_class'];
         $this->content_id = $cmsHierarchyItemDetailsArray['menu_item']['page_content_id'];
         $this->document_id = $cmsHierarchyItemDetailsArray['menu_item']['document_id'];
         $this->direct_url = $cmsHierarchyItemDetailsArray['menu_item']['direct_url'];
         $this->languageId = $cmsHierarchyItemDetailsArray['menu_item']['language'];
         $this->languageCode = Frontend::getLanguageManagerStatic()->getMappingForIdResolveAlias($cmsHierarchyItemDetailsArray['menu_item']['language'])['code'];
     }
     $this->expanded = $displayExpanded;
     $this->displayState = $cmsHierarchyItemDetailsArray['display_state'];
     $this->isFallbackLanguage = isset($cmsHierarchyItemDetailsArray['displaying_fallback_language']) && $cmsHierarchyItemDetailsArray['displaying_fallback_language'];
     foreach ($cmsHierarchyItemDetailsArray['available_menu_items_all_languages'] as $menuItem) {
         $this->addAvailableLanguageCodes(Frontend::getLanguageManagerStatic()->getMappingForIdResolveAlias($menuItem['language'])['code'], $menuItem['id']);
     }
     // create an array with all languages, where the available languages are marked explicitly (this is used to display the existing and non existing language versions in the frontend
     foreach (Frontend::getLanguageManagerStatic()->getAllConfiguredLanguageCodes() as $languageId => $languageCode) {
         $this->allLanguagesWithMarker[] = ['code' => $languageCode, 'language_id' => $languageId, 'available' => array_key_exists($languageCode, $this->availableLanguageCodes), 'menu_item_id' => isset($this->availableLanguageCodes[$languageCode]) ? $this->availableLanguageCodes[$languageCode] : ''];
     }
 }
 /**
  * generate a root item in all configured languages wuth default name and content.
  * This is used in case the database is not setup properly for some reason to prevent an error message being shown due to missing root item.
  * @param language integer the language id to get the menu item for the hierarchy item for
  * @return \schallschlucker\simplecms\models\SimpleHierarchyItem
  */
 public static function autoCreateRootItemInAllConfiguredLanguages($language)
 {
     //FIXME: maybe modify this function to only create a default language instead, some people might not want to have the root item in all configured languages for some reason
     /* @var $languageManager LanguageManager */
     $languageManager = Frontend::getLanguageManagerStatic();
     $rootMenuItemArray = [];
     //check if item exists and maybe is just missing menu items
     $rootHierarchyItem = CmsHierarchyItem::findOne(DefaultController::$ROOT_HIERARCHY_ITEM_ID);
     if ($rootHierarchyItem == null) {
         $rootHierarchyItem = new CmsHierarchyItem();
         $rootHierarchyItem->display_state = CmsHierarchyItem::DISPLAYSTATE_PUBLISHED_VISIBLE_IN_NAVIGATION;
         $rootHierarchyItem->id = DefaultController::$ROOT_HIERARCHY_ITEM_ID;
         $rootHierarchyItem->position = 1;
         if (!$rootHierarchyItem->save(false)) {
             throw new \Exception("failed while trying to auto create missing root hierarchy item. This error was caused whily trying to auto create a root hierarchy item for the cms, since none exists at the moment. Please make sure a hierarchy item with the ID " . DefaultController::$ROOT_HIERARCHY_ITEM_ID . " exists, for the cms to work.");
         }
     }
     $existingCmsMenu = $rootHierarchyItem->getCmsMenus()->all();
     $availableLanguages = [];
     foreach ($existingCmsMenu as $tempMenuItem) {
         $availableLanguages[$tempMenuItem->language] = $tempMenuItem;
     }
     foreach ($languageManager->getAllConfiguredLanguageCodes() as $key => $code) {
         if (!array_key_exists($key, $availableLanguages)) {
             $pageContent = new CmsPageContent();
             $pageContent->content = "Auto generated page content. This page content has been created by the Cms Backend due to the fact that no root item was existing in the database.";
             $pageContent->created_datetime = date('Y-m-d-H:i:s');
             $pageContent->createdby_userid = "1";
             $pageContent->description = "root page of yii2 simplecms plugin tree structure";
             $pageContent->language = $key;
             $pageContent->isNewRecord = true;
             if (!$pageContent->save(false)) {
                 throw new \Exception("failed while trying to auto create page content for missing root hierarchy item. This error was caused whily trying to auto create a root hierarchy item for the cms, since none exists at the moment. Please make sure a hierarchy item with the ID " . DefaultController::$ROOT_HIERARCHY_ITEM_ID . " exists, for the cms to work.");
             }
             $menuItem = new CmsMenuItem();
             $menuItem->alias = 'root';
             $menuItem->name = 'Root';
             $menuItem->cms_hierarchy_item_id = DefaultController::$ROOT_HIERARCHY_ITEM_ID;
             $menuItem->created_datetime = date('Y-m-d-H:i:s');
             $menuItem->createdby_userid = "1";
             //TODO should we get the current user id here? Or is there some kind of a system user id to indidcate auto creation
             $menuItem->isNewRecord = true;
             $menuItem->language = $key;
             $menuItem->page_content_id = $pageContent->id;
             if (!$menuItem->save(false)) {
                 throw new \Exception("failed while trying to auto create menu item for root hierarchy item. This error was caused whily trying to auto create a root hierarchy item for the cms, since none exists at the moment. Please make sure a hierarchy item with the ID " . DefaultController::$ROOT_HIERARCHY_ITEM_ID . " exists, for the cms to work.");
             }
             $availableLanguages[$key] = $menuItem;
             $rootMenuItemArray[] = $menuItem;
         }
     }
     $rootHierarchyItem['menu_item'] = $availableLanguages[$language];
     $rootItem = new SimpleHierarchyItem($rootHierarchyItem, 1, 0);
     return $rootItem;
 }