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

/* Class: TagArchives 
 * Description:
 *   Parses archive-related tags tags
 */
class TagArchives
{
	function register_callbacks( &$callbacks )
	{
		$callbacks['ArchiveList'] = array( 'TagArchives', 'arc_list' );

		$callbacks['ArchiveListHead'] = array( 'TagArchives', 'arc_head' );
		$callbacks['ArchiveListFoot'] = array( 'TagArchives', 'arc_foot' );
	}

	function arc_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';

        if( array_key_exists( 'default_type', $attributes ) &&
        	array_key_exists( 'default_id',   $attributes ) )
        { 
        	switch( $attributes['default_type'] )
        	{
        		case 'category':
        			$type = 'cat_id';
        			break;
        		case 'year':
        			$type = 'year';
        			break;
        		case 'month':
        			$type = 'month';
        			break;
        		case 'week':
        			$type = 'week';
        			break;
        		default:
        			$type = 'month';
        			break;
        	}
        	
			return ArchiveCode::defaults( $type, $attributes['default_id'] ) .
				   ArchiveCode::list_start( $settings['cache_path'], $attributes['maxitems'], $order ) .
				   $content .
				   ArchiveCode::list_end();
		}
		else
			return ArchiveCode::list_start( $settings['cache_path'], $attributes['maxitems'], $order ) .
				   $content .
				   ArchiveCode::list_end();
	}

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

	function arc_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 do_tags( $tag_name, $context, $attributes, $content, $filename, $settings )
	{
		global $W2L;

		switch( $tag_name )
		{
			default:
				return $content;
		}
	}
}

class ArchiveCode
{
	function list_start( $cache, $maxitems = -1, $order = 'false' )
	{
		return <<<EOF
<?php
if( array_key_exists( 'cat_id', \$INPUT ) ||
	array_key_exists( 'year',   \$INPUT ) ||
	( array_key_exists( 'month',  \$INPUT ) && array_key_exists( 'year', \$INPUT ) ) ||
	( array_key_exists( 'week',   \$INPUT ) && array_key_exists( 'year', \$INPUT ) ) )
{
if( array_key_exists( 'cat_id', \$INPUT ) )
{
	\$mode = 'cat';
	\$id = \$INPUT['cat_id']; 
}
if( array_key_exists( 'year', \$INPUT ) )
{
	\$mode = 'year';
	\$id = \$INPUT['year']; 
}
if( array_key_exists( 'month', \$INPUT ) && array_key_exists( 'year', \$INPUT ) )
{
	\$mode = 'month';
	\$id = \$INPUT['month'].'.'.\$INPUT['year']; 
}
if( array_key_exists( 'week', \$INPUT ) && array_key_exists( 'year', \$INPUT ) )
{
	\$mode = 'week';
	\$id = \$INPUT['week'].'.'.\$INPUT['year']; 
}
if( file_exists( "{$cache}arc.\$mode.\$id.cache.php" ) )
{
include_once( "{$cache}arc.\$mode.\$id.cache.php" );
\$posts = \$arc_cache;
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'] ); } 
} // file_exists
} // array_key_exists
?>
EOF;
	}
	
	function defaults( $type, $id )
	{
		return <<<EOF
<?php
if( !array_key_exists( 'cat_id', \$INPUT ) &&
	!array_key_exists( 'year',   \$INPUT ) &&
	!( array_key_exists( 'month',  \$INPUT ) && array_key_exists( 'year', \$INPUT ) ) &&
	!( array_key_exists( 'week',   \$INPUT ) && array_key_exists( 'year', \$INPUT ) ) )
\$INPUT['$type'] = $id;
?>
EOF;
	}
}

$tag_parser[] = new TagArchives();

?>
Return current item: Write2Left