Display expanded customer name
[infodrom.org/service.infodrom.org] / src / InfoCon / stempel / status.wml
1 #include <infocon.style>
2 #include "common.inc"
3
4 <?
5   session_name('STEMPEL');
6   session_start();
7
8   function update_db()
9   {
10     global $dbh;
11
12     $count = 0;
13     for ($idx=0; $idx < $_POST["fields"]; $idx++) {
14       if (isset($_POST["oid_".$idx])) {
15         $query = sprintf('UPDATE stempel SET status=%d WHERE oid=%d',
16                  $_POST["status"], $_POST["oid_".$idx]);
17         $count++;
18         $sth = pg_exec ($dbh, $query);
19       }
20     }
21     return sprintf("<br>%d records updated.", $count);
22   }
23
24   $stati = false;
25   function display_tables()
26   {
27     global $month;
28     global $cust;
29     global $status;
30     global $dbh;
31     global $stati;
32
33     $name = load_customers();
34
35     $table_head = '<h3 class="bar">Kunde: %s</h3><script type="text/javascript">plant(%d);</script>
36   <form action="status.php" method="POST">
37   <table class="smallfont border" width="100%%" cellpadding=0 cellspacing=0>
38   <tr class="head" onclick="edit_task_finish(this)">
39     <th width=15%%>Datum</th>
40     <th width=5%%>Dauer</th>
41     <th width=5%%>St.</th>
42     <th width=75%% align=left>Verwendungszweck</th>
43   </tr>';
44
45     if (!is_array($stati))
46       $stati = find_status();
47
48     $table_foot = '</table><div align="center"><p>';
49
50     foreach ($stati as $k=>$v)
51       $table_foot .= sprintf('<input type="radio" name="status" value="%s"%s>%s&nbsp;', $k, 0==$k?' checked':'', $v);
52       $table_foot .= '<input type="submit" class="button" value="Aktualisieren"></p></div>'.
53                   '<input type="hidden" name="fields" value="%d">'.
54                   '</form>';
55
56     $table_row = '<tr class="t%d"><td>%s</td><td align="center">%s</td><td align="center">%s</td><td onclick="edit_task(this)">%s</td></tr>';
57     $table_sum = '<tr class="t%d"><td>&nbsp;</td><td align="center">%s</td><td>&nbsp;</td><td>Summe</td></tr>';
58
59     $query = "SELECT stempel.oid,start,customer,time,task,kurz FROM stempel JOIN stempel_status ON (stempel.status = stempel_status.id) WHERE time IS NOT NULL ";
60     if (isset($month) && strlen($month))
61       $query .= "AND cast(start AS TEXT) LIKE '".$month."-%' ";
62     if (isset($cust) && strlen($cust))
63       $query .= "AND customer ='".$cust."' ";
64
65     if (isset($status) && strlen($status)) {
66       if ($status != 'all')
67         $query .= "AND status = $status ";
68     } else
69       $query .= "AND status = 0 ";
70     $query .= "ORDER BY customer,start";
71
72     $sth = pg_exec ($dbh, $query);
73
74     $form = 0;
75     $customer = '';
76     while ($row = pg_fetch_array ($sth)) {
77
78       if ($customer != $row['customer']) {
79         if (strlen($customer)) {
80           printf($table_sum, $color, min2hour($sum));
81           printf($table_foot, $fieldnr);
82           $form++;
83         }
84         $cname = $name[$row['customer']];
85         if (!strlen($cname)) $cname = ucfirst($row['customer']);
86         printf($table_head, $cname, $form);
87         $customer = $row['customer'];
88         $fieldnr = 0;
89         $sum = 0;
90         $color = 0;
91       }
92
93       $sum += $row['time'];
94       $d = explode(' ', $row['start']);
95
96       $check = sprintf('<input type="checkbox" class="checkbox" name="oid_%d" value="%d" onclick="add_sum(%d,this)">',
97                        $fieldnr++,
98                        $row['oid'],
99                        $form);
100       printf($table_row, $color, $check.$d[0], min2hour($row['time']), $row['kurz'], htmlspecialchars($row['task']));
101       $color = !$color;
102     }
103     if (pg_num_rows($sth) > 0) {
104       printf($table_sum, $color, min2hour($sum));
105       printf($table_foot, $fieldnr);
106     }
107   }
108
109   function find_customers()
110   {
111     global $dbh;
112
113     $a = array();
114     $query = "SELECT DISTINCT customer FROM stempel ORDER BY customer";
115     $sth = pg_exec ($dbh, $query);
116
117     while ($row = pg_fetch_array ($sth))
118       $a[] = $row[0];
119
120     return $a;
121   }
122
123   $dbh = pg_pconnect ("<dbhost>", "<dbport>", "<dbname>")
124          or die("Unable to connect to SQL server");
125
126   pg_exec ($dbh, "SET DateStyle = 'ISO'") or die("Datenbank-Abfrage!");
127
128   if (isset($_POST["status"]) && isset($_POST["fields"])) {
129     $msg = update_db();
130     $_SESSION['redirect'] = true;
131     header('Location: status.php');
132     exit;
133   } elseif ($_SESSION['redirect']) {
134     $month = $_SESSION['save']["month"];
135     $cust = $_SESSION['save']["customer"];
136     $status = $_SESSION['save']["status"];
137     $_SESSION['redirect'] = false;
138   } else {
139     if (isset($_GET['month'])) {
140       $month = $_GET['month'];
141       $_SESSION['save']['month'] = $month;
142       unset($_SESSION['save']['customer']);
143       unset($_SESSION['save']['status']);
144     } else
145       if (isset($_POST["filter"])) {
146         $month = $_POST["month"];
147         $cust = $_POST["customer"];
148         $status = $_POST["status"];
149         $_SESSION['save']['month'] = $month;
150         $_SESSION['save']['customer'] = $cust;
151         $_SESSION['save']['status'] = $status;
152       } else {
153         $month = date('Y-m');
154         $_SESSION['save']['month'] = $month;
155         unset($_SESSION['save']['customer']);
156         unset($_SESSION['save']['status']);
157       }
158   }
159 ?>
160
161 <script src="<root_prefix>basics.js"></script>
162 <script type="text/javascript">
163 var color_checked = '#c4ffc3';
164 function format_int(value, width)
165 {
166   var s = value.toString();
167
168   while (s.length < width)
169     s = '0' + s;
170
171   return s;
172 }
173
174 function timestr(duration)
175 {
176     return format_int(Math.floor(duration / 60), 1) + ':'
177         + format_int(duration - (Math.floor(duration / 60) * 60), 2);
178 }
179
180 function strtime(time)
181 {
182     var a = time.split(':');
183     return parseInt(a[0],10) * 60 + parseInt(a[1],10);
184 }
185
186 function check(id, value)
187 {
188   var form = document.forms[id];
189   var sum = document.getElementById('sum_'+id.toString()); 
190   var newval = 0;
191
192   for (i=0; i< form.elements.length; i++) {
193       if (form.elements[i].name.substr(0,4) == 'oid_') {
194           form.elements[i].checked = value;
195           if (value) {
196               var ta = form.elements[i].parentNode.nextSibling.innerHTML.split(':');
197               newval = newval + parseInt(ta[0],10) * 60 + parseInt(ta[1],10);
198               form.elements[i].parentNode.parentNode.style.backgroundColor = color_checked;
199           } else
200               form.elements[i].parentNode.parentNode.style.backgroundColor = '';
201       }
202   }
203
204   sum.innerHTML = timestr(newval);
205 }
206
207 function plant(form)
208 {
209   document.write('<div class="jscode">');
210   document.write('<a href="javascript:check('+form+',true)">Check all</a>');
211   document.write("&nbsp;/&nbsp;");
212   document.write('<a href="javascript:check('+form+',false)">Uncheck all</a>');
213   document.write("&nbsp;");
214   document.write('<span id="sum_'+form+'" class="sum">0:00</span>');
215   document.write("&nbsp;");
216   document.write('</div>');
217 }
218
219 function add_sum(form, checkbox)
220 {
221     var sum = document.getElementById('sum_'+form.toString()); 
222     var time = strtime(checkbox.parentNode.nextSibling.innerHTML);
223     var newval;
224
225     if (checkbox.checked) {
226         newval = strtime(sum.innerHTML) + time;
227         checkbox.parentNode.parentNode.style.backgroundColor = color_checked;
228     } else {
229         newval = strtime(sum.innerHTML) - time;
230         checkbox.parentNode.parentNode.style.backgroundColor = '';
231     }
232     sum.innerHTML = timestr(newval);
233 }
234
235 var task_parent = null;
236 var task_title = '';
237 var task_oid = 0;
238 function edit_task(obj)
239 {
240     if (task_oid) {
241         if (task_oid != obj.parentNode.children[0].children[0].value)
242             edit_task_save();
243     }
244
245     if (!task_oid) {
246         task_parent = obj;
247         task_title = obj.innerHTML;
248         task_oid = obj.parentNode.children[0].children[0].value;
249
250         var input = document.createElement('input');
251         input.value = obj.innerHTML
252         input.style.fontSize = '100%';
253         input.style.width = '100%';
254         input.style.background = 'yellow';
255
256         obj.innerHTML = '';
257         obj.appendChild(input);
258         input.focus();
259     }
260 }
261
262 function edit_task_save()
263 {
264     if (!task_parent) return;
265
266     if (task_parent.children[0].value != task_title)
267         ajax_request('save','oid='+task_oid+'&task='+encodeURIComponent(task_parent.children[0].value));
268
269     task_parent.innerHTML = task_parent.children[0].value;
270     task_parent = null;
271     task_title = '';
272     task_oid = 0;
273 }
274
275 function edit_task_finish(obj)
276 {
277     if (task_oid)
278         edit_task_save();
279 }
280
281 </script>
282
283 <style type="text/css">
284 div.jscode {
285   text-align: right;
286   margin-top: -13px;
287   font-size: 12px;
288 }
289 span.sum {
290   display: inline;
291   border: 1px solid #9b9b9b;
292   background: #d9e2ea;
293   width: 20px;
294   margin-left: 3px;
295 }
296 </style>
297
298 <page func=InfoCon title="Stempeluhr">
299
300 <?
301   if (!empty($msg))
302     echo $msg;
303   else {
304     display_tables();
305 ?>
306
307 <h3 class=bar>Display</h3>
308
309 <form action=status.php method=POST>
310 <select name=month>
311 <?
312   if (!is_array($months))
313     $months = find_months();
314
315   printf('<option value=""%s>alle</option>', !strlen($month)?' selected':'');
316   foreach ($months as $m) {
317     printf('<option value="%s"%s>%s</option>', $m, $month==$m?' selected':'', $m);
318   }
319 ?>
320 </select>
321
322 <select name=customer>
323 <?
324   if (!is_array($customers))
325     $customers = find_customers();
326
327   printf('<option value=""%s>alle</option>', !strlen($cust)?' selected':'');
328   foreach ($customers as $c) {
329     printf('<option value="%s"%s>%s</option>', $c, $cust==$c?' selected':'', $c);
330   }
331 ?>
332 </select>
333
334 <?
335   if (!is_array($stati))
336     $stati = find_status();
337
338   printf('<input type="radio" name="status" value="all"%s>alle&nbsp;', $status=='all'?' checked':'');
339   foreach ($stati as $k=>$v)
340     printf('<input type="radio" name="status" value="%s"%s>%s&nbsp;', $k, $status==$k?' checked':'', $v);
341 ?>
342
343 <input type="hidden" name="filter" value="form">
344 <input class="button" type="submit" value="Display">
345 </form>
346
347 <? } ?>
348 </page>
349
350 # Local variables:
351 # mode: php
352 # end: