/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Tournament::find(); $tournamentTable = Tournament::tableName(); $teamTable = Team::tableName(); $leagueTable = League::tableName(); $championshipTable = Championship::tableName(); $seasonTable = Season::tableName(); $query->joinWith(['team' => function ($query) use($teamTable) { $query->from(['team' => $teamTable]); }]); $query->joinWith(['league' => function ($query) use($leagueTable) { $query->from(['league' => $leagueTable]); }]); $query->joinWith(['championship' => function ($query) use($championshipTable) { $query->from(['championship' => $championshipTable]); }]); $query->joinWith(['season' => function ($query) use($seasonTable) { $query->from(['season' => $seasonTable]); }]); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 20], 'sort' => ['defaultOrder' => ['points' => SORT_DESC]]]); // enable sorting for the related columns $addSortAttributes = ["team.name", "league.name", "championship.name", "season.name"]; foreach ($addSortAttributes as $addSortAttribute) { $dataProvider->sort->attributes[$addSortAttribute] = ['asc' => [$addSortAttribute => SORT_ASC], 'desc' => [$addSortAttribute => SORT_DESC]]; } if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(["{$tournamentTable}id" => $this->id, 'command_id' => $this->command_id, 'championship_id' => $this->championship_id, 'season_id' => $this->season_id, 'played' => $this->played, 'won' => $this->won, 'draw' => $this->draw, 'lost' => $this->lost, 'goals_for' => $this->goals_for, 'goals_against' => $this->goals_against, 'points' => $this->points, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'fair_play' => $this->fair_play, 'league_id' => $this->league_id]); $query->andFilterWhere(['like', 'team.name', $this->getAttribute('team.name')]); return $dataProvider; }
/** * Url: /tournament * @return mixed */ public function actionTournament() { $tournamentTable = Tournament::tableName(); $championshipTable = Championship::tableName(); // championship type select $championships = Championship::find()->innerJoin($tournamentTable, "{$tournamentTable}.championship_id = {$championshipTable}.id")->orderBy(['id' => SORT_DESC])->all(); $firstChampionshipObj = array_values($championships)[0]; $firstChampionshipId = $firstChampionshipObj->id; $activeChampionship = $firstChampionshipId; if (isset($_GET['championship'])) { foreach ($championships as $championship) { if ($_GET['championship'] == $championship->id) { $activeChampionship = $_GET['championship']; } } } $championshipsData = []; foreach ($championships as $championship) { $championshipsData[$championship->id] = ['value' => $championship->id, 'text' => $championship->name, 'active' => false]; } $championshipsData[$activeChampionship]['active'] = true; foreach ($championshipsData as $key => $championship) { $championshipsData[$key] = (object) $championship; } // season select $seasons = Season::find()->innerJoinWith('tournaments')->orderBy(['id' => SORT_DESC])->all(); $firstSeasonObj = array_values($seasons)[0]; $firstSeasonId = $firstSeasonObj->id; $activeSeason = $firstSeasonId; if (isset($_GET['season'])) { foreach ($seasons as $season) { if ($_GET['season'] == $season->id) { $activeSeason = $_GET['season']; } } } $seasonsData = []; foreach ($seasons as $season) { $seasonName = $season->name; $seasonsData[$season->id] = ['value' => $season->id, 'text' => $seasonName, 'active' => false]; } $seasonsData[$activeSeason]['active'] = true; foreach ($seasonsData as $key => $season) { $seasonsData[$key] = (object) $season; } $tournamentData = Tournament::find()->where(['season_id' => $activeSeason, 'championship_id' => $activeChampionship])->orderBy(['points' => SORT_DESC])->all(); $tournamentData = Tournament::sort($tournamentData); $forwards = Forward::find()->where(['season_id' => $activeSeason])->orderBy(['goals' => SORT_DESC, 'penalty' => SORT_DESC])->all(); $options = ['templateType' => 'col2', 'title' => 'Dynamomania.com | Турнирная таблица', 'columnFirst' => ['tournament' => ['view' => '@frontend/views/tournament/tournament_full', 'data' => compact('tournamentData', 'championshipsData', 'seasonsData')]], 'columnSecond' => ['short_news' => SiteBlock::getshortNews(50)]]; if (count($forwards) > 0) { $options['columnFirst']['forwards'] = ['view' => '@frontend/views/tournament/forwards', 'data' => compact('forwards')]; } return $this->render('@frontend/views/site/index', $options); }