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