/** * Get the corresponding page where the block is stored. * * @return ActiveQuery */ public function getNavItemPage() { return $this->hasOne(NavItemPage::className(), ['id' => 'nav_item_page_id']); }
/** * * Copy content of type cms_nav_item_page to a target nav item. This will create a new entry in cms_nav_item_page and for every used block a new entry in cms_nav_item_page_block_item * * @param $targetNavItem nav item object * @return bool */ public function copyPageItem($targetNavItem) { if ($this->nav_item_type !== 1) { return false; } $sourcePageItem = NavItemPage::findOne($this->nav_item_type_id); if (!$sourcePageItem) { return false; } $pageItem = new NavItemPage(); $pageItem->attributes = $sourcePageItem->toArray(); $pageItem->nav_item_id = $targetNavItem->id; if (!$pageItem->save()) { return false; } $targetNavItem->nav_item_type_id = $pageItem->id; if (!$targetNavItem->save()) { return false; } // @TODO replace with NavItemPage::copyBlocks($sourcePageItem->id, $pageItem->id); $pageBlocks = NavItemPageBlockItem::findAll(['nav_item_page_id' => $sourcePageItem->id]); $idLink = []; foreach ($pageBlocks as $block) { $blockItem = new NavItemPageBlockItem(); $blockItem->attributes = $block->toArray(); $blockItem->nav_item_page_id = $pageItem->id; $blockItem->insert(); $idLink[$block->id] = $blockItem->id; } // check if prev_id is used, check if id is in set - get new id and set new prev_ids in copied items $newPageBlocks = NavItemPageBlockItem::findAll(['nav_item_page_id' => $pageItem->id]); foreach ($newPageBlocks as $block) { if ($block->prev_id) { if (isset($idLink[$block->prev_id])) { $block->prev_id = $idLink[$block->prev_id]; } } $block->update(false); } return true; }
/** * * @param unknown $navId * @param unknown $langId * @param unknown $title * @param unknown $alias * @param unknown $layoutId * @param unknown $description * @return boolean */ public function createPageItem($navId, $langId, $title, $alias, $layoutId, $description) { $_errors = []; $navItem = new NavItem(); $navItem->parent_nav_id = self::findOne($navId)->parent_nav_id; $navItemPage = new NavItemPage(); $navItem->attributes = ['nav_id' => $navId, 'lang_id' => $langId, 'title' => $title, 'alias' => $alias, 'description' => $description, 'nav_item_type' => 1]; $navItemPage->attributes = ['nav_item_id' => 0, 'layout_id' => $layoutId, 'create_user_id' => Module::getAuthorUserId(), 'timestamp_create' => time(), 'version_alias' => Module::VERSION_INIT_LABEL]; if (!$navItem->validate()) { $_errors = ArrayHelper::merge($navItem->getErrors(), $_errors); } if (!$navItemPage->validate()) { $_errors = ArrayHelper::merge($navItemPage->getErrors(), $_errors); } if (!empty($_errors)) { return $_errors; } $navItemPage->save(); $navItem->nav_item_type_id = $navItemPage->id; $navItemId = $navItem->save(); $navItemPage->updateAttributes(['nav_item_id' => $navItem->id]); return $navItemId; }
/** * Create a new cms_nv_item_page for an existing nav_item, this is also known as a "new version" of a page item. * */ public function actionCreatePageVersion() { $name = Yii::$app->request->post('name'); $fromPageId = Yii::$app->request->post('fromPageId'); $navItemId = Yii::$app->request->post('navItemId'); $layoutId = Yii::$app->request->post('layoutId'); if (empty($name) || empty($navItemId)) { return ['error' => true]; } if (empty($fromPageId) && empty($layoutId)) { return ['error' => true]; } $navItemModel = NavItem::findOne($navItemId); if (!$navItemModel) { throw new \luya\Exception("Unable to find nav item model"); } if (!empty($fromPageId)) { $fromPageModel = NavItemPage::findOne($fromPageId); $layoutId = $fromPageModel->layout_id; } $model = new NavItemPage(); $model->attributes = ['nav_item_id' => $navItemId, 'timestamp_create' => time(), 'create_user_id' => Yii::$app->adminuser->getId(), 'version_alias' => $name, 'layout_id' => $layoutId]; $save = $model->save(false); if (!empty($fromPageId) && $save) { NavItemPage::copyBlocks($fromPageModel->id, $model->id); } if (empty($navItemModel->nav_item_type_id) && $navItemModel->nav_item_type == 1) { $navItemModel->updateAttributes(['nav_item_type_id' => $model->id]); } $navItemModel->updateAttributes(['timestamp_update' => time()]); return ['error' => !$save]; }