Esempio n. 1
0
 /**
  * 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]);
     // multilang
     $titleField = 'title_' . Yii::$app->language;
     $dataProvider->sort->attributes[$titleField] = ['asc' => ['title' => SORT_ASC], 'desc' => ['title' => SORT_DESC]];
     $query->joinWith(['items']);
     $query->andFilterWhere(['like', 'title', $this->{$titleField}]);
     $query->andFilterWhere(['like', 'locale', Yii::$app->language]);
     // multilang
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'menu_type_id' => $this->menu_type_id, 'parent_id' => $this->parent_id]);
     $query->andFilterWhere(['like', 'url', $this->url])->andFilterWhere(['like', 'status', $this->status]);
     return $dataProvider;
 }
Esempio n. 2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getMenuItems()
 {
     return $this->hasMany(MenuItem::className(), ['menu_type_id' => 'id']);
 }
 /**
  * Finds the MenuItem model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return MenuItem the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = MenuItem::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Esempio n. 4
0
$this->title = Yii::t('menu', 'Menu Items');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="menu-item-index">

    <p>
        <?php 
echo Html::a(Yii::t('menu', 'Create {modelClass}', ['modelClass' => 'Menu Item']), ['create'], ['class' => 'btn btn-success']);
?>
    </p>
    <?php 
\yii\widgets\Pjax::begin();
?>
    <?php 
echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], ['attribute' => 'title_' . Yii::$app->language, 'format' => 'text'], ['attribute' => 'menu_type_id', 'format' => 'html', 'value' => function ($model) {
    return '<span class="label label-success">' . MenuItem::getMenuTypes()[$model->menu_type_id] . '</span>';
}, 'filter' => Html::activeDropDownList($searchModel, 'menu_type_id', MenuItem::getMenuTypes(), ['class' => 'form-control', 'prompt' => Yii::t('backend', 'Menu type')])], 'parent_id', 'url:url', ['attribute' => 'status', 'format' => 'html', 'value' => function ($model) {
    if ($model->status == 1) {
        $class = 'label-success';
    } elseif ($model->status == 0) {
        $class = 'label-danger';
    }
    $status = $model->status == 1 ? 'on' : 'off';
    return '<span class="label ' . $class . '">' . $status . '</span>';
}, 'filter' => Html::activeDropDownList($searchModel, 'status', [0 => 'off', 1 => 'on'], ['class' => 'form-control', 'prompt' => Yii::t('backend', 'Status')])], ['class' => 'yii\\grid\\ActionColumn']]]);
?>
    <?php 
\yii\widgets\Pjax::end();
?>
</div>
Esempio n. 5
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getMenuItem()
 {
     return $this->hasOne(MenuItem::className(), ['id' => 'menu_item_id']);
 }
Esempio n. 6
0
 /**
  * @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;
 }