public function actionDetails($id)
 {
     $user = Yii::$app->user;
     Yii::$app->user->returnUrl = Yii::$app->request->url;
     $tournament = Tournaments::findOne($id);
     if ($tournament === null) {
         throw new NotFoundHttpException('Запрашиваемая страница не найдена.');
     }
     $teamParticipants = new ArrayDataProvider(['allModels' => Result::getStandings($id), 'pagination' => false]);
     //games list prep
     if (Yii::$app->request->post('tours')) {
         $tours = Yii::$app->request->post('tours');
     } else {
         $tours = range(1, $tournament->num_tours, 1);
     }
     for ($i = 1; $i <= $tournament->num_tours; $i++) {
         $tour_list[$i] = "{$i} тур";
     }
     $forecasters = new ActiveDataProvider(['query' => UsersTournaments::find()->forecastersStandings($id), 'pagination' => false]);
     //if guest or not participating in the tournament
     if ($user->isGuest || !$user->identity->isUserParticipatesInTournament($id)) {
         $tourGames = Games::getGamesGroupedByTour($id, $tours);
         $viewFile = 'tournamentDetailsGuest';
         $winners = [];
         $forecastedStandings = [];
     } else {
         $forecastedStandings = new ArrayDataProvider(['allModels' => Result::getForecastedStandings($id, $user->id), 'pagination' => false]);
         $tourGames = Games::getGamesGroupedByTourWithForecast($id, $tours, $user->id);
         $viewFile = 'tournamentDetailsUser';
         //preparing tournament winners forecast form
         $winners = new TopTeamsForm($user->id, $id);
         if ($winners->load(Yii::$app->request->post()) && $winners->validate()) {
             if (time() < $tournament->wfDueTo) {
                 $winners->edit();
                 Yii::$app->getSession()->setFlash('success', 'Прогноз на призеров турнира успешно сохранен');
             } else {
                 Yii::$app->getSession()->setFlash('success', 'Не нужно пытаться обмануть :)');
             }
             return $this->refresh();
         }
     }
     $data = compact('tournament', 'teamParticipants', 'forecasters', 'tour_list', 'tourGames', 'winners', 'forecastedStandings');
     if ($tournament->is_active == Tournaments::FINISHED) {
         $data['additionalPoints'] = implode('</br>', Top3TeamsForecast::getClarifications($user->id, $id));
         $userTournamentsModel = UsersTournaments::find()->where(['id_tournament' => $id])->andWhere(['id_user' => $user->id])->with('winnersForecast')->one();
         if (!empty($userTournamentsModel)) {
             $data['totalAdditionalPoints'] = $userTournamentsModel->calculateAdditionalPoints();
         }
     } else {
         $data['additionalPoints'] = '';
         $data['totalAdditionalPoints'] = '';
     }
     return $this->render($viewFile, $data);
 }
 public function actionIndex($tournament)
 {
     Yii::$app->user->returnUrl = Yii::$app->request->url;
     $curr_tournament = Tournaments::findOne($tournament);
     if (Yii::$app->request->post('tours')) {
         $tours = Yii::$app->request->post('tours');
     } else {
         $tours = range(1, $curr_tournament->num_tours, 1);
     }
     $tourGames = Games::getGamesGroupedByTour($tournament, $tours);
     for ($i = 1; $i <= $curr_tournament->num_tours; $i++) {
         $tour_list[$i] = "{$i} тур";
     }
     //preparing model for creating a game
     $newGame = new Games();
     $participants = TeamTournaments::getTournamentParticipantsTeams($tournament);
     //model for uploading games from Excel
     $file = new GamesUploadForm();
     if (Yii::$app->request->post('Games')) {
         $updatedGames = Yii::$app->request->post('Games');
         foreach ($updatedGames as $k => $game) {
             $models[$k] = $this->findModel($k);
         }
         if (Games::loadMultiple($models, Yii::$app->request->post())) {
             $error = '';
             foreach ($models as $model) {
                 if (!$model->save()) {
                     $error = $error . 'Ошибка сохранения игры ' . $model->competitors . "<br>";
                 }
             }
             if ($error === '') {
                 Yii::$app->session->setFlash('status', 'Записи сохранены успешно');
             } else {
                 Yii::$app->session->setFlash('error', $error);
             }
             return $this->goBack();
         }
     }
     return $this->render('index2', compact('tourGames', 'curr_tournament', 'tour_list', 'newGame', 'participants', 'file'));
 }