/** * @return array */ public function getMenuTypes() { $menuTypes = []; foreach (\fonclub\menu\models\MenuType::find()->all() as $type) { $menuTypes[$type->id] = $type->name; } return $menuTypes; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = MenuType::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
/** * Finds the MenuType model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return MenuType the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = MenuType::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getMenuType() { return $this->hasOne(MenuType::className(), ['id' => 'menu_type_id']); }
/** * @param null $parent_id * @param string $menuTypeName * @return array */ public function getTree($parent_id = null, $menuTypeName = 'main') { $items = []; $titleFiled = 'title_' . Yii::$app->language; $model = MenuItem::find()->where(['status' => '1', 'parent_id' => $parent_id]); // Filter by parent if (isset($menuType)) { $menuType = MenuType::findOne(['like', 'name', '%' . $menuTypeName . '%']); $model = $model->andWhere(['menu_type_id' => $menuType->id]); } $menuItems = $model->orderBy(['sort' => 'DESC'])->all(); foreach ($menuItems as $menuItem) { $url = $this->checkParams($menuItem->url); if ($menuItem->visible == 'all') { $visible = true; } elseif ($menuItem->visible == 'notuser') { $visible = Yii::$app->user->isGuest; } else { $visible = Yii::$app->user->can($menuItem->visible); } $item = ['label' => $menuItem->{$titleFiled}, 'url' => is_array($url) ? Yii::$app->urlManager->createUrl($url) : $url, 'visible' => $visible, 'linkOptions' => $menuItem->data_method == 'post' ? ['data-method' => 'post'] : []]; // Get the item's children $children = $this->getTree($menuItem->id, $menuTypeName); if ($children) { $item['items'] = $children; } $items[] = $item; } return $items; }