Overview

Packages

  • Hermes
  • Horde
    • Data
  • Kronolith
  • None

Classes

  • Hermes_Application
  • Hermes_Exception
  • Hermes_Form_JobType_Add
  • Hermes_Slice
  • Hermes_Table
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * The Horde_UI_Table:: class displays and allows manipulation of tabular
  4:  * data.
  5:  *
  6:  * Copyright 2001 Robert E. Coyle <robertecoyle@hotmail.com>
  7:  *
  8:  * See the enclosed file LICENSE for license information (BSD). If you
  9:  * did not receive this file, see http://www.horde.org/licenses/bsdl.php.
 10:  */
 11: class Hermes_Table extends Horde_Core_Ui_Widget
 12: {
 13:     /**
 14:      * Data loaded from the getTableMetaData API.
 15:      *
 16:      * @access private
 17:      * @var array
 18:      */
 19:     private $_metaData = null;
 20: 
 21:     /**
 22:      * @var array
 23:      */
 24:     protected $_formVars = array();
 25: 
 26:     /**
 27:      *
 28:      * @return array
 29:      * @throws Hermes_Exception
 30:      */
 31:     public function getMetaData()
 32:     {
 33:         if (is_null($this->_metaData)) {
 34:             list($app, $name) = explode('/', $this->_config['name']);
 35:             $args = array($name, $this->_config['params']);
 36:             $this->_metaData = $GLOBALS['registry']->callByPackage(
 37:                 $app, 'getTableMetaData', $args);
 38: 
 39: 
 40:             // We need to make vars for the columns.
 41:             foreach ($this->_metaData['sections'] as $secname => $section) {
 42:                 foreach ($section['columns'] as $col) {
 43:                     $title = isset($col['title']) ? $col['title'] : '';
 44:                     $typename = isset($col['type']) ? $col['type'] : 'text';
 45:                     $params = isset($col['params']) ? $col['params'] : array();
 46: 
 47:                     // Column types which begin with % are pseudo-types handled
 48:                     // directly.
 49:                     if (substr($typename, 0, 1) != '%') {
 50:                         // This type needs to be assigned by reference!
 51:                         $type = &Horde_Form::getType($typename, $params);
 52:                         $var = new Horde_Form_Variable($title, $col['name'],
 53:                                                        $type, false, true, '');
 54:                         $this->_formVars[$secname][$col['name']] = $var;
 55:                     }
 56:                 }
 57:             }
 58:         }
 59: 
 60:         return $this->_metaData;
 61:     }
 62: 
 63:     protected function _getData($range = null)
 64:     {
 65:         if (is_null($range)) {
 66:             $range = array();
 67:             foreach (array_keys($this->_metaData['sections']) as $secname) {
 68:                 $range[$secname] = array(
 69:                     0,
 70:                     $this->_metaData['sections'][$secname]['rows']);
 71:             }
 72:         }
 73: 
 74:         list($app, $name) = explode('/', $this->_config['name']);
 75:         $args = array($name, $this->_config['params'], $range);
 76:         return $GLOBALS['registry']->callByPackage($app, 'getTableData', $args);
 77:     }
 78: 
 79:     /**
 80:      * Count the number of columns in this table.
 81:      *
 82:      * Returns the largest column count of any section, taking into account
 83:      * 'colspan' attributes.
 84:      *
 85:      * @return integer number of columns
 86:      * @throws Hermes_Exception
 87:      */
 88:     public function getColumnCount()
 89:     {
 90:         $res = $this->getMetaData();
 91:         $colcount = 0;
 92:         foreach ($this->_metaData['sections'] as $section) {
 93:             $sec_colcount = 0;
 94:             foreach ($section['columns'] as $col) {
 95:                 if (isset($col['colspan'])) {
 96:                     $sec_colcount += $col['colspan'];
 97:                 } else {
 98:                     $sec_colcount++;
 99:                 }
100:             }
101:             if ($sec_colcount > $colcount) {
102:                 $colcount = $sec_colcount;
103:             }
104:         }
105: 
106:         return $colcount;
107:     }
108: 
109:     /**
110:      * Render the table.
111:      */
112:     public function render($data = null)
113:     {
114:         global $notification;
115: 
116:         try {
117:             $result = $this->getMetaData();
118:         } catch (Hermes_Exception $e) {
119:             $notification->push($e->getMessage(), 'horde.error');
120:             return false;
121:         }
122: 
123:         $varRenderer = new Horde_Core_Ui_VarRenderer_Html();
124: 
125:         $html = '<h1 class="header">';
126: 
127:         // Table title.
128:         if (isset($this->_config['title'])) {
129:             $html .= $this->_config['title'];
130:         } else {
131:             $html .= _("Table");
132:         }
133: 
134:         // Hook for icons and things
135:         if (isset($this->_config['title_extra'])) {
136:             $html .= $this->_config['title_extra'];
137:         }
138: 
139:         $html .= '</h1>';
140: 
141:         /*
142:         //
143:         // Export icon.  We store the parameters in the session so that smart
144:         // users can't hack it (in Hermes, you could make it show other
145:         // people's time, for example).
146:         //
147:         $id = $this->_config['name'] . ':' . $this->_name;
148:         $_SESSION['horde']['tables'][$id] = $this->_config;
149:         $exportlink = Horde::url($GLOBALS['registry']->get('webroot', 'horde') .
150:                                  '/services/table/export.php');
151:         $exportlink = Horde_Util::addParameter($exportlink, array('id' => $id));
152: 
153:         $html .= ' &nbsp;' . Horde::link($exportlink, _("Export Data")) .
154:                  Horde::img('data.png', _("Export Data"), 'hspace="2"') .
155:                  '</a>';
156:         */
157: 
158:         // Column titles.
159:         $html .= '<table class="time striped" id="hermes_time" cellspacing="0"><thead><tr class="item">';
160:         foreach ($this->_metaData['sections']['data']['columns'] as $col) {
161:             $html .= '<th' . (isset($col['colspan']) ?
162:                               (' colspan="' . $col['colspan'] . '"') :
163:                               '') . '>' . $col['title'] . '</th>';
164:         }
165:         $html .= '</tr></thead>';
166: 
167:         // Display data.
168:         try {
169:             $data = $this->_getData();
170:         } catch (Hermes_Exception $e) {
171:             $notification->push($e, 'horde.error');
172:             $data = array();
173:         }
174: 
175:         foreach ($this->_metaData['sections'] as $secname => $section) {
176:             if (empty($data[$secname])) {
177:                 continue;
178:             }
179: 
180:             /* Open the table section, either a tbody or the tfoot. */
181:             $html .= ($secname == 'footer') ? '<tfoot>' : '<tbody>';
182: 
183:             /* This Horde_Variables object is populated for each table row
184:              * so that we can use the Horde_Core_Ui_VarRenderer. */
185:             $vars = new Horde_Variables();
186:             $form = null;
187:             foreach ($data[$secname] as $row) {
188:                 $html .= '<tr>';
189:                 foreach ($row as $key => $value) {
190:                     $vars->set($key, $value);
191:                 }
192:                 foreach ($section['columns'] as $col) {
193:                     $value = null;
194:                     if (isset($row[$col['name']])) {
195:                         $value = $row[$col['name']];
196:                     }
197:                     $align = '';
198:                     if (isset($col['align'])) {
199:                         $align = ' align="' . htmlspecialchars($col['align']) . '"';
200:                     }
201:                     $colspan = '';
202:                     if (isset($col['colspan'])) {
203:                         $colspan = ' colspan="' .
204:                                    htmlspecialchars($col['colspan']) . '"';
205:                     }
206:                     $html .= "<td$align$colspan";
207:                     if (!empty($col['nobr'])) {
208:                         $html .= ' class="nowrap"';
209:                     }
210:                     $html .= '>';
211:                     // XXX: Should probably be done at the <tr> with a class.
212:                     if (!empty($row['strong'])) {
213:                         $html .= '<strong>';
214:                     }
215:                     if (isset($col['type']) && substr($col['type'], 0, 1) == '%') {
216:                         switch ($col['type']) {
217:                         case '%html':
218:                             if (!empty($row[$col['name']])) {
219:                                 $html .= $row[$col['name']];
220:                             }
221:                             break;
222:                         }
223:                     } else {
224:                         $html .= $varRenderer->render($form, $this->_formVars[$secname][$col['name']], $vars);
225:                     }
226:                     if (!empty($row['strong'])) {
227:                         $html .= '</strong>';
228:                     }
229:                     $html .= '</td>';
230:                 }
231:                 $html .= '</tr>';
232:             }
233: 
234:             /* Close the table section. */
235:             $html .= ($secname == 'footer') ? '</tfoot>' : '</tbody>';
236:         }
237: 
238:         Horde::addScriptFile('stripe.js', 'horde', true);
239:         return $html . '</table>';
240:     }
241: 
242: }
243: 
API documentation generated by ApiGen