Add more space
[infodrom.org/service.infodrom.org] / src / InfoCon / stempel / status.wml
1 #include <infocon.style>
2 #include "common.inc"
3
4 <script type="text/javascript">
5 function check(id, value)
6 {
7   var form = document.forms[id];
8
9   for (i=0; i< form.elements.length; i++) {
10     if (form.elements[i].name.substr(0,4) == 'oid_')
11       form.elements[i].checked = value;
12   }
13
14   if (value == false) {
15       var sum = document.getElementById('sum_'+id.toString()); 
16       sum.innerHTML = '0';
17   }
18 }
19
20 function plant(form)
21 {
22   document.write('<div class="jscode">');
23   document.write('<a href="javascript:check('+form+',true)">Check all</a>');
24   document.write("&nbsp;/&nbsp;");
25   document.write('<a href="javascript:check('+form+',false)">Uncheck all</a>');
26   document.write("&nbsp;");
27   document.write('<span id="sum_'+form+'" class="sum">0</span>');
28   document.write("&nbsp;");
29   document.write('</div>');
30 }
31
32 var sum = array();
33 function add_sum(form, checkbox)
34 {
35     var sum = document.getElementById('sum_'+form.toString()); 
36     var newval;
37
38     var ta = checkbox.parentNode.nextSibling.innerHTML.split(':');
39     var time = parseInt(ta[0]) * 60 + parseInt(ta[1]);
40
41     if (checkbox.checked)
42         newval = parseInt(sum.innerHTML) + time;
43     else
44         newval = parseInt(sum.innerHTML) - time;
45     sum.innerHTML = newval.toString();
46 }
47 </script>
48
49 <style type="text/css">
50 div.jscode {
51   text-align: right;
52   margin-top: -13px;
53   font-size: 12px;
54 }
55 span.sum {
56   display: inline;
57   border: 1px solid #9b9b9b;
58   background: #d9e2ea;
59   width: 20px;
60   margin-left: 3px;
61 }
62 </style>
63
64 <?
65   function update_db()
66   {
67     global $dbh;
68
69     $count = 0;
70     for ($idx=0; $idx < $_POST["fields"]; $idx++) {
71       if (isset($_POST["oid_".$idx])) {
72         $query = sprintf('UPDATE stempel SET status=%d WHERE oid=%d',
73                  $_POST["status"], $_POST["oid_".$idx]);
74         $count++;
75         $sth = pg_exec ($dbh, $query);
76       }
77     }
78     return sprintf("<br>%d records updated.", $count);
79   }
80
81   $stati = false;
82   function display_tables()
83   {
84     global $month;
85     global $cust;
86     global $status;
87     global $dbh;
88     global $stati;
89
90     $table_head = '<h3 class="bar">Kunde: %s</h3><script type="text/javascript">plant(%d);</script>
91   <form action="status.php" method="POST">
92   <table class="smallfont border" width="100%%" cellpadding=0 cellspacing=0>
93   <tr class="head">
94     <th width=15%%>Datum</th>
95     <th width=5%%>Dauer</th>
96     <th width=5%%>St.</th>
97     <th width=75%% align=left>Verwendungszweck</th>
98   </tr>';
99
100     if (!is_array($stati))
101       $stati = find_status();
102
103     $table_foot = '</table><div align="center"><p>';
104
105     foreach ($stati as $k=>$v)
106       $table_foot .= sprintf('<input type="radio" name="status" value="%s"%s>%s&nbsp;', $k, 0==$k?' checked':'', $v);
107       $table_foot .= '<input type="submit" class="button" value="Aktualisieren"></p></div>'.
108                   '<input type="hidden" name="fields" value="%d">'.
109                   '</form>';
110
111     $table_row = '<tr class=t%d><td>%s</td><td align="center">%s</td><td align="center">%s</td><td>%s</td></tr>';
112     $table_sum = '<tr class="t%d"><td>&nbsp;</td><td align="center">%s</td><td>&nbsp;</td><td>Summe</td></tr>';
113
114     $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 ";
115     if (isset($month) && strlen($month))
116       $query .= "AND cast(start AS TEXT) LIKE '".$month."-%' ";
117     if (isset($cust) && strlen($cust))
118       $query .= "AND customer ='".$cust."' ";
119
120     if (isset($status)) {
121       if ($status != 'all')
122         $query .= "AND status = $status ";
123     } else
124       $query .= "AND status = 0 ";
125     $query .= "ORDER BY customer,start";
126
127     $sth = pg_exec ($dbh, $query);
128
129     $form = 0;
130     $customer = '';
131     while ($row = pg_fetch_array ($sth)) {
132
133       if ($customer != $row['customer']) {
134         if (strlen($customer)) {
135           printf($table_sum, $color, min2hour($sum));
136           printf($table_foot, $fieldnr);
137           $form++;
138         }
139         printf($table_head, ucfirst($row['customer']), $form);
140         $customer = $row['customer'];
141         $fieldnr = 0;
142         $sum = 0;
143         $color = 0;
144       }
145
146       $sum += $row['time'];
147       $d = explode(' ', $row['start']);
148
149       $check = sprintf('<input type="checkbox" class="checkbox" name="oid_'.$fieldnr++.'" value="'.$row['oid'].'" onclick="add_sum(%d,this)">',$form);
150       printf($table_row, $color, $check.$d[0], min2hour($row['time']), $row['kurz'], $row['task']);
151       $color = !$color;
152     }
153     if (pg_num_rows($sth) > 0) {
154       printf($table_sum, $color, min2hour($sum));
155       printf($table_foot, $fieldnr);
156     }
157   }
158
159   function find_customers()
160   {
161     global $dbh;
162
163     $a = array();
164     $query = "SELECT DISTINCT customer FROM stempel ORDER BY customer";
165     $sth = pg_exec ($dbh, $query);
166
167     while ($row = pg_fetch_array ($sth))
168       $a[] = $row[0];
169
170     return $a;
171   }
172
173   $dbh = pg_pconnect ("<dbhost>", "<dbport>", "<dbname>")
174          or die("Unable to connect to SQL server");
175
176   pg_exec ($dbh, "SET DateStyle = 'ISO'") or die("Datenbank-Abfrage!");
177
178   if (isset($_POST["status"]) && isset($_POST["fields"])) {
179     $msg = update_db();
180   } else {
181     if (isset($_GET['month']))
182       $month = $_GET['month'];
183     else
184       if (isset($_POST["filter"])) {
185         $month = $_POST["month"];
186         $cust = $_POST["customer"];
187         $status = $_POST["status"];
188       } else
189         $month = date('Y-m');
190   }
191 ?>
192
193 <page func=InfoCon title="Stempeluhr">
194
195 <?
196   if (!empty($msg))
197     echo $msg;
198   else {
199     display_tables();
200 ?>
201
202 <h3 class=bar>Display</h3>
203
204 <form action=status.php method=POST>
205 <select name=month>
206 <?
207   if (!is_array($months))
208     $months = find_months();
209
210   printf('<option value=""%s>alle</option>', !strlen($_POST['month'])?' selected':'');
211   foreach ($months as $m) {
212     printf('<option value="%s"%s>%s</option>', $m, $_POST['month']==$m?' selected':'', $m);
213   }
214 ?>
215 </select>
216
217 <select name=customer>
218 <?
219   if (!is_array($customers))
220     $customers = find_customers();
221
222   printf('<option value=""%s>alle</option>', !strlen($_POST['month'])?' selected':'');
223   foreach ($customers as $c) {
224     printf('<option value="%s"%s>%s</option>', $c, $_POST['customer']==$c?' selected':'', $c);
225   }
226 ?>
227 </select>
228
229 <?
230   if (!is_array($stati))
231     $stati = find_status();
232
233   printf('<input type="radio" name="status" value="all"%s>alle&nbsp;', $status=='all'?' checked':'');
234   foreach ($stati as $k=>$v)
235     printf('<input type="radio" name="status" value="%s"%s>%s&nbsp;', $k, $status==$k?' checked':'', $v);
236 ?>
237
238 <input type="hidden" name="filter" value="form">
239 <input class="button" type="submit" value="Display">
240 </form>
241
242 <? } ?>
243 </page>
244
245 # Local variables:
246 # mode: text
247 # mode: auto-fill
248 # end: