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