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