public function afterCreate() { if ($this->id > 0) { $activity = new Activities(); $activity->users_id = $this->users_id; $activity->posts_id = $this->posts_id; $activity->type = Activities::NEW_REPLY; $activity->save(); $toNotify = array(); /** * Notify users that always want notifications */ foreach (Users::find(array('notifications = "Y"', 'columns' => 'id')) as $user) { if ($this->users_id != $user->id) { $notification = new Notifications(); $notification->users_id = $user->id; $notification->posts_id = $this->posts_id; $notification->posts_replies_id = $this->id; $notification->type = 'C'; $notification->save(); $activity = new ActivityNotifications(); $activity->users_id = $user->id; $activity->posts_id = $this->posts_id; $activity->posts_replies_id = $this->id; $activity->users_origin_id = $this->users_id; $activity->type = 'C'; $activity->save(); $toNotify[$user->id] = $notification->id; } } /** * Register users subscribed to the post */ foreach (PostsSubscribers::findByPostsId($this->posts_id) as $subscriber) { if (!isset($toNotify[$subscriber->users_id])) { $notification = new Notifications(); $notification->users_id = $subscriber->users_id; $notification->posts_id = $this->posts_id; $notification->posts_replies_id = $this->id; $notification->type = 'C'; $notification->save(); $activity = new ActivityNotifications(); $activity->users_id = $subscriber->users_id; $activity->posts_id = $this->posts_id; $activity->posts_replies_id = $this->id; $activity->users_origin_id = $this->users_id; $activity->type = 'C'; $activity->save(); $toNotify[$subscriber->users_id] = $notification->id; } } /** * Register the user in the post's notifications */ if (!isset($toNotify[$this->users_id])) { $parameters = array('users_id = ?0 AND posts_id = ?1', 'bind' => array($this->users_id, $this->posts_id)); $hasNotifications = PostsNotifications::count($parameters); if (!$hasNotifications) { $notification = new PostsNotifications(); $notification->users_id = $this->users_id; $notification->posts_id = $this->posts_id; $notification->save(); } } /** * Notify users that have commented in the same post */ $postsNotifications = PostsNotifications::findByPostsId($this->posts_id); foreach ($postsNotifications as $postNotification) { if (!isset($toNotify[$postNotification->users_id])) { if ($postNotification->users_id != $this->users_id) { /** * Generate an e-mail notification */ $notification = new Notifications(); $notification->users_id = $postNotification->users_id; $notification->posts_id = $this->posts_id; $notification->posts_replies_id = $this->id; $notification->type = 'C'; $notification->save(); $activity = new ActivityNotifications(); $activity->users_id = $postNotification->users_id; $activity->posts_id = $this->posts_id; $activity->posts_replies_id = $this->id; $activity->users_origin_id = $this->users_id; $activity->type = 'C'; $activity->save(); $toNotify[$postNotification->users_id] = $notification->id; } } } /** * Queue notifications to be sent */ $this->getDI()->getQueue()->put($toNotify); } }
public function afterCreate() { /** * Register a new activity */ if ($this->id > 0) { /** * Register the activity */ $activity = new Activities(); $activity->users_id = $this->users_id; $activity->posts_id = $this->id; $activity->type = Activities::NEW_POST; $activity->save(); /** * Notify users that always want notifications */ $notification = new PostsNotifications(); $notification->users_id = $this->users_id; $notification->posts_id = $this->id; $notification->save(); /** * Notify users that always want notifications */ $toNotify = []; foreach (Users::find(['notifications = "Y"', 'columns' => 'id']) as $user) { if ($this->users_id != $user->id) { $notification = new Notifications(); $notification->users_id = $user->id; $notification->posts_id = $this->id; $notification->type = 'P'; $notification->save(); $toNotify[$user->id] = $notification->id; } } /** * Update the total of posts related to a category */ $this->category->number_posts++; $this->category->save(); /** * Queue notifications to be sent */ $this->getDI()->getQueue()->put($toNotify); } }
public function send(Notifications $notification) { if ($notification->sent == 'Y') { return; } $post = $notification->post; $user = $notification->user; if ($notification->type != 'P') { $reply = $notification->reply; } else { $reply = true; } $from = $this->config->mail->fromEmail; $url = $this->config->site->url; if ($post && $user && $reply) { if ($user->email && $user->notifications != 'N' && strpos($user->email, '@users.noreply.github.com') === false) { try { $message = new \Swift_Message('[Phalcon Forum] ' . $post->title); $message->setTo(array($user->email => $user->name)); $message->addReplyTo('reply-i' . $post->id . '-' . time() . '@phosphorum.com'); if ($notification->type == 'P') { $originalContent = $post->content; $htmlContent = $this->markdown->render($post->content); $message->setFrom(array($from => $post->user->name)); } else { $reply = $notification->reply; $originalContent = $reply->content; $htmlContent = $this->markdown->render($reply->content); $message->setFrom(array($from => $reply->user->name)); } if (trim($originalContent)) { $textContent = nl2br($originalContent); $htmlContent .= '<p style="font-size:small;-webkit-text-size-adjust:none;color:#717171;">'; if ($notification->type == 'P') { $htmlContent .= '—<br>Reply to this email directly or view the complete thread on ' . PHP_EOL . '<a href="' . $url . '/discussion/' . $post->id . '/' . $post->slug . '">Phosphorum</a>. '; } else { $htmlContent .= '—<br>Reply to this email directly or view the complete thread on ' . PHP_EOL . '<a href="' . $url . '/discussion/' . $post->id . '/' . $post->slug . '#C' . $reply->id . '">Phosphorum</a>. '; } $htmlContent .= PHP_EOL . 'Change your e-mail preferences <a href="' . $url . '/settings">here</a></p>'; $bodyMessage = new \Swift_MimePart($htmlContent, 'text/html'); $bodyMessage->setCharset('UTF-8'); $message->attach($bodyMessage); $bodyMessage = new \Swift_MimePart($textContent, 'text/plain'); $bodyMessage->setCharset('UTF-8'); $message->attach($bodyMessage); if (!$this->transport) { $this->transport = \Swift_SmtpTransport::newInstance($this->config->smtp->host, $this->config->smtp->port, $this->config->smtp->security); $this->transport->setUsername($this->config->smtp->username); $this->transport->setPassword($this->config->smtp->password); } if (!$this->mailer) { $this->mailer = \Swift_Mailer::newInstance($this->transport); } $this->mailer->send($message); } } catch (\Exception $e) { echo $e->getMessage(), PHP_EOL; } } } $notification->sent = 'Y'; if ($notification->save() == false) { foreach ($notification->getMessages() as $message) { echo $message->getMessage(), PHP_EOL; } } }