/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Composition::find();
     $compositionTable = Composition::tableName();
     $teamTable = Team::tableName();
     $query->joinWith(['team' => function ($query) use($teamTable) {
         $query->from(['team' => $teamTable]);
     }]);
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 30]]);
     // enable sorting for the related columns
     $addSortAttributes = ["team.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(["{$compositionTable}.id" => $this->id, 'match_id' => $this->match_id, 'contract_id' => $this->contract_id, 'is_substitution' => $this->is_substitution, 'is_basis' => $this->is_basis, 'number' => $this->number, 'is_captain' => $this->is_captain, 'command_id' => $this->command_id]);
     $query->andFilterWhere(['like', 'contract_type', $this->contract_type])->andFilterWhere(['like', 'team.name', $this->getAttribute('team.name')]);
     return $dataProvider;
 }
 /**
  * Vote for selected float answer
  * @param $matchID int
  * @return mixed Json
  */
 public function actionAutogen($matchID)
 {
     $match = \common\models\Match::findOne($matchID);
     if (!isset($match)) {
         throw new NotFoundHttpException('Страница не найдена.');
     }
     if (in_array($match->teamHome->id, \common\models\Team::getTeamsConstants())) {
         $ourTeam = $match->teamHome;
         $opponentTeam = $match->teamGuest;
     } else {
         $ourTeam = $match->teamGuest;
         $opponentTeam = $match->teamHome;
     }
     $basisPlayers = \common\models\Composition::find()->where(['match_id' => $match->id, 'command_id' => $ourTeam->id, 'is_basis' => 1])->all();
     $compositionTable = \common\models\Composition::tableName();
     $matchEventTable = \common\models\MatchEvent::tableName();
     $substitutionPlayers = \common\models\Composition::find()->innerJoin($matchEventTable, "{$matchEventTable}.substitution_id = {$compositionTable}.id")->where([$compositionTable . '.match_id' => $match->id, 'command_id' => $ourTeam->id])->all();
     $teamPlayers = array_merge($basisPlayers, $substitutionPlayers);
     $question = new Question();
     $question->title = 'Оценки игрокам ' . $ourTeam->name . ' в матче с ' . $opponentTeam->name;
     $question->voutes = 0;
     $question->is_active = 1;
     $question->is_float = 1;
     $question->is_multipart = 0;
     $question->mark = 0;
     if ($question->save()) {
         foreach ($teamPlayers as $teamPlayer) {
             $answer = new Question();
             $answer->parent_id = $question->id;
             $answer->title = $teamPlayer->name;
             $answer->mark = 0;
             $answer->save();
         }
     }
     return $this->redirect(['view', 'id' => $question->id]);
 }
 /**
  * Updates list of players.
  * @return mixed
  */
 public function actionUpdateList()
 {
     $list = Yii::$app->request->post('list');
     $teamId = Yii::$app->request->post('teamId');
     $matchId = Yii::$app->request->post('matchId');
     if (isset($list) && $teamId && $matchId) {
         $list = explode(';', $list);
         $composition = (new \yii\db\Query())->select(['contract_id'])->from(Composition::tableName())->where(['match_id' => $matchId, 'command_id' => $teamId])->all();
         $contractIds = [];
         foreach ($composition as $data) {
             $contractIds[] = $data['contract_id'];
         }
         // Remove
         $removeList = [];
         foreach ($contractIds as $id) {
             if (is_numeric($id) && !in_array($id, $list)) {
                 $removeList[] = $id;
             }
         }
         if (count($removeList) > 0) {
             Composition::deleteAll(['match_id' => $matchId, 'command_id' => $teamId, 'contract_id' => $removeList]);
         }
         // Add
         $contractTeams = Team::getContractTeams();
         if (in_array($teamId, $contractTeams)) {
             $contractType = Composition::CONTRACT_TYPE;
             $contractModel = new Contract();
         } else {
             $contractType = Composition::MEMBERSHIP_TYPE;
             $contractModel = new Membership();
         }
         $addList = [];
         foreach ($list as $id) {
             if (is_numeric($id) && !in_array($id, $contractIds)) {
                 // Add
                 $addList[] = $id;
                 $contract = $contractModel::findOne($id);
                 $model = new Composition();
                 $model->contract_type = $contractType;
                 $model->contract_id = $id;
                 $model->command_id = $teamId;
                 $model->match_id = $matchId;
                 $model->is_basis = 1;
                 $model->is_substitution = 0;
                 $model->is_captain = 0;
                 if (isset($contract)) {
                     $model->number = $contract->number;
                     $model->amplua_id = $contract->amplua_id;
                 }
                 $model->save();
             }
         }
         $out = ['success' => 'true', 'removeList' => $removeList, 'addList' => $addList];
     } else {
         $out = ['success' => 'false', 'list' => $list, 'teamId' => $teamId, 'match' => $matchId];
     }
     return Json::encode($out);
 }