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