public function actionChangePredsedatelKomissii() { \Yii::$app->response->format = Response::FORMAT_JSON; $id = $_REQUEST['id']; $rabotnik = RabotnikAttestacionnojKomissii::findOne($id); $result = new JsResponse(); $rabotnik->predsedatel = !$rabotnik->predsedatel; $polzovatel = Polzovatel::find()->where(['fiz_lico' => $rabotnik->fiz_lico])->one(); if ($rabotnik->predsedatel) { if (!$polzovatel->isThereRol(Rol::RUKOVODITEL_ATTESTACIONNOJ_KOMISSII)) { $polzovatel->addRol(Rol::RUKOVODITEL_ATTESTACIONNOJ_KOMISSII); } //$polzovatel->roliAsArray = array_merge($polzovatel->roliAsArray,[Rol::RUKOVODITEL_ATTESTACIONNOJ_KOMISSII]); $current_predsedatel = RabotnikAttestacionnojKomissii::find()->where(['predsedatel' => true, 'attestacionnaya_komissiya' => $rabotnik->attestacionnaya_komissiya])->one(); $current_predsedatel_polzavatel = false; if ($current_predsedatel) { $current_predsedatel->predsedatel = false; $current_predsedatel_polzavatel = Polzovatel::find()->where(['fiz_lico' => $current_predsedatel->fiz_lico])->one(); $countOthersPredsedatel = RabotnikAttestacionnojKomissii::find()->where(['fiz_lico' => $current_predsedatel->fiz_lico])->andWhere(['!=', 'id', $current_predsedatel->id])->andWhere(['predsedatel' => true])->count(); if ($countOthersPredsedatel == 0) { $current_predsedatel_polzavatel->deleteRol(Rol::RUKOVODITEL_ATTESTACIONNOJ_KOMISSII); } } } else { $countOthersPredsedatel = RabotnikAttestacionnojKomissii::find()->where(['fiz_lico' => $rabotnik->fiz_lico])->andWhere(['!=', 'id', $rabotnik->id])->andWhere(['predsedatel' => true])->count(); if ($countOthersPredsedatel == 0) { $polzovatel->deleteRol(Rol::RUKOVODITEL_ATTESTACIONNOJ_KOMISSII); } } $is_error = false; $transaction = \Yii::$app->db->beginTransaction(); try { $rabotnik->save(false); $polzovatel->save(false); if ($rabotnik->predsedatel && $current_predsedatel) { $current_predsedatel->save(false); } if ($rabotnik->predsedatel && $current_predsedatel_polzavatel) { $current_predsedatel_polzavatel->save(false); } $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); $is_error = true; throw $e; } if ($is_error) { $result->type = JsResponse::ERROR; $result->msg = JsResponse::MSG_OPERATION_ERROR; } else { $result->msg = JsResponse::MSG_OPERATION_SUCCESS; $result->data = $rabotnik; } return $result; }