<?php

  /*

      links.php

      Allows the root user to create/edit the links sidebar on the front page.
    
      Changelog:
        2006-01-14 dave: Cleaned up code for v1.0 release

      ## PAGE CONTAINS HANDYANDY MYSQL MODS ##
      ## Copyright (C) 2005 Andy Deakin (handyandy.org.uk) ##

      ----

      Copyright (C) 2003  Central Manchester CLC
      Copyright (C) 2003  David Thorne (davethorne@gmail.com)

      This program 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 version 2.

      This program 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  */

  // Start session and populate it with data
  session_start();
  require_once 'system/db.php';
  require_once 'system/lang.php';

  if ($_SESSION['_usertype'] != 'Root') { ThrowOut(); }

  // Page Title
  $ptitle = 'Links';

  // Includes
  require 'header.php';
  require_once 'system/global_preferences.php';
  require_once 'system/message.php';

  // Language selection
  set_text_domain("links");

  // Retrieve Get/Post variables
  $act = $_REQUEST['act'];

  $id = $_REQUEST['id'];
  $name = $_REQUEST['name'];
  $link = $_REQUEST['link'];
  $text = $_REQUEST['text'];
  $category = $_REQUEST['category'];
  $sortorder = $_REQUEST['sortorder'];
  $msglang = $_REQUEST['msglang'];
  
  if (!$msglang) 
  {
    $msglang = $_lang;
  }

  // Action: Delete a link
  if ($act == 'deletelink') {
    $act = '';

    // Change the sortorder of all links below the current one
    $link = db_recordset("SELECT * FROM tbl_Links WHERE id=$id");
    db_send("UPDATE tbl_Links SET sortorder=-1 WHERE id=$id");

    db_send("UPDATE tbl_Links SET sortorder=sortorder-1 WHERE category={$link[0]['category']} AND sortorder > {$link[0]['sortorder']}");

    $sql = "DELETE FROM tbl_Links WHERE id=$id";
    db_send($sql);

    $message = gettext("NOTE: Link successfully deleted.");
  }

  // Action: Form for adding a link
  if ($act == 'addlink') {
?>
      <h1><?php echo gettext('Add Link')?></h1>
      <h2><?php echo gettext('Please enter the details for the link you wish to add')?>:</h2>
      
      <div class="block">
        <form method="post">
            <input type="hidden" name="act" value="addlinkaction" />
            <input type="hidden" name="category" value="<?php echo $category?>" />
            <input type="hidden" name="msglang" value="<?php echo $msglang?>" />
            <div class="labelfieldpair">
                <div class="name">
                    <label for="text"><?php echo gettext('Link text')?></label>
                </div>
                <div class="field">
                    <input type="text" name="text" size="20" maxlength="100" />
                </div>
            </div>
            <div class="labelfieldpair">
                <div class="name">
                    <label for="link"><?php echo gettext('Link URL')?></label>
                </div>
                <div class="field">
                    <input type="text" name="link" size="20" maxlength="100" />
                </div>
            </div>
            <div class="buttonpanel">
                <input name="submit" type="submit" id="submit" value="<?php echo gettext('Add Link')?>" />
                <input name="reset" type="reset" id="reset" value="<?php echo gettext('Reset')?>" />
            </div>
        </form>
      </div>
<?php
  }

  // Action: Move a category up
  if ($act == 'categoryup') {
    // Find this level's sort order
    $thiscat = db_recordset("SELECT sortorder FROM tbl_Link_Categories WHERE id=$category");

      // Only move a link up if it isn't at the top already
      if ($thiscat[0][sortorder] > 0) {
        // Find the link that is one in line above it
        $newcat = db_recordset("SELECT id FROM tbl_Link_Categories WHERE lang='$msglang' AND sortorder=" . ($thiscat[0][sortorder]-1));

        // Swap the sort orders for the two links
        db_send("UPDATE tbl_Link_Categories SET sortorder=" . ($thiscat[0]['sortorder']-1) . " WHERE id=$category");
        db_send("UPDATE tbl_Link_Categories SET sortorder=" . $thiscat[0]['sortorder'] . " WHERE id=" . $newcat[0]['id']);
      }
      $act = '';
    }

  // Action: Move a category down
  if ($act == 'categorydown') {
    // Find the lowest level
    $categoriesRS = db_recordset("SELECT * FROM tbl_Link_Categories WHERE lang='$msglang'");

    // Find this level's sort order
    $thiscat = db_recordset("SELECT sortorder FROM tbl_Link_Categories WHERE id=$category");

      // Only move a link down if it isn't at the bottom already
      if ($thiscat[0][sortorder] < (count($categoriesRS)-1)) {
        // Find the link that is one in line below it
        $newcat = db_recordset("SELECT id FROM tbl_Link_Categories WHERE lang='$msglang' AND sortorder=" . ($thiscat[0][sortorder]+1));

        // Swap the sort orders for the two links
        db_send("UPDATE tbl_Link_Categories SET sortorder=" . ($thiscat[0]['sortorder']+1) . " WHERE id=$category");
        db_send("UPDATE tbl_Link_Categories SET sortorder=" . $thiscat[0]['sortorder'] . " WHERE id=" . $newcat[0]['id']);
      }
      $act = '';
    }

  // Action: Move a link up
  if ($act == 'linkup') {
    // Find this level's sort order
    $link = db_recordset("SELECT sortorder FROM tbl_Links WHERE id=$id");

      // Only move a link up if it isn't at the top already
      if ($link[0][sortorder] > 0) {
        // Find the link that is one in line above it
        $newlink = db_recordset("SELECT id FROM tbl_Links WHERE category=$category AND sortorder=" . ($link[0][sortorder]-1));

        // Swap the sort orders for the two links
        db_send("UPDATE tbl_Links SET sortorder=" . ($link[0]['sortorder']-1) . " WHERE id=$id");
        db_send("UPDATE tbl_Links SET sortorder=" . $link[0]['sortorder'] . " WHERE id=" . $newlink[0]['id']);
      }
      $act = '';
    }

  // Action: Move a link down
  if ($act == 'linkdown') {
    // Find the lowest level
    $linksRS = db_recordset("SELECT * FROM tbl_Links WHERE category=$category");

    // Find this level's sort order
    $link = db_recordset("SELECT sortorder FROM tbl_Links WHERE id=$id");

      // Only move a link up if it isn't at the top already
      if ($link[0][sortorder] < (count($linksRS)-1)) {
        // Find the link that is one in line above it
        $newlink = db_recordset("SELECT id FROM tbl_Links WHERE category=$category AND sortorder=" . ($link[0][sortorder]+1));

        // Swap the sort orders for the two links
        db_send("UPDATE tbl_Links SET sortorder=" . ($link[0]['sortorder']+1) . " WHERE id=$id");
        db_send("UPDATE tbl_Links SET sortorder=" . $link[0]['sortorder'] . " WHERE id=" . $newlink[0]['id']);
      }
      $act = '';
    }

  // Action: Actually add the link
  if ($act == 'addlinkaction') {
    $act = '';

    $linkcount = db_recordset("SELECT * FROM tbl_Links WHERE category=$category");
    $sql = "INSERT INTO tbl_Links (text, link, category, sortorder) VALUES ('$text', '$link', $category, " . count($linkcount) . ")";
    db_send($sql);

    $message = gettext("NOTE: Link successfully added.");
  }

  // Action: Form for editing a link
  if ($act == 'editlink') {
    $linkRS = db_recordset("SELECT * FROM tbl_Links WHERE id=$id");
?>
      <h1><?php echo gettext('Edit Link')?></h1>
      <h2><?php echo gettext('Here you can change the details of an existing link:')?></h2>
      
      <div class="block">
        <form method="post">
            <input type="hidden" name="act" value="editlinkaction" />
            <input type="hidden" name="id" value="<?php echo $id?>" />
            <input type="hidden" name="msglang" value="<?php echo $msglang?>" />
            <div class="labelfieldpair">
                <div class="name">
                    <label for="text"><?php echo gettext('Link text')?></label>
                </div>
                <div class="field">
                    <input type="text" name="text" size="20" maxlength="100" value="<?php echo $linkRS[0]['text']?>" />
                </div>
            </div>
            <div class="labelfieldpair">
                <div class="name">
                    <label for="link"><?php echo gettext('Link URL')?></label>
                </div>
                <div class="field">
                    <input type="text" name="link" size="20" maxlength="100" value="<?php echo $linkRS[0]['link']?>" />
                </div>
            </div>
            <div class="buttonpanel">
                <input name="submit" type="submit" id="submit" value="<?php echo gettext('Commit Changes')?>" />
                <input name="reset" type="reset" id="reset" value="<?php echo gettext('Reset')?>" />
            </div>
        </form>
      </div>
<?php
  }

  // Action: Actually edit the link
  if ($act == 'editlinkaction') {
    $act = '';

    $sql = "UPDATE tbl_Links SET text = '$text', link = '$link' WHERE id=$id";
    db_send($sql);

    $message = gettext("NOTE: Link successfully changed.");
  }

  // Action: Delete a category
  if ($act == 'deletecategory') {
    $act = '';

    // Change the sortorder of all categories below the current one
    $thiscat = db_recordset("SELECT * FROM tbl_Link_Categories WHERE id=$category");
    db_send("UPDATE tbl_Link_Categories SET sortorder=-1 WHERE id=$category");

    db_send("UPDATE tbl_Link_Categories SET sortorder=sortorder-1 WHERE sortorder > {$thiscat[0][sortorder]}");

    $sql = "DELETE FROM tbl_Links WHERE category=$category";
    db_send($sql);
    $sql = "DELETE FROM tbl_Link_Categories WHERE id=$category";
    db_send($sql);

    $message = gettext("NOTE: Category successfully deleted.");
  }

  // Action: Form for adding a category
  if ($act == 'addcategory') {
?>
      <h1><?php echo gettext('Add Category')?></h1>
      <h2><?php echo gettext('Please enter the name of the category to add')?>:</h2>
      
      <div class="block">
        <form method="post">
            <input type="hidden" name="act" value="addcategoryaction" />
            <input type="hidden" name="msglang" value="<?php echo $msglang?>" />
            <div class="labelfieldpair">
                <div class="name">
                    <label for="name"><?php echo gettext('Category Name')?></label>
                </div>
                <div class="field">
                    <input type="text" name="name" size="20" maxlength="100" />
                </div>
            </div>
            <div class="buttonpanel">
                <input name="submit" type="submit" id="submit" value="<?php echo gettext('Add Category')?>" />
                <input name="reset" type="reset" id="reset" value="<?php echo gettext('Reset')?>" />
            </div>
        </form>
      </div>
<?php
  }

  // Action: Actually add the category
  if ($act == 'addcategoryaction') {
    $act = '';

    $categorycount = db_recordset("SELECT * from tbl_Link_Categories where lang='".$msglang."'");
    $sql = "INSERT INTO tbl_Link_Categories (name, sortorder, lang)
        VALUES ('$name', " . count($categorycount) . ", '".$msglang."')";
    db_send($sql);

    $message = gettext("NOTE: Category successfully added.");
  }

  // Action: Form for editing a category
  if ($act == 'editcategory') {
    $categoryRS = db_recordset("SELECT * FROM tbl_Link_Categories WHERE id=$category");
?>
      <h1><?php echo gettext('Edit Category')?></h1>
      <h2><?php echo gettext('Here you can change the name of the category:')?></h2>
      
      <div class="block">
        <form method="post">
            <input type="hidden" name="act" value="editcategoryaction" />
            <input type="hidden" name="category" value="<?php echo $category?>" />
            <div class="labelfieldpair">
                <div class="name">
                    <label for="name"><?php echo gettext('Category Name')?></label>
                </div>
                <div class="field">
                    <input type="text" name="name" size="20" maxlength="100" 
                        value="<?php echo $categoryRS[0]['name']?>"/>
                </div>
            </div>
            <div class="buttonpanel">
                <input name="submit" type="submit" id="submit" value="<?php echo gettext('Commit Changes')?>" />
                <input name="reset" type="reset" id="reset" value="<?php echo gettext('Reset')?>" />
            </div>
        </form>
      </div>
<?php
  }

  // Action: Actually edit the category
  if ($act == 'editcategoryaction') {
    $act = '';

    $sql = "UPDATE tbl_Link_Categories SET name='$name' WHERE id=$category";
    db_send($sql);

    $message = gettext("NOTE: Category successfully edited.");
  }

  // Default action: Show news and allow user to post more.
  if ($act == '') {


    if ($message) { display($message); }

?>

  <h1><?php echo gettext('Front Page Sidebar')?></h1>
  <h2><?php echo gettext('Here you can edit the sidebar displayed on the front page:')?></h2>
  
  <div class="block">
    <form name="langform" id="langform" method="post" action="links.php">
      <div class="buttonpanel">
        <select name="msglang">
<?php
  if ($langfile = fopen("./locale/locale.alias","r")) 
  {
    while ($aline= fgets($langfile, 1024))
  {
    if (!($aline[0] == '#'))
    {
      $aline = trim($aline);
      if (substr($aline, 1, 7) != "default")
      {
        $res = preg_split('/\s+/',$aline);
        if ($msglang == $res[1])
        {
          print '          <option value="'.$res[1].'" selected="selected">'.$res[0].' ('.$res[1].')</option>\n';
        }
        else
        {
          print '          <option value="'.$res[1].'">'.$res[0].' ('.$res[1].')</option>\n';
        }
      }
    }
  }
  fclose($langfile);
  }
?>
        </select>
        <input type="submit" value="<?php echo gettext("Change Language")?>" />
      </div>
    </form>
        
    <a href="links.php?act=addcategory&amp;msglang=<?php echo $msglang?>"><img src="images/add.png" width="12" height="12" vspace="2" border="0" align="absmiddle" alt="Add a new category" /> <?php echo gettext('Add a new category')?></a>
    <div class="navigation">
      <ul>
        <li><span>Admin</span>
          <ul>
            <li><a href="domains.php">Domains</a></li>
          </ul>
        </li>
        
<?php
    $linkcatRS = db_recordset("SELECT * FROM tbl_Link_Categories WHERE lang='".$msglang."' ORDER BY sortorder");
    foreach ($linkcatRS as $category) {
?>
        <li><span>
          <a href="links.php?act=categoryup&amp;msglang=<?php echo $msglang?>&amp;category=<?php echo $category['id']?>" title="Move Category Up"><img src="images/up.png" width="12" height="10" border="0" align="absmiddle" alt="Move Category Up" /></a>
          <a href="links.php?act=categorydown&amp;msglang=<?php echo $msglang?>&amp;category=<?php echo $category['id']?>" title="Move Category Down"><img src="images/down.png" width="12" height="10" border="0" align="absmiddle" alt="Move Category Down" /></a>
          &nbsp;&nbsp;&nbsp;
          <a href="links.php?act=deletecategory&amp;msglang=<?php echo $msglang?>&amp;category=<?php echo $category['id']?>" title="Delete Category (and all its contents)"><img src="images/delete.png" width="12" height="12" border="0" align="absmiddle" alt="Delete Category (and all its contents)" /></a>
          &nbsp;&nbsp;&nbsp;
          <a href="links.php?act=editcategory&amp;msglang=<?php echo $msglang?>&amp;category=<?php echo $category['id']?>" title="Edit Category"><img src="images/edit.png" width="10" height="12" border="0" align="absmiddle" alt="Edit Category" /></a>
          &nbsp;&nbsp;
          <?php echo $category['name']?></span>

          <ul>
<?php
        $sql = "SELECT * FROM tbl_Links WHERE category={$category['id']} ORDER BY sortorder";
        $linkRS = db_recordset($sql);
        foreach ($linkRS as $link) {
?>
            <li>
              <a href="links.php?act=linkup&amp;id=<?php echo $link['id']?>&amp;msglang=<?php echo $msglang?>&amp;category=<?php echo $link['category']?>" title="Move Link Up"><img src="images/up.png" width="12" height="10" border="0" align="absmiddle" alt="Move Link Up" /></a>
              <a href="links.php?act=linkdown&amp;id=<?php echo $link['id']?>&amp;msglang=<?php echo $msglang?>&amp;category=<?php echo $link['category']?>" title="Move Link Down"><img src="images/down.png" width="12" height="10" border="0" align="absmiddle" alt="Move Link Down" /></a>
              &nbsp;&nbsp;&nbsp;
              <a href="links.php?act=deletelink&amp;msglang=<?php echo $msglang?>&amp;id=<?php echo $link['id']?>" title="Delete Link"><img   src="images/delete.png" width="12" height="12" border="0" align="absmiddle" alt="Delete Link" /></a>
              &nbsp;&nbsp;&nbsp;
              <a href="links.php?act=editlink&amp;msglang=<?php echo $msglang?>&amp;id=<?php echo $link['id']?>" title="Edit Link"><img src="images/edit.png" width="10" height="12" border="0" align="absmiddle" alt="Edit Link" /></a>
              &nbsp;&nbsp;
              <a href="<?php echo $link['link']?>"><?php echo $link['text']?></a>
            </li>
<?php
        }
?>
            <li>
              <a href="links.php?act=addlink&amp;msglang=<?php echo $msglang?>&amp;category=<?php echo $category['id']?>" title="Add a link to this category"><img src="images/add.png" width="12" height="12" vspace="2" border="0" align="absmiddle" alt="Add a link to this category" /></a>
            </li>
          </ul>
        </li>
<?php
    }
?>
      </ul>
    </div>
  </div>

<?php
  }

  require 'footer.php';

?>
