[#1242] tableset, a table-like set variable
Summary tableset, a table-like set variable
Queue Horde Framework Packages
Type Enhancement
State Resolved
Priority 1. Low
Owners Horde Developers
Requester allen (dot) zhao (at) camilion (dot) com
Created 01/24/2005 (1443 days ago)
Due
Updated 05/29/2005 (1318 days ago)
Assigned
Resolved 05/29/2005 (1318 days ago)
Attachments sample.gif Download
sample2.gif Download
Milestone
Patch No

History
05/29/2005 Chuck Hagenbuch Comment #4
State ⇒ Resolved
Reply to this comment
Added to HEAD as seperate files (not in the main Horde_Form file, but 
in the package).
01/27/2005 Chuck Hagenbuch State ⇒ Accepted
Assigned to Horde DevelopersHorde Developers
 
01/24/2005 allen (dot) zhao (at) camilion (dot) com Comment #3
New Attachment: sample2.gif Download
Reply to this comment
screen shot for display
01/24/2005 allen (dot) zhao (at) camilion (dot) com Comment #2
New Attachment: sample.gif Download
Reply to this comment
screen shot for edit
01/24/2005 allen (dot) zhao (at) camilion (dot) com Comment #1
State ⇒ New
Priority ⇒ 1. Low
Type ⇒ Enhancement
Summary ⇒ tableset, a table-like set variable
Queue ⇒ Horde Framework Packages
Reply to this comment
I am trying to use Horde_UI_Table, but I was so frustrated. Then I 
found set is a good choice for me. Only with some other problems ....

Then the tableset coming:

In Horde/Form.php
add:
class Horde_Form_Type_tableset extends Horde_Form_Type {

     var $_values;
     var $_header;

     function init(&$values, &$header)
     {
         $this->_values = $values;
         $this->_header = $header;
     }

     function isValid(&$var, &$vars, $value, &$message)
     {
         if (count($this->_values) == 0 || count($value) == 0) {
             return true;
         }
         foreach ($value as $item) {
             if (!isset($this->_values[$item])) {
                 $error = true;
                 break;
             }
         }
         if (!isset($error)) {
             return true;
         }

         $message = _("Invalid data submitted.");
         return false;
     }

     function getHeader() {
         return $this->_header;
     }

     function getValues()
     {
         return $this->_values;
     }
     /**
      * Return info about field type.
      */
     function about()
     {
         $about = array();
         $about['name'] = _("Table Set");
         $about['params'] = array(
             'values' => array('label' => _("Values"),
                               'type'  => 'stringlist'),
             'header' => array('label' => _("Values"),
                               'type'  => 'stringlist')
                               );
         return $about;
     }
}

In Horde/UI/VarRenderer/html.php add:
     function _renderVarInput_tableset(&$form, &$var, &$vars)
     {
         $header = $var->type->getHeader();
         $name   = $var->getVarName();
         $values = $var->getValues();
         $checkedValues = $var->getValue($vars);
         $actions = $this->_getActionScripts($form, $var);

         $html = '<table width="100%" border="0" cellspacing="0" 
cellpadding="0"><tr><td class="item">';
         $html .= '<table border="0" cellpadding="2" 
cellspacing="1"><tr class="selected">';
         $html .= '<th  class="widget" align="right" width="1%%">&nbsp;</th>';
         foreach ($header as $col_title) {
             $html .= sprintf('<th align="left" 
class="widget">%s</th>', $col_title);
         }
         $html .= '</tr>';

         if (!is_array($checkedValues)) {
             $checkedValues = array();
         }
         $i = 0;
         foreach ($values as $value => $displays) {
             $class   = 'item' . ($i+1) % 2;
             $checked = (in_array($value, $checkedValues)) ? ' 
checked="checked"' : '';
             $html   .= sprintf('<tr class="%s">', $class);
             $html   .= sprintf('<td align="center"><input id="%s%s" 
type="checkbox" name="%s[]" value="%s"%s%s /></td>',
                                $name,
                                $i,
                                $name,
                                $value,
                                $checked,
                                $actions);
             foreach ($displays as $col)
                 $html .= sprintf('<td align="left">&nbsp;%s</td>', $col);
             $html   .= '</tr>';
             $i++;
         }

         return $html . '</table></td></tr></table>';
     }

     function _renderVarDisplay_tableset(&$form, &$var, &$vars)
     {
         $header = $var->type->getHeader();
         $name   = $var->getVarName();
         $values = $var->getValues();
         $checkedValues = $var->getValue($vars);
         $actions = $this->_getActionScripts($form, $var);

         $html = '<table width="100%" border="0" cellspacing="0" 
cellpadding="0"><tr><td>';
         $html .= '<table border="0" cellpadding="2" 
cellspacing="1"><tr class="selected">';
         $html .= '<th  class="widget" align="right" width="1%%">&nbsp;</th>';
         foreach ($header as $col_title) {
             $html .= sprintf('<th align="left" 
class="widget">%s</th>', $col_title);
         }
         $html .= '</tr>';

         if (!is_array($checkedValues)) {
             $checkedValues = array();
         }
         $i = 0;
         foreach ($values as $value => $displays) {
             $class   = 'item' . ($i+1) % 2;
             $checked = (in_array($value, $checkedValues)) ? '[ 
<b><font color=green>V</font></b> ]' : '[ <b><font 
color=red>X</font></b> ]';
             $html   .= sprintf('<tr class="%s">', $class);
             $html   .= sprintf('<td align="center">%s</td>', $checked);
             foreach ($displays as $col)
                 $html .= sprintf('<td align="left">&nbsp;%s</td>', $col);
             $html   .= '</tr>';
             $i++;
         }

         return $html . '</table></td></tr></table>';
     }

As you saw, just a simple change of set