Skip to content

Commit

Permalink
Adding forum subscriptions, see #142
Browse files Browse the repository at this point in the history
  • Loading branch information
reines committed Sep 30, 2010
1 parent 77d9423 commit 68b3934
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 39 deletions.
28 changes: 27 additions & 1 deletion db_update.php
Expand Up @@ -9,7 +9,7 @@
// The FluxBB version this script updates to
define('UPDATE_TO', '1.4.2');

define('UPDATE_TO_DB_REVISION', 8);
define('UPDATE_TO_DB_REVISION', 9);
define('UPDATE_TO_SI_REVISION', 1);
define('UPDATE_TO_PARSER_REVISION', 1);

Expand Down Expand Up @@ -961,6 +961,32 @@ function convert_table_utf8($table, $callback, $old_charset, $key = null, $start
$db->create_table('search_words', $schema);
}

// If the subscription table hasn't been renamed yet, rename it
if ($db->table_exists('subscriptions') && !$db->table_exists('topic_subscriptions'))
$db->query('ALTER TABLE '.$db->prefix.'subscriptions RENAME TO '.$db->prefix.'topic_subscriptions') or error('Unable to rename subscriptions table', __FILE__, __LINE__, $db->error());

This comment has been minimized.

Copy link
@reines

reines Sep 30, 2010

Author Contributor

Turns out this works for MySQL and PostgreSQL, but not SQLite2. Needs fixing...


// if we don't have the forum_subscriptions table, create it
if (!$db->table_exists('forum_subscriptions'))
{
$schema = array(
'FIELDS' => array(
'user_id' => array(
'datatype' => 'INT(10) UNSIGNED',
'allow_null' => false,
'default' => '0'
),
'forum_id' => array(
'datatype' => 'INT(10) UNSIGNED',
'allow_null' => false,
'default' => '0'
)
),
'PRIMARY KEY' => array('user_id', 'forum_id')
);

$db->create_table('forum_subscriptions', $schema) or error('Unable to create forum subscriptions table', __FILE__, __LINE__, $db->error());
}

// Change the default style if the old doesn't exist anymore
if ($pun_config['o_default_style'] != $default_style)
$db->query('UPDATE '.$db->prefix.'config SET conf_value = \''.$db->escape($default_style).'\' WHERE conf_name = \'o_default_style\'') or error('Unable to update default style config', __FILE__, __LINE__, $db->error());
Expand Down
2 changes: 1 addition & 1 deletion include/common.php
Expand Up @@ -12,7 +12,7 @@
// Define the version and database revision that this code was written for
define('FORUM_VERSION', '1.4.2');

define('FORUM_DB_REVISION', 8);
define('FORUM_DB_REVISION', 9);
define('FORUM_SI_REVISION', 1);
define('FORUM_PARSER_REVISION', 1);

Expand Down
23 changes: 21 additions & 2 deletions install.php
Expand Up @@ -9,7 +9,7 @@
// The FluxBB version this script installs
define('FORUM_VERSION', '1.4.2');

define('FORUM_DB_REVISION', 8);
define('FORUM_DB_REVISION', 9);
define('FORUM_SI_REVISION', 1);
define('FORUM_PARSER_REVISION', 1);

Expand Down Expand Up @@ -1201,7 +1201,26 @@ function process_form(the_form)
'PRIMARY KEY' => array('user_id', 'topic_id')
);

$db->create_table('subscriptions', $schema) or error('Unable to create subscriptions table', __FILE__, __LINE__, $db->error());
$db->create_table('topic_subscriptions', $schema) or error('Unable to create topic subscriptions table', __FILE__, __LINE__, $db->error());


$schema = array(
'FIELDS' => array(
'user_id' => array(
'datatype' => 'INT(10) UNSIGNED',
'allow_null' => false,
'default' => '0'
),
'forum_id' => array(
'datatype' => 'INT(10) UNSIGNED',
'allow_null' => false,
'default' => '0'
)
),
'PRIMARY KEY' => array('user_id', 'forum_id')
);

$db->create_table('forum_subscriptions', $schema) or error('Unable to create forum subscriptions table', __FILE__, __LINE__, $db->error());


