1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11: class Hermes_Table extends Horde_Core_Ui_Widget
12: {
13: 14: 15: 16: 17: 18:
19: private $_metaData = null;
20:
21: 22: 23:
24: protected $_formVars = array();
25:
26: 27: 28: 29: 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:
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:
48:
49: if (substr($typename, 0, 1) != '%') {
50:
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: 81: 82: 83: 84: 85: 86: 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: 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:
128: if (isset($this->_config['title'])) {
129: $html .= $this->_config['title'];
130: } else {
131: $html .= _("Table");
132: }
133:
134:
135: if (isset($this->_config['title_extra'])) {
136: $html .= $this->_config['title_extra'];
137: }
138:
139: $html .= '</h1>';
140:
141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156:
157:
158:
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:
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:
181: $html .= ($secname == 'footer') ? '<tfoot>' : '<tbody>';
182:
183: 184:
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:
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:
235: $html .= ($secname == 'footer') ? '</tfoot>' : '</tbody>';
236: }
237:
238: Horde::addScriptFile('stripe.js', 'horde', true);
239: return $html . '</table>';
240: }
241:
242: }
243: