Location: PHPKode > projects > Write2Left > Base/EditPosts.php
<?php
/*
+------------------------------------------------------
| Write2Left
| (c) timdorr
| http://www.write2left.com
| hide@address.com
| See License.txt for license info
|------------------------------------------------------
| Script: EditPosts.php
| Description:
|   Shows previous posts for editing or deletion
| Created Sep-07-2002
+------------------------------------------------------
*/

/* Class: EditPosts
 * Description:
 *   Driver for editing/deleting posts
 */
class EditPosts
{
	var $skin = "";
	var $menu = true;	
	
	function run()
	{
		global $W2L, $userinfo, $output, $db, $func;
		
		// Do some skin schtuff
		require( "./Skin/EditPosts.php" );
		$this->skin = new Skin_EditPosts();
		$output->page_title = "Manage Posts";
		$output->loc_add( "Manage Posts" );
		$output->add( $this->skin->body_top( ) );
		
		// Handle any methods
		if( array_key_exists( 'M', $W2L->input ) )
		{
			if( $W2L->input['M'] == 'del' )
				$this->del_post( $W2L->input['id'] );
		}
		
		// Set the default limit and offset of posts to show
		$limit = 20;
		$start = 0;		
		if( array_key_exists( 'limit', $W2L->input ) )
			$limit = $W2L->input['limit'];
		if( array_key_exists( 'start', $W2L->input ) )
			$start = $W2L->input['start'];
		
		// Get the total post count for calculations
		$data = $db->query_fetch( "SELECT COUNT(*) AS count FROM w2l_posts 
													WHERE log_id={$userinfo->log_id}" );
		$total_posts = $data['count'];
		$total_pages = ceil( $total_posts / $limit );
		$cur_page = ( $start / $limit ) + 1;
		
		// Set default output
		$prev = "Previous {$limit}";
		$next = "Next {$limit}";
		$pages = "Pages: (1) <b>1</b>";
		
		// Compute page links
		if( $start != 0 )
			$prev = "<a href='?Action=EditPosts&amp;limit=$limit&amp;start=". ($start - $limit) ."'>Previous $limit</a>";
		if( $start + $limit < $total_posts )
			$next = "<a href='?Action=EditPosts&amp;limit=$limit&amp;start=". ($start + $limit) ."'>Next $limit</a>";
		
		$page_list = '';
		for( $page = 1; $page < $total_pages + 1; $page++ )
		{
			if( $page == $cur_page )
				$page_list .= "<b>$page</b>";
			else if( $page < $cur_page - 2 && $page == 1 )
				$page_list .= " <a href='?Action=EditPosts&amp;limit=$limit'>&laquo; 1</a> ";
			else if( $page < $cur_page - 3 && $page == 2 )
				$page_list .= ' ... ';
			else if( $page < $cur_page && $page > $cur_page - 3 )
				$page_list .= " <a href='?Action=EditPosts&amp;limit=$limit&amp;start=". $limit * ( $page - 1 ) ."'>$page</a> ";
			else if( $page > $cur_page && $page < $cur_page + 3 )
				$page_list .= " <a href='?Action=EditPosts&amp;limit=$limit&amp;start=". $limit * ( $page - 1 ) ."'>$page</a> ";
			else if( $page > $cur_page + 3 && $page == $total_pages - 1 )
				$page_list .= ' ... ';
			else if( $page > $cur_page + 2 && $page == $total_pages )
				$page_list .= " <a href='?Action=EditPosts&amp;limit=$limit&amp;start=". ( $total_pages - 1 ) * $limit ."'>$total_pages &raquo;</a>";
		}
		
		$pages = "Pages: ($total_pages) $page_list";
		
		// Print the header stuff
		$output->add( $this->skin->body_posts_head( $prev, $next, $pages ) );
		
		// Output our list of posts
		$db->query( "SELECT post_id, 
							a.name AS a_name, 
							title, 
							time, 
							c.name AS c_name 
					 FROM w2l_posts AS p 
					 	  LEFT JOIN w2l_users AS a ON a.user_id = p.author_id 
					 	  LEFT JOIN w2l_categories as c ON p.category_id = c.category_id
					 WHERE p.log_id={$userinfo->log_id} 
					 ORDER BY time DESC
					 LIMIT $start, $limit" );
		while( $row = $db->fetch_array() )
		{
			if ( strlen( $row['title'] ) > 40 ) 
            	$row['title'] = substr( $row['title'], 0, 40 ) . '...';
     		
			$output->add( $this->skin->body_posts_row( $row['post_id'], $row['title'], date( "m/d/y" , $row['time'] ), $row['a_name'], $row['c_name'] ) );
		}
		
		// Print the footer stuff
		$output->add( $this->skin->body_posts_foot( $prev, $next, $pages ) );
		
		$output->add( $this->skin->body_bottom() );
	}
	
	//================
	// Removes a post from the database
	//================
	function del_post( $id )
	{
		global $db;
        
        // Get some data we'll need later...
		$data = $db->query_fetch( "SELECT * FROM w2l_posts WHERE post_id=$id" );
		
		// Remove the post and comments
		$db->query( "DELETE FROM w2l_posts WHERE post_id=$id" );
		$db->query( "DELETE FROM w2l_comments WHERE post_id=$id" );

        // Re-build
		require( "./Build/BuildManager.php" );
		//$BM->cache_data( "News" );
	}
}

$driver = new EditPosts();
?>
Return current item: Write2Left