Location: PHPKode > projects > PapyrusBB > PapyrusBB-0.6/include/admin/adminCategories.php
<?php
defined('WikyBlog') or die("Not an entry point...");

if( $_SESSION['userlevel'] !== 5){
	global $page;
	$page->contentA['Admin Only'] = 'You must be an administrator to access this page.';
	return;
}


class forumCatAdmin{

	function forumCatAdmin(){
		global $page,$langA,$dbObject,$rootDir,$pageOwner,$wbPluginSpace;

		
		pluginIncludeFile('include/allForumScripts.php');
		wbLang::pluginFile('admin.php');
		
		
		$dbObject->links[$langA['forum_admin']] = '/Admin/'.$pageOwner['username'].'/'.$wbPluginSpace.'/Admin';
		$dbObject->links[$langA['categories']] = '/Admin/'.$pageOwner['username'].'/'.$wbPluginSpace.'/Categories';
		$dbObject->links[$langA['permissions']] = '/Admin/'.$pageOwner['username'].'/'.$wbPluginSpace.'/Permissions';
		$dbObject->links['forums'] = '/'.$wbPluginSpace;
		
		
		$this->title = $page->displayTitle = $langA['categories'];
		$page->formAction = $dbObject->links[$this->title];
		ob_start();
		
		$showList = true;
		switch($page->userCmd){
			case 'upcat':
				if( $this->updateCat() ){
					$this->categories();
					$showList = false;
					break;
				}
			
			case 'cedit':
				$this->editCat();
				$showList = false;
			break;
			case 'cdelete':
				$this->deleteCat();
				$this->categories();
				$showList = false;
			break;
			
			case 'addcat':
				$this->addCat();
				$this->categories();
				$showList = false;
			break;
		}
		if($showList){
			$this->categories();
		}
		
		$page->contentA[$this->title] = wb::get_clean();
	}
	
	function categories(){
		global $langA,$pageOwner,$wbPluginSpace;
		
		
		echo '<table cellpadding="11" style="margin:0 auto 0 auto;">';
		echo '<tr><td>';
		$this->showCats();
		echo '</td><td>';
		$this->newCatForm();
		echo '</td></tr></table>';
		
		echo '<div style="text-align:center;margin-top:1em;">';
		echo wbLinks::admin($wbPluginSpace.'/Admin?1','forum_admin');
		echo ' - ';
		echo wbLinks::admin($wbPluginSpace.'/Admin?cmd=newforum','new_forum');
		echo '</div>';
		
	}
	function showCats(){
		global $langA,$pageOwner,$wbPluginSpace;
		
		echo '<table class="tableRows">';
		echo '<tr>';
		echo '<th>';
		echo $langA['category'];
		echo '</th>';
		echo '<th>';
		echo $langA['options'];
		echo '</th>';
		echo '</tr>';
		
		$this->setCategories();
		foreach($this->categories as $id => $title){
			echo '<tr>';
			echo '<td>';
			echo $title;
			echo '</td>';
			echo '<td>';
			
			echo wbLinks::admin($wbPluginSpace.'/Admin?cmd=newforum&cat_id='.$id,'add_forum');
			echo ' &nbsp; ';
			echo wbLinks::admin($wbPluginSpace.'/Categories?cmd=cedit&id='.$id,'<img alt="" height="16" width="16" src="'.wbLinks::getDir('/imgs/icons/pencil.gif').'" /> ');
			echo ' &nbsp; ';
			if( empty($this->categoryContents[$id]) ){
				echo wbLinks::admin($wbPluginSpace.'/Categories?cmd=cdelete&id='.$id,'<img alt="" height="16" width="16" src="'.wbLinks::getDir('/imgs/icons/delete.gif').'" /> ');
			}else{
				echo '<img alt="" height="16" width="16" src="'.wbLinks::getDir('/imgs/blank.gif').'" /> ';
			}
			
			
			echo '</td>';
			echo '</tr>';
		}
		echo '</table>';
		

	}

	
	
	function newCatForm(){
		global $langA;
		echo '<table class="tableRows">';
		echo '<tr>';
		echo '<th colspan="2">';
		echo $langA['new_category'];
		echo '</th>';
		echo '</tr>';		
		echo '<tr>';
		echo '<td>';
		echo '<input type="input" name="title" value="" />';
		echo '<input type="hidden" name="cmd" value="addcat" />';
		echo '</td>';
		echo '<td>';
		echo '<input type="submit" name="" value="'.$langA['submit'].'" />';
		echo ' <input type="submit" name="cmd" value="'.$langA['cancel'].'" />';
		echo '</td>';
		echo '</tr>';
		echo '</table>';
	}
	
	function setCategories(){
		global $wbTables;
		$this->categories = array();
		
		//get categories
		$query = 'SELECT '.$wbTables['bb_categories'].'.* ';
		$query .= ', '.$wbTables['bb_forums'].'.`id` as `not_empty` ';
		$query .= ' FROM '.$wbTables['bb_categories'];
		$query .= ' LEFT JOIN '.$wbTables['bb_forums'].' using(`cat_id`)';
		$query .= ' GROUP BY `cat_id` ';
		$query .= ' ORDER BY `position` ASC ';
		$result = wbDB::runQuery($query);
		while($row = mysql_fetch_assoc($result) ){
			$this->categories[$row['cat_id']] = toDisplay($row['title']);
			$this->categoryContents[$row['cat_id']] = $row['not_empty'];
		}
	}

