Improve sprintf
[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_%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 type="text/javascript">
174 var color_checked = '#c4ffc3';
175 function format_int(value, width)
176 {
177   var s = value.toString();
178
179   while (s.length < width)
180     s = '0' + s;
181
182   return s;
183 }
184
185 function timestr(duration)
186 {
187     return format_int(Math.floor(duration / 60), 1) + ':'
188         + format_int(duration - (Math.floor(duration / 60) * 60), 2);
189 }
190
191 function strtime(time)
192 {
193     var a = time.split(':');
194     return parseInt(a[0],10) * 60 + parseInt(a[1],10);
195 }
196
197 function check(id, value)
198 {
199   var form = document.forms[id];
200   var sum = document.getElementById('sum_'+id.toString()); 
201   var newval = 0;
202
203   for (i=0; i< form.elements.length; i++) {
204       if (form.elements[i].name.substr(0,4) == 'oid_') {
205           form.elements[i].checked = value;
206           if (value) {
207               var ta = form.elements[i].parentNode.nextSibling.innerHTML.split(':');
208               newval = newval + parseInt(ta[0],10) * 60 + parseInt(ta[1],10);
209               form.elements[i].parentNode.parentNode.style.backgroundColor = color_checked;
210           } else
211               form.elements[i].parentNode.parentNode.style.backgroundColor = '';
212       }
213   }
214
215   sum.innerHTML = timestr(newval);
216 }
217
218 function plant(form)
219 {
220   document.write('<div class="jscode">');
221   document.write('<a href="javascript:check('+form+',true)">Check all</a>');
222   document.write("&nbsp;/&nbsp;");
223   document.write('<a href="javascript:check('+form+',false)">Uncheck all</a>');
224   document.write("&nbsp;");
225   document.write('<span id="sum_'+form+'" class="sum">0:00</span>');
226   document.write("&nbsp;");
227   document.write('</div>');
228 }
229
230 function add_sum(form, checkbox)
231 {
232     var sum = document.getElementById('sum_'+form.toString()); 
233     var time = strtime(checkbox.parentNode.nextSibling.innerHTML);
234     var newval;
235
236     if (checkbox.checked) {
237         newval = strtime(sum.innerHTML) + time;
238         checkbox.parentNode.parentNode.style.backgroundColor = color_checked;
239     } else {
240         newval = strtime(sum.innerHTML) - time;
241         checkbox.parentNode.parentNode.style.backgroundColor = '';
242     }
243     sum.innerHTML = timestr(newval);
244 }
245 </script>
246
247 <style type="text/css">
248 div.jscode {
249   text-align: right;
250   margin-top: -13px;
251   font-size: 12px;
252 }
253 span.sum {
254   display: inline;
255   border: 1px solid #9b9b9b;
256   background: #d9e2ea;
257   width: 20px;
258   margin-left: 3px;
259 }
260 </style>
261
262 <page func=InfoCon title="Stempeluhr">
263
264 <?
265   if (!empty($msg))
266     echo $msg;
267   else {
268     display_tables();
269 ?>
270
271 <h3 class=bar>Display</h3>
272
273 <form action=status.php method=POST>
274 <select name=month>
275 <?
276   if (!is_array($months))
277     $months = find_months();
278
279   printf('<option value=""%s>alle</option>', !strlen($month)?' selected':'');
280   foreach ($months as $m) {
281     printf('<option value="%s"%s>%s</option>', $m, $month==$m?' selected':'', $m);
282   }
283 ?>
284 </select>
285
286 <select name=customer>
287 <?
288   if (!is_array($customers))
289     $customers = find_customers();
290
291   printf('<option value=""%s>alle</option>', !strlen($cust)?' selected':'');
292   foreach ($customers as $c) {
293     printf('<option value="%s"%s>%s</option>', $c, $cust==$c?' selected':'', $c);
294   }
295 ?>
296 </select>
297
298 <?
299   if (!is_array($stati))
300     $stati = find_status();
301
302   printf('<input type="radio" name="status" value="all"%s>alle&nbsp;', $status=='all'?' checked':'');
303   foreach ($stati as $k=>$v)
304     printf('<input type="radio" name="status" value="%s"%s>%s&nbsp;', $k, $status==$k?' checked':'', $v);
305 ?>
306
307 <input type="hidden" name="filter" value="form">
308 <input class="button" type="submit" value="Display">
309 </form>
310
311 <? } ?>
312 </page>
313
314 # Local variables:
315 # mode: text
316 # mode: auto-fill
317 # end: