1. <?php
  2. /**
  3. * HTML_Table Decorator for PEAR_PackageFileManager_Frontend
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: This source file is subject to version 3.01 of the PHP license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.php.net/license/3_01.txt.  If you did not receive a copy of
  10. * the PHP License and are unable to obtain it through the web, please
  11. * send a note to license@php.net so we can mail you a copy immediately.
  12. *
  13. * @category   PEAR
  14. * @package    PEAR_PackageFileManager_Frontend
  15. * @author     Laurent Laville <pear@laurent-laville.org>
  16. * @copyright  2005-2006 Laurent Laville
  17. * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
  18. * @version    CVS: $Id:$
  19. * @since      File available since Release 0.1.0
  20. */
  21.  
  22. require_once 'PEAR/PackageFileManager/Frontend/Decorator.php';
  23. require_once 'HTML/QuickForm.php';
  24. require_once 'HTML/Table.php';
  25.  
  26. /**
  27. * Decorator to help with fetching html_table representations of
  28. * replacements, roles and exceptions.
  29. *
  30. * @category   PEAR
  31. * @package    PEAR_PackageFileManager_Frontend
  32. * @author     Laurent Laville <pear@laurent-laville.org>
  33. * @copyright  2005-2006 Laurent Laville
  34. * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
  35. * @version    Release: @package_version@
  36. * @since      Class available since Release 0.1.0
  37. * @abstract
  38. */
  39.  
  40. class PEAR_PackageFileManager_Frontend_Decorator_HTMLTable extends PEAR_PackageFileManager_Frontend_Decorator
  41. {
  42.     /**
  43.      * Decorator container
  44.      * @var object HTML_Table object
  45.      */
  46.     var $table;
  47.  
  48.     /**
  49.      * Decorator constructor
  50.      * @param  object    $fe       PEAR_PackageFileManager_Frontend object
  51.      * @param  object    $table    HTML_Table object
  52.      * @since  0.1.0
  53.      * @access public
  54.      */
  55.     function PEAR_PackageFileManager_Frontend_Decorator_HTMLTable(&$fe)
  56.     {
  57.         parent::PEAR_PackageFileManager_Frontend_Decorator($fe);
  58.         $this->table = false;
  59.     }
  60.  
  61.     /**
  62.      * Gets instance of HTML_Table object
  63.      *
  64.      * @since  0.1.0
  65.      * @access public
  66.      */
  67.     function &getHtmlTable()
  68.     {
  69.         return $this->table;
  70.     }
  71.  
  72.     /**
  73.      * Sets instance of HTML_Table object
  74.      *
  75.      * @since  0.1.0
  76.      * @access public
  77.      */
  78.     function setHtmlTable(&$table)
  79.     {
  80.         $this->table =& $table;
  81.     }
  82.  
  83.     /**
  84.      * Decorator::getMaintList()
  85.      *
  86.      * @see    PEAR_PackageFileManager_Frontend::getMaintList()
  87.      * @since  0.1.0
  88.      * @access public
  89.      */
  90.     function getMaintList($users, $columns, $rowscope = 1, $ckid = 'users')
  91.     {
  92.         $maintainers = $this->fe->getMaintList($users);
  93.         $this->_buildList($maintainers, $columns, $rowscope, $ckid);
  94.     }
  95.  
  96.     /**
  97.      * Decorator::getFileList()
  98.      *
  99.      * @param  boolean  $default  if we get initial data set at first run
  100.      * @param  boolean  $ignore   Either if you want all files or just ignored
  101.      * @param  string   $plugin   PEAR_PackageFileManager filelist generator
  102.      * @return array
  103.      * @since  0.1.0
  104.      * @access public
  105.      */
  106.     function getFileList($default, $ignore, $plugin, $columns, $rowscope = 1, $ckid = 'files')
  107.     {
  108.         $datasrc = $this->fe->getFileList($default, $ignore, $plugin);
  109.  
  110.         $filelist = array();
  111.         foreach ($datasrc['mapping'] as $k => $filename) {
  112.             $number = count($datasrc[$k]['replacements']);
  113.             $filelist[$k] = array('path' => $filename, 'replaces' => $number);
  114.         }
  115.         $this->_buildList($filelist, $columns, $rowscope, $ckid);
  116.     }
  117.  
  118.     /**
  119.      * Decorator::getDepList()
  120.      *
  121.      * @since  0.1.0
  122.      * @access public
  123.      */
  124.     function getDepList($columns, $rowscope = 4, $ckid = 'deps')
  125.     {
  126.         $deps = $this->fe->getDepList();
  127.         $this->_buildList($deps, $columns, $rowscope, $ckid);
  128.     }
  129.  
  130.     /**
  131.      * Decorator::getRoleList()
  132.      *
  133.      * @since  0.1.0
  134.      * @access public
  135.      */
  136.     function getRoleList($default, $columns, $rowscope = -1, $ckid = 'roles')
  137.     {
  138.         $rolelist = $this->fe->getRoleList($default);
  139.         $this->_buildList($rolelist, $columns, $rowscope, $ckid);
  140.     }
  141.  
  142.     /**
  143.      * Decorator::getExceptionList()
  144.      *
  145.      * @since  0.1.0
  146.      * @access public
  147.      */
  148.     function getExceptionList($columns, $rowscope = 1, $ckid = 'files')
  149.     {
  150.         $datasrc = $this->fe->getFileList();
  151.  
  152.         $filelist = array();
  153.         foreach ($datasrc['mapping'] as $k => $filename) {
  154.             $filelist[$k] = array('path' => $filename, 'role' => $datasrc[$k]['role']);
  155.         }
  156.         $this->_buildList($filelist, $columns, $rowscope, $ckid);
  157.     }
  158.  
  159.     /**
  160.      * Returns the table structure as HTML
  161.      *
  162.      * @since  0.1.0
  163.      * @access public
  164.      */
  165.     function toHtml()
  166.     {
  167.         return $this->table->toHtml();
  168.     }
  169.  
  170.     /**
  171.      * Builds selection list
  172.      *
  173.      * @param  mixed   $datasrc  Data source
  174.      * @param  array   $columns  Names list of each column
  175.      * @return object   instance of html_table
  176.      * @since  0.1.0
  177.      * @access private
  178.      */
  179.     function _buildList($datasrc, $columns, $rowscope, $ckid)
  180.     {
  181.         $thead = &$this->table->getHeader();
  182.         $tbody = &$this->table->getBody();
  183.         $tfoot = &$this->table->getFooter();
  184.  
  185.         // add column for selection by checkboxes
  186.         array_unshift($columns, '&nbsp;');
  187.         $cc1 = count($columns);
  188.  
  189.         // add header cells of outer table
  190.         $attr = array();
  191.         for($c = 0; $c < $cc1; $c++) {
  192.             $attr[$c] = 'class="'.$ckid.'th'.($c+1).'" scope="col"';
  193.         }
  194.         $thead->addRow($columns, $attr, 'th');
  195.  
  196.         $t2 = new HTML_Table(array('class' => 'tabletwo'));
  197.         // add body contents of inner table
  198.         if (is_array($datasrc)) {
  199.             foreach ($datasrc as $id => $data) {
  200.                 $ck[$id] = &HTML_QuickForm::createElement('checkbox', $ckid."[$id]", null, null, array('id' => $ckid));
  201.                 $contents = array();
  202.                 foreach($columns as $k => $col) {
  203.                     if ($k == 0) {
  204.                         $contents[] = $col;
  205.                     } else {
  206.                         $contents[] = $data[strtolower($columns[$k])];
  207.                     }
  208.                 }
  209.                 $r = $t2->addRow($contents);
  210.                 $t2->setCellContents($r, 0, $ck[$id]);
  211.             }
  212.  
  213.             $cc2 = $t2->getColCount(0);
  214.             $attr = array();
  215.             for($c = 0; $c < $cc2; $c++) {
  216.                 if ($c == $rowscope) {
  217.                     $attr[$c] = 'class="'.$ckid.'td'.($c+1).'" scope="row"';
  218.                 } else {
  219.                     $attr[$c] = 'class="'.$ckid.'td'.($c+1).'"';
  220.                 }
  221.             }
  222.             $t2->updateRowAttributes(0, $attr);
  223.  
  224.             // alternate row colors
  225.             $altRow1 = array('class' => 'odd');
  226.             $altRow2 = array('class' => 'even');
  227.             $t2->altRowAttributes(0, $altRow1, $altRow2, true);
  228.  
  229.         }
  230.  
  231.         // add footer cells of outer table
  232.         $rowCount = $t2->getRowCount();
  233.         if ($rowCount == 0) {
  234.             $r = $t2->addRow(array('&nbsp;'), array('colspan' => $cc1, 'style' => 'background-color:transparent;'));
  235.         } else {
  236.             $ftr = array("$rowCount ". (($rowCount > 1) ? $ckid: substr($ckid, 0, -1)));
  237.             $r = $tfoot->addRow($ftr);
  238.             $tfoot->setCellAttributes($r, 0, array('colspan' => $cc1, 'class' => 'total'));
  239.  
  240.             $ftr  = '<img src="'. $_SERVER['PHP_SELF'] .'?arrow_ltr" border="0" alt="^--" />';
  241.             $ftr .= " [<a href=\"javascript:doSelection('$ckid', 1);\">Select All</a>]";
  242.             $ftr .= " [<a href=\"javascript:doSelection('$ckid', 0);\">Select None</a>]";
  243.             $ftr .= " [<a href=\"javascript:doSelection('$ckid', 2);\">Toggle Selection</a>]";
  244.  
  245.             $ftr = array($ftr);
  246.         }
  247.         $r = $tfoot->addRow($ftr);
  248.         $tfoot->setCellAttributes($r, 0, array('colspan' => $cc1));
  249.  
  250.         // add body cells of inner table
  251.         $innerd = "<div class=\"autoscroll\">\n".$t2->toHtml()."\n</div>";
  252.         $tbody->addRow(array($innerd), array('colspan' => $cc1));
  253.     }
  254. }
  255. ?>