Replacement Tag: Comments

Post custom hacks and enhancements for phpwcms here only. Maybe some of these things will be included in official release later.
Cassiel
Posts: 24
Joined: Tue 22. Jun 2004, 13:52
Location: Friedrichshafen, Germany
Contact:

Replacement Tag: Comments

Post by Cassiel »

Comments Replacement Tag

The following replacement tag lets you insert the count of comments from within an article and displays it within an article listing. Great for using phpwcms as webblog or news overview if you have a comments section using the contet type "guestbook/comments" within the full articles. Note: You can only have one guestbook/comments section on every article.

Image


Step 1
Put the file info.cassiel.comments.php into the folder "/phpwcms_template/inc_script/frontend_render/"


Step 2
Add the following code to "/config/phpwcms/conf.template_default.inc.php":

Code: Select all

// Comments replacement tag
$template_default['comments']['comment_one']     = '%d Comment';    // Text for one comment, %d gets replaced by num of comments
$template_default['comments']['comment_more']    = '%d Comments';   // Text for comments >1, %d gets replaced by num of comments
$template_default['comments']['comment_false']   = 'Write comment'; // Text for the link if no 
$template_default['comments']['anchor_comments'] = 'comments';      // Anchor to jump to if comments
$template_default['comments']['anchor_form']     = 'comments_form'; // Anchor to jump to if no comments
$template_default['comments']['class_comments']  = 'comments';      // CSS class for the link if comments
$template_default['comments']['class_form']      = 'comments_form'; // CSS class for the link of no comments

Step 3
Edit your corresponding template file for your article listing and add the following code to the place where the count of the comments should be placed:

Code: Select all

[PHP]
// Strange hack, but it works
$my_article_id = "{ARTICLEID}";
$my_article_cid = $GLOBALS['content']['articles'][$my_article_id]['article_cid'];

echo('{COMMENTS:' . $my_article_id . ':' . $my_article_cid . '}')
[/PHP]

Step 4
Open the file "front.func.inc.php" within the folder "/include/inc_front/" and search for the function called "function url_search($query)". Now edit this function to match the following code:

Code: Select all

function url_search($query) { 
	// HACK BEGIN: Cassiel
	// Match for anchor
	$cnt = preg_match('/\#(.+)/i', $query, $anchor);
	
	// Only if matching has worked
	if ($cnt && $anchor) {
		// Remove anchor from query
		$query = preg_replace('/\#(.+)/i', '', $query);
		
		// Get anchor's name
		$anchor = $anchor[1];
	}
	// HACK:END
	
	if ( substr($query,0,4) == '?id=') { 
		$noid = substr($query, 4); 
		$file = str_replace(',', '.', $noid).'.html';  
		//$file = str_replace(',', '.', $noid).'.phtml'; 
		//$file = $noid.'.phtml'; 
	} else { 
		$noid = substr($query,1); 
		$file = str_replace(',', '.', $noid).'.html'; 
		//$file = str_replace(',', '.', $noid).'.phtml';
		//$file = $noid.'.phtml'; 
	} 
	
	// HACK:BEGIN Cassiel
	$link = ($anchor) ? ' href="' . $file . '#' . $anchor . '"' : $link = ' href="' . $file . '"';
	// HACK:END 
	return($link); 
}

Step 6
Search within the same file for the function called "function js_url_search($query)" and edit it to match the following code:

Code: Select all

function js_url_search($query) { 
	// HACK BEGIN: Cassiel
	// Match for anchor
	$cnt = preg_match('/\#(.+)/i', $query, $anchor);
	
	// Only if matching has worked
	if ($cnt && $anchor) {
		// Remove anchor from query
		$query = preg_replace('/\#(.+)/i', '', $query);
		
		// Get anchor's name
		$anchor = $anchor[1];
	}
	// HACK:END
	
	if ( substr($query,0,4) == '?id=') { 
		$noid = substr($query, 4);
		$file = str_replace(',', '.', $noid).'.html'; 
		//$file = str_replace(',', '.', $noid).'.phtml';
		$file = $noid.'.phtml'; 
	} else { 
		$noid = substr($query,1); 
		$file = str_replace(',', '.', $noid).'.html';
		//$file = str_replace(',', '.', $noid).'.phtml';
		$file = $noid.'.phtml';
	}
	
	// HACK:BEGIN Cassiel
	$link = ($anchor) ? "onclick=\"location.href='" . $file . '#' . $anchor . "'" : "onclick=\"location.href='" . $file . "'";
	// HACK:END 
	 
	return($link); 
} 

Syntax of the replacement tag
{COMMENTS:1:2}

- The first value defines the article ID and the second value defines the category ID which contains the article. Both values are needed! Let both values be auto detected by the code posted on Step 3.


Notice
If you're not using the mod_rewrite function of phpwcms you don't have to add the core code hack in Steps 5 and 6.

