<?php
/**
 * This file is part of
 * pragmaMx - Web Content Management System.
 * Copyright by pragmaMx Developer Team - http://www.pragmamx.org
 *
 * pragmaMx 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 3 of the License, or
 * (at your option) any later version.
 *
 * $Revision: 1.8.2.8.2.5.2.1 $
 * $Author: tora60 $
 * $Date: 2010/06/24 06:38:09 $
 *
 * 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/
 */

defined('mxMainFileLoaded') or die('access denied');

/* Rechte Bloecke anzeigen?
 * 1 = ja
 * 0 = nein */
$index = 0;

$module_name = basename(dirname(__FILE__));
mxGetLangfile($module_name);

/* Seitentitel-Fallback */
$pagetitle = _FAQ2;

/* was du wolle? */
switch (true) {
    /* neue FAQ anfuegen */
    case (isset($_REQUEST['faqaddnew']) && $faqaddnew == 'y'):
        $faqaddsave = (empty($_POST['faqaddsave'])) ? '' : $_POST['faqaddsave'];
        $faqname = (empty($_POST['faqname'])) ? '' : mxStripSlashes($_POST['faqname']);
        $faqemail = (empty($_POST['faqemail'])) ? '' : mxStripSlashes($_POST['faqemail']);
        $faqquestion = (empty($_POST['faqquestion'])) ? '' : mxStripSlashes($_POST['faqquestion']);
        $faqaddcat = (empty($_POST['faqaddcat'])) ? 0 : $_POST['faqaddcat'];

        $error = "";

        if ($faqaddsave == 'y') {
            $faqname = str_replace('"', "'", $faqname);
            $faqquestion = str_replace('"', "'", $faqquestion);
            $faqaddsave = '';

            if ($faqquestion == '') {
                /* Frage gestellt? */
                $error = _QUESTION . '?';
            } else if (!empty($faqemail) && !mxCheckEmail($faqemail)) {
                /* gueltige Mailadresse? */
                $error = "'" . $faqemail . "' " . _WRONGMAIL;
                $faqemail = '';
            } else if (empty($faqname)) {
                /* Name vorhanden? */
                $error = _ENTERNAME;
            }
            /* Captcha pruefen */
            if (!$error) {
                $captcha_object = load_class('Captcha', 'faqon');
                if (!$captcha_object->check($_POST, 'captcha')) {
                    $error = _CAPTCHAWRONG;
                }
            }
            /* wenn kein Fehler */
            if (!$error) {
                $faqaddsave = 'y';
                /* Frage in DB schreiben */
                $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)) . "',
    			NOW(),
    			" . intval($faqaddcat) . ",
    			'" . MX_REMOTE_ADDR . "')";
                $result1 = sql_query($query);
                if (!$result1) {
                    mxErrorScreen('Database-Error');
                    return;
                }
                /* User benachrichtigen */
                if (!empty($faqemail) && $result1) {
                    $message = _DEAR . " " . $faqname . "\n\n"
                     . _USEMAIL1 . " " . MX_REMOTE_ADDR . " " . _USEMAIL2 . "\n"
                     . _USEMAIL3 . "\n"
                     . $GLOBALS['sitename'] . " (" . PMX_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'];
                    mxMail($faqemail, $subject, $message);
                }
                /* Admin benachrichtigen */
                if (!empty($GLOBALS['notify']) && !empty($GLOBALS['notify_email']) && $result1) {
                    $message = _HI . " " . $GLOBALS['sitename'] . " " . _ADMIN . ",\n\n"
                     . _USER1 . " '" . $faqname . "' ($faqemail) [" . MX_REMOTE_ADDR . "] " . _QUESTION3 . "\n\n"
                     . "\t$faqquestion\n\n"
                     . _DIRECT . " " . PMX_HOME_URL . "/admin.php?op=FaqAnswerNew " . _ANSWER . "\n\n"
                     . _GREETS . "\n" . _TEAM;
                    $subject = _QUESTION2 . " '" . $GLOBALS['sitename'] . "'";
                    mxMail($GLOBALS['adminmail'], $subject, $message, $faqemail);
                }
                // die("Subject:".$subject.", Message:".$message." Senderemail:".$faqemail."
                $msg = '<b>' . _THANKS1 . '</b><br>' . _THANKS3;
                mxRedirect('modules.php?name=' . $module_name . '&cat=' . intval($faqaddcat), $msg, 5);
                return;
            }
        }
        /* User oder Gast? */
        if (MX_IS_USER) {
            $usersession = mxGetUserSession();
            $faqname = $usersession[1];
        } else {
            $faqname = $GLOBALS['anonymous'];
        }

        if ($faqaddsave != 'y') {
            $categories = (empty($_REQUEST['categories'])) ? "" : $_REQUEST['categories'];
            $cat = (empty($_REQUEST['cat'])) ? "" : $_REQUEST['cat'];

            if ($GLOBALS["multilingual"] == 1) {
                if (strpos($GLOBALS['currentlang'], 'german') === 0) $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");
            while (list($idcat, $categorie) = sql_fetch_row($result)) {
                $sel = ($cat == $idcat) ? " selected='selected'" : '';
                $options[] = "<option value='" . $idcat . "'" . $sel . ">" . $categorie . "</option>";
            }
            $captcha_object = load_class('Captcha', 'faqon');
            include('header.php');
            title(_ADDFAQ);
            if ($error) {
                openTableAl();
                echo '<b>' . _FERROR . '</b>:<br>' . $error . '';
                closeTableAl();
                echo '<br><br>';
            }
            OpenTable();
            echo '<font class="title">' . _NEWQUEST . ':</font>';
            echo '<form method="post" action="modules.php?name=' . $module_name . '">';
            echo '<table cellspacing="0" cellpadding="5">';
            echo '<tr><td>' . _YNAME . '</td><td><input type="text" size="25" name="faqname" value="' . $faqname . '" /></td></tr>';
            echo '<tr><td>' . _YMAIL . '</td><td>' . _YMAILINFO . '<br><input type="text" size="40" name="faqemail" value="' . $faqemail . '" /></td></tr>';
            echo '<tr><td>' . _YQUEST . '</td><td>' . _MAXCHAR . '<br><input type="text" size="80" name="faqquestion" value="' . $faqquestion . '" /></td></tr>';
            if (isset($options)) {
                echo '<tr><td>' . _CATEGORY . '</td><td><select name="cat">' . implode("\n", $options) . '</select></td></tr>';
            }
            echo '<tr><td>&nbsp;</td><td>' . $captcha_object->complete() . '</td></tr>';
            echo '<tr><td colspan="2"><input type="submit" value="' . _FAQSEND . '" />';
            echo '<input type="hidden" name="categories" value="' . $categories . '" />';
            echo '<input type="hidden" name="faqaddcat" value="' . $cat . '" />';
            echo '<input type="hidden" name="faqaddnew" value="y" />';
            echo '<input type="hidden" name="faqaddsave" value="y" />';
            echo '<input type="hidden" name="name" value="' . $module_name . '" /> </td></tr>';
            echo '</table>';
            echo '</form>';
            CloseTable();
            include('footer.php');
        }

        break;

    /* Liste aller Kategorien (faq-Startseite) */
    case empty($_GET['cat']):
        /* Wenn Mehrsprachigkeit aktiviert ist,
         * die aktuelle Sprach bei der DB-Abfrage beruecksichtigen */
        if ($GLOBALS['multilingual'] == 1) {
            if (strpos($GLOBALS['currentlang'], 'german') === 0) {
                $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");
        include('header.php');
        title(_FAQ2);
        OpenTable();
        echo "<table width=\"100%\" cellpadding=\"4\" cellspacing=\"0\" border=\"0\">"
         . "<tr><td class=\"bgcolor2\"><font class=\"option\"><b>" . _CATEGORIES . "</b></font></td></tr><tr><td>";
        while (list($idcat, $categories) = sql_fetch_row($result)) {
            echo "<strong><big>&middot;</big></strong>&nbsp;<a href=\"modules.php?name=" . $module_name . "&amp;cat=" . $idcat . "\">$categories</a><br>";
        }
        echo "</td></tr></table>";
        CloseTable();
        include('footer.php');

        break;

    /* Anzeige einer Kategorie */
    default:

/*
  Change -- add jQuery Accordion
 */
pmxHeader::add_jquery(
	'ui/jquery-ui.min.js',
	'ui/jquery.ui.accordion.min.js'
	);



        $cat = (empty($_REQUEST['cat'])) ? 0 : intval($_REQUEST['cat']);
        $result = sql_query("select categories from ${prefix}_faqcategories where id_cat=" . intval($cat));
        list($categories) = sql_fetch_row($result);
        $pagetitle = _FAQ . ': ' . $categories;
        include('header.php');
        title(_FAQ2);
        OpenTable();
        $categories = strip_tags(urldecode($categories));
        $result = sql_query("select id, question, answer from ${prefix}_faqanswer where id_cat='" . intval($cat) . "' order by id");
        echo "<a name=\"top\"></a>"
         . "<br>" . _CATEGORY . ": <a href=\"modules.php?name=" . $module_name . "\">" . _MAIN . "</a> -&gt; " . ($categories) . "<br><br>";

		 echo "<br><b>" . _INFO1 . " <a href='modules.php?name=" . $module_name . "&amp;faqaddnew=y&amp;cat=" . $cat . "'>" . _INFO2 . "</a> " . _INFO3 . "</b>";

        CloseTable();
        echo '<br>';
        OpenTable();

		 echo  ' <script type="text/javascript">
		  $(document).ready(function() {
			$("#accordion").accordion({ autoHeight: false });
			$("#accordion").accordion( "option", "collapsible", true );
			$("#accordion").accordion( "option", "active", 0 );

		  });
		  </script>
		  ';


		echo "<div>";
		 echo '	<div id="accordion" >';

        $result = sql_query("select id, question, answer from ${prefix}_faqanswer where id_cat='" . intval($cat) . "' order by id");
        while (list($id, $question, $answer) = sql_fetch_row($result)) {
			echo