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