<?php

    // this class provide sorted table
    class SortedTable
    {
        var $fields;
        var $labels;
        var $sql; // without ORDER BY clause
        var $sql_count;

        var $def_field;
        var $col_width;
        var $col_sort_type;
        var $not_sorted;

        var $row_count;

        function SortedTable($fields, $labels, $sql, $sql_count, $def_field = 0, $col_width = NULL, $col_sort_type = "", $not_sorted = "", $row_count=20)
        {
            $this->fields    = $fields;
            $this->labels    = $labels;
            $this->sql       = $sql;
            $this->sql_count = $sql_count;
            $this->def_field = $def_field;
            $this->col_width = $col_width;
            $this->col_sort_type = $col_sort_type;
            $this->row_count = $row_count;
            $this->not_sorted = $not_sorted;
        }

        function processCell($row, $i)
        {
        }

        function urlParameters($i)
        {
            return "";
        }

        function showTable($path, $sort, $sort_type, $offset)
        {
            if(!$sort || !compat_in_array($sort, $this->fields))
            {
                $sort = $this->fields[$this->def_field];
            }

            if(!$sort_type && ($sort_type != "ASC" || $sort_type != "DESC"))
            {
                $sort_type = "DESC";
            }

            $reverse_sort = "ASC";
            if($sort_type == "ASC")
            {
                $reverse_sort = "DESC";
            }

            $len = count($this->fields);
            $sort_types = array();
            for($i = 0; $i<$len; $i++)
            {
                if($sort != $this->fields[$i])
                {
                    if($i < array_key_exists($i, $this->col_sort_type) && $this->col_sort_type[$i])
                    {
                        $sort_types[] = $this->col_sort_type[$i];
                    }
                    else
                        $sort_types[] = "DESC";
                }
                else
                    $sort_types[] = $reverse_sort;
            }

            if(!$offset)
                $offset = 0;

            include "db_connect.inc";


            mysql_db_query($db_database,
                           "set option SQL_BIG_TABLES=1",
                           $link);

            $result = mysql_db_query($db_database,
                                     $this->sql_count,
                                     $link);
            $row = mysql_fetch_array($result);
            $total = $row[0];

            $result = mysql_db_query($db_database,
                                     $this->sql .
                                     " ORDER BY " . addslashes($sort) . " " . addslashes($sort_type) .
                                     " LIMIT " . addslashes($offset) . "," . addslashes($this->row_count),
                                     $link);
            
            $pages_str = "";

            $pages_str .= ("<CENTER>");
            $pages_str .= ("<TABLE BGCOLOR=\"#DCEFEF\" BORDER=\"0\" CELLSPACING=\"1\" CELLPADDING=\"3\" WIDTH=\"100%\">");
            $pages_str .= ("<TR>");
            $pages_str .= ("<TD ALIGN=LEFT>");
            $pages_str .= ("<B>Страницы:</B>");
            $pages_str .= ("<TD/>");
            $pages_str .= ("<TD ALIGN=CENTER WIDTH =\"100%\">");

            $pages = (int)($total/$this->row_count);
            if($total % $this->row_count > 0)
                $pages++;

            if($pages > 1)
            {
                $curpage = (int)($offset  / $this->row_count );
                $cur10   = (int)($curpage / 10               );
  
                if($curpage > 0)
                {
                    if($cur10 > 0)
                    {
                        // Prev 10
                        $pages_str .= ("<A HREF=\"" . $path . // first page
                                       "?sort=" . $sort .
                                       "&sort_type=" . $sort_type .
                                       "&pos=" . (($cur10-1)*10*$this->row_count) .
                                       $this->urlParameters(0) .  // TODO correct
                                       "\">" . "[Предыдущие 10]" . "</A>&nbsp;");
                    }

                    $pages_str .= ("<A HREF=\"" . $path . // prev page
                    "?sort=" . $sort .
                    "&sort_type=" . $sort_type .
                    "&pos=" . ($offset - $this->row_count) .
                    $this->urlParameters(0) .  // TODO correct
                    "\">" . "[Предыдущая]" . "</A>&nbsp;");
                }
                $pages_str .= ("&nbsp;");

                for($i=$cur10*10; $i<$pages && $i<(($cur10+1)*10); $i++)
                {
                    if($i == $curpage)
                    {
                        $pages_str .=  "<b>" . ($i + 1) . "</b>";
                    } else
                    {
                        $pages_str .= ("<A HREF=\"" . $path . // next page
                        "?sort=" . $sort .
                        "&sort_type=" . $sort_type .
                        "&pos=" . ($i * $this->row_count) .
                        $this->urlParameters(0) .  // TODO correct
                        "\">" . ($i + 1). "</A>");
                    }
                    $pages_str .= ("&nbsp;");

                }
                $pages_str .= ("&nbsp;");

                if($curpage < $pages-1)
                {
                    $pages_str .= ("<A HREF=\"" . $path . // next page
                                   "?sort=" . $sort .
                                   "&sort_type=" . $sort_type .
                                   "&pos=" . ($offset + $this->row_count) .
                                   $this->urlParameters(0) .  // TODO correct
                                   "\">" . "[Следующая]" . "</A>&nbsp;");
                    
                    $last10   = (int)($pages / 10);
                    if($cur10 != $last10)
                    {
                        // next 10
                        $pages_str .= ("<A HREF=\"" . $path . // last page
                                       "?sort=" . $sort .
                                       "&sort_type=" . $sort_type .
                                       "&pos=" . (($cur10+1)*10*$this->row_count) .
                                       $this->urlParameters(0) .  // TODO correct
                                       "\">" . "[Следующие 10]" . "</A>&nbsp;");
                    }
                }
            }
            else
                $pages_str .= ("&nbsp;");


            $pages_str .= ("</TD>");
            $pages_str .= ("</TR>");
            $pages_str .= ("</TABLE>\n");

            if($pages > 1)
                print($pages_str);
//            else
//                print("<BR>");

            print("<TABLE BORDER=\"0\" CELLSPACING=\"1\" CELLPADDING=\"2\" WIDTH=\"100%\" style='border: 1px solid black'>\n");
            print("<TR BGCOLOR=\"#33CCCC\">\n");
            for($i=0; $i<$len; $i++)
            {
                $s = "";
                if(array_key_exists($i,$this->col_width))
                {
                    $w = $this->col_width[$i];
                    if($w)
                        $s = " WIDTH=\"" . $w . "%\"";
                }

                $bold_start = "";
                $bold_end = "";
                if($sort == $this->fields[$i])
                {
                    $bold_start = "<B>";
                    $bold_end = "</B>";
                }

                if(compat_in_array($i, $this->not_sorted))
                {
                    print("<TD ALIGN=CENTER " . $s .">" . $this->labels[$i] . "</TD>");
                }
                else
                {
                    print("<TD ALIGN=CENTER" . $s . ">" .
                                "<A HREF=\"" . $path .
                                "?sort=" . $this->fields[$i] .
                                "&sort_type=" . $sort_types[$i] .
                                "&pos=" . $offset .
                                $this->urlParameters($i) .
                                "\">". $bold_start . $this->labels[$i] . $bold_end . "</A></TD>\n");
                }
            }
            print("</TR>\n");

            $j=0;
            if($result)
            {
                while($row = mysql_fetch_array($result))
                {
                    if($j % 2 == 0)
                        print("<TR BGCOLOR=\"#DDDDDD\">\n");
                    else 
                        print("<TR BGCOLOR=\"#CCCCCC\">\n");
                    for($i = 0; $i<$len; $i++)
                    {
                        $this->processCell($row, $i);
                    }
                    print("</TR>\n");
                    $j++;
                }
                mysql_free_result ($result);
            }

            print("</TABLE>");
            print("</CENTER>");

            if($pages > 1)
                print($pages_str);
//            else
//                print("<BR>");
        }
    }

/*
    // This is an example of using sorted table class
    class A extends SortedTable
    {
        function A()
        {
            $this->SortedTable(array("Title", "Id"), array("Title", "Id"), "SELECT Title, Id FROM Texts");
        }

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

    $table = new A;
    $table->showTable("script_name.php", $sort, $sort_type);
*/
?>