Happy tagging,
Cassiel
Last edited by Cassiel on Fri 16. Dec 2005, 14:13, edited 2 times in total.
User avatar
Fulvio Romanin
Posts: 394
Joined: Thu 4. Dec 2003, 11:12
Location: Udine, Italy
Contact:

Post by Fulvio Romanin »

nice work, man - really
but can't i just say {COMMENTS} on the template on each article? maybe i didn't get it...
Completeness is reached through subtraction, not through addition
Cassiel
Posts: 24
Joined: Tue 22. Jun 2004, 13:52
Location: Friedrichshafen, Germany
Contact:

Post by Cassiel »

Thanks, you can't only use {COMMENT} because you must get the article and category ID within the article listing, and there's no other way to get it if you aren't using the PHP-style hack within every article listing template.

Btw: I've forgot the core code hack I've made if you're using the anchors, I'll add them a few minutes later. If you don't do this core code hack you won't be able to use the mod_rewrite function correctly.

Edit: Core code hack added, please see above. Only needed if you're using the mod_rewrite function of phpwcms.
User avatar
Kosse
Posts: 1066
Joined: Thu 9. Sep 2004, 12:08
Location: Brussels, Belgium
Contact:

Post by Kosse »

Hi Cassiel,

nice job, really neat, do u have a working sample?
(me 2 lazzy to install all... must confess) :oops:

Thx
Cheers
Cassiel
Posts: 24
Joined: Tue 22. Jun 2004, 13:52
Location: Friedrichshafen, Germany
Contact:

Post by Cassiel »

Hi Kosse, yes of course! Just visit my website (click on the www-button below this post). ;)
User avatar
Kosse
Posts: 1066
Joined: Thu 9. Sep 2004, 12:08
Location: Brussels, Belgium
Contact:

Post by Kosse »

Schön, schön! Very nice indeed

thx!! (and btw I did never noticed/used the link www in this forum... :oops:

Cheers
Cassiel
Posts: 24
Joined: Tue 22. Jun 2004, 13:52
Location: Friedrichshafen, Germany
Contact:

Post by Cassiel »

Thanks dude, there are also two other replacement tags I wrote on my website:

- Category "Notizblock": iTunes integration (uploading of images and saving to database done through AppleScript) but only on Mac OS X

- Category "Ausrüstung": Screenshot integration (uploading of concurrent screenshot also done through AppleScript) but only on Mac OS X

If somene has interests on this replacement tag (although there only in combination with Mac OS X) just post a comment ...
FeldeMathi
Posts: 58
Joined: Fri 15. Jul 2005, 11:04

Post by FeldeMathi »

Schöne Sache !!
Sowas habe ich schon lange gesucht ! DANKE

Ich hab nur ein Problem mit Schritt 3.

Wenn ich den Code in meine sample.tmpl einfüge dann gibt er mir einfach den Code als Text aus. Wenn ich das per Hand probiere, also {COMMENTS:8:5}, dann steht in jedem Fall "write comment".

Ne Ahnung was ich falsch mache ? Habe auch probiert die Datei unter sample.php zu speichern... Wahrscheinlich hab ich irgendwas noch nicht verstanden...
Cassiel
Posts: 24
Joined: Tue 22. Jun 2004, 13:52
Location: Friedrichshafen, Germany
Contact:

Post by Cassiel »

Which version of phpwcms are you using?
The replacement tag was developed using version 1.2.5-DEV.
FeldeMathi
Posts: 58
Joined: Fri 15. Jul 2005, 11:04

Post by FeldeMathi »

1.2.3....

could cause the problem. I'll try to update my system.
Is it important to rename the sample.tmpl to sample.php ??

Thanks for the answer !
Cassiel
Posts: 24
Joined: Tue 22. Jun 2004, 13:52
Location: Friedrichshafen, Germany
Contact:

Post by Cassiel »

There's no need to rename your template file. The PHP-code doesn't gets intepreted by your web-servers PHP module, instead it gets intepreted by phpwcms internal functions.
frold
Posts: 2151
Joined: Tue 25. Nov 2003, 22:42

Post by frold »

seems like this is a demosite: http://www.cassiel.info/
http://www.studmed.dk Portal for doctors and medical students in Denmark
Cassiel
Posts: 24
Joined: Tue 22. Jun 2004, 13:52
Location: Friedrichshafen, Germany
Contact:

Post by Cassiel »

This is my real web-site, not only a demo-site. :P
FeldeMathi
Posts: 58
Joined: Fri 15. Jul 2005, 11:04

Post by FeldeMathi »

Ok,

I just tried it with an running 1.2.5 DEV version and I have still the same issue. Looks like that it reads the article id, but not the article cid.

I still see the full php code written on my website instead of the "... comments".

Any ideas ?
Cassiel
Posts: 24
Joined: Tue 22. Jun 2004, 13:52
Location: Friedrichshafen, Germany
Contact:

Post by Cassiel »

Have you done any hack to the core code of phpwcms?
Post Reply