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