Firefox PHP

Ajax "last post" preview now working in a Phorum template

Posted by sheik 
Ajax "last post" preview now working in a Phorum template
February 05, 2007 10:54AM
(this thread is made as a result of [www.phorum.org])

As per the original thread, I now have a proof-of-concept Ajax preview working in Phorum.

This simply brings up a popup whenever a user mouseovers a "last post" link in a Phorum message list, and the popup then loads the contents of that post from the server.
The advantage for the user is that it lets them have a preview of what the thread is about before clicking it.

It's easier to see for yourself how it works though, so please look at [www.cantonese.sheik.co.uk] and hover over any "last post" link to see the popup.

To get this working on your own Phorums, you will need:
- The OverlibMWS Javascript package
- a working knowledge of OverlibMWS - I'd suggest trying something easy first of all like creating a test popup within your forum (no Ajax to start off with!).
- a PHP script that has access to the Phorum API, and outputs formatted HTML given a Phorum message ID.
- some free time to read the Ajax examples at [www.macridesweb.com] (I am using the ALTERNATIVE FORMAT example rather than the basic one, and strongly advise you to do the same, as having the popup display first and *then* make the Ajax call will save you some headaches).
UPDATE: ignore that advice, both examples work equally well, it was my fault <blush!>

Unfortunately I don't have time to write a proper case study at the moment (my code is a mess from hacking this altogether) but I will try and help out as best I can if you have any questions.

Here is my server side script, as noted, it a bit of a hack!
In particular, I have duplicated the Phorum bbcode and smiley functions so that the previews look better. I wasn't too sure how to get access to the actual Phorum functions, hence the duplication. If a Phorum dev could suggest a better way of formatting a single message so that it appears exactly as it would in my actual Phorum, please let me know.

<?php
	header('Content-Type: text/plain'); // good AJAX practise

	include_once("./common.php"); // from your Phorum directory!
	include_once ("./include/text_formatting.php"); // this contains a copy of bbcode

	//$thread_id = $_GET["thread_id"];
	//$forum_id = $_GET["forum_id"];
	$message_id = $_GET["message_id"];

	if (!($message_id > 0)){
		echo "message ID not set";
		exit();
	}

	// function phorum_db_get_message($value, $field="message_id", $ignore_forum_id=false)
	$message = phorum_db_get_message($message_id,"message_id",true);
	$thisbody = phorum_mod_smileys_format2($message["body"]);
	$thisbody = nl2br(phorum_bb_code_2($thisbody));

	if (strlen($thisbody)>0){

		$thisauthor = $message["author"];
		if (strlen($thisauthor)>0){
			$messagenumber++;
			// $thislink = "$host/phorum/read.php?" . $message["forum_id"] . "," . $message["thread_id"] . "," . $message["message_id"] . "#msg-" . $message["message_id"];
			$body .= "<div class=\"$class\">\n$thisbody\n</div>\n";
		}
	}

	echo $body;



function phorum_mod_smileys_format2($body)
{
    $PHORUM = $GLOBALS["PHORUM"];

    // Return immediately if we have no active smiley replacements.
    if (!isset($PHORUM["mod_smileys"])||!$PHORUM["mod_smileys"]["do_smileys"]){
        return $data;
    }

    // Run smiley replacements.
    $replace = $PHORUM["mod_smileys"]["replacements"];

	// Do body replacements.
	if (isset($replace["body"])) {
		$body = str_replace ($replace["body"][0] , $replace["body"][1], $body );
	}

	//echo $data['body'] . "<br/>";
	return $body;
}
?>

--
My notable Phorum sites:
Movie Deaths Database - "review comments" system mostly powered by Phorum
Learn Chinese! - integrated forum quiz




Edited 1 time(s). Last edit at 02/05/2007 05:57PM by sheik.
Re: Ajax "last post" preview now working in a Phorum template
February 05, 2007 11:04AM
One question I have, is the purpose of $ignore_forum_id in phorum_db_get_message()?
I just set this to true and just pass a message ID to my script to pull back a message. Is there any "Gotcha" I need to be aware of?

/\dam

--
My notable Phorum sites:
Movie Deaths Database - "review comments" system mostly powered by Phorum
Learn Chinese! - integrated forum quiz
Re: Ajax "last post" preview now working in a Phorum template
February 05, 2007 02:20PM
First of all: great start on getting some AJAX rolling :-) Having a preview for the start message might be more useful than one of the last message IMO, but it's not too hard to get that working for the start message too once this is finished.

Quote

Is there any "Gotcha" I need to be aware of?

There is a gotcha with your code, but this doesn't involve the $ignore_forum_id. It's good practice to set that flag if you want to retrieve a message by its message id. The gotcha is that I don't see any permission checking code. That could make this script a security risk for people that run their forums using strict security settings, where users aren't allowed to access all forums. By probing for message ids, I could use this script to pull messages from the database.

I don't have any ready-to-go code for you to show how this should be handled. I'm currently writing a module that implements easier to use and more extensive moderator options and there I'm also grabbing message bodies from the database using AJAX. So I guess I might be able to show some code soon.

Thinking of this, it might be a good idea to implement some generic mod which implements calls which can be used by AJAX based Phorum extensions. I forsee more and more tools using AJAX functionality and canalizing the development of the server side code is important to keep things straight forward, consistent and secure.


Maurice Makaay
Phorum Development Team
my blog linkedin profile secret sauce
Re: Ajax "last post" preview now working in a Phorum template
February 05, 2007 02:35PM
Great work on this code, I really like the idea of it and I'll throw a second in on the preview of the start of the thread. On question I have seeing as how the entire post is shown (no just the first couple of lines) is what this will do the the who view count idea. A thread may show 10 views, but in reality many viewed the preview and didn't see a need to view the thread. Don't know that it is really an issue, just a thought I had.
Re: Ajax "last post" preview now working in a Phorum template
February 05, 2007 05:55PM
Good point on the security. It isn't a major problem for me, but obviously needs to be considered for some sites.

The view count will be messed up too, but as you say, you could always truncate the preview to x number of characters if you wanted. It's also possible that Maurice's thoughts about a Phorum AJAX API could include an "increment view count" function.

Adding the same functionality to show the first message in a thread is a definite candidate for this kind of system. Another idea is to have a mini search form appear as a popup, which would be practical and space efficient.

By the way, there is an errata to my advice above about not using the basic Ajax example at OverlibMWS - after speaking with the developer the problem was traced to me using two lousy brackets when I shouldn't have done. Once I fixed that both methods worked superbly. :-)

/\dam

--
My notable Phorum sites:
Movie Deaths Database - "review comments" system mostly powered by Phorum
Learn Chinese! - integrated forum quiz
Re: Ajax "last post" preview now working in a Phorum template
February 05, 2007 06:00PM
I probably didn't make it clear enough that the OverlibMWS library has plenty of configuration options to stop popups from being activated by accident (and perhaps causing unnecessary load on your server as a result). For example, you can make them require a click, or you can configure a "tooltip delay" to only activate them after x milliseconds of hovering etc.

It is an extremely comprehensive library!

/\dam

--
My notable Phorum sites:
Movie Deaths Database - "review comments" system mostly powered by Phorum
Learn Chinese! - integrated forum quiz
Re: Ajax "last post" preview now working in a Phorum template
February 05, 2007 06:49PM
Quote
sheik
It is an extremely comprehensive library!

And on the largish side... An layer appearing without any bells and whistles would not require any libraries, but then if you want them it's nice to have them in one package.

---
-=[ Panu ]=-
Re: Ajax "last post" preview now working in a Phorum template
February 06, 2007 03:30AM
They split the package up though, so if you don't need drop shadows, modal popups, calendars etc you don't need to include those file.
But yes, you're looking at 100KB+ if you want AJAX popups with filtering display effects. This will be cached by the browser but can still not be considered a lightweight solution.

That's why I wanted to use it in more than one area, to get my "money's" worth :-)

/\dam

--
My notable Phorum sites:
Movie Deaths Database - "review comments" system mostly powered by Phorum
Learn Chinese! - integrated forum quiz
Re: Ajax "last post" preview now working in a Phorum template
February 10, 2007 03:43AM
I've got a photo gallery and I'm using phorum behind it to keep the picture blogs (like myspace,etc).

guests just see "pic 1 of x" and users see, the previous comments via ajax which grabs the threads in a JSON string, then on javascript I build the table which displays them.

there's a text box and 'add my comment' button which runs the same ajax bits as a get which inserts the message, and javascript replaces the TD value of the last entry from the miniform to the text.

I'm thinking of adding some code in that doesn't display the textbox if the last added comment was from the current user.

there's a lot of 'hacks' in mine too so it's not quite in sharable mode.
hopefully I'll have some time once I get everything done to post some useful snippets.
ideally it would be a phorum mod--I wasn't familiar enough with when I started to go that route, so I'm not sure how hard it would be to move everything into that format, so it may just stay the way it is.
Sorry, only registered users may post in this forum.

Click here to login