$schema = array(
Expand Down
5 changes: 4 additions & 1 deletion lang/English/forum.php
Expand Up @@ -9,6 +9,9 @@
'Sticky' => 'Sticky:',
'Closed' => 'Closed:',
'Empty forum' => 'Forum is empty.',
'Mod controls' => 'Moderator controls'
'Mod controls' => 'Moderator controls',
'Is subscribed' => 'You are currently subscribed to this forum',
'Unsubscribe' => 'Unsubscribe',
'Subscribe' => 'Subscribe to this forum'

);
11 changes: 11 additions & 0 deletions lang/English/mail_templates/new_topic.tpl
@@ -0,0 +1,11 @@
Subject: New topic in forum: <forum_name>

<poster> has posted a new topic <topic_subject> in the forum <forum_name>, to which you are subscribed.

The topic is located at <topic_url>

You can unsubscribe by going to <unsubscribe_url>

--
<board_mailer>
(Do not reply to this message)
18 changes: 18 additions & 0 deletions lang/English/mail_templates/new_topic_full.tpl
@@ -0,0 +1,18 @@
Subject: New topic in forum: <forum_name>

<poster> has posted a new topic <topic_subject> in the forum <forum_name>, to which you are subscribed.

The message reads as follows:
-----------------------------------------------------------------------

<message>

-----------------------------------------------------------------------

The topic is located at <topic_url>

You can unsubscribe by going to <unsubscribe_url>

--
<board_mailer>
(Do not reply to this message)
6 changes: 4 additions & 2 deletions lang/English/misc.php
Expand Up @@ -29,9 +29,11 @@
'Reason desc' => 'Please enter a short reason why you are reporting this post',

// Subscriptions
'Already subscribed' => 'You are already subscribed to this topic.',
'Already subscribed topic' => 'You are already subscribed to this topic.',
'Already subscribed forum' => 'You are already subscribed to this forum.',
'Subscribe redirect' => 'Your subscription has been added. Redirecting …',
'Not subscribed' => 'You are not subscribed to this topic.',
'Not subscribed topic' => 'You are not subscribed to this topic.',
'Not subscribed forum' => 'You are not subscribed to this forum.',
'Unsubscribe redirect' => 'Your subscription has been removed. Redirecting …',

// General forum and topic moderation
Expand Down
75 changes: 55 additions & 20 deletions misc.php
Expand Up @@ -287,46 +287,81 @@
}


else if (isset($_GET['subscribe']))
else if ($action == 'subscribe')
{
if ($pun_user['is_guest'] || $pun_config['o_subscriptions'] != '1')
message($lang_common['No permission']);

$topic_id = intval($_GET['subscribe']);
if ($topic_id < 1)
$topic_id = isset($_GET['tid']) ? intval($_GET['tid']) : 0;
$forum_id = isset($_GET['fid']) ? intval($_GET['fid']) : 0;
if ($topic_id < 1 && $forum_id < 1)
message($lang_common['Bad request']);

// Make sure the user can view the topic
$result = $db->query('SELECT 1 FROM '.$db->prefix.'topics AS t LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.id='.$topic_id.' AND t.moved_to IS NULL') or error('Unable to fetch topic info', __FILE__, __LINE__, $db->error());
if (!$db->num_rows($result))
message($lang_common['Bad request']);
if ($topic_id)
{
// Make sure the user can view the topic
$result = $db->query('SELECT 1 FROM '.$db->prefix.'topics AS t LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=t.forum_id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.id='.$topic_id.' AND t.moved_to IS NULL') or error('Unable to fetch topic info', __FILE__, __LINE__, $db->error());
if (!$db->num_rows($result))
message($lang_common['Bad request']);

$result = $db->query('SELECT 1 FROM '.$db->prefix.'topic_subscriptions WHERE user_id='.$pun_user['id'].' AND topic_id='.$topic_id) or error('Unable to fetch subscription info', __FILE__, __LINE__, $db->error());
if ($db->num_rows($result))
message($lang_misc['Already subscribed topic']);

$db->query('INSERT INTO '.$db->prefix.'topic_subscriptions (user_id, topic_id) VALUES('.$pun_user['id'].' ,'.$topic_id.')') or error('Unable to add subscription', __FILE__, __LINE__, $db->error());

redirect('viewtopic.php?id='.$topic_id, $lang_misc['Subscribe redirect']);
}

if ($forum_id)
{
// Make sure the user can view the forum
$result = $db->query('SELECT 1 FROM '.$db->prefix.'forums AS f LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$pun_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND f.id='.$forum_id) or error('Unable to fetch forum info', __FILE__, __LINE__, $db->error());
if (!$db->num_rows($result))
message($lang_common['Bad request']);

$result = $db->query('SELECT 1 FROM '.$db->prefix.'subscriptions WHERE user_id='.$pun_user['id'].' AND topic_id='.$topic_id) or error('Unable to fetch subscription info', __FILE__, __LINE__, $db->error());
if ($db->num_rows($result))
message($lang_misc['Already subscribed']);
$result = $db->query('SELECT 1 FROM '.$db->prefix.'forum_subscriptions WHERE user_id='.$pun_user['id'].' AND forum_id='.$forum_id) or error('Unable to fetch subscription info', __FILE__, __LINE__, $db->error());
if ($db->num_rows($result))
message($lang_misc['Already subscribed forum']);

$db->query('INSERT INTO '.$db->prefix.'subscriptions (user_id, topic_id) VALUES('.$pun_user['id'].' ,'.$topic_id.')') or error('Unable to add subscription', __FILE__, __LINE__, $db->error());
$db->query('INSERT INTO '.$db->prefix.'forum_subscriptions (user_id, forum_id) VALUES('.$pun_user['id'].' ,'.$forum_id.')') or error('Unable to add subscription', __FILE__, __LINE__, $db->error());

redirect('viewtopic.php?id='.$topic_id, $lang_misc['Subscribe redirect']);
redirect('viewforum.php?id='.$forum_id, $lang_misc['Subscribe redirect']);
}
}


else if (isset($_GET['unsubscribe']))
else if ($action == 'unsubscribe')
{
if ($pun_user['is_guest'] || $pun_config['o_subscriptions'] != '1')
message($lang_common['No permission']);

$topic_id = intval($_GET['unsubscribe']);
if ($topic_id < 1)
$topic_id = isset($_GET['tid']) ? intval($_GET['tid']) : 0;
$forum_id = isset($_GET['fid']) ? intval($_GET['fid']) : 0;
if ($topic_id < 1 && $forum_id < 1)
message($lang_common['Bad request']);

$result = $db->query('SELECT 1 FROM '.$db->prefix.'subscriptions WHERE user_id='.$pun_user['id'].' AND topic_id='.$topic_id) or error('Unable to fetch subscription info', __FILE__, __LINE__, $db->error());
if (!$db->num_rows($result))
message($lang_misc['Not subscribed']);
if ($topic_id)
{
$result = $db->query('SELECT 1 FROM '.$db->prefix.'topic_subscriptions WHERE user_id='.$pun_user['id'].' AND topic_id='.$topic_id) or error('Unable to fetch subscription info', __FILE__, __LINE__, $db->error());
if (!$db->num_rows($result))
message($lang_misc['Not subscribed topic']);

$db->query('DELETE FROM '.$db->prefix.'topic_subscriptions WHERE user_id='.$pun_user['id'].' AND topic_id='.$topic_id) or error('Unable to remove subscription', __FILE__, __LINE__, $db->error());

$db->query('DELETE FROM '.$db->prefix.'subscriptions WHERE user_id='.$pun_user['id'].' AND topic_id='.$topic_id) or error('Unable to remove subscription', __FILE__, __LINE__, $db->error());
redirect('viewtopic.php?id='.$topic_id, $lang_misc['Unsubscribe redirect']);
}

if ($forum_id)
{
$result = $db->query('SELECT 1 FROM '.$db->prefix.'forum_subscriptions WHERE user_id='.$pun_user['id'].' AND forum_id='.$forum_id) or error('Unable to fetch subscription info', __FILE__, __LINE__, $db->error());
if (!$db->num_rows($result))
message($lang_misc['Not subscribed forum']);

$db->query('DELETE FROM '.$db->prefix.'forum_subscriptions WHERE user_id='.$pun_user['id'].' AND forum_id='.$forum_id) or error('Unable to remove subscription', __FILE__, __LINE__, $db->error());

redirect('viewtopic.php?id='.$topic_id, $lang_misc['Unsubscribe redirect']);
redirect('viewforum.php?id='.$forum_id, $lang_misc['Unsubscribe redirect']);
}
}


Expand Down

0 comments on commit 68b3934

Please sign in to comment.