/** * Returns a tree of menu-items that belong to the menu * * @param array $settings A settings array that holds the parent-id and level * @return array */ public function getTree($settings = ['subMenu' => true, 'parentId' => 0, 'level' => 0, 'includeLanguage' => true, 'convertBr' => false]) { $items = []; $menuItems = $this->getItems($settings)->all(); foreach ($menuItems as $menuItem) { // First we need to check if the item has a non-public page attached // If so, and no user is logged in, the item is skipped if ($menuItem->entity == Page::className() && Yii::$app->user->isGuest) { $menuItemEntity = $menuItem->entityModel; if (isset($menuItemEntity->public) && $menuItemEntity->public == false) { continue; } } $item = ['label' => isset($settings['convertBr']) ? str_replace("\n", '<br>', $menuItem->name) : str_replace("\n", ' ', $menuItem->name), 'url' => $menuItem->getUrl($settings['includeLanguage']), 'entity' => $menuItem->entity, 'entity_id' => $menuItem->entity_id, 'active' => in_array($menuItem->entity == MenuItem::className() ? $menuItem->entity_id : $menuItem->id, Yii::$app->page->linkedMenuItemsIds), 'options' => ['class' => "menu-item-{$menuItem->id}"]]; if (isset($settings['includeObject']) && $settings['includeObject'] == true) { $item['object'] = $menuItem; } // A menu-item that links to an url has to open in a new window if ($menuItem->entity == MenuItem::ENTITY_URL) { $item['linkOptions'] = ['target' => '_blank']; } if ($settings['subMenu'] == true) { // Get the item's children $children = $this->getTree(['parentId' => $menuItem->id, 'level' => $menuItem->level + 1, 'includeLanguage' => $settings['includeLanguage'], 'subMenu' => $settings['subMenu'], 'convertBr' => isset($settings['convertBr'])]); if ($children) { $item['items'] = $children; } } $items[$menuItem->id] = $item; } return $items; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = MenuItem::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'menu_id' => $this->menu_id, 'parent_id' => $this->parent_id, 'entity_id' => $this->entity_id, 'level' => $this->level, 'position' => $this->position, 'active' => $this->active, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'entity', $this->entity])->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'url', $this->url]); if (Yii::$app->getModule('menu')->enablePrivateMenuItems) { $query->andFilterWhere(['public' => $this->public]); } return $dataProvider; }
public function setEventHandlers() { /** * Update menuitem active state */ Event::on(Page::className(), Page::EVENT_BEFORE_ACTIVE, function ($event) { $menuItem = MenuItem::find()->where(['entity' => Page::className(), 'entity_id' => $event->sender->id])->one(); if ($menuItem) { $menuItem->active = $event->sender->active == 1 ? 0 : 1; $menuItem->save(); } }); // Set eventhandlers for the 'Menu' model Event::on(Page::className(), ActiveRecord::EVENT_BEFORE_DELETE, function ($event) { $menuItem = MenuItem::find()->where(['entity' => Page::className(), 'entity_id' => $event->sender->id])->one(); if ($menuItem) { throw new \yii\base\Exception(Yii::t('app', "Deze pagina is gekoppeld aan menu item '{$menuItem->name}', verwijder eerst het menu item")); } }); }
/** * Returns a recursive list of all parents of the item * * @param int|null The id of the item for which the parents have to be loaded. * When null is passed, the id of the loaded MenuItem instance is taken. */ public function getParents($id = null, $parents = []) { if ($id == null) { $item = $this; } else { $item = MenuItem::findOne($id); } if ($item->parent) { $parents[] = $item->parent; return $this->getParents($item->parent->id, $parents); } return $parents; }
} else { ?> <span class="glyphicon glyphicon-lock"></span> <?php } ?> </a> <?php } ?> <?php // Weburl ?> <a href="<?php echo Yii::getAlias('@baseUrl/') . MenuItem::findOne($item['item']->id)->getUrl(false, true); ?> " data-toggle="tooltip" target="_blank" title="<?php echo Yii::t('app', 'View'); ?> "> <span class="glyphicon glyphicon-globe"></span> </a> <?php // Manage page entity ?> <?php if ($item['item']->entity == MenuItem::ENTITY_PAGE) { ?> <a href="<?php
/** * Loads all the menu items that are (in)directly linked to the page */ protected function loadLinkedMenuItems() { // Load the menu-items to which the page is directly linked $menuItems = MenuItem::findAll(['entity' => $this->entity['type'], 'entity_id' => $this->entity['id'], 'active' => 1]); // Merge directly linked menu items $this->linkedMenuItems = ArrayHelper::merge($this->linkedMenuItems, ArrayHelper::index($menuItems, 'id')); // Add all parents of the directly linked items foreach ($menuItems as $menuItem) { $this->linkedMenuItems = ArrayHelper::merge($this->linkedMenuItems, ArrayHelper::index($menuItem->getParents(), 'id')); } $this->linkedMenuItemsIds = $this->linkedMenuItemsIds(); // Reverse the sorting order of the menu items $this->linkedMenuItems = array_reverse($this->linkedMenuItems); }
public function setEventHandlers() { // Set eventhandlers for the 'Menu' model Event::on(Menu::className(), ActiveRecord::EVENT_AFTER_DELETE, function ($event) { // Delete the children if (!$event->sender->deleteChildren()) { throw new \yii\base\Exception(Yii::t('app', 'There was an error while deleting this item')); } }); // Set eventhandlers for the 'MenuItem' model Event::on(MenuItem::className(), ActiveRecord::EVENT_AFTER_DELETE, function ($event) { // Delete the children if (!$event->sender->deleteChildren()) { throw new \yii\base\Exception(Yii::t('app', 'There was an error while deleting this item')); } }); }
/** * Updates the positions of the provided menu items * * @param array $items The menu items * @param infoweb\menu\models\MenuItem * @return boolean */ protected function updatePositions($items = [], $parent = null) { // Determine the parentId and level $parentId = $parent !== null ? $parent->id : 0; $level = $parent !== null ? $parent->level + 1 : 0; foreach ($items as $k => $item) { // Update the menu item $menuItem = MenuItem::findOne($item['id']); $menuItem->parent_id = $parentId; $menuItem->level = $level; $menuItem->position = $k + 1; if (!$menuItem->save()) { throw new \Exception("Error while saving menuItem #{$menuItem->id}"); } // Update the position of the item's children if (isset($item['children'])) { $this->updatePositions($item['children'], $menuItem); } } return true; }
/** * @return \yii\db\ActiveQuery */ public function getChildren() { return $this->hasMany(MenuItem::className(), ['menu_id' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getMenu() { return $this->hasOne(MenuItem::className(), ['id' => 'menu_item_id']); }
/** * Checks if a page is used in a menu * * @return boolean */ public function isUsedInMenu() { return (new \yii\db\Query())->select('id')->from(\infoweb\menu\models\MenuItem::tableName())->where(['entity' => \infoweb\menu\models\MenuItem::ENTITY_PAGE, 'entity_id' => $this->id])->exists(); }