<?php
/*
Copyright (C) 2005-2011 UserDot
Visit XennoBB at www.userdot.net/projects/xennobb
Originally based on PunBB www.punbb.org
XennoBB 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.
XennoBB 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA
*/
define('IN_FORUM_ADMIN', 1);
require dirname(__FILE__) . '/../include/init.php';
if ($forum_user['g_id'] > USER_ADMIN)
BB_Functions::Message($lang['No permission'], true, 1);
$page = isset($_GET['Page']) ? BB_Input::Htmlspecialchars($_GET['Page']) : null;
$submenu = array($lang['Add'] => 'categories.php?Page=Add', $lang['Delete'] => 'categories.php?Page=Delete', $lang['Edit'] => 'categories.php?Page=Edit');
if (isset($_POST['add_cat']))
{
BB_Input::Confirm_Referrer('categories.php');
$new_cat_name = trim($_POST['new_cat_name']);
if ($new_cat_name == '')
BB_Functions::Message($lang['You must enter a name for the category.']);
$db->query('INSERT INTO ' . $db->prefix . 'categories (cat_name) VALUES(\'' . $db->escape($new_cat_name) . '\')') or BB_Functions::Error($lang['error_55'], __FILE__, __LINE__, $db->error());
BB_Admin_Functions::Log_Action('Category added ' . $new_cat_name, 1);
BB_Functions::Redirect('categories.php?Page=Edit', $lang['redirect_cat_added']);
}
else if (isset($_POST['del_cat']) || isset($_POST['del_cat_comply']))
{
BB_Input::Confirm_Referrer('categories.php');
$cat_to_delete = intval($_POST['cat_to_delete']);
if ($cat_to_delete < 1)
BB_Functions::Message($lang['Bad request']);
if (isset($_POST['del_cat_comply']))
{
@set_time_limit(0);
$result = $db->query('SELECT id FROM ' . $db->prefix . 'forums WHERE cat_id=' . $db->escape($cat_to_delete)) or BB_Functions::Error($lang['error_54'], __FILE__, __LINE__, $db->error());
$num_forums = $db->num_rows($result);
for ($i = 0; $i < $num_forums; ++$i)
{
$cur_forum = $db->result($result, $i);
BB_Admin_Functions::Prune($cur_forum, 1, -1);
$db->query('DELETE FROM ' . $db->prefix . 'forums WHERE id=' . $cur_forum) or BB_Functions::Error($lang['error_53'], __FILE__, __LINE__, $db->error());
}
$result = $db->query('SELECT t1.id FROM ' . $db->prefix . 'topics AS t1 LEFT JOIN ' . $db->prefix . 'topics AS t2 ON t1.moved_to=t2.id WHERE t2.id IS NULL AND t1.moved_to IS NOT NULL') or BB_Functions::Error($lang['error_52'], __FILE__, __LINE__, $db->error());
$num_orphans = $db->num_rows($result);
if ($num_orphans)
{
for ($i = 0; $i < $num_orphans; ++$i)
$orphans[] = $db->result($result, $i);
$db->query('DELETE FROM ' . $db->prefix . 'topics WHERE id IN(' . implode(',', $orphans) . ')') or BB_Functions::Error($lang['error_51'], __FILE__, __LINE__, $db->error());
}
$db->query('DELETE FROM ' . $db->prefix . 'categories WHERE id=' . $db->escape($cat_to_delete)) or BB_Functions::Error($lang['error_50'], __FILE__, __LINE__, $db->error());
BB_Admin_Functions::Log_Action('Category removed ' . $cat_to_delete, 3);
BB_Functions::Redirect('categories.php?Page=Delete', $lang['redirect_cat_deleted']);
}
else
{
$result = $db->query('SELECT cat_name FROM ' . $db->prefix . 'categories WHERE id=' . $db->escape($cat_to_delete)) or BB_Functions::Error($lang['error_49'], __FILE__, __LINE__, $db->error());
$cat_name = $db->result($result);
$page_title = BB_Input::Htmlspecialchars(bb_board_title) . $lang['Admin'] . $lang['Categories'];
require FORUM_ROOT . 'admin/header.php';
?>
<div class="blockform">
<h2><span><?php echo$lang['Cat_delete'] ?></span></h2>
<form method="post" action="categories.php">
<input type="hidden" name="cat_to_delete" value="<?php echo $cat_to_delete ?>" />
<div class="inform">
<div class="infldset file" style="padding:10px">
<p><?php echo$lang['Conf_cat_delete_phrase'] ?> "<?php echo $cat_name ?>"?</p>
<p><?php echo$lang['Conf_cat_delete_warn'] ?></p>
</div>
</div>
<div><input type="button" class="b1" onclick="javascript:history.go(-1)" value="<?php echo $lang['Go back'] ?>"> <input type="submit" name="del_cat_comply" class="b1" value="<?php echo $lang['Delete'] ?>" /></div>
</form>
</div>
<?php
require FORUM_ROOT . 'admin/footer.php';
}
}
else if (isset($_POST['update']))
{
BB_Input::Confirm_Referrer('categories.php');
$cat_order = $_POST['cat_order'];
$cat_name = $_POST['cat_name'];
$result = $db->query('SELECT id, disp_position FROM ' . $db->prefix . 'categories ORDER BY disp_position') or BB_Functions::Error($lang['error_47'], __FILE__, __LINE__, $db->error());
$num_cats = $db->num_rows($result);
for ($i = 0; $i < $num_cats; ++$i)
{
if ($cat_name[$i] == '')
BB_Functions::Message('You must enter a category name.');
if (!preg_match('#^\d+$#', $cat_order[$i]))
BB_Functions::Message('Position must be an integer value.');
list($cat_id, $position) = $db->fetch_row($result);
$db->query('UPDATE ' . $db->prefix . 'categories SET cat_name=\'' . $db->escape($cat_name[$i]) . '\', disp_position=' . $db->escape($cat_order[$i]) . ' WHERE id=' . $db->escape($cat_id)) or BB_Functions::Error($lang['error_48'], __FILE__, __LINE__, $db->error());
}
BB_Functions::Redirect('categories.php?Page=Edit', $lang['redirect_cat_updated']);
}
$result = $db->query('SELECT id, cat_name, disp_position FROM ' . $db->prefix . 'categories ORDER BY disp_position') or BB_Functions::Error($lang['error_47'], __FILE__, __LINE__, $db->error());
$num_cats = $db->num_rows($result);
for ($i = 0; $i < $num_cats; ++$i)
$cat_list[] = $db->fetch_row($result);
$page_title = BB_Input::Htmlspecialchars($lang['Admin'] . $lang['Categories']);
require FORUM_ROOT . 'admin/header.php';
if ($page == 'Add')
{
echo BB_Template::Generate_Menu($submenu);
?>
<div class="blockform">
<form method="post" action="categories.php">
<div class="inform">
<div class="infldset file" style="padding:10px">
<table class="aligntop" cellspacing="0">
<tr>
<th class="header">Name</th>
<td class="tips"><?php BB_Template::Print_Tooltip($lang['tip_33']) ?></td>
<td><input type="text" class="textbox" name="new_cat_name" size="35" maxlength="80" tabindex="1" /></td>
</tr>
</table>
</div>
</div>
<div><input type="submit" class="b1" name="add_cat" value="<?php echo $lang['Add'] ?>" tabindex="2" /></div>
</form>
</div>
<?php
}
else if ($page == 'Delete')
{
echo BB_Template::Generate_Menu($submenu);
if ($num_cats)
{
?>
<div class="blockform">
<form method="post" action="categories.php">
<div class="inform">
<div class="infldset file" style="padding:10px">
<table class="aligntop" cellspacing="0">
<tr>
<th class="header"><?php echo $lang['Name'] ?></th>
<td class="tips"><?php BB_Template::Print_Tooltip($lang['tip_34']) ?></td>
<td><select name="cat_to_delete" tabindex="3"><?php while (list(, list($cat_id, $cat_name,, )) = @each($cat_list))
echo "\t\t\t\t\t\t\t\t\t\t" . '<option value="' . $cat_id . '">' . BB_Input::Htmlspecialchars($cat_name) . '</option>' . "\n"; ?></select></td>
</tr>
</table>
</div>
</div>
<div><input type="submit" class="b1" name="del_cat" value="<?php echo $lang['Remove'] ?>" tabindex="4" /></div>
</form>
</div>
<?php
}
}
else if ($page == 'Edit')
{
echo BB_Template::Generate_Menu($submenu);
if ($num_cats)
{
?>
<div class="blockform">
<form method="post" action="categories.php">
<div class="inform">
<div class="infldset file" style="padding:0px">
<table>
<thead>
<tr>
<th class="tcl"><?php echo $lang['Name']; ?></th>
<th><?php echo $lang['Position']; ?></th>
</tr>
</thead>
<tbody>
<?php
@reset($cat_list);
for ($i = 0; $i < $num_cats; ++$i)
{
list(, list($cat_id, $cat_name, $position)) = @each($cat_list);
?>
<tr>
<td><input type="text" class="textbox" name="cat_name[<?php echo $i ?>]" value="<?php echo BB_Input::Htmlspecialchars($cat_name) ?>" size="50" maxlength="80" /></td>
<td><input type="text" class="textbox" name="cat_order[<?php echo $i ?>]" value="<?php echo $position ?>" size="3" maxlength="2" /></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
<div><input class="b1" type="submit" name="update" value="<?php echo $lang['Update'] ?>" /></div>
</form>
</div>
<?php
}
}
else
BB_Functions::Message($lang['Bad request']);
require FORUM_ROOT . 'admin/footer.php';
?>