<?php
// Links
// Copyright (C) 2002, 2003 Mike Little -- mike@zed1.com
$title = 'Link Categories';
$this_file='link-categories.php';
$parent_file = 'link-manager.php';

$wpvarstoreset = array('action','standalone','cat', 'auto_toggle');
for ($i=0; $i<count($wpvarstoreset); $i += 1) {
    $wpvar = $wpvarstoreset[$i];
    if (!isset($$wpvar)) {
        if (empty($_POST["$wpvar"])) {
            if (empty($_GET["$wpvar"])) {
                $$wpvar = '';
            } else {
                $$wpvar = $_GET["$wpvar"];
            }
        } else {
            $$wpvar = $_POST["$wpvar"];
        }
    }
}

switch ($action) {
  case 'addcat':
  {
      $standalone = 1;
      include_once('admin-header.php');

      if ($user_level < get_settings('links_minadminlevel'))
          die (_LANG_P_CHEATING_ERROR);

      $cat_name = addslashes($_POST['cat_name']);
      $auto_toggle = $_POST['auto_toggle'];
      if ($auto_toggle != 'Y') {
          $auto_toggle = 'N';
      }

      $show_images = $_POST['show_images'];
      if ($show_images != 'Y') {
          $show_images = 'N';
      }

      $show_description = $_POST['show_description'];
      if ($show_description != 'Y') {
          $show_description = 'N';
      }

      $show_rating = $_POST['show_rating'];
      if ($show_rating != 'Y') {
          $show_rating = 'N';
      }

      $show_updated = $_POST['show_updated'];
      if ($show_updated != 'Y') {
          $show_updated = 'N';
      }

      $sort_order = $_POST['sort_order'];

      $sort_desc = $_POST['sort_desc'];
      if ($sort_desc != 'Y') {
          $sort_desc = 'N';
      }
      $text_before_link = addslashes($_POST['text_before_link']);
      $text_after_link = addslashes($_POST['text_after_link']);
      $text_after_all = addslashes($_POST['text_after_all']);

      $list_limit = $_POST['list_limit'];
      if ($list_limit == '')
          $list_limit = -1;

      $wpdb->query("INSERT INTO $tablelinkcategories (cat_id, cat_name, auto_toggle, show_images, show_description, \n" .
             " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, text_after_all, list_limit) \n" .
             " VALUES (DEFAULT, '$cat_name', '$auto_toggle', '$show_images', '$show_description', \n" .
             " '$show_rating', '$show_updated', '$sort_order', '$sort_desc', '$text_before_link', '$text_after_link', \n" .
             " '$text_after_all', $list_limit)");

      header('Location: link-categories.php');
    break;
  } // end addcat
  case 'Delete':
  {
    $standalone = 1;
    include_once('admin-header.php');

    $cat_id = $_GET['cat_id'];
    $cat_name=get_linkcatname($cat_id);
    $cat_name=addslashes($cat_name);

    if ($cat_id=="1")
        die(sprintf(_LANG_WLC_DONOT_DELETE, $cat_name));

    if ($user_level < get_settings('links_minadminlevel'))
      die (_LANG_P_CHEATING_ERROR);

    $wpdb->query("DELETE FROM $tablelinkcategories WHERE cat_id='$cat_id'");
    $wpdb->query("UPDATE $tablelinks SET link_category=1 WHERE link_category='$cat_id'");

    header('Location: link-categories.php');
    break;
  } // end delete
  case 'Edit':
  {
    include_once ('admin-header.php');
    $cat_id = $_GET['cat_id'];
    $row = $wpdb->get_row("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, "
         . " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, "
         . " text_after_all, list_limit FROM $tablelinkcategories WHERE cat_id=$cat_id");
    if ($row) {
        if ($row->list_limit == -1) {
            $row->list_limit = '';
        }
?>

<ul id="adminmenu2">
	<li><a href="link-manager.php" ><?php echo _LANG_WLA_MANAGE_LINK; ?></a></li>
	<li><a href="link-add.php"><?php echo _LANG_WLA_ADD_LINK; ?></a></li>
	<li><a href="link-categories.php" class="current"><?php echo _LANG_WLA_LINK_CATE; ?></a></li>
	<li class="last"><a href="link-import.php"><?php echo _LANG_WLA_IMPORT_BLOG; ?></a></li>
</ul>

<div class="wrap">
  <h2><?php printf(_LANG_WLC_TITLE_TEXT, $row->cat_name); ?></h2>

  <form name="editcat" method="post">
      <input type="hidden" name="action" value="editedcat" />
      <input type="hidden" name="cat_id" value="<?php echo $row->cat_id ?>" />
<fieldset class="options">
<legend><?php echo _LANG_WLC_CATE_OPTION; ?></legend>
<table class="editform" width="100%" cellspacing="3" cellpadding="2">
<tr>
	<th id="leftmark" width="33%" scope="row"><?php echo _LANG_WLC_SUBEDIT_NAME; ?></th>
	<td width="67%"><input name="cat_name" type="text" value="<?php echo stripslashes($row->cat_name)?>" size="30" /></td>
</tr>
<tr>
	<th id="leftmark" scope="row"><?php echo _LANG_WLC_SUBEDIT_SHOW; ?></th>
        <td>
            <label>
            <input type="checkbox" name="show_images"  value="Y" <?php checked('Y', $row->show_images); ?> /> 
            <?php echo _LANG_WLC_SUBEDIT_IMAGES; ?></label> <br />
            <label>
            <input type="checkbox" name="show_description" value="Y" <?php checked('Y', $row->show_description); ?> /> 
            <?php echo _LANG_WLC_SUBEDIT_DESC; ?></label> 
            (<?php echo _LANG_WLC_SUBEDIT_REGARD; ?>)<br />
            <label>
            <input type="checkbox" name="show_rating"  value="Y" <?php checked('Y', $row->show_rating); ?> /> 
            <?php echo _LANG_WLC_SUBEDIT_RATE; ?></label> <br />
            <label>
            <input type="checkbox" name="show_updated" value="Y" <?php checked('Y', $row->show_updated); ?> /> 
            <?php echo _LANG_WLC_SUBEDIT_UPDATED; ?></label>
(<?php echo _LANG_WLC_SUBEDIT_REGARD; ?>)</td>
</tr>
<tr>
	<th id="leftmark" scope="row"><?php echo _LANG_WLC_SUBEDIT_SORT; ?></th>
	<td>
	<select name="sort_order" size="1">
            <option value="name"    <?php echo ($row->sort_order == 'name') ? 'selected' : ''?>><?php echo _LANG_WLC_SORT_NAME; ?></option>
            <option value="id"      <?php echo ($row->sort_order == 'id') ? 'selected' : ''?>>ID</option>
            <option value="url"     <?php echo ($row->sort_order == 'url') ? 'selected' : ''?>>URL</option>
            <option value="rating"  <?php echo ($row->sort_order == 'rating') ? 'selected' : ''?>><?php echo _LANG_WLC_SUBEDIT_RATE; ?></option>
            <option value="updated" <?php echo ($row->sort_order == 'updated') ? 'selected' : ''?>><?php echo _LANG_WLC_SUBEDIT_UPDATED; ?></option>
            <option value="rand"  <?php echo ($row->sort_order == 'rand') ? 'selected' : ''?>><?php echo _LANG_WLC_SORT_RANDOM; ?></option>
            <option value="length"  <?php echo ($row->sort_order == 'length') ? 'selected' : ''?>><?php echo _LANG_WLC_SORT_LENGTH; ?></option>
	</select>
	<label>
	<input type="checkbox" name="sort_desc" value="Y" <?php checked('Y', $row->sort_desc); ?> /> 
	<?php echo _LANG_WLC_SUBEDIT_DESCEND; ?></label>
	</td>
</tr>
<tr>
	<th id="leftmark" scope="row"><?php echo _LANG_WLC_SUBEDIT_LIMIT; ?></th>
	<td>
	<input type="text" name="list_limit" size="5" value="<?php echo $row->list_limit ?>" /> 
	(<?php echo _LANG_WLC_EPAGE_EMPTY; ?>)
	</td>
</tr>
<tr>
	<th id="leftmark" scope="row"><?php echo _LANG_WLC_SUBEDIT_TOGGLE; ?></th>
	<td><label>
		<input type="checkbox" name="auto_toggle"  value="Y" <?php checked('Y', $row->auto_toggle); ?> /> 
		<?php echo _LANG_WLC_SUBEDIT_INVISIBLE; ?></label></td>
</tr>

</table>
</fieldset>
<fieldset class="options">
<legend><?php echo _LANG_WLC_SUBEDIT_FORMAT; ?></legend>
<table class="editform" width="100%" cellspacing="3" cellpadding="2">
<tr>
	<th id="leftmark" width="33%" scope="row"><?php echo _LANG_WLC_SUBEDIT_BEFORE; ?></th>
	<td width="67%"><input type="text" name="text_before_link" size="45" value="<?php echo stripslashes($row->text_before_link)?>" /></td>
</tr>
<tr>
<th id="leftmark" scope="row"><?php echo _LANG_WLC_SUBEDIT_BETWEEN; ?></th>
<td><input type="text" name="text_after_link" size="45" value="<?php echo stripslashes($row->text_after_link)?>" /></td>
</tr>
<tr>
<th id="leftmark" scope="row"><?php echo _LANG_WLC_SUBEDIT_AFTER; ?></th>
<td><input type="text" name="text_after_all" size="45" value="<?php echo stripslashes($row->text_after_all)?>"/></td>
</tr>
</table>
</fieldset>
<p class="submit"><input type="submit" name="submit" value="<?php echo _LANG_WLC_SAVEBUTTON_TEXT; ?>" /></p>
</form>

</div>
<?php
    } // end if row
    break;
  } // end Edit
  case "editedcat":
  {
    $standalone = 1;
    include_once("./admin-header.php");

    if ($user_level < get_settings('links_minadminlevel'))
      die (_LANG_P_CHEATING_ERROR);

    $submit=$_POST["submit"];
    if (isset($submit)) {

    $cat_id=$_POST["cat_id"];

    $cat_name=addslashes(stripslashes($_POST["cat_name"]));
    $auto_toggle = $_POST["auto_toggle"];
    if ($auto_toggle != 'Y') {
        $auto_toggle = 'N';
    }

    $show_images = $_POST["show_images"];
    if ($show_images != 'Y') {
        $show_images = 'N';
    }

    $show_description = $_POST["show_description"];
    if ($show_description != 'Y') {
        $show_description = 'N';
    }

    $show_rating = $_POST["show_rating"];
    if ($show_rating != 'Y') {
        $show_rating = 'N';
    }

    $show_updated = $_POST["show_updated"];
    if ($show_updated != 'Y') {
        $show_updated = 'N';
    }

    $sort_order = $_POST["sort_order"];

    $sort_desc = $_POST["sort_desc"];
    if ($sort_desc != 'Y') {
        $sort_desc = 'N';
    }
    $text_before_link = addslashes($_POST["text_before_link"]);
    $text_after_link = addslashes($_POST["text_after_link"]);
    $text_after_all = addslashes($_POST["text_after_all"]);

    $list_limit = $_POST["list_limit"];
    if ($list_limit == '')
        $list_limit = -1;

    $wpdb->query("UPDATE $tablelinkcategories set
            cat_name='$cat_name',
            auto_toggle='$auto_toggle',
            show_images='$show_images',
            show_description='$show_description',
            show_rating='$show_rating',
            show_updated='$show_updated',
            sort_order='$sort_order',
            sort_desc='$sort_desc',
            text_before_link='$text_before_link',
            text_after_link='$text_after_link',
            text_after_all='$text_after_all',
            list_limit=$list_limit
            WHERE cat_id=$cat_id
            ");
    } // end if save


    header("Location: link-categories.php");
    break;
  } // end editcat
  default:
  {
    $standalone=0;
    include_once ("./admin-header.php");
    if ($user_level < get_settings('links_minadminlevel')) {
      die(_LANG_WLC_RIGHT_PROM);
    }
?>
<ul id="adminmenu2">
	<li><a href="link-manager.php" ><?php echo _LANG_WLA_MANAGE_LINK; ?></a></li>
	<li><a href="link-add.php"><?php echo _LANG_WLA_ADD_LINK; ?></a></li>
	<li><a href="link-categories.php" class="current"><?php echo _LANG_WLA_LINK_CATE; ?></a></li>
	<li class="last"><a href="link-import.php"><?php echo _LANG_WLA_IMPORT_BLOG; ?></a></li>
</ul>
<div class="wrap">
            <h2><?php printf(_LANG_WLC_EPAGE_TITLE, gethelp_link(_LANG_DOC_LINK, '#edit_link_category')); ?></h2>
            <table width="100%" cellpadding="5" cellspacing="0" border="0">
              <tr>
 	        <th><?php echo _LANG_IMG_NAME; ?></th>
                <th><?php echo _LANG_IMG_ID; ?></th>
                <th><?php echo _LANG_IMG_TOGGLE; ?></th>
                <th><?php echo _LANG_IMG_IMAGES; ?></th>
                <th><?php echo _LANG_IMG_DESC; ?></th>
                <th><?php echo _LANG_IMG_RATE; ?></th>
                <th><?php echo _LANG_IMG_UPDATED; ?></th>
                <th><?php echo _LANG_IMG_SORT; ?></th>
                <th><?php echo _LANG_IMG_DESCEND; ?></th>
                <th><?php echo _LANG_IMG_BEFORE; ?></th>
                <th><?php echo _LANG_IMG_BETWEEN; ?></th>
                <th><?php echo _LANG_IMG_AFTER; ?></th>
                <th><?php echo _LANG_IMG_LIMIT; ?></th>
                <th colspan="2"><?php echo _LANG_IMG_ACTION; ?></th>
              </tr>
<?php
$results = $wpdb->get_results("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, "
         . " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, "
         . " text_after_all, list_limit FROM $tablelinkcategories ORDER BY cat_id");
$i = 1;
foreach ($results as $row) {
    if ($row->list_limit == -1) {
        $row->list_limit = 'none';
    }
    $style = ($i % 2) ? ' class="alternate"' : '';
?>
              <tr valign="middle" align="center" <?php echo $style ?> style="border-bottom: 1px dotted #9C9A9C;">
                <td><?php echo stripslashes($row->cat_name)?></td>
				<td ><?php echo $row->cat_id?></td>
                <td><?php echo $row->auto_toggle?></td>
                <td><?php echo $row->show_images?></td>
                <td><?php echo $row->show_description?></td>
                <td><?php echo $row->show_rating?></td>
                <td><?php echo $row->show_updated?></td>
                <td><?php echo $row->sort_order?></td>
                <td><?php echo $row->sort_desc?></td>
                <td nowrap="nowrap"><?php echo htmlentities($row->text_before_link)?>&nbsp;</td>
                <td nowrap="nowrap"><?php echo htmlentities($row->text_after_link)?>&nbsp;</td>
                <td nowrap="nowrap"><?php echo htmlentities($row->text_after_all)?></td>
                <td><?php echo $row->list_limit?></td>
                <td nowrap="nowrap"><a href="link-categories.php?cat_id=<?php echo $row->cat_id?>&amp;action=Edit" class="edit"><?php echo _LANG_WLC_SUBCATE_EDIT; ?></a></td>
                <td nowrap="nowrap"><a href="link-categories.php?cat_id=<?php echo $row->cat_id?>&amp;action=Delete" onclick="return confirm('You are about to delete this category.\\n  \'Cancel\' to stop, \'OK\' to delete.');" class="delete"><?php echo _LANG_WLC_SUBCATE_DELETE; ?></a></td>
              </tr>
<?php
        ++$i;
    }
?>
            </table>
<p><?php echo _LANG_WLC_EPAGE_OVER; ?></p>

</div>

<div class="wrap">
    <form name="addcat" method="post">
      <input type="hidden" name="action" value="addcat" />
	  <h2><?php printf(_LANG_WLC_ADD_TITLE, gethelp_link(_LANG_DOC_LINK, '#add_link_category')); ?></h2>
<fieldset class="options">
<legend><?php echo _LANG_WLC_CATE_OPTION; ?></legend>
<table class="editform" width="100%" cellspacing="3" cellpadding="2">
<tr>
	<th id="leftmark" width="33%" scope="row"><?php echo _LANG_WLC_SUBEDIT_NAME; ?></th>
	<td width="67%"><input type="text" name="cat_name" size="30" /></td>
</tr>
<tr>
	<th id="leftmark" scope="row"><?php echo _LANG_WLC_SUBEDIT_SHOW; ?></th>
        <td>
            <label>
            <input type="checkbox" name="show_images"  value="Y" /> 
            <?php echo _LANG_WLC_SUBEDIT_IMAGES; ?></label> <br />
            <label>
            <input type="checkbox" name="show_description" value="Y" /> 
            <?php echo _LANG_WLC_SUBEDIT_DESC; ?></label> 
            (<?php echo _LANG_WLC_SUBEDIT_REGARD; ?>)<br />
            <label>
            <input type="checkbox" name="show_rating"  value="Y" /> 
            <?php echo _LANG_WLC_SUBEDIT_RATE; ?></label> <br />
            <label>
            <input type="checkbox" name="show_updated" value="Y" /> 
            <?php echo _LANG_WLC_SUBEDIT_UPDATED; ?></label>
(<?php echo _LANG_WLC_SUBEDIT_REGARD; ?>)</td>
</tr>
<tr>
	<th id="leftmark" scope="row"><?php echo _LANG_WLC_SUBEDIT_ORDER; ?></th>
	<td>
	<select name="sort_order" size="1">
	<option value="name"><?php echo _LANG_WLC_SORT_NAME; ?></option>
	<option value="id">ID</option>
	<option value="url">URL</option>
	<option value="rating"><?php echo _LANG_WLC_SUBEDIT_RATE; ?></option>
	<option value="updated"><?php echo _LANG_WLC_SUBEDIT_UPDATED; ?></option>
	<option value="rand"><?php echo _LANG_WLC_SORT_RANDOM; ?></option>
	</select>
	<label>
	<input type="checkbox" name="sort_desc" value="Y" /> 
	<?php echo _LANG_WLC_SUBEDIT_DESCEND; ?></label>
	</td>
</tr>
<tr>
	<th id="leftmark" scope="row"><?php echo _LANG_WLC_SUBEDIT_LIMIT; ?></th>
	<td>
	<input type="text" name="list_limit" size="5" value="" /> (<?php echo _LANG_WLC_EPAGE_EMPTY; ?>)
	</td>
</tr>
<tr>
	<th id="leftmark" scope="row"><?php echo _LANG_WLC_SUBEDIT_TOGGLE; ?></th>
	<td><label>
		<input type="checkbox" name="auto_toggle"  value="Y" /> 
		<?php echo _LANG_WLC_SUBEDIT_INVISIBLE; ?></label></td>
</tr>

</table>
</fieldset>
<fieldset class="options">
<legend><?php echo _LANG_WLC_SUBEDIT_FORMAT; ?></legend>
<table class="editform" width="100%" cellspacing="3" cellpadding="2">
<tr>
	<th id="leftmark" width="33%" scope="row"><?php echo _LANG_WLC_SUBEDIT_BEFORE; ?></th>
	<td width="67%"><input type="text" name="text_before_link" size="45" value="&lt;li&gt;" /></td>
</tr>
<tr>
<th id="leftmark" scope="row"><?php echo _LANG_WLC_SUBEDIT_BETWEEN; ?></th>
<td><input type="text" name="text_after_link" size="45" value="&lt;br /&gt;" /></td>
</tr>
<tr>
<th id="leftmark" scope="row"><?php echo _LANG_WLC_SUBEDIT_AFTER; ?></th>
<td><input type="text" name="text_after_all" size="45" value="&lt;/li&gt;"/></td>
</tr>
</table>
</fieldset>
<p class="submit"><input type="submit" name="submit" value="<?php echo _LANG_WLC_ADDBUTTON_TEXT; ?>" /></p>
  </form>
</div>
<div class="wrap">
    <p><strong>Note:</strong></p>
    <?php printf(_LANG_WLC_EPAGE_NOTE, get_linkcatname(1)) ?>
</div>
<?php
    break;
  } // end default
} // end case
?>
<?php include('admin-footer.php'); ?>
