Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-07-31 02:18:04

kkobashi
Member
Registered: 2008-01-27
Posts: 51
Website

Showing recent comments for a section

txp:recent_comments doesn’t seem to do the trick as it is appears to generate comments across the entire website. i just want to show recent comments coming from a particular section (and even category).


Kerry Kobashi
Kobashi Computing

Offline

#2 2008-07-31 04:29:13

whaleen
Member
From: Portland
Registered: 2006-05-11
Posts: 373
Website

Re: Showing recent comments for a section

You might try wyn_recent_comments.

txp:recent_comments doesn’t have that capability.


txtstrap (Textpattern + Twitter Bootstrap + etc…)

Offline

#3 2008-07-31 05:07:07

kkobashi
Member
Registered: 2008-01-27
Posts: 51
Website

Re: Showing recent comments for a section

I was just looking at the textpattern source code for txp:recent_comments and about to create a plug in with a minor mod.

WynRecentComments could save me some time. The code looks a lot cleaner and easier to read too.

Nice catch whaleen. I will give it a try.

Last edited by kkobashi (2008-07-31 05:13:05)


Kerry Kobashi
Kobashi Computing

Offline

#4 2008-07-31 05:43:17

kkobashi
Member
Registered: 2008-01-27
Posts: 51
Website

Re: Showing recent comments for a section

I got this thing to work and what a bizarre plug-in this one is for formatting. Very tricky.

Due to the lack of an example by the author to show how to use the tag and his website to be in some Asian language (Chinese? Japanese? Korean?), I’m providing what I did to get it to work here. At first if you use this tag you may think that it is not working and get nothing as output. But after tinkering with the format attribute you’ll see how to use it.

First, here is the CSS used to make things look good:

.recent-comments h2 {
  color: red;
}
.recent-comments p.recent-comment-content {
  text-decoration: none;
  color: black;
}
.recent-comments p.recent-comment-time {
  text-decoration: none;
  font-size: 80%;
  color: black;
}

Second, here is the tag call:

<txp:wyn_recent_comments section="foobar" class="recent-comments" label="Recent Comments"
       labeltag="h1" limit="10" wordcount="100"
       format="<p>Here are the most recent comments from the foobar section</p><h2>%title%</h2><p class='recent-comment-content'>%content%</p><p class='recent-comment-time'>%time% by %commenter%"/>

Since I cannot duplicate the output here as textile is so fussy, try the above. It assumes there is a foobar section with comments in it. Notice that you also have to use single quotes inside the format attribute or you will get errors. The format attribute is where all the magic happens.

The plug-in wraps the entire output in a HTML anchor tag so the CSS basically turns off the underlining in the content and time paragraphs. Weird but it works only in IE (doesnt work in Firefox). If anyone knows how to make it so that it works for both let me know. Note that I tried using:

.recent-comments p.recent-comment-content a {
  text-decoration: none;
  color: black;
}
.recent-comments p.recent-comment-time a {
  text-decoration: none;
  font-size: 80%;
  color: black;
}

And in this case Firefox gets it right but IE does the opposite and shows all red text. There’s got to be a fix with this.

Last edited by kkobashi (2008-07-31 07:31:16)


Kerry Kobashi
Kobashi Computing

Offline

#5 2008-07-31 13:08:04

kkobashi
Member
Registered: 2008-01-27
Posts: 51
Website

Re: Showing recent comments for a section

wyn_recent_comments needed some mods to allow for easier styling and better semantic markup.

Change 1: Semantically, each comment should point to the comment fragment itself within the article and not the article itself.

Change 2: Added a quoteChar attribute to allow the programmer to add character to the front and end of the comment in quote style.

Change 3: SQL cols statement needs the discussid field to uniquely identify the comment

Change 4: Don’t allow the comment to be part of the anchor tag title.

Change 5: The anchor tag is a excerpt of the comment.

Change 6: The anchor tag href points to the article and the comment within it (named anchor)

Change 7: Detect no comments returned and return a no comments string.

