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

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

    class TextsTable extends SortedTable
    {
        var $title;
        var $text;

        function TextsTable($title, $text)
        {
            $this->title = $title;
            $this->text  = $text;

            $b = FALSE;
            $selector = "";
            if(strlen(trim($title)))
                $selector_title = "Texts.Title LIKE '%" . quote_LIKE(urldecode(trim($title))) . "%'";
            else
                $selector_title = NULL;
            
            if(strlen(trim($text)))
                $selector_text = "Texts.Text LIKE '%" . quote_LIKE(urldecode(trim($text))) . "%'";
            else
                $selector_text = NULL;

            if($selector_title)
            {
                $selector .= $selector_title;
                if($selector_text)
                    $selector .= " AND ";
            }
            
            if($selector_text)
            {
                $selector .= $selector_text;
            }

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

            $this->SortedTable(array("TextTitle", "Name",  "T1.PublishedAt", "TextBytes", "MarksValue", "MarksCount", "TextViews", "CommentsThread"),
                               array("Название",  "Автор", "Дата", "Размер", "Рейтинг", "Оценок", "Просмотры", "Отзывы"),

                               "SELECT STRAIGHT_JOIN Texts.Id, Texts.Title AS TextTitle, UNIX_TIMESTAMP(Texts.PublishedAt) AS TextPublishedAt, " .
                                    "Name, Texts.AuthorId, Texts.Bytes AS TextBytes, Texts.MarksValue AS MarksValue, Texts.MarksCount AS MarksCount, Texts.Views AS TextViews, " .
                                    "Texts.CommentThreadId AS CommentsThread " .
                               "FROM Texts AS T1, Texts, Authors " .
                               "WHERE T1.Id=Texts.Id and Texts.AuthorId=Authors.Id " .
                               $selector,

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

                               2,
                               array(0 => 40, 1 => 40),
                               array(0 => "ASC", 1 => "ASC", 2 => "DESC", 3 => "DESC", 4 => "DESC", 5 => "DESC"),
                               array(7)
                              );
//            print($this->sql);
        }

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

                case 1:
                    print("<TD><A HREF = \"author.php?Id=" . htmlspecialchars($row[4]) . "\">" . $row[3] . "</A></TD>\n");
                    break;

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

                case 3:
                    print("<TD>" . htmlspecialchars($row[5]) . "b</TD>\n");
                    break;

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

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

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

                case 7:
                    print("<TD ALIGN=CENTER>");
                    if($row[9] > 0)
                    {
                        print("<A HREF=\"thread.php?thread_id=" . htmlspecialchars($row[9]) . "\">");
                        print("...");
                        print("</A>");
                    }
                    else
                        print("&nbsp;");

                    print("</TD>\n");
                    break;
            }
        }

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

    show_header();

    print("<CENTER>");

    if(!isset($title)) $title=NULL;
    if(!isset($text)) $text=NULL;

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

    if($text)
        print("Результат поиска <B><I>" . htmlspecialchars($text) . "</I></B> в содержимом работ.<BR>");
    print("</CENTER>");

    $table = new TextsTable($title, $text);

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

    show_footer();
?>

</BODY>
</HTML>
