<?php

  /*

      frontpagepara.php
      Allows the root user to change the opening/closing paragraphs on the
      front page.

      Changelog:
        2006-01-14 dave: Cleaned up code for v1.0 release
  
      ----

      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 = 'Front Page Paragraph';

  //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("frontpagepara");

  // Retrieve Get/Post variables
  $act = $_REQUEST['act'];
  $opening = $_POST['opening'];
  $closing = $_REQUEST['closing'];
  $msglang = $_REQUEST['msglang'];

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

  // Action: Set the public notice of this domain
  if ($act == 'action') {
    $act = '';

    $op = db_recordset("SELECT count(*) FROM tbl_News WHERE frontpagepara=1 and lang='".$msglang."'");

    //print "<h1>$_lang : ".$op[0][count]."</h1>\n";

    if ($op[0][count] == 0) {
      db_send("INSERT INTO tbl_News (content,frontpagepara,lang) VALUES ('" . $opening . "','1','".$msglang."')");
    } else {
      db_send("UPDATE tbl_News SET content='" . $opening . "' WHERE frontpagepara=1 and lang='".$msglang."'");
    }

    $cp = db_recordset("SELECT count(*) FROM tbl_News WHERE frontpagepara=2 and lang='".$msglang."'");

    //print "<h1>$_lang : ".$cp[0][count]."</h1>\n";
    if ($cp[0][count] == 0) {
      db_send("INSERT INTO tbl_News (content,frontpagepara,lang) VALUES ('" . $closing . "','2','".$msglang."')");
    } else {
      db_send("UPDATE tbl_News SET content='" . $closing . "' WHERE frontpagepara=2 and lang='".$msglang."'");
    }

    $message = gettext("NOTE: Paragraphs successfully updated.");
  }

  // Default action: Request details of the publi notice and choose action...
  if ($act == '') {

    $opening = db_recordset("SELECT * FROM tbl_News WHERE frontpagepara=1 and lang='".$msglang."'");
    $closing = db_recordset("SELECT * FROM tbl_News WHERE frontpagepara=2 and lang='".$msglang."'");

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

?>

  <h1><?php echo gettext('Front Page Paragraphs')?></h1>
  <h2><?php echo gettext('Here you can edit the opening/closing paragraphs on the front page.')?></h2>
  
  <div class="block">
    <form name="langform" id="langform" method="post" action="frontpagepara.php">
      <div class="buttonpanel">
      <select name="msglang">
<?php
    if ($langfile = fopen($conf['locale_dir']."/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="frontpagepara.php">
      <input type="hidden" name="act" value="action" />
     <input type="hidden" name="msglang" value="<?php echo $msglang?>" />
      <div class="labelfieldpair">
        <div class="name">
          <label for="opening"><?php echo gettext('Opening Paragraph')?>:</label>
        </div>
        <div class="field">
          <textarea name="opening" id="opening" cols="60" rows="6"><?php echo $opening[0][content]?></textarea>
        </div>
      </div>
      <div class="labelfieldpair">
        <div class="name">
          <label for="closing"><?php echo gettext('Closing Paragraph')?>:</label>
        </div>
        <div class="field">
          <textarea name="closing" id="closing" cols="60" rows="6"><?php echo $closing[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
  }
  // Include
  require 'footer.php';
?>

