/** * if the module could not be found (not registered in the cms) the method returns the provided module name. * * returns only ONE even if there are more! not good structure inside the cms? * * @param string $moduleName * @return url */ public static function toModule($moduleName) { $model = NavItem::find()->leftJoin('cms_nav_item_module', 'nav_item_type_id=cms_nav_item_module.id')->where(['nav_item_type' => 2, 'cms_nav_item_module.module_name' => $moduleName])->asArray()->one(); if ($model) { $link = Yii::$app->links->findOneByArguments(['id' => $model['id']]); if ($link) { return $link['full_url']; } } return $moduleName; }
/** * admin/api-cms-navitem/update-item?navItemId=2. * * @param unknown_type $navItemId * * @return unknown */ public function actionUpdateItem($navItemId) { $model = NavItem::find()->where(['id' => $navItemId])->one(); if (!$model) { throw new \Exception('could not find the model to validate'); } $model->scenario = 'meta'; $model->attributes = $_POST; $v = $model->validate(); if ($model->validate()) { if ($model->save()) { return true; } } return $model->getErrors(); }
/** * Get full constructed of a nav item. * * @param $navId * @return string Path */ public function actionGetNavItemPath($navId) { $data = ""; $node = NavItem::find()->where(['nav_id' => $navId])->one(); if ($node) { $data .= $node->title; $parentNavId = $navId; while ($parentNavId != 0) { $parentNavId = Nav::findOne($parentNavId)->parent_nav_id; if ($parentNavId != 0) { $node = NavItem::find()->where(['nav_id' => $parentNavId])->one(); if ($parentNavId) { $data = $node->title . '/' . $data; } } } } return $data; }
public function actionDelete($navId) { $model = \cmsadmin\models\Nav::find()->where(['id' => $navId])->one(); if ($model) { Yii::$app->menu->flushCache(); // check for internal redirects $redirectResult = false; $redirects = NavItemRedirect::find()->where(['value' => $navId])->asArray()->all(); foreach ($redirects as $redirect) { $navItem = NavItem::find()->where(['nav_item_type' => 3, 'nav_item_type_id' => $redirect['id']])->one(); $redirectResult = empty(Nav::find()->where(['id' => $navItem->nav_id, 'is_deleted' => 0])->one()) ? $redirectResult : true; } if ($redirectResult) { Yii::$app->response->statusCode = 417; return; } $model->is_deleted = 1; foreach (NavItem::find()->where(['nav_id' => $navId])->all() as $navItem) { $navItem->setAttribute('alias', date('Y-m-d-H-i') . '-' . $navItem->alias); $navItem->update(false); } return $model->update(false); } }
/** * * Create a new nav item with a specific language, title and alias based on a given nav item id. * All content of the source nav item will be copied dependent on the nav item type (page content, module link, redirect informations). * * @param $navItemId source nav item * @param $langId (new) target language * @param $title title of nav item * @param $alias alias of nav item * @return bool */ public function createItemLanguageCopy($navItemId, $langId, $title, $alias) { $sourceNavItem = NavItem::findOne($navItemId); if (!$sourceNavItem) { return false; } if (NavItem::find()->where(['nav_id' => $sourceNavItem->nav_id, 'lang_id' => $langId])->one()) { return false; } $navItem = new NavItem(); $navItem->attributes = $sourceNavItem->toArray(); $navItem->title = $title; $navItem->alias = $alias; $navItem->lang_id = $langId; $navItem->setParentFromModel(); if (!$navItem->save()) { return false; } return $sourceNavItem->copyTypeContent($navItem); }
public function actionDelete($navId) { $model = \cmsadmin\models\Nav::find()->where(['id' => $navId])->one(); if ($model) { $model->is_deleted = 1; foreach (NavItem::find()->where(['nav_id' => $navId])->all() as $navItem) { $navItem->setAttribute('alias', date('Y-m-d-H-i') . '-' . $navItem->alias); $navItem->update(false); } return $model->update(false); } }
/** * Check for duplicate alias in same parent_nav_id context of targetNav, comparing with currentNav item. * Additional this checks for matching language contexts when comparing aliases. * * @param $currentNavId * @param $targetNav * @return bool true when duplicate found */ public static function checkDuplicateAlias($currentNavId, $parentNavId) { $currentNavItems = NavItem::find()->where(['nav_id' => $currentNavId])->asArray()->all(); foreach (self::find()->where(['parent_nav_id' => $parentNavId])->andWhere(['<>', 'id', $currentNavId])->asArray()->all() as $item) { foreach ($currentNavItems as $currentNavItem) { $itemNavItem = NavItem::findOne(['nav_id' => $item['id'], 'lang_id' => $currentNavItem['lang_id']]); if ($itemNavItem && $currentNavItem['alias'] == $itemNavItem->alias) { return false; } } } return true; }