Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Email alerts when users like/comment on items, closes #75

  • Loading branch information...
commit 2205b841bc70ca88d6f7f5f51e0895677a6cfa3a 1 parent 1307083
@DHS authored
View
1  config/routes.php
@@ -20,6 +20,7 @@ class Routes {
'/settings' => array('controller' => 'users', 'action' => 'update'),
'/settings/profile' => array('controller' => 'users', 'action' => 'update', 'id' => 'profile'),
'/settings/password' => array('controller' => 'users', 'action' => 'update', 'id' => 'password'),
+ '/settings/emails' => array('controller' => 'users', 'action' => 'update', 'id' => 'emails'),
'/feed' => array('controller' => 'items', 'action' => 'feed'),
// More complex
View
15 controllers/comments_controller.php
@@ -12,6 +12,21 @@ function add() {
// Add comment
$comment_id = Comment::add($_SESSION['user_id'], $_POST['item_id'], $_POST['content']);
+ $item = Item::get_by_id($_POST['item_id']);
+
+ if ($item->user->email_notifications['item_comment']) {
+
+ $to = array('name' => $item->user->username, 'email' => $item->user->email);
+ $subject = '[' . $this->config->name . '] Someone left a ' . strtolower($this->config->items['comments']['name']) . ' on your ' . strtolower($this->config->items['titles']['name']) . ' on ' . $this->config->name . '!';
+ $body = $this->twig_string->render(file_get_contents("themes/{$this->config->theme}/emails/item_comment.html"), array('link' => substr($this->config->url, 0, -1) . $this->url_for('items', 'show', $item->id), 'app' => array('config' => $this->config), 'user' => $item->user));
+
+ // Email user
+ $this->email->send_email($to, $subject, $body);
+
+ } else {
+ ECHO 'NO';
+ }
+
// Log new comment
if (isset($this->plugins->log)) {
$this->plugins->log->add($_SESSION['user_id'], 'comment', $comment_id, 'add', $_POST['content']);
View
13 controllers/likes_controller.php
@@ -8,6 +8,19 @@ function add($item_id) {
$like_id = Like::add($_SESSION['user_id'], $item_id);
+ $item = Item::get_by_id($item_id);
+
+ if ($item->user->email_notifications['item_like']) {
+
+ $to = array('name' => $item->user->username, 'email' => $item->user->email);
+ $subject = '[' . $this->config->name . '] Someone clicked ' . strtolower($this->config->items['likes']['name']) . ' on your ' . strtolower($this->config->items['name']) . ' on ' . $this->config->name . '!';
+ $body = $this->twig_string->render(file_get_contents("themes/{$this->config->theme}/emails/item_like.html"), array('link' => substr($this->config->url, 0, -1) . $this->url_for('items', 'show', $item->id), 'app' => array('config' => $this->config), 'user' => $item->user));
+
+ // Email user
+ $this->email->send_email($to, $subject, $body);
+
+ }
+
if (isset($this->plugins->log)) {
$this->plugins->log->add($_SESSION['user_id'], 'like', $like_id, 'add');
}
View
51 controllers/users_controller.php
@@ -143,6 +143,12 @@ function update($page = 'profile') {
$user->url = $_POST['url'];
}
+ } elseif ($page == 'emails') {
+
+ if (isset($_POST['submit'])) {
+ $user->email_notifications = $this->update_email_notifications();
+ }
+
}
$this->loadView('users/update', array('page' => $page, 'user' => $user));
@@ -357,6 +363,51 @@ private function update_profile() {
}
+ // Helper function: update profile
+ private function update_email_notifications() {
+
+ if (isset($_POST['submit'])) {
+
+ $user = User::get_by_id($_SESSION['user_id']);
+
+ // Find new email settings, first unset submit var
+ unset($_POST['submit']);
+
+ // Then loop through post vars fetching new email settings
+ foreach ($_POST as $key => $value) {
+
+ if ($value == 'on') {
+ $emails[$key] = 1;
+ }
+
+ }
+
+ // Check for existing settings in order to update
+ // with negative values
+ foreach ($user->email_notifications as $key => $value) {
+
+ if ( ! isset($_POST[$key])) {
+ $emails[$key] = 0;
+ }
+
+ }
+
+ // Call update_emails in user model
+ $user->update_email_notifications($emails);
+
+ // Set success message
+ Application::flash('success', 'Email settings updated!');
+
+ } else {
+
+ Application::flash('error', 'Email settings not updated.');
+
+ }
+
+ return $emails;
+
+ }
+
// Helper function: signup with an invite code
private function signup_code() {
View
41 models/user.php
@@ -45,7 +45,11 @@ public static function get_by_id($id) {
} else {
- return new User($result);
+ $user = new User($result);
+
+ $user->email_notifications = $user->email_notifications();
+
+ return $user;
}
@@ -343,6 +347,25 @@ public function comments($limit = 10, $offset = 0) {
}
+ // Get email notification settings
+ public function email_notifications() {
+
+ global $mysqli;
+ $config = new AppConfig;
+
+ $sql = "SELECT `notification`, `value` FROM `{$config->database[SITE_IDENTIFIER]['prefix']}users_email_notifications` WHERE `user_id` = $this->id";
+
+ $query = mysqli_query($mysqli, $sql);
+
+ $emails = array();
+ while ($result = mysqli_fetch_assoc($query)) {
+ $emails[$result['notification']] = $result['value'];
+ }
+
+ return $emails;
+
+ }
+
// Add a friend, returns friendship id
public function friend_add($friend_user_id) {
@@ -499,6 +522,22 @@ public function update_profile($name = NULL, $bio = NULL, $url = NULL) {
}
+ // Update email notification settings
+ public function update_email_notifications(array $emails = null) {
+
+ global $mysqli;
+ $config = new AppConfig;
+
+ foreach ($emails as $key => $value) {
+
+ $sql = "INSERT INTO `{$config->database[SITE_IDENTIFIER]['prefix']}users_email_notifications` (user_id, notification, value) VALUES ($this->id, '$key', $value) ON DUPLICATE KEY UPDATE `value` = $value;";
+
+ $query = mysqli_query($mysqli, $sql);
+
+ }
+
+ }
+
// Update a user's number of invites
public function update_invites($invites) {
View
7 rat.sql
@@ -19,6 +19,13 @@ CREATE TABLE `users_password_reset` (
`date` timestamp NOT NULL default CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
+CREATE TABLE `users_email_notifications` (
+ `user_id` int(11) unsigned NOT NULL,
+ `notification` varchar(255) DEFAULT NULL,
+ `value` tinyint(1) NOT NULL,
+ PRIMARY KEY (`user_id`, `notification`)
+) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
+
CREATE TABLE `items` (
`id` int(10) NOT NULL auto_increment,
`user_id` bigint(15) NOT NULL default '0',
View
5 themes/default/emails/item_comment.html
@@ -0,0 +1,5 @@
+<p>Hi {{ user.username }},</p>
+<p>Someone commented on your {{ app.config.items.name }} on {{ app.config.name }}:</p>
+<p>{{ link }}</p>
+<p>Regards,</p>
+<p>The {{ app.config.name }} team</p>
View
5 themes/default/emails/item_like.html
@@ -0,0 +1,5 @@
+<p>Hi {{ user.username }},</p>
+<p>Someone liked your {{ app.config.items.name }} on {{ app.config.name }}:</p>
+<p>{{ link }}</p>
+<p>Regards,</p>
+<p>The {{ app.config.name }} team</p>
View
36 themes/default/users/update.html
@@ -5,6 +5,7 @@
<ul class="nav nav-tabs">
<li{% if page == 'profile' %} class="active"{% endif %}>{{ app.link_to('Profile', 'settings', 'profile') }}</li>
<li{% if page == 'password' %} class="active"{% endif %}>{{ app.link_to('Password', 'settings', 'password') }}</li>
+ <li{% if page == 'emails' %} class="active"{% endif %}>{{ app.link_to('Emails', 'settings', 'emails') }}</li>
</ul>
{% if page == 'password' %}
@@ -77,6 +78,41 @@
</fieldset>
</form>
+{% elseif page == 'emails' %}
+
+ <form class="form-horizontal" action="{{ app.url_for('users', 'update', 'emails') }}" method="post">
+
+ <input type="hidden" name="submit" value="true" />
+
+ <fieldset>
+
+ <legend>Update email settings</legend>
+
+ <div class="control-group">
+ <div class="controls">
+ <label class="checkbox">
+ <input type="checkbox" name="item_like" id="item_like" {% if user.emails.item_like %}checked="checked"{% endif %}/>
+ Email me when someone likes one of my {{ app.config.items.name_plural }}
+ </label>
+ </div>
+ </div>
+
+ <div class="control-group">
+ <div class="controls">
+ <label class="checkbox">
+ <input type="checkbox" name="item_comment" id="item_comment" {% if user.emails.item_comment %}checked="checked"{% endif %}/>
+ Email me when someone comments on one of my {{ app.config.items.name_plural }}
+ </label>
+ </div>
+ </div>
+
+ <div class="form-actions">
+ <button type="submit" class="btn btn-primary">Save</button>
+ </div>
+
+ </fieldset>
+ </form>
+
{% endif %}
</div>
Please sign in to comment.
Something went wrong with that request. Please try again.