Location: PHPKode > projects > Write2Left > Base/Cats.php
<?php
/*
+------------------------------------------------------
| Write2Left
| (c) timdorr
| http://www.write2left.com
| hide@address.com
| See License.txt for license info
|------------------------------------------------------
| Script: Cats.php
| Description:
|   Manages categories for posting
| Created Dec-05-2002
+------------------------------------------------------
*/

/* Class: Cats
 * Description:
 *   Driver for our category settings page. Meow!
 */
class Cats
{
	var $skin = '';
	var $menu = true;
	var $message = '';
	
	function run()
	{
		global $W2L, $userinfo, $output, $db;
		
		// Do various skin thingys
		require( "./Skin/Cats.php" );
		$this->skin = new Skin_Cats();
		$output->page_title = "Manage Categories";
		$output->loc_add( "Categories" );
		$output->add( $this->skin->body_top() );
		
		// Check for a method
		if( array_key_exists( 'M', $W2L->input ) )
		{
			if( $W2L->input['M'] == 'doadd' )
				$this->do_add( $W2L->input['name'], $W2L->input['desc'] );
			if( $W2L->input['M'] == 'edit' )
				$this->edit_cat( $W2L->input['id'] );
			if( $W2L->input['M'] == 'doedit' )
				$this->do_edit( $W2L->input['id'], $W2L->input['name'], $W2L->input['desc'] );
			if( $W2L->input['M'] == 'del' )
				$this->do_del( $W2L->input['id'] );
		}

		// Show whatever result we get, if any
		if( $this->message != "" )
			$output->add( "<div class=\"message\">\n" . $this->message . "\n</div><br />\n" );

		
		// Get the category list
		$db->query( "SELECT c.category_id, 
							c.name, 
							COUNT(post_id) as post_count 
						FROM w2l_categories AS c 
						LEFT JOIN w2l_posts AS p
							ON p.category_id = c.category_id 
						WHERE c.log_id = $userinfo->log_id
						GROUP BY c.name" );
		
		// Render the list of categories
		$output->add( $this->skin->cats_head() );
		while( $row = $db->fetch_array() )
		{
			$output->add( $this->skin->cats_row( $row['name'], 
												 $row['post_count'], 
												 $row['category_id'] ) );
		}
		$output->add( $this->skin->cats_foot() );
		
		$output->add( $this->skin->body_bottom() );
	}
	
	//================
	// Adds a new category
	//================
	function do_add( $name, $desc )
	{
		global $db, $userinfo;
		$db->query( "INSERT INTO w2l_categories ( log_id, 			 name, 	  description ) 
										 VALUES ( $userinfo->log_id, '$name', '$desc' )" );
		
		$this->message = 'Category Added';
	}
	
	//================
	// Changes the category details
	//================
	function do_edit( $id, $name, $desc )
	{
		global $db;
		$db->query( "UPDATE w2l_categories SET name='$name', description='$desc' WHERE category_id=$id" );
		
		$this->message = 'Category Edited';
	}
	
	//================
	// Removes a category. PHP/MySQL handle this fairly well without removing news items.
	//================
	function do_del( $id )
	{
		global $db;
		$db->query( "DELETE FROM w2l_categories WHERE category_id=$id" );
		$db->query( "UPDATE FROM w2l_posts SET category_id = 0 WHERE category_id=$id" );
		
		$this->message = 'Category Removed';
	}
	
	//================
	// Show the edit category screen
	//================
	function edit_cat( $id )
	{
		global $db, $output;
		
		$cat = $db->query_fetch( "SELECT * FROM w2l_categories WHERE category_id=$id" );
		$output->add( $this->skin->cat_edit_form( $cat['category_id'], 
												  $cat['name'], 
												  $cat['description'] ) );
	}
}

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