/** * 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); }