Location: PHPKode > projects > Write2Left > Build/Tags/Comments.php
<?php
/*
+------------------------------------------------------
| Write2Left
| (c) timdorr
| http://www.write2left.com
| hide@address.com
| See License.txt for license info
|------------------------------------------------------
| Script: Comments.php
| Description:
|   Adds comments to a page
| Created Jul-30-2003
+------------------------------------------------------
*/

/* Class: TagComments
 * Description:
 *   Parses block tags
 */
class TagComments
{
	function register_callbacks( &$callbacks )
	{
		$callbacks['CommentsList']   = array( 'TagComments', 'comment_list' );
		
		$callbacks['CommentID']                = array( 'TagComments', 'do_tags' );
		$callbacks['CommentBody']              = array( 'TagComments', 'do_tags' );
		$callbacks['CommentAuthorName']        = array( 'TagComments', 'do_tags' );
		$callbacks['CommentAuthorEmail']       = array( 'TagComments', 'do_tags' );
		$callbacks['CommentAuthorURL']         = array( 'TagComments', 'do_tags' );
		$callbacks['CommentTime']              = array( 'TagComments', 'do_tags' );
	}

	function comment_list( $tag_name, $context, $attributes, $content, $filename, $settings )
	{
	    global $W2L;

		if( !array_key_exists( 'maxitems', $attributes ) ) 
            $attributes['maxitems'] = -1;
        if( !array_key_exists( 'order', $attributes ) ) 
            $attributes['order'] = 'descend';
        if( $attributes['order'] == 'ascend' )
        	$order = 'true'; 
        else
        	$order = 'false';

		return CommentsCode::list_start( $settings['cache_path'], $attributes['maxitems'],  $order ) .
			   $content .
			   CommentsCode::list_end();
	}

	function do_tags( $tag_name, $context, $attributes, $content, $filename, $settings )
	{
		global $W2L;

		switch( $tag_name )
		{
			case 'CommentID':
                return '<?= $comment_data[\'comment_id\']; ?'.'>';
            case 'CommentBody':
                return '<?= $comment_data[\'body\']; ?'.'>';
            case 'CommentAuthorName':
                return '<?= $comment_data[\'author_name\']; ?'.'>';
            case 'CommentAuthorEmail':
                return '<?= $comment_data[\'author_email\']; ?'.'>';
            case 'CommentAuthorURL':
                return '<?= $comment_data[\'author_url\']; ?'.'>';
	        case 'CommentTime':
				if( !array_key_exists( 'format', $attributes ) )
					$attributes['format'] = $settings['date_format'];
	            return '<?= date( \'' . $attributes['format'] . '\', $comment_data[\'time\'] ); ?'.'>';
			default:
				return $content;
		}
	}
}

class CommentsCode
{
	function list_start( $cache, $maxitems = -1, $order = 'false' )
	{
		return <<<EOF
<?php
if( array_key_exists( 'id', \$INPUT ) || isset( \$item_data ) )
{
	if( isset( \$item_data ) )
		\$id = \$item_data['post_id'];
	else
		\$id = \$INPUT['id'];
	if( file_exists( "{$cache}item.\$id.cache.php" ) )
	{
		include_once( "{$cache}item.\$id.cache.php" );
		
		if($order)
		\$comments = array_reverse( \$comments );
		for( \$c = 0; \$c < count( \$comments ); \$c++ )
		{
		if( \$c == {$maxitems} ) break;
		\$comment_data = \$comments[\$c];
?>
EOF;
	}

	function list_end()
	{
		return <<<EOF
<?php } } } ?>
EOF;
	}
}

$tag_parser[] = new TagComments();
?>
Return current item: Write2Left