/** * Creates a new Composition model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate($matchId = null, $teamId = null) { $model = new Composition(); if (!isset($matchId) || !isset($teamId)) { throw new \yii\web\BadRequestHttpException('Unidentified matchId and teamId'); } $match = Match::findOne($matchId); $team = Team::findOne($teamId); if (!isset($match) || !isset($team)) { throw new \yii\web\BadRequestHttpException('Unidentified match and team models'); } $model->command_id = $teamId; $model->match_id = $matchId; $model->is_basis = 1; $contractTeams = Team::getContractTeams(); if (in_array($teamId, $contractTeams)) { $model->contract_type = Composition::CONTRACT_TYPE; $contractModel = new Contract(); $contractModel->season_id = $match->season_id; $contractModel->is_active = 1; } else { $model->contract_type = Composition::MEMBERSHIP_TYPE; $contractModel = new Membership(); } $contractModel->command_id = $teamId; if ($model->load(Yii::$app->request->post()) && $contractModel->load(Yii::$app->request->post()) && $contractModel->validate()) { if ($contractModel->save(false)) { $model->contract_id = $contractModel->id; $model->number = $contractModel->number; $model->save(); } if (Yii::$app->request->isAjax) { $out = ['success' => 'true']; return Json::encode($out); } return $this->redirect(['view', 'id' => $model->id]); } else { if (Yii::$app->request->isAjax) { return $this->renderAjax('create', ['model' => $model, 'contractModel' => $contractModel]); } return $this->render('create', ['model' => $model, 'contractModel' => $contractModel]); } }