/** * 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; }
/** * Render level select html * * @param array $settings * @return string */ public static function level_select($settings = []) { $default_settings = ['active-only' => FALSE, 'parent' => 0, 'relations' => array(), 'menu-id' => 0, 'ancestor' => 0, 'active-ancestor' => 0, 'selected' => 0, 'active-menu-item-id' => 0]; $settings = array_merge($default_settings, $settings); $result = ''; $items = MenuItem::find()->where(['menu_id' => $settings['menu-id']])->orderby('position ASC')->all(); foreach ($items as $item) { if ($settings['parent'] == $item->parent_id) { ob_start(); ?> <option value="<?php echo $item->id; ?> " <?php if ($settings['selected'] != 0 && $settings['selected'] == $item->id) { ?> selected="selected"<?php } ?> <?php if (in_array($item->id, Menu::findChildren(['id' => $settings['active-menu-item-id'], 'menu-id' => $settings['menu-id']]))) { ?> disabled="disabled" <?php } ?> > <?php echo str_repeat("-", 2 * $item->level); if ($item->level != 0) { ?> > <?php } echo $item->name; ?> </option> <?php $result .= ob_get_clean(); if (Menu::has_children($item->id)) { $result .= Menu::level_select(array('active-only' => $settings['active-only'], 'parent' => $item->id, 'relations' => $settings['relations'], 'menu-id' => $settings['menu-id'], 'ancestor' => $item->parent_id, 'active-ancestor' => $settings['active-ancestor'], 'selected' => $settings['selected'], 'active-menu-item-id' => $settings['active-menu-item-id'])); } } } return $result; }
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")); } }); }