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