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