<?php
/**
 * pragmaMx  Content Management System
 * Copyright (c) 2006 pragmaMx Dev Team - http://pragmaMx.org
 * 
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * $Source: /home/www/dps3311/home/cvsroot/pragmamx/html/modules/FAQ/index.php,v $
 * $Revision: 1.5.4.5 $
 * $Author: tora60 $
 * $Date: 2006/12/03 11:54:57 $
 * 
 * this file based on:
 * Automated FAQ
 * Copyright ©2001 by Richard Tirtadji AKA King Richard (rtirtadji@hotmail.com)
 * and Hutdik Hermawan AKA hotFix (hutdik76@hotmail.com)
 * http://www.phpnuke.web.id
 * php-nuke Web Portal System - http://phpnuke.org/
 * Thatware - http://thatware.org/
 */

if (!defined("mxMainFileLoaded")) die ("You can't access this file directly...");

$module_name = basename(dirname(__FILE__));
mxGetLangfile($module_name);
$pagetitle = _FAQ2;
// ###########################################################
function ShowFaq($id_cat, $categories)
{
    global $bgcolor2, $prefix;
    $categories = strip_tags(urldecode($categories));
    $result = sql_query("select id, question, answer from ${prefix}_faqanswer where id_cat='" . intval($id_cat) . "' order by id DESC");
    echo"<center><a name=\"top\">&nbsp;</a><font class=\"content\"><b>" . $GLOBALS['sitename'] . " " . _FAQ2 . "</b></font></center><br><br>"
     . "<br>" . _CATEGORY . ": <a href=\"modules.php?name=FAQ\">" . _MAIN . "</a> -> " . ($categories) . "<br><br>";
    echo "<table width=\"100%\" cellpadding=\"4\" cellspacing=\"0\" border=\"0\">"
     . "<tr bgcolor=\"$bgcolor2\"><td><font class=\"option\"><font class='option'>" . _QUESTION . "</font></font></td></tr><tr><td>";
    while (list($id, $question, $answer) = sql_fetch_row($result)) {
        echo"<strong><big>&middot;</big></strong>&nbsp;&nbsp;<a href=\"#$id\">$question</a><br>";
    } 
    echo "<br><b>" . _INFO1 . " <a href='modules.php?name=FAQ&amp;faqaddnew=y&amp;id_cat=" . $id_cat . "'>" . _INFO2 . "</a> " . _INFO3 . "</b>";
    echo "</td></tr></table><br>";
} 
// ###########################################################
function ShowFaqAll($id_cat)
{
    global $bgcolor2, $bgcolor3, $prefix;
    echo "<table width=\"100%\" cellpadding=\"4\" cellspacing=\"0\" border=\"0\">"
     . "<tr bgcolor=\"$bgcolor2\"><td><font class=\"option\"><font class='option'>" . _ANSWER . "</font></font></td></tr>";
    $result = sql_query("select id, id_cat, question, answer from ${prefix}_faqanswer where id_cat='" . intval($id_cat) . "' order by id DESC");
    while (list($id, $id_cat, $question, $answer) = sql_fetch_row($result)) {
        echo"<tr><td class='content'><a name=\"$id\"></a>"
         . "<hr size='1' noshade style='border: 1px solid $bgcolor3;'><strong><big>&middot;</big></strong>&nbsp;&nbsp;<b><span style='font-size:larger'>$question</span></b>"
         . "<div align=\"justify\" class='content'>" . mxNL2BR($answer) . "</div>"
         . "<br><div align='right'>[&nbsp;<a href=\"#top\">" . _BACKTOTOP . "</a>&nbsp;]</div><br>"
         . "</td></tr>";
    } 
    echo "</table><br><br>"
     . "<div align=\"center\"><b>[&nbsp;<a href=\"modules.php?name=FAQ\">" . _BACKTOFAQINDEX . "</a>&nbsp;]</b></div>";
} 
// ###########################################################
function ShowFaqNew()
{
    global $prefix;

    $faqaddsave = (empty($_REQUEST['faqaddsave'])) ? "" : $_REQUEST['faqaddsave'];
    $faqemail = (empty($_REQUEST['faqemail'])) ? "" : mxStripSlashes($_REQUEST['faqemail']);
    $faqquestion = (empty($_REQUEST['faqquestion'])) ? "" : mxStripSlashes($_REQUEST['faqquestion']);
    $faqaddcat = (empty($_REQUEST['faqaddcat'])) ? 0 : $_REQUEST['faqaddcat'];

    $error = (empty($error)) ? "" : $error;

    if (MX_IS_USER) {
        $cookie = mxGetUserSession();
        $faqname = $cookie[1];
    } else {
        $faqname = $GLOBALS['anonymous'];
    } 

    if ($faqaddsave == 'y') {
        $faqname = str_replace('"', "'", $faqname);
        $faqemail = str_replace('"', "'", $faqemail);
        $faqquestion = str_replace('"', "'", $faqquestion);
        $faqip = MX_REMOTE_ADDR;
        $faqaddsave = '';
        $error = "<span style='color:red'><b>" . _FERROR . "</b>: ";
        if (empty($faqname)) {
            $error .= _ENTERNAME . "</span><br><br>\n";
        } else if ($faqemail != '' and (!strchr($faqemail, '@') or !strchr($faqemail, '.'))) {
            $error .= "'$faqemail' " . _WRONGMAIL . "</span><br><br>\n";
            $faqemail = '';
        } else if ($faqquestion == '') {
            $error .= _QUESTION . "</span><br><br>\n";
        } else {
            $faqaddsave = 'y';
            $error = '';
            $query = "INSERT INTO ${prefix}_faqnew (name,email,question,date,id_cat,ip) VALUES (
			'" . mxAddSlashesForSQL(strip_tags($faqname)) . "',
			'" . mxAddSlashesForSQL(strip_tags($faqemail)) . "',
			'" . mxAddSlashesForSQL(strip_tags($faqquestion)) . "',
			'" . date("%Y-%m-%d %H:%i:%s") . "',
			'" . $faqaddcat . "',
			'" . $faqip . "')"; 
            // print $query;
            $result1 = sql_query($query);
            if (!$result1) mxErrorScreen("Database-Error");
            if (!empty($faqemail) && $result1) {
                $message = _DEAR . " " . $faqname . "\n\n"
                 . _USEMAIL1 . " $faqip " . _USEMAIL2 . "\n"
                 . _USEMAIL3 . "\n"
                 . $GLOBALS['sitename'] . " (" . MX_HOME_URL . ")\n\n"
                 . _QUEST1 . "\n"
                 . "\t$faqquestion\n\n"
                 . _NOMESS . "\n\n"
                 . _SEE1 . " " . $GLOBALS['sitename'] . " " . _WEBSITE . "\n\n"
                 . _GREETS . "\n"
                 . $GLOBALS['sitename'] . " " . _TEAM;
                $subject = _THANKS1 . " " . $GLOBALS['sitename'];
                $sender = $faqemail; 
                // mxDebugFuncVars($message);
                mxMail($faqemail, $subject, $message, $sender, "text", "", "" . $GLOBALS['sitename'] . " " . _VISITOR . "");
            } 
            if (!empty($GLOBALS['adminmail']) && $result1) {
                $message = _HI . " " . $GLOBALS['sitename'] . " " . _ADMIN . ",\n\n"
                 . _USER1 . " '" . $faqname . "' ($faqemail) [$faqip] " . _QUESTION3 . "\n\n"
                 . "\t$faqquestion\n\n"
                 . _DIRECT . " " . MX_HOME_URL . "/admin.php?op=FaqAnswerNew " . _ANSWER . "\n\n"
                 . _GREETS . "\n" . _TEAM;
                $subject = _QUESTION2 . " '" . $GLOBALS['sitename'] . "'";
                $sender = $faqemail; 
                // mxDebugFuncVars($sender, $subject);
                @mxMail($GLOBALS['adminmail'], $subject, $message, $sender, "text", "", "" . $GLOBALS['sitename'] . " " . _VISITOR . "");
            } 
            // die("Subject:".$subject.", Message:".$message." Senderemail:".$sender."
            include("header.php");
            OpenTable();
            echo "<center><b>" . _THANKS1 . "</b></center><br>\n" 
            // .""._THANKS2."<br>"
            . _THANKS3 . "<br><br>\n"
             . "<center><b>" . _FINISH . "<br>\n";
            CloseTable();
            include("footer.php");
        } 
    } 

    if ($faqaddsave != 'y') {
        $categories = (empty($_REQUEST['categories'])) ? "" : $_REQUEST['categories'];
        $id_cat = (empty($_REQUEST['id_cat'])) ? "" : $_REQUEST['id_cat'];

        if ($GLOBALS["multilingual"] == 1) {
            if (ereg("german", $GLOBALS["currentlang"])) $thislang = "german";
            else $thislang = $GLOBALS["currentlang"];
            $querylang = "WHERE (flanguage LIKE '" . $thislang . "%' OR flanguage='')";
        } else {
            $querylang = "";
        } 
        $result = sql_query("select id_cat, categories from ${prefix}_faqcategories $querylang ORDER BY categories DESC");
        while (list($idcat, $categorie) = sql_fetch_row($result)) {
            $sel = ($id_cat == $idcat) ? " selected" : "";
            $options[] = "<option value='" . $idcat . "'" . $sel . ">" . $categorie . "</option>";
        } 
        include("header.php");
        OpenTable();
        echo "<center><font class=\"option\">" . _ADDFAQ . "</font></center><br><br>\n";
        echo "<font class=\"title\">" . _NEWQUEST . ":</font>\n";
        echo "<br><br><br>" . $error . "<br>";
        echo "<form method='POST' action='modules.php'>\n";
        echo "<table border='0' cellspacing='0' cellpadding='3'>\n";
        echo "<tr><td height=50>" . _YNAME . "</td><td><input type='text' size=25 name='faqname' value=\"" . $faqname . "\"></td></tr>\n";
        echo "<tr><td height=50>" . _YMAIL . "</td><td>" . _YMAILINFO . "<br><input type='text' size=40 name='faqemail' value=\"" . $faqemail . "\"></td></tr>\n";
        echo "<tr><td height=50>" . _YQUEST . "</td><td>" . _MAXCHAR . "<br><input type='text' size=80 name='faqquestion' value=\"" . $faqquestion . "\"></td></tr>\n";
        if (isset($options)) echo "<tr><td height=50>" . _CATEGORY . "</td><td><select name='id_cat'>" . implode("\n", $options) . "</select></td></tr>\n";
        echo "<input type='hidden' name='categories' value='" . $categories . "'>\n";
        echo "<input type='hidden' name='faqaddcat' value='" . $id_cat . "'>\n";
        echo "<input type='hidden' name='faqaddnew' value='y'>\n";
        echo "<input type='hidden' name='faqaddsave' value='y'>\n";
        echo "<input type='hidden' name='name' value='FAQ'>\n";
        echo "<tr><td colspan='2' height=50><input type='submit' name='submit' value='" . _FAQSEND . "'></td></tr>\n";
        echo "</table>\n";
        echo "</form>\n";
        CloseTable();
        include("footer.php");
    } 
} 
// ###########################################################
$myfaq = (empty($_REQUEST['myfaq'])) ? "" : $_REQUEST['myfaq'];
$faqaddnew = (empty($_REQUEST['faqaddnew'])) ? "" : $_REQUEST['faqaddnew'];

