e85e31398c3c13ecfd9a79d89f02082a35a6e44c
[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   $ret = array();
16
17   $ret[] = '<div class="form" style="clear: both;">';
18   $ret[] = '<form id="form_edit">';
19
20   $ret[] = '<input type="hidden" id="edit_id" name="id" value="">';
21   $ret[] = sprintf('<input type="hidden" id="edit_source" name="source" value="%s">', $name);
22
23   foreach ($fields as $id => $info) {
24     if ($info['type'] == 'text' || $info['type'] == 'number') {
25       $v = array('id="edit_'.$id.'"',
26                  'name="'.$id.'"',
27                  'size="'.$info['size'].'"',
28                  'type="text"');
29       $ret[] = sprintf('<label for="edit_%s">%s</label><br>', $id, $info['name']);
30       $ret[] = sprintf('<input %s><br>', implode(' ', $v));
31     } elseif ($info['type'] == 'boolean') {
32       $ret[] = sprintf('<label for="edit_%s">%s</label><br>', $id, $info['name']);
33       $ret[] = sprintf('<input type="checkbox" id="edit_%s" name="%s"><br>', $id, $id);
34     } elseif ($info['type'] == 'select') {
35       $ret[] = sprintf('<label for="edit_%s">%s</label><br>', $id, $info['name']);
36       $ret[] = sprintf('<select id="edit_%s" name="%s">', $id, $id);
37       if (is_array($info['options'])) $options = $info['options'];
38       else $options = query_db($info['options']);
39       foreach ($options as $row)
40         $ret[] = sprintf('<option value="%s">%s</option>', $row['id'], $row['text']);
41       $ret[] = '</select><br>';
42     } elseif ($info['type'] == 'date') {
43       error_log('type = date');
44     }
45   }
46
47   $ret[] = '<div align="center"><button onclick="return form_save(this);">Speichern</button></div>';
48
49   $ret[] = '</form>';
50   $ret[] = '</div>';
51
52   return $ret;
53 }
54
55 function build_grid($name, $mask)
56 {
57   global $jscode;
58   $ret = array();
59
60   $jscode[] = 'Rico.writeDebugMsg = function(msg, resetFlag) {};';
61
62   $opts = array("click: gridDrillDown",
63                 "menuEvent: 'contextmenu'",
64                 "highlightElem: 'menuRow'");
65
66   if (array_key_exists('rows', $mask)) $opts[] = 'visibleRows: ' . $mask['rows'];
67   if (array_key_exists('sort', $mask)) $opts[] = 'sortCol: ' . $mask['sort'];
68   if ($mask['prefetch'] === false) $opts[] = 'prefetch: false';
69   # $opts[] = 'frozenColumns: ' . count($mask['list']);
70   $opts[] = 'saveColumnInfo: {width: true, filter: true, sort: true}';
71
72   $ret[] = sprintf('<table id="grid_%s" class="ricoLiveGrid">', $name);
73   $ret[] = '  <tr>';
74   $specs = array();
75   $fields = array();
76   foreach ($mask['list'] as $field => $data) {
77     $ret[] = sprintf('  <th>%s</th>', $data['name']);
78     $s = array(sprintf("FieldName: 'input_%s'", $field),
79                sprintf("ColName: 'input_%s'", $data['name']));
80     $s = array();
81     if ($data['visible'] === false) $s[] = 'visible: false';
82     if ($data['width'] > 0) $s[] = 'width: ' . $data['width'];
83     if (array_key_exists('type', $data)) $s[] = "type: '" . $data['type'] . "'";
84     if (array_key_exists('specs', $data)) $s[] = $data['specs'];
85     $specs[] = '{' . implode(', ', $s) . '}';
86
87     if (array_key_exists('sql', $data))
88       $fields[] = $data['sql'] . ' AS ' . $field;
89     else
90       $fields[] = $field;
91   }
92   $_SESSION['grid_' . $name] = sprintf("SELECT %s FROM %s",
93                                        implode(',', $fields), $mask['table']);
94   if (array_key_exists('join', $mask)) $_SESSION['grid_' . $name] .= ' JOIN ' . join(' JOIN ', $mask['join']);
95   if (array_key_exists('where', $mask)) $_SESSION['grid_' . $name] .= ' WHERE ' . $mask['where'];
96
97   $ret[] = '  </tr>';
98   $ret[] = '</table>';
99
100   $opts[] = 'columnSpecs: [' . implode(', ', $specs) . ']';
101
102   if (strstr($opts[count($opts)-1], 'filterUI') !== false) $opts[] = 'FilterLocation: -1';
103
104   $jscode[] = 'var grid;';
105   $jscode[] = 'Rico.onLoad( function() {';
106   $jscode[] = sprintf("var %s_opts = {\n  %s\n};", $name, implode(",\n  ", $opts));
107   $jscode[] = sprintf("grid = new Rico.LiveGrid ('grid_%s', new Rico.Buffer.AjaxSQL('ajax/ricoXMLquery.php'), %s_opts);", $name, $name);
108   $jscode[] = "grid.menu = new Rico.GridMenu();";
109   $jscode[] = "grid.edit = new Rico.TableEdit(grid);";
110   $jscode[] = '});';
111   $jscode[] = "Rico.acceptLanguage('de-de,de;q=0.8,en;q=0.5,en-us;q=0.3');";
112   $jscode[] = "Rico.loadModule('Effect','Calendar','LiveGridForms','LiveGridAjax','LiveGridMenu');";
113
114   return $ret;
115 }
116
117 function build_details($name, $details)
118 {
119   $ret = array();
120
121
122   $ret[] = '<div class="box" id="details">';
123   $ret[] = sprintf('<h3>%s</h3>', $details['title']);
124   if (array_key_exists('subtitle', $details))
125     $ret[] = sprintf('<p class="subtitle">%s</p>', $details['subtitle']);
126   $ret[] = '<ul>';
127   foreach ($details['list'] as $name => $info) {
128     $ret[] = sprintf('<li>%s: <span id="detail_%s"></span></li>', $info['name'], $name);
129   }
130   $ret[] = '</ul>';
131   $ret[] = '</div>';
132
133   return $ret;
134 }
135
136 function build_mask($name, $mask)
137 {
138   $grid = build_grid($name, $mask);
139   $status = array('<span id="status"></span><br>');
140
141   if (array_key_exists('details', $mask))
142     $details = build_details($name, $mask['details']);
143   else
144     $details = array();
145
146   $title = $mask['title'];
147   if (array_key_exists('subtitle', $mask))
148     $title .= ' &ndash; ' . $mask['subtitle'];
149
150   $head = array();
151   $head[] = sprintf('<h3>%s</h3>', $title);
152   $head[] = '<div class="right">';
153
154   return array_merge($head,
155                      $grid,
156                      array('</div>','<div class="left">'),
157                      $status,
158                      $details,
159                      array('</div>'));
160 }
161
162 function mask($name)
163 {
164   global $jsfiles;
165   global $mask;
166
167   $ret = array();
168
169   if (load_mask($name) === false)
170     return;
171
172   $jsfiles[] = 'lib/functions.js';
173   $jsfiles[] = 'lib/rico/rico.js';
174
175   if (array_key_exists('table',$mask) && array_key_exists('list',$mask))
176     $ret = build_mask($name, $mask);
177
178   if (array_key_exists('edit', $mask))
179     $ret = array_merge($ret, build_form($name, $mask['edit']));
180
181   return implode("\n", $ret);
182 }
183
184 ?>