/**
  * show the page for a given page content id 
  * @menuLabel __HIDDEN__
  * @param integer $pageContentId the cmsPageContent item id to be displayed
  * @throws NotFoundHttpException an exception when the page content with the given id does not exists or does not have a linked page content
  * @return View
  */
 public function actionContent($pageContentId)
 {
     $isFallback = false;
     $pageContentId = intval($pageContentId);
     $pageContent = CmsPageContent::findOne(['id' => $pageContentId]);
     if ($pageContent == null) {
         throw new NotFoundHttpException('No page content could be found for the menu id', 404);
     }
     return $this->render('page', ['pageContentModel' => $pageContent, 'isfallbacklanguage' => $isFallback]);
 }
 /**
  * Finds the CmsPageContent model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  *
  * @param integer $id        	
  * @return CmsPageContent the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = CmsPageContent::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
예제 #3
0
 /**
  *
  * @return \yii\db\ActiveQuery
  */
 public function getPageContent()
 {
     return $this->hasOne(CmsPageContent::className(), ['id' => 'page_content_id']);
 }
 /**
  * @menuLabel __HIDDEN__
  * @menuIcon <span class="glyphicon glyphicon-list-alt"></span>
  */
 public function actionEditMenuLanguageVersion($menuItemId, $useget = false)
 {
     $menuItemId = intval($menuItemId);
     $model_menu = CmsMenuItem::find()->where(['id' => $menuItemId])->with('cmsHierarchyItem')->one();
     $hierarchyItem = $model_menu->cmsHierarchyItem;
     $languageCode = $this->module->getLanguageManager()->getMappingForIdResolveAlias($model_menu->language);
     $model_wrapperform = new MenuItemAndContentForm();
     if ($useget) {
         $model_wrapperform->load(Yii::$app->request->get());
     } else {
         $model_wrapperform->load(Yii::$app->request->post());
     }
     $model_content = null;
     $model_document = null;
     $message = null;
     $page_content_id_set = $model_menu->page_content_id != null;
     $contentType = $page_content_id_set ? MenuItemAndContentForm::CONTENT_TYPE_PAGE : intval($model_wrapperform->contentType);
     /**
      * **** PAGE CONTENT FORM ****************
      */
     if ($page_content_id_set || $contentType == intval(MenuItemAndContentForm::CONTENT_TYPE_PAGE)) {
         $model_content = new CmsPageContent();
         if ($page_content_id_set) {
             $model_content = CmsPageContent::findOne($model_menu->page_content_id);
         }
         $loadSuccess_content = $model_content->load(Yii::$app->request->post());
         // check if menu item needs to be saved
         $model_content->language = $model_menu->language;
         if ($loadSuccess_content) {
             if ($model_content->created_datetime == null) {
                 $model_content->created_datetime = new Expression('NOW()');
                 $model_content->createdby_userid = Yii::$app->user->id;
             }
             if ($model_content->validate() && $model_content->save()) {
                 // link new item to menu if needed
                 if (!$page_content_id_set) {
                     $model_menu->page_content_id = $model_content->id;
                     if ($model_menu->save()) {
                         $message .= '<span class="success">' . Yii::t('simplecms', 'Content linked to Menu language version') . '</span>';
                     } else {
                         $message .= '<span class="error">' . Yii::t('simplecms', 'Error: content saved, yet it could not be linked to the menu language version!') . implode($model_content->getFirstErrors(), ' ') . '</span>';
                     }
                 }
                 $message .= '<span class="success">' . Yii::t('simplecms', 'Content saved successfully') . '</span>';
             } else {
                 $message .= '<span class="error">' . Yii::t('simplecms', 'Error: saving content failed!') . implode($model_content->getFirstErrors(), ' ') . '</span>';
             }
         }
         // update menu item
         // $menu_item_new_name = new CmsMenuItem();
         if ($model_menu->load(Yii::$app->request->post())) {
             if ($model_menu->save()) {
                 $message .= '<span class="success">' . Yii::t('simplecms', 'Menu details saved successfully') . '</span>';
             } else {
                 $message .= '<span class="error">' . Yii::t('simplecms', 'Error: saving new menu name!') . '</span>';
             }
         }
         $model_wrapperform->contentType = MenuItemAndContentForm::CONTENT_TYPE_PAGE;
     } else {
         if ($model_menu->document_id != null || $contentType == MenuItemAndContentForm::CONTENT_TYPE_DOCUMENT) {
             /**
              * **** DOCUMENT FORM ****************
              */
             if ($model_menu->document_id != null) {
                 $model_document = CmsDocument::findOne($model_menu->document_id);
             } else {
                 $model_document = new CmsDocument();
                 $model_document->created_datetime = new Expression('NOW()');
                 $model_document->createdby_userid = Yii::$app->user->id;
             }
             $loadSuccess_document = $model_document->load(Yii::$app->request->post());
             // set fixed values
             $model_document->language = $model_menu->language;
             if ($loadSuccess_document) {
                 if ($model_document->save()) {
                     // link new item to menu if needed
                     if ($model_menu->document_id == null) {
                         $model_menu->document_id = $model_document->id;
                         $model_menu->save();
                     }
                     $message .= '<span class="success">' . Yii::t('simplecms', 'Document saved successfully') . '</span>';
                 } else {
                     $message .= '<span class="error">' . Yii::t('simplecms', 'Error: saving document failed!') . '</span>';
                 }
             }
             // check if menu item name has been altered
             if ($model_menu->load(Yii::$app->request->post())) {
                 if ($model_menu->save()) {
                     $message .= '<span class="success">' . Yii::t('simplecms', 'Menu details saved successfully') . '</span>';
                 } else {
                     $message .= '<span class="error">' . Yii::t('simplecms', 'Error: saving new menu name!') . '</span>';
                 }
             }
             $model_wrapperform->contentType = MenuItemAndContentForm::CONTENT_TYPE_DOCUMENT;
         } else {
             if ($model_menu->direct_url != null || $contentType == MenuItemAndContentForm::CONTENT_TYPE_URL) {
                 /**
                  * **** DIRECT URL FORM ****************
                  */
                 if ($model_menu->load(Yii::$app->request->post())) {
                     if ($model_menu->save()) {
                         $message .= '<span class="success">' . Yii::t('simplecms', 'Menu details saved successfully') . '</span>';
                     } else {
                         $message .= '<span class="error">' . Yii::t('simplecms', 'Error: saving new menu name!') . '</span>';
                     }
                 }
                 $model_wrapperform->contentType = MenuItemAndContentForm::CONTENT_TYPE_URL;
             } else {
                 // illegal state, no content found for item, menu might not have been created properly or not completed, display selector for new content type
                 $model_wrapperform->contentType = MenuItemAndContentForm::CONTENT_TYPE_UNDEFINED;
             }
         }
     }
     // get post parameters
     // TODO
     // set default values
     return $this->render('editMenuAndContent', ['model_wrapperform' => $model_wrapperform, 'model_content' => $model_content, 'hierarchy_item' => $hierarchyItem, 'languageCode' => $languageCode, 'model_document' => $model_document, 'model_menu' => $model_menu, 'message' => $message]);
 }
 /**
  * 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;
 }