	////////////////////////////////////////////////////////////////////////////////////
	//
	//	Category Creationg/Editing
	//
	function addCat(){
		global $wbTables;
		if( empty($_POST['title']) ){
			message('EMPTY_TITLE');
			return false;
		}
		
		$query = 'INSERT INTO'.$wbTables['bb_categories'];
		$query .= ' (`title`,`position`) ';
		$query .= ' SELECT ';
		$query .= ' "'. wbDB::escape(toStorage($_POST['title'])) .'" ';
		$query .= ' , COUNT(*) FROM '.$wbTables['bb_categories'];
		$result = wbDB::runQuery($query);
		
		return true;
	}
	

	
	function editCat(){
		global $langA,$wbTables;
		if( isset($_POST['id']) && is_numeric($_POST['id']) ){
			$id = $_POST['id'];
			
		}elseif( isset($_GET['id']) && is_numeric($_GET['id']) ){
			$id = $_GET['id'];
		}else{
			message('INVALID_REQUEST');
			return;
		}

		
		$query = 'SELECT * FROM '.$wbTables['bb_categories'];
		$query .= ' WHERE `cat_id` = "'. wbDB::escape($id) .'" ';
		$query .= ' LIMIT 1 OFFSET 0';
		$result = wbDB::runQuery($query);
		if( !$result){
			message('INVALID_REQUEST');
			return;
		}
		
		$row = mysql_fetch_assoc($result);
		
		echo '<input type="hidden" name="id" value="'.$id.'" />';
		
		echo '<table class="tableRows" style="margin:0 auto 0 auto;">';
		echo '<tr>';
		echo '<th colspan="2">';
		echo $langA['edit_category'];
		echo '</th>';
		echo '</tr>';		
		echo '<tr>';
		echo '<td>';
		echo '<input type="input" name="title" value="'.htmlspecialchars(toDisplay($row['title'])).'" />';
		echo '<input type="hidden" name="cmd" value="upcat" />';
		echo '</td>';
		echo '<td>';
		echo '<input type="submit" name="" value="'.$langA['submit'].'" />';
		echo ' <input type="submit" name="cmd" value="'.$langA['cancel'].'" />';
		echo '</td>';
		echo '</tr>';
		echo '</table>';		
	}
	function updateCat(){
		global $wbTables;
		
		if( !isset($_POST['id']) || !is_numeric($_POST['id']) ){
			message('INVALID_REQUEST');
			return false;
		}
		if( empty($_POST['title']) ){
			message('EMPTY_TITLE');
			return false;
		}
		
		$query = 'UPDATE '.$wbTables['bb_categories'];
		$query .= ' SET ';
		$query .= ' `title` = "'. wbDB::escape(toStorage($_POST['title'])) .'" ';
		$query .= ' WHERE ';
		$query .= ' `cat_id` = "'. wbDB::escape($_POST['id']) .'" ';
		$query .= ' LIMIT 1';
		if( !wbDB::runQuery($query) ){
			return false;
		}
		return true;
	}
	
	function deleteCat(){
		global $wbTables;
		
		if( !isset($_GET['id']) || !is_numeric($_GET['id']) ){
			message('INVALID_REQUEST');
			return;
		}
		
		//check for forums within category
		$query = 'SELECT 1 FROM '.$wbTables['bb_forums'];
		$query .= 'WHERE `cat_id` = "'. wbDB::escape($_GET['id']) .'" ';
		$result = wbDB::runQuery($query);
		if( !$result){
			message('INVALID_REQUEST');
			return;
		}
		if( mysql_num_rows($result) > 0 ){
			message('CATEGORY_NOT_EMPTY');
			return;
		}
		
		//get position
		$query = 'SELECT `position` FROM '.$wbTables['bb_categories'];
		$query .= 'WHERE `cat_id` = "'. wbDB::escape($_GET['id']) .'" ';
		$result = wbDB::runQuery($query);
		if( !$result){
			message('INVALID_REQUEST');
			return;
		}
		$row = mysql_fetch_assoc($result);
		$position = $row['position'];
		
		
		$query = 'DELETE FROM '.$wbTables['bb_categories'];
		$query .= 'WHERE `cat_id` = "'. wbDB::escape($_GET['id']) .'" ';
		if( !wbDB::runQuery($query) ){
			message('INVALID_REQUEST');
			return;
		}
		
		//move positions
		$query = 'UPDATE '.$wbTables['bb_categories'];
		$query .= ' SET ';
		$query .= ' `position` = `position`-1 ';
		$query .= ' WHERE ';
		$query .= ' `position` > "'. wbDB::escape($position) .'" ';
		wbDB::runQuery($query);
		
	}	
	

}
new forumCatAdmin();
Return current item: PapyrusBB