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