Encode html characters
[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">
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>%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_'.$fieldnr++.'" value="'.$row['oid'].'" onclick="add_sum(%d,this)">',$form);
109       printf($table_row, $color, $check.$d[0], min2hour($row['time']), $row['kurz'], htmlspecialchars($row['task']));
110       $color = !$color;
111     }
112     if (pg_num_rows($sth) > 0) {
113       printf($table_sum, $color, min2hour($sum));
114       printf($table_foot, $fieldnr);
115     }
116   }
117
118   function find_customers()
119   {
120     global $dbh;
121
122     $a = array();
123     $query = "SELECT DISTINCT customer FROM stempel ORDER BY customer";
124     $sth = pg_exec ($dbh, $query);
125
126     while ($row = pg_fetch_array ($sth))
127       $a[] = $row[0];
128
129     return $a;
130   }
131
132   $dbh = pg_pconnect ("<dbhost>", "<dbport>", "<dbname>")
133          or die("Unable to connect to SQL server");
134
135   pg_exec ($dbh, "SET DateStyle = 'ISO'") or die("Datenbank-Abfrage!");
136
137   if (isset($_POST["status"]) && isset($_POST["fields"])) {
138     $msg = update_db();
139     $_SESSION['redirect'] = true;
140     header('Location: status.php');
141     exit;
142   } elseif ($_SESSION['redirect']) {
143     $month = $_SESSION['save']["month"];
144     $cust = $_SESSION['save']["customer"];
145     $status = $_SESSION['save']["status"];
146     $_SESSION['redirect'] = false;
147   } else {
148     if (isset($_GET['month'])) {
149       $month = $_GET['month'];
150       $_SESSION['save']['month'] = $month;
151       unset($_SESSION['save']['customer']);
152       unset($_SESSION['save']['status']);
153     } else
154       if (isset($_POST["filter"])) {
155         $month = $_POST["month"];
156         $cust = $_POST["customer"];
157         $status = $_POST["status"];
158         $_SESSION['save']['month'] = $month;
159         $_SESSION['save']['customer'] = $cust;
160         $_SESSION['save']['status'] = $status;
161       } else {
162         $month = date('Y-m');
163         $_SESSION['save']['month'] = $month;
164         unset($_SESSION['save']['customer']);
165         unset($_SESSION['save']['status']);
166       }
167   }
168 ?>
169
170 <script type="text/javascript">
171 var color_checked = '#c4ffc3';
172 function format_int(value, width)
173 {
174   var s = value.toString();
175
176   while (s.length < width)
177     s = '0' + s;
178
179   return s;
180 }
181
182 function timestr(duration)
183 {
184     return format_int(Math.floor(duration / 60), 1) + ':'
185         + format_int(duration - (Math.floor(duration / 60) * 60), 2);
186 }
187
188 function strtime(time)
189 {
190     var a = time.split(':');
191     return parseInt(a[0]) * 60 + parseInt(a[1]);
192 }
193
194 function check(id, value)
195 {
196   var form = document.forms[id];
197   var sum = document.getElementById('sum_'+id.toString()); 
198   var newval = 0;
199
200   for (i=0; i< form.elements.length; i++) {
201       if (form.elements[i].name.substr(0,4) == 'oid_') {
202           form.elements[i].checked = value;
203           if (value) {
204               var ta = form.elements[i].parentNode.nextSibling.innerHTML.split(':');
205               newval = newval + parseInt(ta[0]) * 60 + parseInt(ta[1]);
206               form.elements[i].parentNode.parentNode.style.backgroundColor = color_checked;
207           } else
208               form.elements[i].parentNode.parentNode.style.backgroundColor = '';
209       }
210   }
211
212   sum.innerHTML = timestr(newval);
213 }
214
215 function plant(form)
216 {
217   document.write('<div class="jscode">');
218   document.write('<a href="javascript:check('+form+',true)">Check all</a>');
219   document.write("&nbsp;/&nbsp;");
220   document.write('<a href="javascript:check('+form+',false)">Uncheck all</a>');
221   document.write("&nbsp;");
222   document.write('<span id="sum_'+form+'" class="sum">0:00</span>');
223   document.write("&nbsp;");
224   document.write('</div>');
225 }
226
227 function add_sum(form, checkbox)
228 {
229     var sum = document.getElementById('sum_'+form.toString()); 
230     var time = strtime(checkbox.parentNode.nextSibling.innerHTML);
231     var newval;
232
233     if (checkbox.checked) {
234         newval = strtime(sum.innerHTML) + time;
235         checkbox.parentNode.parentNode.style.backgroundColor = color_checked;
236     } else {
237         newval = strtime(sum.innerHTML) - time;
238         checkbox.parentNode.parentNode.style.backgroundColor = '';
239     }
240     sum.innerHTML = timestr(newval);
241 }
242 </script>
243
244 <style type="text/css">
245 div.jscode {
246   text-align: right;
247   margin-top: -13px;
248   font-size: 12px;
249 }
250 span.sum {
251   display: inline;
252   border: 1px solid #9b9b9b;
253   background: #d9e2ea;
254   width: 20px;
255   margin-left: 3px;
256 }
257 </style>
258
259 <page func=InfoCon title="Stempeluhr">
260
261 <?
262   if (!empty($msg))
263     echo $msg;
264   else {
265     display_tables();
266 ?>
267
268 <h3 class=bar>Display</h3>
269
270 <form action=status.php method=POST>
271 <select name=month>
272 <?
273   if (!is_array($months))
274     $months = find_months();
275
276   printf('<option value=""%s>alle</option>', !strlen($month)?' selected':'');
277   foreach ($months as $m) {
278     printf('<option value="%s"%s>%s</option>', $m, $month==$m?' selected':'', $m);
279   }
280 ?>
281 </select>
282
283 <select name=customer>
284 <?
285   if (!is_array($customers))
286     $customers = find_customers();
287
288   printf('<option value=""%s>alle</option>', !strlen($cust)?' selected':'');
289   foreach ($customers as $c) {
290     printf('<option value="%s"%s>%s</option>', $c, $cust==$c?' selected':'', $c);
291   }
292 ?>
293 </select>
294
295 <?
296   if (!is_array($stati))
297     $stati = find_status();
298
299   printf('<input type="radio" name="status" value="all"%s>alle&nbsp;', $status=='all'?' checked':'');
300   foreach ($stati as $k=>$v)
301     printf('<input type="radio" name="status" value="%s"%s>%s&nbsp;', $k, $status==$k?' checked':'', $v);
302 ?>
303
304 <input type="hidden" name="filter" value="form">
305 <input class="button" type="submit" value="Display">
306 </form>
307
308 <? } ?>
309 </page>
310
311 # Local variables:
312 # mode: text
313 # mode: auto-fill
314 # end: