Topic: Forum Subscription

If someone could create a Mod that allowed users to subscribe to a Forum (as opposed to a individual topics) so that they could be notified of new topics in that forum but not replies within topics (current subscription feature does that - no need to re-invent the wheel!)

I'd try myself but this is my first experience with PHP.

Find what you want...  Where you want it... www.truelocal.com

2 (edited by Cailean 2004-03-15 17:51)

Re: Forum Subscription

k, so no takers... wink

can someone point me in the right direction?  I've looked at the 'subscribe/unsubscribe' code from viewtopic.php and misc.php but am unclear how to proceed.

EDIT:
So I just looked at misc.php again...  what if I:

1) add new 'sub/unsub' sections with, say, 'subscribe_forum' instead of 'subscribe'
2) copy everything from the topic sub/unsub sections
3) change all references of '$topic_id' to 'forum_id'

and then, of course, copy the link info from viewtopic to viewforum...

Thoughts?

Find what you want...  Where you want it... www.truelocal.com

3 (edited by Cailean 2004-03-16 09:46)

Re: Forum Subscription

OK, so I tried out my plan and I think it's almost working except that I'm getting an "unable to fetch subscription info' error.  This would be a db problem, right?

Here's what I did:

viewforum.php: (around line 239 or so)

<!--SUBSCRIBE FORUM MOD-->
<?php

if (!$cookie['is_guest'] && $pun_config['o_subscriptions'] == '1')
{
    if ($is_subscribed)
        // I apologize for the variable naming here. It's a mix of subscription and action I guess :-)
        $subscraction = $lang_forum['Is subscribed'].' - <a href="misc.php?unsubscribe_forum='.$id.'">'.$lang_forum['Unsubscribe'].'</a>';
    else
        $subscraction = '<a href="misc.php?subscribe_forum='.$id.'">'.$lang_forum['Subscribe'].'</a>';
}
else
    $subscraction = ' ';


?>
<!-- END SUBSCRIBE FORUM MOD-->

<table class="punspacer" cellspacing="1" cellpadding="4">
    <tr>
        <td style="width: 46%"><?php echo $lang_common['Pages'].': '.paginate($num_pages, $p, 'viewforum.php?id='.$id) ?></td>
<!--SUBSCRIBE FORUM MOD-->
        <td class="punright" style="width: 35%"><?php echo $subscraction ?></td>
<!--END SUBSCRIBE FORUM MOD-->
        <td class="punright" style="width: 19%"><b><?php echo $post_link ?></b></td>
    </tr>
</table>

misc.php: (line 288 or so)

// SUBSCRIBE FORUM MOD

else if (isset($_GET['subscribe_forum']))
{
    $forum_id = intval($_GET['subscribe_forum']);
    if (empty($forum_id))
        message($lang_common['Bad request']);

    if ($cookie['is_guest'])
        message($lang_common['No permission']);

    $result = $db->query('SELECT 1 FROM '.$db->prefix.'subscriptions_forums WHERE user_id='.$cur_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_f']);
    
    $db->query('INSERT INTO '.$db->prefix.'subscriptions_forums (user_id, forum_id) VALUES('.$cur_user['id'].' ,'.$forum_id.')') or error('Unable to add subscription', __FILE__, __LINE__, $db->error());

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


else if (isset($_GET['unsubscribe_forum']))
{
    $forum_id = intval($_GET['unsubscribe_forum']);
    if (empty($forum_id))
        message($lang_common['Bad request']);

    if ($cookie['is_guest'])
        message($lang_common['No permission']);

    $result = $db->query('SELECT 1 FROM '.$db->prefix.'subscriptions_forums WHERE user_id='.$cur_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_f']);

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

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

// end SUBSCRIBE FORUM MOD

I also made changes to the forum and misc lang files but they don't seem to causing problems...

EDIT:  I checked the db and noticed that there is no 'forum_id' field in the 'subscriptions' table...  If I just add this field will my problems be solved?

EDIT2: OK so I added the field to the db and now get no errors...  but the mail doesn't go through....  not really surprising since I didn't change any mailing stuff...  next?  Also, viewforum.php isn't recognizing me as subscribed so is showing the 'subscribe to this forum' link but when I click it it says I'm already subscribed -- I'm guessing that '$is_subscribed' isn't set but I don't know how to do that.

EDIT3!!: updated code boxes to reflect current status.

Any help would be appreciated....   my MySQL knowledge is minimal...

Find what you want...  Where you want it... www.truelocal.com

Re: Forum Subscription

I'm afraid it will be quite a lot more complicated than that. I think the easiest way will be to add a new table to the database that keeps track of forum subscriptions. You can then do something along the lines of what you did above as well as add code to post.php to send an e-mail whenever a new topic is posted.

"Programming is like sex: one mistake and you have to support it for the rest of your life."

5 (edited by Cailean 2004-03-15 19:32)

Re: Forum Subscription

k, getting closer....

I realised that I needed to add the subscribe/emailing code to the part of post.php that does new topics...  so I did that but now I get an 'unable to fetch", etc. error.
I know that it's this line that causing problems... how do I change for forums? (you'll notice i tried replacing every occurrence of 'topic' with 'forum' -- no dice)

$result = $db->query('SELECT u.id, u.email, u.notify_with_post FROM '.$db->prefix.'users AS u INNER JOIN '.$db->prefix.'subscriptions AS s ON u.id=s.user_id LEFT JOIN '.$db->prefix.'bans AS b ON u.username=b.username WHERE b.username IS NULL AND s.forum_id='.$fid.' AND u.last_action>'.$previous_post_time.' AND u.id!='.intval($cur_user['id'])) or error('Unable to fetch subscription info', __FILE__, __LINE__, $db->error());
Find what you want...  Where you want it... www.truelocal.com

Re: Forum Subscription

Rickard wrote:

I think the easiest way will be to add a new table to the database that keeps track of forum subscriptions. You can then do something along the lines of what you did above

So I should add a table identical to 'subscriptions' and call it forum_subscriptions' or something so that one will handle topics and the other forums?  That makes sense.

I'll still need some help with the db queries in post.php as I said above.  (my last post was posted before I read your post...)

Find what you want...  Where you want it... www.truelocal.com

7 (edited by Cailean 2004-03-15 20:24)

Re: Forum Subscription

so  I added the table (only thing I was stumped on was cardinality in the indexes... what IS that?)
I still get errors on the transplanted db query:

$result = $db->query('SELECT u.id, u.email, u.notify_with_post FROM '.$db->prefix.'users AS u INNER JOIN '.$db->prefix.'subscriptions_forums AS s ON u.id=s.user_id LEFT JOIN '.$db->prefix.'bans AS b ON u.username=b.username WHERE b.username IS NULL AND s.topic_id='.$tid.' AND u.last_action>'.$previous_post_time.' AND u.id!='.intval($cur_user['id'])) or error('Unable to fetch subscription info', __FILE__, __LINE__, $db->error());

'subscriptions_forums' is the new table I created... what do i do with the topic_id stuff and how do add the forum_id?  I tried just replacing them but that didn't seem to work.

Find what you want...  Where you want it... www.truelocal.com

Re: Forum Subscription

this is the 'debug mode' error i get

File: /home/forum/public_html/post.php
Line: 315

PunBB reported: Unable to fetch subscription info
Database reported: You have an error in your SQL syntax near 'AND u.last_action> AND u.id!=8' at line 1 (Errno: 1064)

Find what you want...  Where you want it... www.truelocal.com

9 (edited by Cailean 2004-03-15 20:31)

Re: Forum Subscription

alright, I feel I am close!!
I replaced s.topic_id with s.forum_id and $tid with $fid
now I only get this:

File: /home/forum/public_html/post.php
Line: 315

PunBB reported: Unable to fetch subscription info
Database reported: You have an error in your SQL syntax near 'AND u.id!=8' at line 1 (Errno: 1064)

edit: don't know if this is relevant, but I noticed that the new topic does get posted...

Find what you want...  Where you want it... www.truelocal.com

Re: Forum Subscription

BTW, thanks to anyone (like Rickard) who has taken the time to try and decipher my ramblings...   I feel I am so-o-o close to getting this...!

Find what you want...  Where you want it... www.truelocal.com

Re: Forum Subscription

SUCCESS!!!!!!!!

i modified the above mentioned db query to read:

$result = $db->query('SELECT u.id, u.email, u.notify_with_post FROM '.$db->prefix.'users AS u INNER JOIN '.$db->prefix.'subscriptions_forums AS s ON u.id=s.user_id LEFT JOIN '.$db->prefix.'bans AS b ON u.username=b.username WHERE b.username IS NULL AND s.forum_id='.$fid.' AND u.id!='.intval($cur_user['id'])) or error('Unable to fetch subscription info', __FILE__, __LINE__, $db->error());

Now all I have to do is add some e-mail templates cuz it sends reply e-mails at the moment.  I also still have that problem with viewforum.php not realizing that I'm subscribed and offering the unsubscribe link.

Find what you want...  Where you want it... www.truelocal.com

Re: Forum Subscription

Two last things and then I'm done and I'll compile all of this into a proper mod.

1) Mail Templates
I basically copied and modified reply_topic.tpl and reply_topic_full.tpl and renamed them new_topic.tpl and new_topic_full.tpl.
How do I go about adding 'forum_name' to the new templates?

2) $is_subscribed
I have hit a brick wall on setting $is_subscribed in viewforum.php.  I've tried transplanting some queries from viewtopic but nothing I try seems to work.  (the subscription itself works but it always says "Subscribe to this forum" even if you already are!)

I've amended my code posts above for viewforum.php and misc.php to reflect how they look now.

Find what you want...  Where you want it... www.truelocal.com

13 (edited by Cailean 2004-03-19 18:08)

Re: Forum Subscription

Cailean wrote:

1) Mail Templates
I basically copied and modified reply_topic.tpl and reply_topic_full.tpl and renamed them new_topic.tpl and new_topic_full.tpl.
How do I go about adding 'forum_name' to the new templates?

2) $is_subscribed
I have hit a brick wall on setting $is_subscribed in viewforum.php.  I've tried transplanting some queries from viewtopic but nothing I try seems to work.  (the subscription itself works but it always says "Subscribe to this forum" even if you already are!)

I'd really like to solve these two issues so I can release this as a Mod...  perhaps I need to explain the problems more clearly...

1)Mail Templates
My new_topic.tpl look like this:

Subject: New topic: <topic_subject> in a subscribed forum.

<replier> has created a topic <topic_subject> in a forum to which you are subscribed.

The post is located at <post_url>

You can unsubscribe by going to <unsubscribe_url>

-- 
<board_mailer>
(Do not reply to this message)

I would like to be able to identify the forum in which the topic was created.

2)$is_subscribed
This is severely annoying...  I'm not sure how to explain this better...  basically, I transplanted the code from viewtopic.php that displays either 'Subscribe to this topic' or 'You are subscribed to this topic - Unsubscribe' changing 'topic' to 'forum'.  Unfortunately it always displays the first option, even if you are subscribed!  I'm sure ut has to do with the $is_subscribed reference but since I don't understand fully how it works, I can't debug it... sad

Find what you want...  Where you want it... www.truelocal.com

14

Re: Forum Subscription

Shouldn't this be implemented by default? Can't really be considered bloat can it?

Ah, well, I'm trying to implement it now, but it's hard to do since the code has changed since this thread died.

Re: Forum Subscription

i think we've discussed this before, not sure if it was just automatic subscriptions though, anyway the easiest way to "subscribe" to a forum is to use its rss feed

16

Re: Forum Subscription

Connorhd wrote:

i think we've discussed this before, not sure if it was just automatic subscriptions though, anyway the easiest way to "subscribe" to a forum is to use its rss feed

Tell that to the users of my forum... wink
(I'd surprised if any of 'em has even heard the word RSS)

Re: Forum Subscription

well its up to you to educate them then tongue

18 (edited by Cailean 2005-06-18 23:35)

Re: Forum Subscription

I did eventually get this functionality working nicely...  II can get it to anyone that wants it but I'm not sure if I remember exactly how to set it up -- it was a one-time "set it and forget it" feature...

BTW, when did punBB get RSS?  It's been too long since my lat visit, apparently... Speaking of RSS, it's not really a great alternative to the forum subscription since it's really only useful to web-geek early-adopter type folks...  alot of people use forums for non-webby stuff and can barely understand how to use as it is...

Find what you want...  Where you want it... www.truelocal.com

19

Re: Forum Subscription

My users are requesting this.  Actually they are requesting to get an email whenever there is any new post of any kind, to which I say, "you want a listserv not a forum."  Anyway, this would work for those who want that.  They could just subscribe to every forum and every topic (can you imagine?).

So, can you remember how you implemented it?

Re: Forum Subscription

When I did it, it was back in the 1.1.* days... so I don't know exactly what's changed since then.  I've just upgraded (finally!) to 1.2.5b and so I'll have to figure it again for myself -- I'll post here when I do.

Here's what I remember off the top of my head...
1) copy the code for topic subscription in viewtopic.php and transplant it into viewforum taking care to re-assign various variable to represent forums rather than topics.

2) create a database table to hold the forum subscription info... again modelled on the topic subscription table

3) create custom .tpl files for the emails that get sent -- once again modelled on the topic subscription template.

That's all I can remember...  When I get around to doing for myself, I'll do up a proper mod...

Find what you want...  Where you want it... www.truelocal.com

21

Re: Forum Subscription

That would be great.  My forum is a ski related one, so there isn't much activity until it starts snowing giving me some time.

Re: Forum Subscription

of course if one of the punBB code gurus wanted to whip one up, it would probably be faster... wink

Find what you want...  Where you want it... www.truelocal.com

23

Re: Forum Subscription

There is a MOD i think it maybe usfull
http://www.punres.org/desc.php?pid=47

If your people come crazy, you will not need to your mind any more.

Re: Forum Subscription

just to point out, you do need access to setup crontabs for that mod

25

Re: Forum Subscription

That mod looks perfect.  But... I've never done a cron job.  I have the capability and it looks like I can copy and paste from the readme file, but it makes me nervous.