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

<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=koi8-r">
<TITLE>OBEC Authors</TITLE>
</HEAD>
<BODY link="#000033" vlink="#333333" alink="#CC0000">

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

    class AuthorsTable extends SortedTable
    {
        var $letter;

        function AuthorsTable($l)
        {
            if(!isset($l)) $l=NULL;
            $this->letter   = $l;
            $decoded_letter = urldecode($l[0]);
            $selector       = "Name LIKE '" . 
                              addslashes(strtolower($decoded_letter)) . "%'" ;

            $this->SortedTable(array("Name", "Email", "Articles", "MarksValue", "MarksCount"),
                               array("Имя", "Адрес", "Количество работ", "Рейтинг", "Количество  Оценок"),

                               "SELECT Authors.Id, Name, Email, COUNT(Texts.Id) AS Articles, Authors.MarksValue AS MarksValue, Authors.MarksCount AS MarksCount " .
                               "FROM Authors LEFT JOIN Texts " .
                               "ON Texts.AuthorId = Authors.Id WHERE " . $selector . " GROUP BY Authors.Id",

                               "SELECT COUNT(*) FROM Authors WHERE " . $selector,
                               0,
                               array( 0 => 80),
                               array(0 => "ASC", 1 => "DESC")
                              );
        }

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

                case 1:
                    print("<TD><A HREF = \"mailto:" . htmlspecialchars($row[2]) . "\">&lt;" . $row[2] . "&gt;</A></TD>\n");
                    break;

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

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

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

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

    show_header();

    if(!isset($start)) $start=NULL;
    if(!isset($sort))      $sort=NULL;
    if(!isset($sort_type)) $sort_type=NULL;
    if(!isset($pos))       $pos=NULL;

    $letters  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ*АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЭЮЯ";
    print("<CENTER>");
    if(!$start)
        print("<B>");
    print("<A HREF=\"authors.php?sort=" . $sort . "&sort_type=" . $sort_type . "&pos=0&start=\">Все авторы</A><BR>");
    if(!$start)
        print("</B>");
    for($i=0;$i<strlen($letters); $i++)
    {
        if($letters[$i] == $start)
            print("<B>" . $start . "</B>");
        else
        if($letters[$i] == "*")
            print("<BR>");
        else
        {
            print("<A HREF=\"authors.php?sort=" . $sort . "&sort_type=" . $sort_type . "&pos=0&start=" . urlencode($letters[$i]) . "\">" . $letters[$i] . "</A>");
        }

        print("&nbsp");
    }
    print("</CENTER><BR>");

    $table = new AuthorsTable($start);
    $table->showTable("authors.php", $sort, $sort_type, $pos);

    show_footer();
?>

</BODY>
</HTML>