if ($faqaddnew == 'y') {
    ShowFaqNew();
} else if (empty($myfaq)) {
    if ($GLOBALS["multilingual"] == 1) {
        if (ereg("german", $GLOBALS["currentlang"])) $thislang = "german";
        else $thislang = $GLOBALS["currentlang"];
        $querylang = "WHERE (flanguage LIKE '" . $thislang . "%' OR flanguage='')";
    } else {
        $querylang = "";
    } 
    include("header.php");
    OpenTable();
    echo "<center><font class=\"option\">" . _FAQ2 . "</font></center><br><br>"
     . "<table width=\"100%\" cellpadding=\"4\" cellspacing=\"0\" border=\"0\">"
     . "<tr><td bgcolor=\"$bgcolor2\"><font class=\"option\"><b>" . _CATEGORIES . "</b></font></td></tr><tr><td>";
    $result = sql_query("select id_cat, categories from ${prefix}_faqcategories $querylang");
    while (list($id_cat, $categories) = sql_fetch_row($result)) {
        echo"<strong><big>&middot;</big></strong>&nbsp;<a href=\"modules.php?name=FAQ&amp;myfaq=yes&amp;id_cat=$id_cat\">$categories</a><br>";
    } 
    echo "</td></tr></table>";
    CloseTable();
    include("footer.php");
} else {
    $result = sql_query("select categories from ${prefix}_faqcategories where id_cat=" . intval($id_cat));
    list($categories) = sql_fetch_row($result);
    $pagetitle = _FAQ . ': ' . $categories;
    include("header.php");
    OpenTable();
    ShowFaq($id_cat, $categories);
    ShowFaqAll($id_cat);
    CloseTable();
    include("footer.php");
} 
