<?php

  /*

      managenews.php

      Allows the root user to create/edit the front page news.

      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();

  // Page security
  if ($_SESSION['_usertype'] != 'Root') {
    require_once 'system/security.php';
    ThrowOut();
  }

  // Page Title
  $ptitle = 'Manage News';

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

  // Language selection
  set_text_domain("managenews");

  // Retrieve Get/Post variables
  $act = $_REQUEST['act'];
  $content = $_POST['content'];
  $id = $_REQUEST['id'];
  $msglang = $_REQUEST['msglang'];

  if (!$msglang) {
    $msglang = $_lang;
  }

  // Action: Post news
  if ($act == 'post') {
    $act = '';

    $sql = "INSERT INTO tbl_News (content,lang) VALUES ('$content','$msglang')";
    db_send($sql);

    $message = gettext("NOTE: News successfully posted.");
  }

  // Action: Delete News
  if ($act == 'delete') {
    $act = '';
    $res = db_recordset("SELECT lang FROM tbl_News WHERE id=$id");

    $msglang = $res[0]['lang'];

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

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

  // Action: Show form for editing news
  if ($act == 'edit') {
    $newsRS = db_recordset("SELECT id,content,lang FROM tbl_News WHERE id=$id");

    $msglang = $newsRS[0]['lang'];
?>
  <h1><?php echo gettext('Edit News')?></h1>
  <h2><?php echo gettext('Here you can edit the news displayed on the front page:')?></h2>
  
  <div class="block">
    <form name="langform" id="langform" method="post" action="managenews.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>
    <form name="mainform" id="mainform" method="post" action="">
      <input type="hidden" name="act" value="editaction" />
      <input type="hidden" name="id" value="<?php echo $newsRS[0]['id']?>" />
    <input type="hidden" name="msglang" value="<?php echo $msglang?>" />
      <div class="labelfieldpair">
        <div class="name">
          <label for="content"><?php echo gettext('News')?>:</label>
        </div>
        <div class="field">
          <textarea name="content" id="content" cols="60" rows="6"><?php echo $newsRS[0]['content']?></textarea>
        </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: Edit a new item
  if ($act == 'editaction') {
    $act = '';

    $sql = "UPDATE tbl_News SET content='$content' WHERE ID=$id";
    db_send($sql);

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

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

//     $newsRS = db_recordset("SELECT * FROM tbl_News WHERE frontpagepara=0 ORDER BY date DESC, id DESC");

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

?>

  <h1><?php echo gettext('Front Page News')?></h1>
  <h2><?php echo gettext('Here you can add, edit and delete the news displayed on the front page:')?></h2>
  
  <div class="block">
    <form name="langform" id="langform" method="post" action="managenews.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>
    <form name="mainform" id="mainform" method="post" action="">
      <input type="hidden" name="act" value="post" />
    <input type="hidden" name="msglang" value="<?php echo $msglang?>" />
      <div class="labelfieldpair">
        <div class="name">
          <label for="content"><?php echo gettext('News')?>:</label>
        </div>
        <div class="field">
          <textarea name="content" id="content" cols="60" rows="6"></textarea>
        </div>
      </div>
      <div class="buttonpanel">
        <input name="submit" type="submit" id="submit" value="<?php echo gettext('Post News')?>" />
        <input name="reset" type="reset" id="reset" value="<?php echo gettext('Reset')?>" />
      </div>
    </form>
<?php
    $result = db_recordset("SELECT id,content,date FROM tbl_News WHERE frontpagepara=0 and lang='".$msglang."' ORDER BY date DESC, id DESC");
    foreach ($result as $news) {
      echo "    <h3>[{$news['date']}]";
      echo " (<a href=\"managenews.php?act=delete&amp;id={$news['id']}\">".gettext('Delete')."</a>)";
      echo " (<a href=\"managenews.php?act=edit&amp;id={$news['id']}\">".gettext('Edit')."</a>)";
      echo "</h3>\n";
      echo "<p>" . nl2br($news['content']) . "</p>";
    }
?>
  </div>

<?php
  }
  // Include
  require 'footer.php';
?>
