<?php
    include "misc.inc";
    start_page();
?>
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=koi8-r">
<TITLE>OBEC Discussions</TITLE>
</HEAD>
<BODY link="#000033" vlink="#333333" alink="#CC0000">

<?php
    include "common.inc";
    include "sorted_table.inc";

    class ThreadsTable extends SortedTable
    {
        var $title;

        function ThreadsTable($title)
        {
            $this->title = $title;

            $selector = "";
            if(strlen(trim($title)))
                $selector = "Threads.Title LIKE '%" . addslashes(urldecode(trim($title))) . "%'";

            if($selector)
            {
                $selector_count = "WHERE " . $selector;
                $selector = " AND " . $selector;
            } else
                $selector_count = "";

            $this->SortedTable(array("Title", "Total", "PublishedAt"),
                               array("Тема",  "Всего", "Дата"),

                               "SELECT Threads.Id, Threads.Title, COUNT(Comments.Id) AS Total, UNIX_TIMESTAMP(PublishedAt) " .
                               "FROM Threads, Comments " .
                               "WHERE Threads.Id=Comments.ThreadId" .
                               $selector .
                               " GROUP BY Threads.Id",

                               "SELECT COUNT(*) FROM Threads " . $selector_count,

                               2,
                               array(0 => 60, 2 => 20, 3 => 20),
                               array(0 => "ASC", 1 => "DESC", 2 => "DESC")
                              );
        }

        function processCell($row, $i)
        {
            include "config.inc"; //??
            switch($i)
            {
                case 0:
                    $title = htmlspecialchars(trim($row[1]));
                    print("<TD><A HREF = \"thread.php?thread_id=" . htmlspecialchars($row[0]) . "\">" . $title . "</A></TD>\n");
                    break;

                case 1:
                    print("<TD ALIGN=CENTER>" . htmlspecialchars($row[2]) . "</TD>\n");
                    break;

                case 2:
                    print("<TD ALIGN=CENTER>" . htmlspecialchars(gmstrftime($DATE_FORMAT, $row[3])) . "</TD>\n");
                    break;
            }
        }

        function urlParameters($i)
        {
            return "&title=" . urlencode($this->title);
        }
    }

    show_header();

    print("<CENTER>");
    if(!isset($title)) $title=NULL;
    if($title)
    {
        print("Результат поиска <B><I>" . htmlspecialchars($title) . "</I></B> в названиях работ.<BR>");
    }
    print("</CENTER>");

    $table = new ThreadsTable($title);

    if(!isset($sort))      $sort=NULL;
    if(!isset($sort_type)) $sort_type=NULL;
    if(!isset($pos))       $pos=NULL;
    $table->showTable("threads.php", $sort, $sort_type, $pos);

    show_footer();
?>

</BODY>
</HTML>
