/** * Finds the Calendar model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Calendar the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Calendar::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function getCalendar() { if (self::isFamilyLogin()) { throw new \yii\base\Exception("Este utilizador é familiar não tem calendário."); } //Embora no futuro possam existir mais do que um //neste momento só existe um return Calendar::findOne(['id' => 'calendar_id']); }
public static function joinMemberToFamilyAndSave($model, $user_id, $family_id) { if (!$user_id || !$family_id) { return 'Não foi fornecido um parametro necessário.'; } $user = \app\models\User::findIdentity($user_id); if (!$user) { return 'O utilizador ainda não tem registo no memoboard.'; } //O utilizador não pode pertencer a nenhuma familia ou ser membro if ($user->family_id) { return 'Este utilizador já pertence a uma família.'; } $family = \app\models\Family::find()->where(['id' => $family_id])->one(); if (!$family) { return 'A familia não exste.'; } if (!$family->accepts_members) { return 'A familia ' . $family->name . ' não está a aceitar novos membros.'; } //Se já existir um user não pode associar outros if ($model->user_id) { return 'Não pode alterar membros da família por outros.'; } //Cria membro $model->user_type = \app\enum\UserType::Utilizador_registado; $model->family_id = $family->id; $model->user_id = $user->id; if (!$model->validate()) { return 'Dados não válidos:<br>' . implode('<br>', $model->errors); } $transaction = Yii::$app->db->beginTransaction(); if (!$model->save()) { $transaction->rollBack(); return 'Não foi possível salvar os dados.'; } //Cria um calendário para oo membro se não existir $my_cal = \app\models\Calendar::findOne(['member_id' => $model->id]); if (!$my_cal) { $my_cal = new \app\models\Calendar(); $my_cal->member_id = $model->id; $my_cal->name = $model->name; if (!$my_cal->save()) { $transaction->rollBack(); return 'Não foi possível salvar o calendário.'; } } //Coloca valores no utilizador $user->family_id = $family->id; $user->family_member_id = $model->id; $user->calendar_id = $my_cal->id; if (!$user->save(['family_id', 'family_member_id', 'calendar_id'])) { $transaction->rollBack(); return 'Não foi possível atualizar o utilizador.'; } $transaction->commit(); //Coloca nas prefeencias do utilizador o calendário //para mostrar imediatamente $user->setPreference('tasks_selected', $model->id); $user->setPreference('calendars_selected', $model->id); return true; }