/** * Creates a new Participant model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate($meeting_id) { $mtg = new Meeting(); $title = $mtg->getMeetingTitle($meeting_id); $model = new Participant(); $model->meeting_id = $meeting_id; $model->invited_by = Yii::$app->user->getId(); // load friends for auto complete field $friends = Friend::getFriendList(Yii::$app->user->getId()); if ($model->load(Yii::$app->request->post())) { if (!User::find()->where(['email' => $model->email])->exists()) { // if email not already registered // create new user with temporary username & password $temp_email_arr[] = $model->email; $model->username = Inflector::slug(implode('-', $temp_email_arr)); $model->password = Yii::$app->security->generateRandomString(12); $model->participant_id = $model->addUser(); } else { // add participant from user record $usr = User::find()->where(['email' => $model->email])->one(); $model->participant_id = $usr->id; } // validate the form against model rules if ($model->validate()) { // all inputs are valid $model->save(); return $this->redirect(['/meeting/view', 'id' => $meeting_id]); } else { // validation failed return $this->render('create', ['model' => $model, 'title' => $title, 'friends' => $friends]); } } else { return $this->render('create', ['model' => $model, 'title' => $title, 'friends' => $friends]); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Friend::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'friend_id' => $this->friend_id, 'status' => $this->status, 'number_meetings' => $this->number_meetings, 'is_favorite' => $this->is_favorite, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); return $dataProvider; }
/** * Finds the Friend model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Friend the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Friend::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public static function getFriendList($user_id) { // load user's friends into email list array for autocomplete $friend_list = \frontend\models\Friend::find()->where(['user_id' => $user_id])->all(); $email_list = []; foreach ($friend_list as $x) { $email_list[] = $x->friend->email; } return $email_list; }