<?php

    // this class provides simple table TODO: SortedTable should be inherited from this class
    class Table
    {
        var $fields;
        var $labels;
        var $sql;
        var $sql_count;

        var $col_width;

        var $row_count;

        function Table($fields, $labels, $sql, $sql_count, $offset, $col_width = "", $row_count=20)
        {
            $this->fields    = $fields;
            $this->labels    = $labels;
            $this->sql       = $sql;
            $this->sql_count = $sql_count;
            $this->row_count = $row_count;
            $this->col_width = $col_width;
            $this->offset    = $offset;
            if(!$this->offset) 
                $this->offset = 0;
        }

        function processCell($row, $i)
        {
        }

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

        function showTable($path)
        {
            $len = count($this->fields);

            include "db_connect.inc";

            $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 .
                                     " LIMIT " . addslashes($this->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=CENTER>");

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

            if($pages > 1)
            {
                $curpage = (int)($this->offset / $this->row_count);
  
                if($curpage <= 0)
                {
                    $pages_str .=  "|&lt;&nbsp;&lt;&nbsp;";
                }
                else
                {
                    $pages_str .= ("<A HREF=\"" . $path . // first page
                    "?pos=" . 0 .
                    $this->urlParameters(0) .  // TODO correct
                    "\">" . "|&lt;" . "</A>&nbsp;");

                    $pages_str .= ("<A HREF=\"" . $path . // prev page
                    "?pos=" . ($this->offset - $this->row_count) .
                    $this->urlParameters(0) .  // TODO correct
                    "\">" . "&lt;" . "</A>&nbsp;");
                }
                $pages_str .= ("&nbsp;");

                for($i=0; $i<$pages; $i++)
                {
                    if($i == $curpage)
                    {
                        $pages_str .=  "<b>" . ($i + 1) . "</b>";
                    } else
                    {
                        $pages_str .= ("<A HREF=\"" . $path . // next page
                        "?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 .=  ">&nbsp;>|&nbsp;";
                }
                else
                {
                    $pages_str .= ("<A HREF=\"" . $path . // next page
                    "?pos=" . ($this->offset + $this->row_count) .
                    $this->urlParameters(0) .  // TODO correct
                    "\">" . "&gt;" . "</A>&nbsp;");

                    $pages_str .= ("<A HREF=\"" . $path . // last page
                    "?pos=" . (($pages-1) * $this->row_count) .
                    $this->urlParameters(0) .  // TODO correct
                    "\">" . "&gt|" . "</A>&nbsp;");
                }
            }
            else
                $pages_str .= ("&nbsp;");

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

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

            print("<TABLE BORDER=\"0\" CELLSPACING=\"1\" CELLPADDING=\"2\" WIDTH=\"100%\" style='border: 1px solid black'>");
            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 . "%\"";
                }

                print("<TD ALIGN=CENTER" . $s . ">" .
                            "<B>" . $this->labels[$i] . "</B></TD>\n");
            }

            $j=0;
            while($row = mysql_fetch_array($result))
            {
                if($j % 2 == 0)
                    print("<TR BGCOLOR=\"#DDDDDD\">\n");
                else
                    print("<TR BGCOLOR=\"#CCCCCC\">\n");
//                print("<TR BGCOLOR=\"#DDDDDD\">\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>");
        }
    }
?>
