/** * Finds the WidgetMenu model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return WidgetMenu the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = WidgetMenu::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Creates data provider instance with search query applied * @return ActiveDataProvider */ public function search($params) { $query = WidgetMenu::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'status' => $this->status]); $query->andFilterWhere(['like', 'key', $this->key])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'items', $this->items]); return $dataProvider; }
public function init() { $cacheKey = [WidgetMenu::className(), $this->key]; $this->items = Yii::$app->cache->get($cacheKey); if ($this->items === false) { if (!($model = WidgetMenu::findOne(['key' => $this->key, 'status' => WidgetMenu::STATUS_ACTIVE]))) { throw new InvalidConfigException(); } $this->items = json_decode($model->items, true); Yii::$app->cache->set($cacheKey, $this->items, 60 * 60 * 24); } }
/** * Create new menu and menu items. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new WidgetMenu(); if ($model->load(Yii::$app->request->post()) && $model->save() && $model->setItems(Yii::$app->request->post($model->formName()))) { return $this->redirect(['update', 'id' => $model->id]); } return $this->render('create', ['model' => $model->loadDefaultValues()]); }
public static function getMenuLinks($key, $additionalLinks) { if (!($model = WidgetMenu::findOne(['key' => $key, 'status' => WidgetMenu::STATUS_ACTIVE]))) { throw new EmptyMenuItemsException(); } $links = array(); foreach ($model->items as $item) { if (WidgetMenuItem::STATUS_ACTIVE == $item->active && !empty($item->name) && $item->url) { $links[] = ['label' => $item->name, 'url' => [$item->url]]; } } if (!empty($additionalLinks) && is_array($additionalLinks)) { $links = array_merge($links, $additionalLinks); } return $links; }
<?php use yii\bootstrap\Nav; use yii\bootstrap\NavBar; /* @var $this \yii\web\View */ /* @var $content string */ $this->beginContent('@frontend/views/layouts/_clear.php'); ?> <div class="wrap"> <?php NavBar::begin(['brandLabel' => Yii::$app->name, 'brandUrl' => Yii::$app->homeUrl, 'options' => ['class' => 'navbar-inverse navbar-fixed-top']]); ?> <?php echo Nav::widget(['options' => ['class' => 'navbar-nav navbar-right'], 'items' => \common\models\WidgetMenu::getMenuLinks('main', [['label' => Yii::$app->user->isGuest ? '' : Yii::$app->user->identity->getPublicIdentity(), 'visible' => !Yii::$app->user->isGuest, 'items' => [['label' => Yii::t('frontend', 'Settings'), 'url' => ['/user/default/index']], ['label' => Yii::t('frontend', 'Backend'), 'url' => Yii::getAlias('@backendUrl'), 'visible' => Yii::$app->user->can('manager')], ['label' => Yii::t('frontend', 'Logout'), 'url' => ['/user/sign-in/logout'], 'linkOptions' => ['data-method' => 'post']]]], ['label' => Yii::t('frontend', 'Language'), 'items' => array_map(function ($code) { return ['label' => \common\models\Languages::getLanguages()[$code], 'url' => ['/site/set-locale', 'locale' => $code], 'active' => Yii::$app->language === $code]; }, array_keys(\common\models\Languages::getLanguages()))]])]); ?> <?php NavBar::end(); ?> <?php echo $content; ?> </div> <footer class="footer"> <div class="container"> <p class="pull-left">© My Company <?php echo date('Y');