public function sendNotificationEmail(Group $group, User $user) { if ($user->verified == 1) { // Establish timestamp for notifications from membership data (when was an email sent for the last time?) $membership = \App\Membership::where('user_id', '=', $user->id)->where('group_id', "=", $group->id)->firstOrFail(); $last_notification = $membership->notified_at; // find unread discussions since timestamp $discussions = QueryHelper::getUnreadDiscussionsSince($user->id, $group->id, $membership->notified_at); // find new files since timestamp $files = \App\File::where('updated_at', '>', $membership->notified_at)->where('group_id', "=", $group->id)->get(); // find new members since timestamp $users = QueryHelper::getNewMembersSince($user->id, $group->id, $membership->notified_at); // find future actions until next 2 weeks, this is curently hardcoded... TODO use the mail sending interval to determine stop date $actions = \App\Action::where('start', '>', Carbon::now())->where('stop', '<', Carbon::now()->addWeek()->addWeek())->where('group_id', "=", $group->id)->orderBy('start')->get(); // we only trigger mail sending if a new action has been **created** since last notfication email. // BUT we will send actions for the next two weeks in all cases, IF a mail must be sent $actions_count = \App\Action::where('created_at', '>', $membership->notified_at)->where('group_id', "=", $group->id)->count(); // in all cases update timestamp $membership->notified_at = Carbon::now(); $membership->save(); // if we have anything, build the message and send // removed that : or count($users) > 0 // because we don't want to be notified just because there is a new member if (count($discussions) > 0 or count($files) > 0 or $actions_count > 0) { Mail::send('emails.notification', ['user' => $user, 'group' => $group, 'membership' => $membership, 'discussions' => $discussions, 'files' => $files, 'users' => $users, 'actions' => $actions, 'last_notification' => $last_notification], function ($message) use($user, $group) { $message->from(env('MAIL_NOREPLY', '*****@*****.**'), env('APP_NAME', 'Laravel'))->to($user->email)->subject('[' . env('APP_NAME') . '] ' . trans('messages.news_from_group_email_subject') . ' "' . $group->name . '"'); }); return true; } return false; } }
public function actionLogin($code) { $query = QueryHelper::createQuery('access_token', ['code' => $code]); $response = QueryHelper::doQuery($query); $user = User::findOrCreateUser($response['user_id']); $user->token = $response['access_token']; $user->save(); Yii::$app->user->login($user, 3600 * 24 * 30); return $this->redirect(['site/index']); }
/** * Execute the console command. * * @return mixed */ public function handle() { $notifications = QueryHelper::getNotificationsToSend(); if (count($notifications > 0)) { foreach ($notifications as $notification) { $user = \App\User::find($notification->user_id); $group = \App\Group::find($notification->group_id); if ($user && $group) { $this->info('Checking if there is something to send to user:'******' (' . $user->email . ')' . ' for group:' . $group->id . ' (' . $group->name . ')'); $mailer = new AppMailer(); if ($mailer->sendNotificationEmail($group, $user)) { $this->info('Message sent'); } else { $this->info('Nothing sent'); } } } } }
<?php /* @var $this yii\web\View */ use app\helpers\QueryHelper; $this->title = 'My Yii Application'; ?> <div class="site-index"> <div class="jumbotron"> <h1>Welcome!</h1> <p><a class="btn btn-lg btn-success" href="<?php echo QueryHelper::createQuery('authorize'); ?> ">Log in</a></p> </div> </div>