Location: PHPKode > projects > Write2Left > Base/FindPosts.php
<?php
/*
+------------------------------------------------------
| Write2Left
| (c) timdorr
| http://www.write2left.com
| hide@address.com
| See License.txt for license info
|------------------------------------------------------
| Script: FindPosts.php
| Description:
|   Searchs for and displays found posts
| Created Dec-21-2002
+------------------------------------------------------
*/

/* Class: FindPosts
 * Description:
 *   Driver for our post finder
 */
class FindPosts
{
	var $skin = "";
	var $menu = true;
	
	function run()
	{
		global $W2L, $userinfo, $output, $db, $func;
		
		// Do skin related stuff...
		require( "./Skin/FindPosts.php" );
		$this->skin = new Skin_FindPosts();
		$output->page_title = "Find Posts";
		$output->loc_add( "Find Posts" );
		$output->add( $this->skin->body_top() );
		
		// Handle methods now to deal with submitted posts
		if ( array_key_exists( 'M', $W2L->input ) )
		{
			if( $W2L->input['M'] == 'search' )
			{
				if ( !array_key_exists( 'all_words', $W2L->input ) )
					$W2L->input['all_words'] = 0;
				$this->show_results( $W2L->input['keywords'], 
									 $W2L->input['cat_id'],
									 $W2L->input['all_words'] );
										 
			}
		}
		
		// Get the  category list for the form
		$category_ids[]   = 0;
		$category_names[] = 'All Categories';
		
		$db->query( "SELECT category_id, name FROM w2l_categories
											  WHERE log_id=$userinfo->log_id" );
		while( $row = $db->fetch_array() )
		{
			$category_ids[]   = $row['category_id'];
			$category_names[] = $row['name'];
		}
		$cats = $func->create_option_list( $category_ids, 
										   $category_names, 
										   0 );
		
		$output->add( $this->skin->search_form( $cats ) );
		
		$output->add( $this->skin->body_bottom() );
	}	
	
	function show_results( $keywords, $cat_id, $all_words )
	{
		global $userinfo, $db, $output;
		
		$felix_the_cat = "";
		if( $cat_id != 0 )
			$felix_the_cat = "AND p.category_id = $cat_id";
			
		// Clean up
		$keywords = trim( strtolower( $keywords ) );
		$keywords = str_replace( "%", "\\%", $keywords );
		$keywords = str_replace( "_", "\\_", $keywords );
		
		// Boom!
		$keywords = explode( ' ', $keywords );
		
		// Compile the where clause
		$w3rds = '';
		$murf = '';
		foreach( $keywords as $word )
		{
			$w3rds .= "$murf ( LOWER(p.title)    LIKE '%$word%' 
						    OR LOWER(p.body)     LIKE '%$word%'  
						    OR LOWER(p.extended) LIKE '%$word%' ) ";  
			if( $all_words == 1 )
				$murf = 'AND';
			else
				$murf = 'OR';
		}
		
		
		// Find the buggers
		$id = $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
					        $felix_the_cat
					    AND ( $w3rds )
					  ORDER BY time DESC" );
		
		$output->add( $this->skin->result_head( $db->num_rows( $id ) ) );
		while( $row = $db->fetch_array( $id ) )
		{
			if ( strlen( $row['title'] ) > 40 ) 
            	$row['title'] = substr( $row['title'], 0, 40 ) . '...';
            	
			$output->add( $this->skin->result_row( $row['post_id'], 
												   $row['title'], 
												   date( "m/d/y" , $row['time'] ), 
												   $row['a_name'], 
												   $row['c_name'] ) );
		}
		$output->add( $this->skin->result_foot() );
	}
}

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