<?php
if (!defined('_NT_TABLE_'))
{
  define('_NT_TABLE_', 'true');
class   NtTable
{
  private $_field = array('Title' => array('name1' => 'data1', 'name2' => 'data2', 'name3' => 'data3'));
  private $_arrayToDisplay = array();
  private $_links = array();
  private $_addName = "addValue";
  private $_addValue = "Ajouter une valeur";
  private $_eventtile = "Actions";

  public function    __construct()
  {   
  }
  
  public  function  setEventTitle($title)
  {
    $this->_eventtitle = $title;
  }
  
  /*
  $field => contenu du tableau: 1ere colonne
  forme: array('title' => array('name1' => 'data1', 'name2' => 'data2', 'name3' => 'data3'));
  
  */
  public function    setField($field)
  {
    $this->_field = $field;
  }
  /*
  permet associer une image a name soumis au clic du type:
  array($srcimg, $name, $iwidth, $iheight)
  */
  public function    setEvent($event)
  {
    $srcimg = '';
    $sname = '';
    $iwidth = 0;
    $iheight = 0;
    foreach ($event as $data)
    {
      if ($srcimg == '')
        $srcimg = $data;
      else if ($sname == '')
        $sname = $data;
      else if ($iwidth == 0)
        $iwidth = (int)$data;
      else if ($iheight == 0)
        $iheight = (int)$data;
    }
    $slink = '<input  type=submit style="font-size:0px;width:'.$iwidth.'px; height:'.$iheight.'px;background-image: url('.$srcimg.') ;border:0;margin-left: 3px;cursor: pointer" name="'.$sname.'"'; // .'/>';
    $this->_links[] = $slink;
  }
  static public  function   includeSortTabLib()
  {
    $lib = '<script type="text/javascript" src="../modules/webcamsocialshopper/js/jquery-latest.js"></script> 
        <script type="text/javascript" src="../modules/webcamsocialshopper/js/jquery.tablesorter.js"></script>';
    return ($lib);
  }
  private function   _startConstruct($contents)
  {
    $i = 0;
    $toDisplay = array();
    asort($contents);
    foreach ($contents as $name => $content)
    {
        $this->_arrayToDisplay[$name][] = $content;
      $i++;
    }
  }
  
  public function    setAddName($name)
  {
    $this->_addName = $name;
  }
  
  public function    setAddValue($value)
  {
    $this->_addValue = $value;
  }
  
  public function    getContent()
  {
    //if ($_POST['AddNtArray']) return ($this->getForm());
    $html = '<form action='.$_SERVER['REQUEST_URI'].' method=post><table id="myTable" class="tablesorter NtTable table"><THEAD>';
    $atitle = array();
    $contents = array();
    foreach ($this->_field as $title => $contents)
    {
      $atitle[] = $title;
      $this->_startConstruct($contents);
    }
    $html .= '<tr class="NtTrHeader">';
    foreach ($atitle as $stitle)
      $html .= '<th class="NtTh">'.$stitle.'</th>';
    $html .= '<th class="NtTh" style="cursor:default;text-decoration:none;">'.$this->_eventtitle.'</th></tr></THEAD><TBODY>';
    $g = 0;
    foreach ($this->_arrayToDisplay as $key => $alines)
    {
      $html .= '<tr class="NtTr"';  
	  ($g % 2) ?   $html .= ' style="background-color:#EFEFEF;">' : $html .= ' style="background-color:#FFF;">';
      foreach ($alines as $sCell)
        $html .= '<td class="NtTd">'.$sCell.'</td>';
      $html .= '<td class="NtEvent">';
      foreach ($this->_links as $link)
        $html .= $link.' value="'.$key.'"/>';
      $html .= '</td>'; 
      $html .= '</tr>';
      $g++;
      
    }
    $html .= '</TBODY></table></form><script type="text/javascript">$(document).ready(function() 
    { 
        $("#myTable").tablesorter(); 
    } 
    ); </script>';
    return ($html);
    //constructToEnd();
  }
}
}
?>