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