function wyn_recent_comments ($atts, $thing) {
	extract (lAtts (array (
		'class'    => __FUNCTION__,
		'wraptag' => 'ul',
		'break'  => 'li',
		'label' => '',
		'labeltag' => '',
		'limit'    => 10,
                  'quotechar' => '"',  /* KHK added */
		'wordcount' => 10,
		'messagebreak' => '...',
		'format' => '%content%',
		'atitle' => '%commenter% on %time%',
		'section' => '',
		'timeformat' => '%d %b %Y · %X',
		), $atts));

	/* create sql things */
	/* $cols = 'ID, name, unix_timestamp(txp_discuss.posted) as time, message, Title'; */

         /* KHK use the comment id (discussid) to uniquely identify in URI */
         $cols = 'ID, name, unix_timestamp(txp_discuss.posted) as time, message, Title, discussid';
	$tables = safe_pfx_j ('txp_discuss, textpattern');
	$where = 'parentid=ID and visible=1' . ('' == trim ($section) ? '' : " and Section='$section'") . ' order by time desc limit ' . $limit;

	$sql = 'SELECT ' . $cols . ' FROM ' . $tables . ' WHERE ' . $where . ';';

	$rs = startRows ($sql);
	if ($rs) {
		$out = array ();
		while ($o = nextRow ($rs)) {
			extract ($o);

			$time = safe_strftime($timeformat, $time);

			/* 0 if for no limit */
			if ($wordcount != 0) {
				$message = trim (doStripTags ($message));
				$message = utf8_substr ($message, 0, $wordcount, $messagebreak);
			}

/* KHK: The anchor tag title CAN contain the title, commenter and time. It makes no sense to put in the message as it can contain a crapload of content */

			$atitleattr = preg_replace ('/%title%/', $Title, $atitle);
			$atitleattr = preg_replace ('/%commenter%/', $name, $atitleattr);
			$atitleattr = preg_replace ('/%time%/', $time, $atitleattr); 
/*			$atitleattr = preg_replace ('/%content%/', $message, $atitleattr);  who will use content here ??? */

			$cont = preg_replace ('/%title%/', $Title, $format);
			$cont = preg_replace ('/%commenter%/', $name, $cont);
			$cont = preg_replace ('/%time%/', $time, $cont);
			$cont = preg_replace ('/%content%/', $message, $cont);

                        /* KHK: The anchor text will be the message itself. Add the comment id and quoteChar */
                        /* $link = '<a href="' . permlinkurl_id ($ID) . '" title="' . $atitleattr . '">' . $cont . '</a>'; */

			$link = '<p class="recent-comment-clink"><a href="' . permlinkurl_id ($ID) . '#c' . $discussid . '" title="' . $atitleattr . '">' . $quotechar . $message . $quotechar  . '</a></p>';
                           /* $out[] = $link; */

                           /* KHK: link message first, content next */
                           $out[] = $link . $cont;
		}

                /* KHK Detect no comments */
                if (count($out) <= 0) {
                   $out[] = "<p>No comments.</p>";
                   return doLabel ($label, $labeltag) . doWrap ($out, "", "", $class);
                }

		return doLabel ($label, $labeltag) . doWrap ($out, $wraptag, $break, $class);
	}
}

function utf8_substr($string, $start, $length, $wordbreak) {
	/* just return original thing if length is 0 or less than 0 */
	if ($length <= 0) {
		return $string;
	}

	preg_match_all ('/[\x00-\x7F]|[\xC2-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF][\x80-\xBF]|[\xF0-\xF4][\x80-\xBF][\x80-\xBF][\x80-\xBF]/', $string, $rs);

	$out = '';
        $size = count ($rs[0]);

	$end = $start + $length;
	if ($end > $size - 1) {
		$end = $size;
	}

	for ($i = $start; $i < $end; $i++) {
      	        $out .= $rs[0][$i];
	}

	if ($end < $size - 1) {
		$out .= $wordbreak;
	}

	return $out;
}

In the comments form that gets iterated repeatedly for each comment, one needs to do the following to allow for named bookmarks:


<a name="c<txp:comment_id/>"></a>
<txp:comment_time format="%A, %B %e %Y at %H:%m" /> by <txp:comment_name/>
<txp:comment_message/>

Notice that an empty anchor tag is created and the comment_id is inserted. And no, that is not a typo with the leading ‘c’. Textpattern comment anchors are cXXXXXX where X is 0-9.

Example usage of modified wyn_recent_comments:

<txp:wyn_recent_comments section="foobar" class="recent-comments" label="Recent Comments" labeltag="h3" limit="10" wordcount="100" format="<p class='recent-comment-author'>- %commenter%</p><p class='recent-comment-title'>%title%" atitle="%title% - %commenter% on %time%"/>

The CSS stylesheet

.recent-comments li {
  margin-bottom: 0.5em;
  padding-bottom: 0.5em;
  border-bottom: 1px dotted #333;
}
.recent-comments li a {
  text-decoration: none;
}
.recent-comment-author {
  color: black;
  text-align: right;
  font-size: 90%;
}
.recent-comment-title {
  color: black;
  text-align: right;
  font-size: 90%;
  line-height: 0; /* simulate a br */
}
.recent-comment-clink {
  font-style: italic;
}
.recent-comment-clink a:hover {
  text-decoration: underline;
}

Last edited by kkobashi (2008-08-07 07:27:56)


Kerry Kobashi
Kobashi Computing

Offline

Board footer

Powered by FluxBB