<?php
    include "misc.inc";
    start_page();
?>

<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=koi8-r">
<?php
    print("<link rel=\"alternate\" type=\"application/rss+xml\" title=\"RSS\" href=\"" . $PROGRAM_URL . "commentsrss.php?Id=" . $Id . "\">");

    include "db_connect.inc";
    include "common.inc";

    $result = mysql_db_query($db_database,
                             "SELECT Texts.Title, Texts.Text, Texts.AuthorId, Name, CommentThreadId, COUNT(Comments.Id), UNIX_TIMESTAMP(Texts.PublishedAt), Texts.Views, Texts.MarksCount, Texts.MarksValue, Texts.Hidden " .
                             "FROM Texts " .
                             "LEFT JOIN Authors ON Authors.Id = Texts.AuthorId " .
                             " LEFT JOIN Comments ON Texts.CommentThreadId=Comments.ThreadId " .
                             "WHERE Texts.Id = " . addslashes($Id) .
                             " GROUP BY Texts.CommentThreadId",
                             $link);
    $row = mysql_fetch_array($result);
    $title = htmlspecialchars(trim($row[0]));
    print("<TITLE>" . $title . "</TITLE>");

?>
</HEAD>
<BODY>

<?php
    show_header();

    print("\n");
    print("<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=8 WIDTH=\"100%\">\n");
        print("<TR><TD>\n");

            print("<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=\"100%\">\n");
                print("<TR>\n");
                    print("<TD NOWRAP><B>&nbsp;Автор</B></TD>\n");
                    print("<TD NOWRAP>&nbsp;&nbsp;</TD>\n");
                    print("<TD WIDTH=\"100%\"><A HREF = \"author.php?Id=" . htmlspecialchars($row[2]) . "\">" . htmlspecialchars($row[3]) . "</A></TD>\n");
                print("</TR>\n");
                print("<TR>\n");
                    print("<TD NOWRAP><B>&nbsp;Название</B></TD>\n");
                    print("<TD NOWRAP>&nbsp;&nbsp;</TD>\n");
                    print("<TD WIDTH=\"100%\" ALIGN=LEFT>" . $title . "</TD>\n");
                print("</TR>\n");
                print("<TR>\n");
                    print("<TD NOWRAP><B>&nbsp;Дата</B></TD>\n");
                    print("<TD NOWRAP>&nbsp;&nbsp;</TD>\n");
                    print("<TD NOWRAP>" . htmlspecialchars(gmstrftime($DATE_TIME_FORMAT, $row[6])) . "</TD>");
                print("</TR>\n");
                print("<TR>\n");
                    print("<TD NOWRAP><B>&nbsp;Просмотры</B></TD>\n");
                    print("<TD NOWRAP>&nbsp;&nbsp;</TD>\n");
                    print("<TD NOWRAP>" . htmlspecialchars($row[7]) . "</TD>");
                print("</TR>\n");
                if($row[8])
                {
                  print("<TR>\n");
                      print("<TD NOWRAP><B>&nbsp;Всего оценок</B></TD>\n");
                      print("<TD NOWRAP>&nbsp;&nbsp;</TD>\n");
                      print("<TD NOWRAP>" . htmlspecialchars($row[8]) . "</TD>");
                  print("</TR>\n");
                  print("<TR>\n");
                      print("<TD NOWRAP><B>&nbsp;Средняя оценка</B></TD>\n");
                      print("<TD NOWRAP>&nbsp;&nbsp;</TD>\n");
                      print("<TD NOWRAP>" . htmlspecialchars($row[9]) . "</TD>");
                  print("</TR>\n");
                }
                if($row[4])
                {
                    print("<TR>\n");
                        print("<TD NOWRAP><B>&nbsp;Комментарии</B></TD>\n");
                        print("<TD NOWRAP>&nbsp;&nbsp;</TD>\n");
                        print("<TD WIDTH=\"100%\"><A HREF = \"thread.php?thread_id=" . htmlspecialchars($row[4]) . "\">" . htmlspecialchars($row[5]) . "</A></TD>\n");
                    print("</TR>\n");
                }
                print("<TR>");
                    print("<TD NOWRAP COLSPAN=3 WIDTH=\"100%\"><HR SIZE=1 NOSHADE></TD>");
                print("</TR>");

            print("</TABLE>\n");
        print("</TD></TR>");

        print("<TR BGCOLOR=\"#EEEEEE\">");
        if($row[10]=="No")
        {
            print("<TD BGCOLOR=\"#EEEEEE\" WIDTH=\"100%\"><PRE>" . htmlspecialchars($row[1]) . "</PRE></TD>");
        } else
        {
            print("<TD BGCOLOR=\"#FFEEEE\" WIDTH=\"100%\">Автор запретил просмотр этого произведения.</TD>");
        }
        print("</TR>");

        if($row[10]=="No")
        {
                print("<TR>\n");
                print("<TD>");
                print("<FORM METHOD=\"GET\" ACTION=\"vote_handler.php\">\n");
                print("Оценка: ");
                print("<SELECT NAME=\"mark\">\n");
                print("<OPTION VALUE=\"1\">1\n");
                print("<OPTION VALUE=\"2\">2\n");
                print("<OPTION VALUE=\"3\">3\n");
                print("<OPTION VALUE=\"4\">4\n");
                print("<OPTION VALUE=\"5\">5\n");
                print("</SELECT>\n");
                print "<INPUT TYPE=\"hidden\" NAME=id VALUE=\"$Id\">";
                print("<INPUT TYPE=\"submit\" VALUE=\"Оценить\">\n");
                print("</FORM>\n");
                print("</TD>\n");
                print("</TR>\n");
        }

        print("<TR>");
        print("<TD>");
        print("<FORM METHOD=\"GET\" ACTION=\"followup.php\">\n");
        print "<INPUT TYPE=\"hidden\" NAME=TextId VALUE=\"$Id\">";
        print("<INPUT TYPE=\"submit\" VALUE=\"Послать комментарий\">\n");
        print("</FORM>\n");
        print("</TD>\n");
        print("</TR>\n");

        if($row[10]=="No"):
?>

                <TR>
                    <TD>
                        <FORM METHOD="GET" ACTION="download.php">
                            Загрузить в формате 
                            <SELECT NAME="Format">
                                <OPTION VALUE="pilotdoc">Pilot DOC
                                <OPTION VALUE="text">Unix Plain Text (LF)
                                <OPTION VALUE="crlftext">Windows Plain Text (CR LF)
                            </SELECT>
                            кодировке
                            <SELECT NAME="Charset">
                                <OPTION VALUE="cp1251">CP-1251
                                <OPTION VALUE="koi8">KOI8-r
                                <OPTION VALUE="cp866">CP-866
                                <OPTION VALUE="mac">Macintosh
                            </SELECT>
                            <?php print "<INPUT TYPE=\"hidden\" NAME=Id VALUE=\"$Id\">"; ?>
                            <INPUT TYPE="submit" VALUE="Начать">
                        </FORM>
                    </TD>
                </TR>


<?php
    endif;

    print("</TABLE>\n");

    show_footer();

    mysql_free_result ($result);
    mysql_db_query($db_database, "UPDATE Texts SET Views = Views+1 WHERE Id = " . addslashes($Id) , $link);

?>

</BODY>
</HTML>
