Location: PHPKode > scripts > Yet Another Link Directory > yald2/admin/categories.php
<?php
/******************************************************************************
* This file is part of Yet Another Link Directory.                            *
*                                                                             *
* Yet Another Link Directory is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or           *
* (at your option) any later version.                                         *
*                                                                             *
* Yet Another Link Directory is distributed in the hope that it will be       *
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of      *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
* GNU General Public License for more details.                                *
*                                                                             *
* You should have received a copy of the GNU General Public License           *
* along with Yet Another Link Directory; if not, write to the Free Software   *
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
******************************************************************************/

require('../inc/config.php');
require('../inc/functions.php');
mysql_connect($mysql['host'],$mysql['username'],$mysql['password']);
mysql_select_db($mysql['db']);

$settings = getSettings();

session_start();

if(!isset($_SESSION['yald_admin_logged_in'])){
	header('Location: login.php');
	exit;
}

$template = file_get_contents('template.html');


if(!empty($_GET['delete'])){
	if(categoryExists($_GET['delete'])){
		$category = $_GET['delete'];
		recursiveCategoryDelete($category);
		header('Location: '.$_SERVER['PHP_SELF']);
		exit;
	}
}

$yald_body = '<b>Manage Categories:</b><br /><small>To add a new category to the index page, use the form at the bottom. To add a subcategory, click the &quot;+&quot; next to any category. The move feature will allow you to move all data, including links and subcategories to another category. Checking the &quot;Display subcategories underneath&quot; box will display the subcategories of that category underneath it in the main directory view.</small><br /><br />';

if (isset($_REQUEST['newsubparent']) && categoryExists($_REQUEST['newsubparent'])){
	if(empty($_POST['name'])){
		$yald_body .= '<form action="'.$_SERVER['PHP_SELF'].'" method="post">
New Subcategory: <input type="text" name="name" /> Display subcategories underneath <input type="checkbox" name="disp_subs" value="1"> 
<input type="hidden" name="newsubparent" value="'.$_GET['newsubparent'].'" />
<input type="submit" value="Submit" /><input type="button" value="Cancel" onclick="window.location=\''.$_SERVER['PHP_SELF'].'\'"/></form><br />';
	} else {

		if(isset($_POST['disp_subs'])){
			$disp_subs = 'true';
		} else {
			$disp_subs = 'false';
		}
		
		$time = time();
		$query = 'INSERT INTO `'.$settings['categories_table'].'` (`name`,`parent`, `disp_subs`,`date`) VALUES ("'.mysql_safe($_POST['name']).'","'.mysql_safe($_POST['newsubparent']).'", "'.$disp_subs.'","'.$time.'")';
		mysql_query($query);
		
		$query = 'SELECT id FROM `'.$settings['categories_table'].'` WHERE parent="'.mysql_safe($_POST['newsubparent']).'" and name="'.mysql_safe($_POST['name']).'" and date="'.$time.'"';
		$result = mysql_query($query);
		$newid = mysql_fetch_array($result);
		$newid = $newid['id'];
		
		logEvent('add_cat','0',$_POST['name']);
		$error = 'Category added successfully. <a href="'.$_SERVER['PHP_SELF'].'">OK</a>';
		rebuildTree('1',1);
		updateDbPath($newid);
	}
} elseif(isset($_GET['move'])){
	if(!isset($_GET['category'])){
		$menu = categoryMenu('1',$_GET['move']);
		$yald_body .= '<form action="'.$_SERVER['PHP_SELF'].'" method="get">
Move data from '.categoryName($_GET['move']).' to: '.$menu.'
<input type="hidden" name="move" value="'.$_GET['move'].'" />
<input type="submit" value="Move" /></form><br />';
	} else {
		if(categoryExists($_GET['category']) && categoryExists($_GET['move'])){
			$query = 'SELECT id FROM `'.$settings['categories_table'].'` WHERE parent="'.mysql_safe($_GET['move']).'"';
			$result = mysql_query($query);
			$affectedcats = array();
			$allaffected = array();
			while($row = mysql_fetch_array($result)){
				$affectedcats[] = subCategories($row['id']);
			}
			foreach($affectedcats as $array){
				$allaffected = array_merge($allaffected,$array);
			}
			
			$query = 'UPDATE '.$settings['links_table'].' SET category="'.mysql_safe($_GET['category']).'" WHERE category="'.mysql_safe($_GET['move']).'"';
			mysql_query($query);
			$query = 'UPDATE '.$settings['categories_table'].' SET parent="'.mysql_safe($_GET['category']).'" WHERE parent="'.mysql_safe($_GET['move']).'"';
			mysql_query($query);
			rebuildTree('1',1);
			updateDbPath($allaffected);
			$error = 'Data was moved successfully from '.categoryName($_GET['move']).' to '.categoryName($_GET['category']).'. <a href="'.$_SERVER['PHP_SELF'].'">OK</a>';
		} else {
			$error = 'The category selected does not exist. <a href="'.$_SERVER['PHP_SELF'].'">OK</a>';
		}
	}
}

if(isset($error)){
	$yald_body .= '<div align="center"><div class="errorbox"><b>'.$error.'</b></div></div>';
}

$yald_body .= displayTree('1');

$yald_head = '<script type="text/javascript">
function delete_category(catid,catname){
input_box=confirm("Are you sure you want to delete \""+catname+"\"? All contents (links and subcategories) will be permanently removed.");
if (input_box==true){
window.location = "'.$_SERVER['PHP_SELF'].'?delete="+catid+"&name="+catname
}
}</script>';

if(!isset($_GET['newsubparent']) || !categoryExists($_GET['newsubparent'])){
	$yald_body .= '<br /><br /><form action="'.$_SERVER['PHP_SELF'].'" method="post">
New Category: <input type="text" name="name" /> Display subcategories underneath <input type="checkbox" name="disp_subs" value="1"> 
<input type="hidden" name="newsubparent" value="1" />
<input type="submit" value="Submit" /></form><br />';
}


$template = admin_output('categories');
print $template;
?>
Return current item: Yet Another Link Directory