Support tooltips for menu items
[misc/kostenrechnung] / lib / mask.php
1 <?php
2
3 # function mask_fields($list)
4 # {
5 #   $ret = array();
6
7 #   foreach ($list as $field => $data)
8 #     $ret[] = $field;
9
10 #   return $ret;
11 # }
12
13 function build_form($name, $fields)
14 {
15   global $jscode;
16   $ret = array();
17
18   $jscode[] = 'Rico.onLoad( function() {';
19   $jscode[] = "form_init();";
20   $jscode[] = '});';
21
22   $ret[] = '<div class="form">';
23   $ret[] = '<p class="title">Datensatz bearbeiten</p>';
24   $ret[] = '<form id="form_edit">';
25
26   $ret[] = '<input type="hidden" id="edit_id" name="id" value="">';
27   $ret[] = sprintf('<input type="hidden" id="edit_source" name="source" value="%s">', $name);
28
29   foreach ($fields as $id => $info) {
30     if ($info['type'] == 'text' || $info['type'] == 'passwd' ||
31         $info['type'] == 'decimal' || $info['type'] == 'number') {
32       $v = array('id="edit_'.$id.'"',
33                  'name="'.$id.'"');
34       $v[] = 'size="'.(empty($info['size'])?'10':$info['size']).'"';
35       $v[] = 'type="'.($info['type']=='passwd'?'password':'text').'"';
36       $ret[] = sprintf('<label for="edit_%s">%s</label><br>', $id, $info['name']);
37       $ret[] = sprintf('<input %s>', implode(' ', $v));
38     } elseif ($info['type'] == 'date') {
39       $ret[] = sprintf('<label for="edit_%s">%s</label><br>', $id, $info['name']);
40       $ret[] = sprintf('<input id="edit_%s" name="%s" size="8">&nbsp;'.
41                        '<img class="calendar" src="images/icons/calendar.gif" onclick="calendar(\'edit_%s\',event)" />',
42                        $id, $id, $id);
43     } elseif ($info['type'] == 'boolean') {
44       $ret[] = sprintf('<label for="edit_%s">%s</label><br>', $id, $info['name']);
45       $ret[] = sprintf('<input type="checkbox" id="edit_%s" name="%s">', $id, $id);
46     } elseif ($info['type'] == 'select') {
47       $ret[] = sprintf('<label for="edit_%s">%s</label><br>', $id, $info['name']);
48       $ret[] = sprintf('<select id="edit_%s" name="%s">', $id, $id);
49       if (is_array($info['options'])) $options = $info['options'];
50       else $options = query_db($info['options']);
51       foreach ($options as $row)
52         $ret[] = sprintf('<option value="%s">%s</option>', $row['id'], $row['text']);
53       $ret[] = '</select>';
54     }
55     if (array_key_exists('comment',$info))
56       $ret[] = sprintf('<span class="comment">%s</span>', $info['comment']);
57     $ret[] = '<br>';
58   }
59
60   $ret[] = '<span id="form_status">&nbsp;</span>';
61   $ret[] = '<div class="buttons">';
62   $ret[] = '<button id="button_save" onclick="return form_save(this);">Speichern</button>';
63   $ret[] = '<span></span>';
64   $ret[] = '<button id="button_insert" onclick="return form_insert(this);">Hinzufügen</button>';
65   $ret[] = '<span></span>';
66   $ret[] = '<button id="button_delete" onclick="return form_delete(this);">Löschen</button>';
67   $ret[] = '</div>';
68
69   $ret[] = '</form>';
70   $ret[] = '</div>';
71
72   return $ret;
73 }
74
75 function build_grid($name, $mask)
76 {
77   global $jscode;
78   $ret = array();
79
80   $jscode[] = 'Rico.writeDebugMsg = function(msg, resetFlag) {};';
81
82   $opts = array("click: gridDrillDown",
83                 "menuEvent: 'contextmenu'",
84                 "highlightElem: 'menuRow'");
85
86   if (array_key_exists('rows', $mask)) $opts[] = 'visibleRows: ' . $mask['rows'];
87   if (array_key_exists('sort', $mask)) $opts[] = 'sortCol: ' . $mask['sort'];
88   if ($mask['prefetch'] === false) $opts[] = 'prefetch: false';
89   # $opts[] = 'frozenColumns: ' . count($mask['list']);
90   $opts[] = 'saveColumnInfo: {width: true, filter: true, sort: true}';
91
92   $ret[] = sprintf('<table id="grid_%s" class="ricoLiveGrid">', $name);
93   $ret[] = '  <tr>';
94   $specs = array();
95   $fields = array();
96   foreach ($mask['list'] as $field => $data) {
97     $ret[] = sprintf('  <th>%s</th>', $data['name']);
98     $s = array(sprintf("FieldName: 'input_%s'", $field),
99                sprintf("ColName: 'input_%s'", $data['name']));
100     $s = array();
101     if ($data['visible'] === false) $s[] = 'visible: false';
102     if ($data['width'] > 0) $s[] = 'width: ' . $data['width'];
103     if (array_key_exists('type', $data)) $s[] = "type: '" . $data['type'] . "'";
104     if (array_key_exists('specs', $data)) $s[] = $data['specs'];
105     if (array_key_exists('control', $data)) $s[] = 'control: ' . $data['control'];
106     $specs[] = '{' . implode(', ', $s) . '}';
107
108     if (array_key_exists('sql', $data))
109       $fields[] = $data['sql'] . ' AS ' . $field;
110     else
111       $fields[] = $field;
112   }
113
114   grid_sql($name, $mask);
115
116   $ret[] = '  </tr>';
117   $ret[] = '</table>';
118
119   $opts[] = 'columnSpecs: [' . implode(', ', $specs) . ']';
120
121   if (strstr($opts[count($opts)-1], 'filterUI') !== false) $opts[] = 'FilterLocation: -1';
122
123   $jscode[] = 'var grid;';
124   $jscode[] = 'Rico.onLoad( function() {';
125   $jscode[] = sprintf("var %s_opts = {\n  %s\n};", $name, implode(",\n  ", $opts));
126   $jscode[] = sprintf("grid = new Rico.LiveGrid ('grid_%s', new Rico.Buffer.AjaxSQL('ajax/ricoXMLquery.php'), %s_opts);", $name, $name);
127   $jscode[] = "grid.menu = new Rico.GridMenu();";
128   $jscode[] = "grid.edit = new Rico.TableEdit(grid);";
129   $jscode[] = '});';
130   $jscode[] = "Rico.acceptLanguage('de-de,de;q=0.8,en;q=0.5,en-us;q=0.3');";
131   $jscode[] = "Rico.loadModule('Effect','Calendar','LiveGridForms','LiveGridAjax','LiveGridMenu');";
132
133   return $ret;
134 }
135
136 function build_details($name, $details)
137 {
138   $ret = array();
139
140   $ret[] = '<div class="box" id="details">';
141   $ret[] = sprintf('<h3>%s</h3>', $details['title']);
142   if (array_key_exists('subtitle', $details))
143     $ret[] = sprintf('<p class="subtitle">%s</p>', $details['subtitle']);
144   $ret[] = '<ul>';
145   foreach ($details['list'] as $name => $info) {
146     $ret[] = sprintf('<li>%s: <span id="detail_%s"></span></li>', $info['name'], $name);
147   }
148   $ret[] = '</ul>';
149   $ret[] = '</div>';
150
151   return $ret;
152 }
153
154 function build_select($name, $details)
155 {
156   $ret = array();
157
158   $ret[] = '<div class="form">';
159   $ret[] = sprintf('<p class="title">%s</p>', $details['title']);
160   $ret[] = sprintf ('<select%s>', array_key_exists('onchange',$details)?sprintf(' onchange="%s"',$details['onchange']):'');
161   if (is_array($details['options']))
162     $options = $details['options'];
163   else
164     $options = query_db($details['options']);
165
166   if (array_key_exists('default',$details))
167     $ret[] = sprintf('<option value="">%s</option>', $details['default']);
168
169   foreach ($options as $row) {
170     $selected = array_key_exists('selected',$details) && $details['selected'] == $row['id'] ? true : false;
171     $ret[] = sprintf('<option value="%s"%s>%s</option>', $row['id'], $selected?' selected':'',$row['text']);
172   }
173
174   $ret[] = '</select>';
175   $ret[] = '</div>';
176
177   return $ret;
178 }
179
180 function build_mask($name, $mask)
181 {
182   $grid = build_grid($name, $mask);
183   $status = array('<span id="status"></span><br>');
184
185   if (array_key_exists('details', $mask))
186     $details = build_details($name, $mask['details']);
187   else
188     $details = array();
189
190   if (array_key_exists('select', $mask))
191     $select = build_select($name, $mask['select']);
192   else
193     $select = array();
194
195   if (array_key_exists('edit', $mask))
196     $edit = build_form($name, $mask['edit']);
197   else
198     $edit = array();
199
200   $title = $mask['title'];
201   if (array_key_exists('subtitle', $mask))
202     $title .= ' &ndash; ' . $mask['subtitle'];
203
204   $head = array();
205   $head[] = sprintf('<h3>%s</h3>', $title);
206   $head[] = '<div class="right">';
207
208   return array_merge($head,
209                      $grid,
210                      array('</div>','<div class="left">'),
211                      $details,
212                      $select,
213                      $edit,
214                      $status,
215                      array('</div>'));
216 }
217
218 function mask($name)
219 {
220   global $jsfiles;
221   global $mask;
222
223   $ret = array();
224
225   if (load_mask($name) === false)
226     return;
227
228   $jsfiles[] = 'lib/functions.js';
229   $jsfiles[] = 'lib/rico/rico.js';
230
231   if (array_key_exists('table',$mask) && array_key_exists('list',$mask))
232     $ret = build_mask($name, $mask);
233
234   error_log(count($ret));
235   return implode("\n", $ret);
236 }
237
238 ?>