Move find_status into omnipresent file
[infodrom.org/service.infodrom.org] / src / InfoCon / stempel / status.wml
1
2 #include <infocon.style>
3
4 <?
5   function min2hour($minutes)
6   {
7       return sprintf('%02d:%02d', $minutes/60, $minutes%60);
8   }
9
10   function update_db()
11   {
12     global $dbh;
13
14     $count = 0;
15     for ($idx=0; $idx < $_POST["fields"]; $idx++) {
16       if (isset($_POST["oid_".$idx])) {
17         $query = sprintf('UPDATE stempel SET status=%d WHERE oid=%d',
18                  $_POST["status"], $_POST["oid_".$idx]);
19         $count++;
20         $sth = pg_exec ($dbh, $query);
21 echo '<br'.$query;
22       }
23     }
24     return sprintf("<br>%d records updated.", $count);
25   }
26
27   $stati = false;
28   function display_tables()
29   {
30     global $month;
31     global $status;
32     global $dbh;
33     global $stati;
34
35     $table_head = '<h3 class="bar">Kunde: %s</h3><form action="status.php" method="POST">
36   <table class="smallfont border" width="100%%" cellpadding=0 cellspacing=1>
37   <tr class="head">
38     <th width=15%%>Datum</th>
39     <th width=5%%>Dauer</th>
40     <th width=5%%>St.</th>
41     <th width=75%%>Verwendungszweck</th>
42   </tr>';
43
44     if (!is_array($stati))
45       $stati = find_status();
46
47     $table_foot = '</table><div align="center"><p>';
48
49     foreach ($stati as $k=>$v)
50       $table_foot .= sprintf('<input type="radio" name="status" value="%s"%s>%s&nbsp;', $k, 0==$k?' checked':'', $v);
51       $table_foot .= '</p><p><input type="submit" value="Aktualisieren"></div>'.
52                   '<input type="hidden" name="fields" value="%d"></p>'.
53                   '</form>';
54
55     $table_row = '<tr class=t%d><td>%s</td><td align="center">%s</td><td align="center">%s</td><td>%s</td></tr>';
56     $table_sum = '<tr class="t%d"><td>&nbsp;</td><td align="center">%s</td><td>&nbsp;</td><td>Summe</td></tr>';
57
58     $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 ";
59     if (isset($month) && $month !== 'all')
60       $query .= "AND cast(start AS TEXT) LIKE '".$month."-%' ";
61     if (isset($status)) {
62       if ($status != 'all')
63         $query .= "AND status = $status ";
64     } else
65       $query .= "AND status = 0 ";
66     $query .= "ORDER BY customer,start";
67
68     $sth = pg_exec ($dbh, $query);
69
70     $customer = '';
71     while ($row = pg_fetch_array ($sth)) {
72
73       if ($customer != $row['customer']) {
74         if (strlen($customer)) {
75           printf($table_sum, $color, min2hour($sum));
76           printf($table_foot, $fieldnr);
77         }
78         printf($table_head, $row['customer']);
79         $customer = $row['customer'];
80         $fieldnr = 0;
81         $sum = 0;
82         $color = 0;
83       }
84
85       $sum += $row['time'];
86       $d = explode(' ', $row['start']);
87
88       $check = '<input type="checkbox" name="oid_'.$fieldnr++.'" value="'.$row['oid'].'">';
89       printf($table_row, $color, $check.$d[0], min2hour($row['time']), $row['kurz'], $row['task']);
90       $color = !$color;
91     }
92     printf($table_sum, $color, min2hour($sum));
93     printf($table_foot, $fieldnr);
94   }
95
96   $dbh = pg_pconnect ("<dbhost>", "<dbport>", "<dbname>")
97          or die("Unable to connect to SQL server");
98
99   pg_exec ($dbh, "SET DateStyle = 'ISO'") or die("Datenbank-Abfrage!");
100
101   if (isset($_POST["status"]) && isset($_POST["fields"])) {
102     $msg = update_db();
103   } else {
104     if (isset($_GET['month']))
105       $month = $_GET['month'];
106     else
107       if (isset($_POST["filter"])) {
108         $month = $_POST["month"];
109         $status = $_POST["status"];
110       } else
111         $month = date('Y-m');
112   }
113 ?>
114
115 <page func=InfoCon title="Stempeluhr">
116
117 <?
118   if (!empty($msg))
119     echo $msg;
120   else {
121     display_tables();
122 ?>
123
124 <h3 class=bar>Display</h3>
125
126 <form action=status.php method=POST>
127 <select name=month>
128 <?
129   if (!is_array($months))
130     $months = find_months();
131
132   printf('<option value="all"%s>alle</option>', $_POST['month']=='alle'?' selected':'');
133   foreach ($months as $m) {
134     printf('<option value="%s"%s>%s</option>', $m, $_POST['month']==$m?' selected':'', $m);
135   }
136 ?>
137 </select>
138
139 <?
140   if (!is_array($stati))
141     $stati = find_status();
142
143   printf('<input type="radio" name="status" value="all"%s>alle&nbsp;', $status=='all'?' checked':'');
144   foreach ($stati as $k=>$v)
145     printf('<input type="radio" name="status" value="%s"%s>%s&nbsp;', $k, $status==$k?' checked':'', $v);
146 ?>
147
148 <input type="hidden" name="filter" value="form">
149 <input class="button" type="submit" value="Display">
150 </form>
151
152 <? } ?>
153 </page>
154
155 # Local variables:
156 # mode: text
157 # mode: auto-fill
158 # end: