/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Menu::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['position' => SORT_ASC]]]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
     $query->andFilterWhere(['like', 'name', $this->name]);
     return $dataProvider;
 }
 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'));
         }
     });
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getItems($options = [])
 {
     $items = parent::getItems()->where(['active' => 1]);
     // Filter by parent
     if (isset($options['parentId'])) {
         $items = $items->andWhere(['parent_id' => $options['parentId']]);
     }
     // Filter by level
     if (isset($options['level'])) {
         $items = $items->andWhere(['level' => $options['level']]);
     }
     // Only show public items to guest users if this options is enabled in the "menu" module
     if (Yii::$app->getModule('menu') && Yii::$app->getModule('menu')->enablePrivateMenuItems && Yii::$app->user->isGuest) {
         $items = $items->andWhere(['public' => 1]);
     }
     return $items->orderBy('position', 'ASC');
 }
 /**
  * Returns a tree of all items, grouped by menu, formatted for usage in a
  * Html::dropDownList widget
  *
  * @return  array
  */
 public function getAllForDropDownList($language = null)
 {
     $language = $language ?: Yii::$app->language;
     $items = [];
     foreach (Menu::find()->all() as $menu) {
         $items[$menu->name] = $menu->getAllForDropDownList(0, $language);
     }
     return $items;
 }
 protected function findActiveMenu()
 {
     // If no valid active menu-id is set, search the first menu and use it's id
     if (in_array(Yii::$app->session->get('menu-items.menu-id'), [0, null])) {
         $menu = Menu::find()->one();
         Yii::$app->session->set('menu-items.menu-id', $menu->id);
     } else {
         $menu = Menu::findone(Yii::$app->session->get('menu-items.menu-id'));
     }
     return $menu;
 }
 /**
  * Get all children by id
  *
  * @param int $id
  * @return array
  */
 protected static function findChildren($settings = [])
 {
     $default_settings = ['id' => 0, 'ids' => [], 'menu-id' => 0];
     $settings = array_merge($default_settings, $settings);
     if (!in_array($settings['id'], $settings['ids'])) {
         $settings['ids'][] = $settings['id'];
     }
     $query = new Query();
     $results = $query->select('id')->from(MenuItem::tableName())->where(['parent_id' => $settings['id'], 'menu_id' => $settings['menu-id']])->all();
     foreach ($results as $result) {
         $settings['ids'][] = $result['id'];
         $settings['ids'] = Menu::findChildren(['id' => $result['id'], 'ids' => $settings['ids'], 'menu-id' => $settings['menu-id']]);
     }
     return $settings['ids'];
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getMenu()
 {
     if (Yii::$app->getModule('pages')->enableMenu) {
         return $this->hasOne(\infoweb\menu\models\Menu::className(), ['id' => 'menu_id']);
     } else {
         return null;
     }
 }
 /**
  * Finds the Menu model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param string $id
  * @return Menu the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Menu::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException(Yii::t('app', 'The requested item does not exist'));
     }
 }
 /**
  * Returns an array of the default params that are passed to a view
  *
  * @param Page $model The model that has to be passed to the view
  * @return array
  */
 protected function getDefaultViewParams($model = null)
 {
     return ['model' => $model, 'templates' => $this->getTemplates(), 'sliders' => $this->module->enableSliders ? ArrayHelper::map(\infoweb\sliders\models\Slider::find()->select(['id', 'name'])->orderBy('name')->all(), 'id', 'name') : [], 'forms' => $this->module->enableForm ? ArrayHelper::map(\infoweb\form\models\form\FormLang::find()->select(['form_id', 'name'])->where(['=', 'language', Yii::$app->language])->orderBy('name')->all(), 'form_id', 'name') : [], 'menus' => $this->module->enableMenu ? ArrayHelper::map(\infoweb\menu\models\Menu::find()->select(['id', 'name'])->orderBy('name')->all(), 'id', 'name') : [], 'allowContentDuplication' => $this->module->allowContentDuplication];
 }