<?php
    include "misc.inc";
    start_page();
?>
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=koi8-r">

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

    $result = mysql_db_query($db_database,
                             "SELECT Name, Email, COUNT(Texts.Id) " .
                             "FROM Authors " .
                             "LEFT JOIN Texts ON Texts.AuthorId = Authors.Id " .
                             "WHERE Authors.Id = " . addslashes($Id) .
                             " GROUP BY Authors.Id",
                             $link);

    $row = mysql_fetch_array($result);
    print("<TITLE>" . htmlspecialchars($row[0]) . "</TITLE>")
?>
</HEAD>

<BODY link="#000033" vlink="#333333" alink="#CC0000">
<?php
    show_header();
?>


<p>
<center>
<table border=0 BGCOLOR="#000000" cellspacing=0 cellpadding=0>
<tr><td>
<table border=0 cellspacing=1 cellpadding=3>
        <tr>
                <td BGCOLOR = "#CCCCCC">
                        <B>Имя:</B>
                </td>
                <td BGCOLOR = "#EEEEEE">
                        <?php print htmlspecialchars($row[0]) ?>
                </td>
        </tr>
        <tr>
                <td BGCOLOR = "#CCCCCC">
                        <B>Адрес:</B>
                </td>
                <td BGCOLOR = "#EEEEEE">                
                        <?php print "<A HREF = \"mailto:" . htmlentities($row[1]) . "\">" . stripslashes($row[1]) . "</A>" ?>
                </td>
        </tr>
        <tr>
                <td BGCOLOR = "#CCCCCC">
                        <B>Количество работ:</B>
                </td>
                <td BGCOLOR = "#EEEEEE">
                        <?php print htmlspecialchars($row[2]) ?>
                </td>
        </tr>
        <tr>
                <td colspan="2" BGCOLOR = "#EEEEEE">
                    <?php
                        $fav = false;
                        $login = current_user();
                        if($login)
                        {
                            $fav=is_favorie($db_database, $link, $Id, $login);
                        }
                        if($fav)
                        {
                            print "<a href=\"remove_author_handler.php?id=" . $Id . "\">TODO: localize: Remove from favorites</a>";
                        } else
                        {
                            print "<a href=\"add_author_handler.php?id=" . $Id . "\">TODO: localize: add as favorite</a>";
                        }
                    ?>
                </td>
        </tr>
</table>
</td></tr></table>
</center>

<?php

    mysql_free_result ($result);

    include "sorted_table.inc";

    class AuthorTextsTable extends SortedTable
    {
        var $Id;

        function AuthorTextsTable($Id)
        {
            $this->SortedTable(array("TextTitle", "TextPublishedAt", "TextBytes",  "TextViews", "MarksValue", "MarksCount", "CommentsThread"),
                               array("Название", "Дата", "Размер", "Просмотры", "Рейтинг", "Количество  Оценок", "Отзывы"),

                               "SELECT Texts.Id, Texts.Title AS TextTitle, UNIX_TIMESTAMP(Texts.PublishedAt) AS TextPublishedAt, " .
                                    "Texts.Bytes AS TextBytes, Texts.Views AS TextViews, " .
                                    "Texts.MarksValue, Texts.MarksCount," .
                                    "Texts.CommentThreadId " .
                               "FROM Texts " .
                               "WHERE Texts.AuthorId = " . addslashes($Id),

                               "SELECT COUNT(*) FROM Texts WHERE AuthorId = " . addslashes($Id),
                               1,
                               array(0=>50),
                               array(0 => "ASC", 1 => "DESC", 2 => "DESC", 3 => "DESC", 4 => "DESC")
                              );
            $this->Id = $Id;
        }

        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 ALIGN=CENTER>" . htmlspecialchars(gmstrftime($DATE_FORMAT, $row[2])) . "</TD>\n");
                    break;

                case 2:
                    print("<TD ALIGN=CENTER>" . htmlspecialchars($row[3]) . "b</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;

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

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

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

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

    $table = new AuthorTextsTable($Id);

    print("<CENTER><H3>Работы</H3></CENTER>");

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

    show_footer();
?>

</BODY>
</HTML>
