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

/* Class: TagPosts 
 * Description:
 *   Parses post-related tags tags
 */
class TagPosts
{
	function register_callbacks( &$callbacks )
	{
		$callbacks['PostList'] = array( 'TagPosts', 'post_list' );

		$callbacks['PostListHead'] = array( 'TagPosts', 'post_head' );
		$callbacks['PostListFoot'] = array( 'TagPosts', 'post_foot' );

		$callbacks['DateHead'] = array( 'TagPosts', 'date_head' );
		$callbacks['DateFoot'] = array( 'TagPosts', 'date_foot' );

		$callbacks['PostID']                = array( 'TagPosts', 'do_tags' );
		$callbacks['PostTitle']             = array( 'TagPosts', 'do_tags' );
		$callbacks['PostBody']              = array( 'TagPosts', 'do_tags' );
		$callbacks['PostExtended']          = array( 'TagPosts', 'do_tags' );
		$callbacks['PostAuthorName']        = array( 'TagPosts', 'do_tags' );
		$callbacks['PostAuthorID']          = array( 'TagPosts', 'do_tags' );
		$callbacks['PostCommentCount']      = array( 'TagPosts', 'do_tags' );				$callbacks['PostLastCommentName']   = array( 'TagPosts', 'do_tags' );
		$callbacks['PostTime']              = array( 'TagPosts', 'do_tags' );
		$callbacks['PostCategory']          = array( 'TagPosts', 'do_tags' );
        $callbacks['PostCategoryID']        = array( 'TagPosts', 'do_tags' );
        
		$callbacks['PostIfTitle']			= array( 'TagPosts', 'do_ifs' );
		$callbacks['PostIfExtended']        = array( 'TagPosts', 'do_ifs' );
		$callbacks['PostIfCategory']        = array( 'TagPosts', 'do_ifs' );
		$callbacks['PostIfAllowComments']   = array( 'TagPosts', 'do_ifs' );
		$callbacks['PostIfAllowPings']      = array( 'TagPosts', 'do_ifs' );
	}

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

        if( !array_key_exists( 'category', $attributes ) ) 
            $attributes['category'] = 'all';
		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 PostCode::list_start( $settings['cache_path'], $attributes['maxitems'], $attributes['category'], $order ) .
			   $content .
			   PostCode::list_end();
	}

	function post_head( $tag_name, $context, $attributes, $content, $filename, $settings )
	{
		return '<?php if( $i == 0 ) { ?'.'>' .
			   $content .
			   '<?php } ?'.'>';
	}

	function post_foot( $tag_name, $context, $attributes, $content, $filename, $settings )
	{
		return '<?php if( $i+1 == count( $posts ) )' .
			   "	  if( date( 'z',\$item_data['time'] ) != date( 'z', \$posts[\$i+1]['time'] ) ) { ?".'>' .
			   $content .
			   '<?php } ?'.'>';
	}

	function date_head( $tag_name, $context, $attributes, $content, $filename, $settings )
	{
		return "<?php if( date( 'z', \$item_data['time'] ) != \$last_time ) { ?".">" .
			   $content .
			   "<?php } ?".">";
	}

	function date_foot( $tag_name, $context, $attributes, $content, $filename, $settings )
	{
		return "<?php if( \$i+1 < count( \$posts[\$i] ) )
					  if( date( 'z', \$item_data['time'] ) != date( 'z', \$posts[\$i+1]['time'] ) ) { ?".">" .
			   $content .
			   "<?php } ?".">";
	}

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

		switch( $tag_name )
		{
	        case 'PostID':
                return '<?= $item_data[\'post_id\']; ?'.'>';
	        case 'PostTitle':
	            return '<?= $item_data[\'title\']; ?'.'>';
	        case 'PostBody':
	        	if( array_key_exists( 'words', $attributes ) )
	            	return "<?php \$words = explode( ' ', \$item_data['body'] ); for(\$ii = 0; \$ii < $attributes[words]; \$ii++ ) print \$words[\$ii] . ' '; ?".">";
	            else
	            	return '<?= $item_data[\'body\']; ?'.'>';
	        case 'PostExtended':
	            if( array_key_exists( 'words', $attributes ) )
	            	return "<?php \$words = explode( ' ', \$item_data['extended'] ); for(\$ii = 0; \$ii < $attributes[words]; \$ii++ ) print \$words[\$ii] . ' '; ?".">";
	            else
	            	return '<?= $item_data[\'extended\']; ?'.'>';
	        case 'PostAuthorName':
	            return '<?= $item_data[\'author_name\']; ?'.'>';
	        case 'PostAuthorID':
	            return '<?= $item_data[\'author_id\']; ?'.'>';
	        case 'PostCommentCount':
	            return '<?= $item_data[\'comment_count\']; ?'.'>';
	        case 'PostLastCommentName':
	            return '<?= $item_data[\'last_comment_name\']; ?'.'>';
	        case 'PostTime':
				if( !array_key_exists( 'format', $attributes ) )
					$attributes['format'] = $settings['date_format'];
	            return '<?= date( \'' . $attributes['format'] . '\', $item_data[\'time\'] ); ?'.'>';
            case 'PostCategory':
                 return '<?= $categories[$item_data[\'category_id\']]; ?'.'>';
            case 'PostCategoryID':
                 return '<?= $item_data[\'category_id\']; ?'.'>';
			default:
				return $content;
		}
	}
	
	function do_ifs( $tag_name, $context, $attributes, $content, $filename, $settings )
	{
		global $W2L;

		switch( $tag_name )
		{
			case 'PostIfTitle':
                 return '<?php if( $item_data[\'title\'] != "" ) { ?>'
                 		. $content
                 		. '<?php } ?>';
			case 'PostIfExtended':
                 return '<?php if( $item_data[\'extended\'] != "" ) { ?>'
                 		. $content
                 		. '<?php } ?>';
            case 'PostIfCategory':
                 return '<?php if( $item_data[\'category_id\'] != 0 ) { ?>'
                 		. $content
                 		. '<?php } ?>';
            case 'PostIfAllowComments':
                 return '<?php if( $item_data[\'allow_comments\'] == 1 ) { ?>'
                 		. $content
                 		. '<?php } ?>';
            case 'PostIfAllowPings':
                 return '<?php if( $item_data[\'allow_pings\'] == 1 ) { ?>'
                 		. $content
                 		. '<?php } ?>';
			default:
				return $content;
		}
	}
}

class PostCode
{
	function list_start( $cache, $maxitems = -1, $cat = 0, $order = 'false' )
	{
		return <<<EOF
<?php
include_once( '{$cache}post.cache.php' );
\$posts = \$post_cache['$cat'];
if($order)
	\$posts = array_reverse( \$posts );
\$last_time = -1;
for( \$i = 0; \$i < count( \$posts ); \$i++ )
{
if( \$i == {$maxitems} ) break;
\$item_data = \$posts[\$i];
?>
EOF;
	}

	function list_end()
	{
		return <<<EOF
<?php \$last_time = date( 'z', \$item_data['time'] ); } ?>
EOF;
	}
}

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