public function afterSave($insert, $changedAttributes) { //$oldCategorias = array("0"=>1); $actualAssignments = []; if (($actualAssignments = AuthAssignment::find()->andWhere("user_id = {$this->id}")->asArray()->all()) !== null) { $actualAssignments = ArrayHelper::getColumn($actualAssignments, 'item_name'); } //verifica se é um array $this->assignmentIds = is_array($this->assignmentIds) ? $this->assignmentIds : [$this->assignmentIds]; $inserirAssignments = array_diff($this->assignmentIds, $actualAssignments); if (!empty($inserirAssignments)) { //save the relations foreach ($inserirAssignments as $id) { $r = new AuthAssignment(); $r->user_id = $this->id; $r->item_name = $id; $r->save(); } } $delAssignments = array_diff($actualAssignments, $this->assignmentIds); if (!empty($delAssignments)) { foreach ($delAssignments as $remove) { $r = AuthAssignment::findOne(['item_name' => $remove, 'user_id' => $this->id]); $r->delete(); } } parent::afterSave($insert, $changedAttributes); //don't forget this }
/** * Returns "league" role of user, from roles attributiion. Default is golfer. Null if not loggued in. */ public static function getRole() { if (!Yii::$app->user->isGuest) { if ($role = AuthAssignment::findOne(['user_id' => Yii::$app->user->identity->id])) { if ($key = array_search($role->item_name, Yii::$app->params['league_roles'])) { return $key; } } return self::DEFAULT_ROLE; } return null; }
/** * Finds the AuthAssignment model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return AuthAssignment the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = AuthAssignment::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Finds the AuthAssignment model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $item_name * @param string $user_id * @return AuthAssignment the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($item_name, $user_id) { if (($model = AuthAssignment::findOne(['item_name' => $item_name, 'user_id' => $user_id])) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionCheckApplication($id) { //$model = $this->findModel($id); $query = Application::find(); $query->joinWith(['user', 'reasonApplication']); $query->where(['application.id' => $id]); $model = $query->one(); if ($model === null) { throw new NotFoundHttpException('The letter has been deleted.'); } if ($model->load(Yii::$app->request->post())) { // Gui mail bao cho manager biet co dua xin nghi. $link = Html::a('Click me!', Yii::$app->urlManager->createAbsoluteUrl(['application/view', 'id' => $model->id])); $user_manager = UserInfo::findOne(['user_id' => $model->user->manager]); if (Yii::$app->user->can('manager')) { if ($model->manager_ok == 0) { $title = "Manager Refuse: "; } if ($model->manager_ok == 1) { $model->manager_id_ok = Yii::$app->user->id; $model->hrm_ok = 1; $title = "Manager Accept: "; } } if (Yii::$app->user->can('hrm')) { if ($model->hrm_ok == 0) { $title = "HRM Refuse: "; } if ($model->hrm_ok == 1) { $model->hrm_id_ok = Yii::$app->user->id; $title = "HRM Accept: "; } } $body = $this->renderPartial('email_result', ['model' => $model, 'link' => $link, 'user_manager' => $user_manager]); if ($model->manager_ok == 1 && Yii::$app->user->can('manager')) { //Gửi mail báo cho quản lý nhân sự $link2 = Html::a('Click me!', Yii::$app->urlManager->createAbsoluteUrl(['application/check-application', 'id' => $model->id])); $body = $this->renderPartial('email_result', ['model' => $model, 'link' => $link2, 'user_manager' => $user_manager]); $hrm_id = AuthAssignment::findOne(['item_name' => 'hrm'])->user_id; $user_hrm = UserInfo::findOne(['user_id' => $hrm_id]); $this->sendMail($user_hrm->email, $user_manager->email, $user_manager->full_name, $title . $model->reasonApplication->name, $body); } if ($model->manager_ok == 0 || $model->hrm_ok == 0) { $model->save(false); if ($this->sendMail($model->user->email, $user_manager->email, $user_manager->full_name, $title . $model->reasonApplication->name, $body)) { Yii::$app->session->setFlash('success', 'Your message has been sent to the staff !'); } else { Yii::$app->session->setFlash('error', 'Unable to send a letter to the staff. Please call the staff. Staff\'s phone: .' . $user_manager->phone); } return $this->redirect(['application/application-of-room', 'id' => Yii::$app->user->id]); } else { if ($model->save() && $user_manager !== null && ($model->manager_ok != -1 || $model->hrm_ok != -1)) { if ($this->sendMail($model->user->email, $user_manager->email, $user_manager->full_name, $title . $model->reasonApplication->name, $body)) { Yii::$app->session->setFlash('success', 'Your message has been sent to the staff !'); } else { Yii::$app->session->setFlash('error', 'Unable to send a letter to the staff. Please call the staff. Staff\'s phone: .' . $user_manager->phone); } return $this->redirect(['application/application-of-room', 'id' => Yii::$app->user->id]); } } } else { //Thiet lap truong da doc don cua nhan vien if (Yii::$app->user->can('hrm')) { $model->hrm_readed = 1; } if (Yii::$app->user->can('manager')) { $model->manager_readed = 1; } if (!$model->save(true, ['hrm_readed', 'manager_readed'])) { throw new NotFoundHttpException('The Error in saving status readed of manager and hrm.'); } } return $this->render('check-application', ['model' => $model]); }
/** * Finds the AuthAssignment model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $item_name * @param string $user_id * @return AuthAssignment the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($user_id) { if (($model = AuthAssignment::findOne(['user_id' => $user_id])) !== null) { return $model; } else { return new AuthAssignment(); } }