public function checkValue($event) { $homepage = $this->owner->homepage; // Not set as homepage if ($homepage == 0) { // If no other pages are set as homepage, the owner is automatically // set as homepage $homepageExists = (bool) Page::find()->where(['homepage' => 1])->count(); if (!$homepageExists) { $this->owner->homepage = 1; // The homepage is always a public page $this->owner->public = 1; } } else { // The flag for the current homepage has to be unset only if the // 'homepage' attribute of the owner changed (meaning that the // owner was not already the current homepage) if (in_array('homepage', array_keys($this->owner->getDirtyAttributes()))) { $currentHomepage = Page::findOne(['homepage' => 1]); if ($currentHomepage) { $currentHomepage->homepage = 0; $currentHomepage->save(); } } // The homepage is always a public page if ($this->owner->public != 1) { $this->owner->public = 1; } } }
/** * Creates data provider instance with search query applied * @return ActiveDataProvider */ public function search($params) { $query = Page::find(); $query->andFilterWhere(['language' => Yii::$app->language]); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['name' => SORT_ASC]], 'pagination' => ['pageSize' => 50]]); // Join the entity model as a relation $query->joinWith(['translations']); // enable sorting for the related column $dataProvider->sort->attributes['name'] = ['asc' => ['name' => SORT_ASC], 'desc' => ['name' => SORT_DESC]]; if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'active' => $this->active, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); if (Yii::$app->getModule('pages')->enablePrivatePages) { $query->andFilterWhere(['public' => $this->public]); } $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }