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