Preserve htmlspecial characters in task description
[infodrom.org/service.infodrom.org] / src / InfoCon / stempel / status.wml
1 #include <infocon.style>
2 #include "common.inc"
3
4 <future>
5 <page func=InfoCon title="Stempeluhr">
6
7 <?
8   session_name('STEMPEL');
9   session_start();
10
11   function update_db()
12   {
13     global $dbh;
14
15     $count = 0;
16     for ($idx=0; $idx < $_POST["fields"]; $idx++) {
17       if (isset($_POST["oid_".$idx])) {
18         $query = sprintf('UPDATE stempel SET status=%d WHERE oid=%d',
19                  $_POST["status"], $_POST["oid_".$idx]);
20         $count++;
21         $sth = pg_exec ($dbh, $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 $cust;
32     global $status;
33     global $dbh;
34     global $stati;
35
36     $name = load_customers();
37
38     $table_head = '<h3 class="bar">Kunde: %s</h3><script type="text/javascript">plant(%d,%.2f);</script>
39   <form action="status.php" method="POST">
40   <table class="smallfont border" id="table_%d" width="100%%" cellpadding=0 cellspacing=0>
41   <thead>
42   <tr class="head" onclick="edit_task_finish(this)">
43     <th width=15%%>Datum</th>
44     <th width=5%%>Dauer</th>
45     <th width=5%%>St.</th>
46     <th width=75%% align=left>Arbeitsbeschreibung</th>
47   </tr>
48   </thead>
49   <tbody>';
50
51     if (!is_array($stati))
52       $stati = find_status();
53
54     $table_foot = '</table><div align="center"><p>';
55
56     foreach ($stati as $k=>$v)
57       $table_foot .= sprintf('<input type="radio" name="status" value="%s"%s>%s&nbsp;', $k, 0==$k?' checked':'', $v);
58       $table_foot .= '<input type="submit" class="button" value="Aktualisieren"></p></div>'.
59                   '<input type="hidden" name="fields" value="%d">'.
60                   '</form>';
61
62     $table_row = '<tr class="t%d"><td>%s</td><td align="center" onclick="toggle_checkbox(%d,this)">%s</td>' .
63       '<td align="center" onclick="toggle_checkbox(%d,this)">%s</td><td onclick="edit_task(this)">%s</td></tr>';
64     $table_sum = '</tbody><tfoot><tr class="t%d"><td>&nbsp;</td><td align="center">%s</td><td>&nbsp;</td><td>Summe</td></tr></tfoot>';
65
66     $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 ";
67     if (isset($month) && strlen($month))
68       $query .= "AND cast(start AS TEXT) LIKE '".$month."-%' ";
69     if (isset($cust) && strlen($cust))
70       $query .= "AND customer ='".$cust."' ";
71
72     if (isset($status) && strlen($status)) {
73       if ($status != 'all')
74         $query .= "AND status = $status ";
75     } else
76       $query .= "AND status = 0 ";
77     $query .= "ORDER BY customer,start";
78
79     $sth = pg_exec ($dbh, $query);
80
81     $form = 0;
82     $customer = '';
83     while ($row = pg_fetch_array ($sth)) {
84
85       if ($customer != $row['customer']) {
86         if (strlen($customer)) {
87           printf($table_sum, $color, min2hour($sum));
88           printf($table_foot, $fieldnr);
89           $form++;
90         }
91         $cname = $name[$row['customer']]['name'];
92         if (!strlen($cname)) $cname = ucfirst($row['customer']);
93         printf($table_head, $cname, $form, $name[$row['customer']]['rate'], $form);
94         $customer = $row['customer'];
95         $fieldnr = 0;
96         $sum = 0;
97         $color = 0;
98       }
99
100       $sum += $row['time'];
101       $d = explode(' ', $row['start']);
102
103       $check = sprintf('<input type="checkbox" class="checkbox" name="oid_%d" value="%d" onclick="add_sum(%d,this)">',
104                        $fieldnr++,
105                        $row['oid'],
106                        $form);
107       $date = sprintf('<span onclick="toggle_checkbox(%d,this)">%s</span>', $form, $d[0]);
108       printf($table_row, $color, $check.$date, $form, min2hour($row['time']), $form, $row['kurz'], htmlspecialchars($row['task'], ENT_COMPAT | ENT_HTML401, 'ISO-8859-1'));
109       $color = !$color;
110     }
111     if (pg_num_rows($sth) > 0) {
112       printf($table_sum, $color, min2hour($sum));
113       printf($table_foot, $fieldnr);
114     }
115   }
116
117   function find_customers()
118   {
119     global $dbh;
120
121     $a = array();
122     $query = "SELECT DISTINCT customer FROM stempel ORDER BY customer";
123     $sth = pg_exec ($dbh, $query);
124
125     while ($row = pg_fetch_array ($sth))
126       $a[] = $row[0];
127
128     return $a;
129   }
130
131   $dbh = pg_pconnect ("<dbhost>", "<dbport>", "<dbname>")
132          or die("Unable to connect to SQL server");
133
134   pg_exec ($dbh, "SET DateStyle = 'ISO'") or die("Datenbank-Abfrage!");
135
136   if (isset($_POST["status"]) && isset($_POST["fields"])) {
137     $msg = update_db();
138     $_SESSION['redirect'] = true;
139     header('Location: status.php');
140     exit;
141   } elseif ($_SESSION['redirect']) {
142     $month = $_SESSION['save']["month"];
143     $cust = $_SESSION['save']["customer"];
144     $status = $_SESSION['save']["status"];
145     $_SESSION['redirect'] = false;
146   } else {
147     if (isset($_GET['month'])) {
148       $month = $_GET['month'];
149       $_SESSION['save']['month'] = $month;
150       unset($_SESSION['save']['customer']);
151       unset($_SESSION['save']['status']);
152     } else
153       if (isset($_POST["filter"])) {
154         $month = $_POST["month"];
155         $cust = $_POST["customer"];
156         $status = $_POST["status"];
157         $_SESSION['save']['month'] = $month;
158         $_SESSION['save']['customer'] = $cust;
159         $_SESSION['save']['status'] = $status;
160       } else {
161         $month = date('Y-m');
162         $_SESSION['save']['month'] = $month;
163         unset($_SESSION['save']['customer']);
164         unset($_SESSION['save']['status']);
165       }
166   }
167 ?>
168
169 <script src="<root_prefix>basics.js"></script>
170 <script type="text/javascript">
171 <protect>
172 var color_checked = '#c4ffc3';
173 function format_int(value, width)
174 {
175   var s = value.toString();
176
177   while (s.length < width)
178     s = '0' + s;
179
180   return s;
181 }
182
183 function timestr(duration)
184 {
185     return format_int(Math.floor(duration / 60), 1) + ':'
186         + format_int(duration - (Math.floor(duration / 60) * 60), 2);
187 }
188
189 function strtime(time)
190 {
191     var a = time.split(':');
192     return parseInt(a[0],10) * 60 + parseInt(a[1],10);
193 }
194
195 function check(id, value)
196 {
197     var hours = $('#time_'+id);
198     var rate = $('#rate_'+id);
199     var sum = $('#sum_'+id);
200     var newval = 0;
201
202     $('table#table_'+id+' tbody tr:visible').each(function(i,e){
203         var checkbox = $(this).find('input[type="checkbox"]');
204         if (checkbox.prop('checked') != value) {
205             checkbox.prop('checked', !checkbox.prop('checked'));
206             if (checkbox.prop('checked'))
207                 $(this).css('background-color', color_checked);
208             else
209                 $(this).css('background-color', '');
210             add_sum(id, checkbox);
211         }
212     });
213 }
214
215 function plant(form, rate)
216 {
217   document.write('<div class="jscode">');
218   document.write('<input class="filter" id="filter_'+form+'" size="10" value="" style="font-size: 90%; position: relative; bottom: 2px;">');
219   document.write('<img class="filter" id="img_'+form+'" src="/pix/Actions-edit-delete-icon.png" border="0" ' +
220                  'style="position: relative; bottom: -3px; margin-left: 1px; margin-right: 10px;" title="Filter löschen">');
221   document.write('<input type="hidden" id="rate_'+form+'" value="'+rate+'">');
222   document.write('<a href="javascript:check('+form+',true)">Check all</a>');
223   document.write("&nbsp;/&nbsp;");
224   document.write('<a href="javascript:check('+form+',false)">Uncheck all</a>');
225   document.write("&nbsp;");
226   document.write('<span id="time_'+form+'" class="sum">0:00</span>' );
227   document.write('<span id="sum_'+form+'" class="sum">&euro;0.00</span>');
228   document.write("&nbsp;");
229   document.write('</div>');
230 }
231
232 function add_sum(form, checkbox)
233 {
234     var hours = $('#time_'+form);
235     var rate = $('#rate_'+form);
236     var sum = $('#sum_'+form);
237     var time = strtime($(checkbox).parents('tr:first').find('td:nth-child(2)').text());
238     var newval;
239
240     if ($(checkbox).prop('checked')) {
241         newval = strtime(hours.text()) + time;
242         $(checkbox).parents('tr:first').css('background-color', color_checked);
243     } else {
244         newval = strtime(hours.text()) - time;
245         $(checkbox).parents('tr:first').css('background-color', '');
246     }
247     hours.text(timestr(newval));
248     sum.html('&euro;' + ((newval/60)*rate.val()).toFixed(2));
249 }
250
251 var task_parent = null;
252 var task_title = '';
253 var task_oid = 0;
254 function edit_task(obj)
255 {
256     if (task_oid) {
257         if (task_oid != obj.parentNode.children[0].children[0].value)
258             edit_task_save();
259     }
260
261     if (!task_oid) {
262         task_parent = obj;
263         task_title = obj.innerHTML;
264         task_oid = obj.parentNode.children[0].children[0].value;
265
266         var input = $('<input>');
267 <protect>
268         input.val(obj.innerHTML.replace('&gt;', '>').replace('&lt;', '<').replace('&amp;', '&'));
269 </protect>
270         input.css('fontSize', '100%').css('width', '100%').css('background', 'yellow');
271         obj.innerHTML = '';
272         $(obj).append(input);
273         input.focus();
274     }
275 }
276
277 function edit_task_save()
278 {
279     if (!task_parent) return;
280
281     if (task_parent.children[0].value != task_title)
282         ajax_request('save','oid='+task_oid+'&task='+encodeURIComponent(task_parent.children[0].value));
283
284     task_parent.innerHTML = task_parent.children[0].value;
285     task_parent = null;
286     task_title = '';
287     task_oid = 0;
288 }
289
290 function edit_task_finish(obj)
291 {
292     if (task_oid)
293         edit_task_save();
294 }
295
296 function toggle_checkbox(form, obj)
297 {
298     edit_task_finish(obj);
299
300     var row = $(obj).parents('tr:first');
301     var checkbox = row.find('input[type="checkbox"]')
302     checkbox.prop('checked', !checkbox.prop('checked'));
303
304     checkbox.checked = !checkbox.checked;
305     add_sum(form, checkbox);
306 }
307
308 function filter_change(e)
309 {
310     var form = $(this).attr('id').split('_')[1];
311     var filter = $('input#filter_'+form).val();
312
313     $('table#table_'+form+' tbody tr').each(function(i,e){
314         if (filter.length) {
315             var td = $(this).find('td:nth-child(4)');
316             if (td.text().toLowerCase().indexOf(filter) > -1)
317                 $(this).show();
318             else {
319                 if ($(this).find('input[type="checkbox"]').prop('checked')) {
320                     toggle_checkbox(form, td);
321                 }
322                 $(this).hide();
323             }
324         } else {
325             $(this).show();
326         }
327     });
328 }
329
330 function filter_clear(e)
331 {
332     var form = $(this).attr('id').split('_')[1];
333     $('div.jscode input.filter#filter_'+form).val('').change();
334 }
335
336 $(function(){
337     $('div.jscode input.filter').change(filter_change);
338     $('div.jscode img.filter').click(filter_clear);
339 });
340 </protect>
341 </script>
342
343 <style type="text/css">
344 div.jscode {
345   text-align: right;
346   margin-top: -13px;
347   font-size: 12px;
348 }
349 span.sum {
350   display: inline;
351   border: 1px solid #9b9b9b;
352   background: #d9e2ea;
353   width: 20px;
354   margin-left: 3px;
355 }
356 </style>
357
358 <?
359   if (!empty($msg))
360     echo $msg;
361   else {
362     display_tables();
363 ?>
364
365 <h3 class=bar>Display</h3>
366
367 <form action=status.php method=POST>
368 <select name=month>
369 <?
370   if (!is_array($months))
371     $months = find_months();
372
373   printf('<option value=""%s>alle</option>', !strlen($month)?' selected':'');
374   foreach ($months as $m) {
375     printf('<option value="%s"%s>%s</option>', $m, $month==$m?' selected':'', $m);
376   }
377 ?>
378 </select>
379
380 <select name=customer>
381 <?
382   if (!is_array($customers))
383     $customers = find_customers();
384
385   printf('<option value=""%s>alle</option>', !strlen($cust)?' selected':'');
386   foreach ($customers as $c) {
387     printf('<option value="%s"%s>%s</option>', $c, $cust==$c?' selected':'', $c);
388   }
389 ?>
390 </select>
391
392 <?
393   if (!is_array($stati))
394     $stati = find_status();
395
396   printf('<input type="radio" name="status" value="all"%s>alle&nbsp;', $status=='all'?' checked':'');
397   foreach ($stati as $k=>$v)
398     printf('<input type="radio" name="status" value="%s"%s>%s&nbsp;', $k, $status==$k?' checked':'', $v);
399 ?>
400
401 <input type="hidden" name="filter" value="form">
402 <input class="button" type="submit" value="Display">
403 </form>
404
405 <? } ?>
406 </page>
407
408 # Local variables:
409 # mode: php
410 # end: