forked from leonardoxc/leonardoxc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CL_dialogfilter.php
763 lines (668 loc) · 19.2 KB
/
CL_dialogfilter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
<?
//************************************************************************
// Leonardo XC Server, http://www.leonardoxc.net
//
// Copyright (c) 2004-2010 by Andreadakis Manolis
//
// This program is free software. You can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License.
//
// $Id: CL_dialogfilter.php,v 1.12 2010/03/14 20:56:09 manolis Exp $
//
//************************************************************************
/**
* Class to handle multi-select filter controls for GUI_filter and the related popup dialogs.
* So the class has essentially two modes of operation, the filter- and the dialog-mode.
* Martin Jursa, 18.05.2007
*/
class dialogfilter {
var $datakey=''; # the data key identifying the kind of data to be handled
var $data=array(); # an associative array with the current selection (filter-mode) or the data to select from (dialog-mode)
var $numeric=true; # whether the underlying data type is numeric (only int supported)
var $nacid=0; # the NAC ID; needed if $datakey==nacclub
var $nacname='';
var $dataelement=''; # name and id of the form element containing the list of the selected ids (usually something like FILTER_xyz)
var $textelement=''; # name and id of the div containing the text-values of the selction
var $datavalue=''; # value of $dataelement
var $textvalue=''; # innerHTML of $textelement
var $inclusive=true; # if false, the selected data will be excluded from the query, else included
var $selecteddata_key='datastring'; # the GET variable name for the string of currently selected items
var $selecteddatavalues=array(); # in dialog mode: the values of the currently selected items
var $filter_initialised=false;
var $dialog_initialised=false;
var $dialog_width=480; # width of the dialog window
var $dialog_height=0; // by default fill in the whole height
var $errmsg=''; #
var $msg_no_data_selected=_Filter_NoSelection;
function dialogfilter($datakey='', $filtername='', $nacid=0) {
$errmsg='';
if (empty($datakey)) {
$datakey=isset($_GET['datakey']) ? $_GET['datakey'] : '';
if (!$datakey) {
$errmsg='Datakey is missing.';
}
}
if (!$errmsg && empty($filtername)) {
$filtername=isset($_GET['filtername']) ? $_GET['filtername'] : '';
if (!$filtername) {
$errmsg='Filtername is missing.';
}
}
if (!$errmsg) {
switch ($datakey) {
case 'pilot':
$this->dialog_width=350;
break;
case 'country':
$this->numeric=false;
$this->dialog_width=340;
break;
case 'nationality':
$this->numeric=false;
$this->dialog_width=340;
break;
case 'takeoff':
$this->dialog_width=430;
break;
case 'server':
$this->dialog_width=250;
$this->dialog_height=420;
break;
case 'nacclub':
$this->dialog_width=480;
if (empty($nacid)) {
$nacid=isset($_GET['NACid']) ? $_GET['NACid'] : 0;
if (!$nacid) {
$errmsg='NAC-ID is missing.';
}
}
break;
default:
$errmsg='Invalid Filter Data-Key "'.$datakey.'" in '.__CLASS__.'.';
break;
}
}
if (!$errmsg) {
if (substr($filtername, -4, 4)=='excl') $this->inclusive=false;
$this->datakey=$datakey;
$this->dataelement=$filtername;
$this->textelement=$filtername.'_selected';
$this->nacid=$nacid;
if ($nacid) {
global $CONF_NAC_list;
$this->nacname=!empty($CONF_NAC_list[$nacid]['name']) ? $CONF_NAC_list[$nacid]['name'] : 'NAC '.$nacid;
}
}else {
$this->errmsg=$errmsg;
}
}
/**
* Transforms a comma delimited string of key values into a comma-delimited string that may be used in an SQL IN-Clause
*
* @param string $datastring
* @return string
*/
function to_in_string($datastring) {
$in_string='';
if ($datastring) {
$parts=explode(',', $datastring);
$newparts=array();
foreach ($parts as $part) {
$newpart=trim($part);
if ($this->numeric) {
settype($newpart, 'int');
}else {
$newpart=mysql_escape_string(trim($newpart, "'"));
}
if ($newpart) {
$newparts[]=$this->numeric ? $newpart : "'$newpart'";
}
}
if (count($newparts)>1) sort($newparts);
$in_string=implode(',', $newparts);
}
return $in_string;
}
/**
* Management of data queries
*
* @param string $in_string that's a string that can be used in an SQL IN-clause like pkfield IN (1,2,3,4)
*/
function query_data($in_string='') {
$this->data=array();
switch ($this->datakey) {
case 'pilot':
$this->data=$this->getPilotList($in_string);
break;
case 'country':
$this->data=$this->getCountriesList($in_string);
break;
case 'nationality':
$this->data=$this->getNationalitiesList($in_string);
break;
case 'takeoff':
$this->data=$this->getTakeoffList($in_string);
break;
case 'server':
$this->data=$this->getServerList($in_string);
break;
case 'nacclub':
$this->data=$this->getClubs($in_string);
break;
}
}
/**
* Initialisation for filter-mode
*/
function init_filter() {
if (!$this->filter_initialised) {
if (!$this->errmsg) {
$val=isset($GLOBALS[$this->dataelement]) ? $GLOBALS[$this->dataelement] : '';
if ($val) {
$in_string=$this->to_in_string($val);
$this->query_data($in_string);
}
if (count($this->data)>0) {
$this->datavalue=implode(',', array_keys($this->data));
$this->textvalue=implode('<br/>', $this->data);
}else {
$this->textvalue=$this->msg_no_data_selected;
}
}
$this->filter_initialised=true;
}
}
/**
* Returns the where clause for use in GUI_filter.php
* Returns an empty string in case no criteria are defined
*
* @return string
*/
function filter_clause() {
$clause='';
$this->init_filter();
if (!$this->errmsg && $this->datavalue) {
$in_string=$this->to_in_string($this->datavalue);
// echo $this->datavalue."#". $in_string;
if ($in_string) {
global $flightsTable;
switch ($this->datakey) {
case 'pilot':
$clause='userID';
break;
case 'country':
$clause='countryCode';
break;
case 'nationality':
global $pilotsTable;
$clause=$pilotsTable.'.countryCode';
break;
case 'takeoff':
$clause='takeoffID';
break;
case 'server':
$in_string=str_replace('-1','0',$in_string);
$clause=$flightsTable.'.serverID';
break;
case 'nacclub':
$clause=$flightsTable.'.NACclubID';
break;
default:
$this->errmsg='Filter Data-Key '.$this->datakey.' not handled by '.__FUNCTION__.'.';
break;
}
if (!$this->errmsg){
$clause.=$this->inclusive ? ' IN ' : ' NOT IN ';
$clause.='('.$in_string.')';
if ($this->datakey=='nacclub') {
$clause="($flightsTable.NACid=$this->nacid AND $clause)";
}
}
}
}
return $clause;
}
function filter_html() {
$this->init_filter();
if ($this->errmsg) {
$html='
<tr>
<td class="infoHeader"><div align="right"><strong>'.$this->filter_title().'</strong></div></td>
<td class="infoHeader">ERROR: '.$this->errmsg.'</td>
</tr>
';
}else {
$html='
<tr>
<td class="infoHeader" colspan="2" valign="bottom" height="44">'.$this->filter_js_functions().'<div align="right"><strong>'.$this->filter_title().'
<input type="button" value="'._Filter_Button_Select.'" onclick="'.$this->filter_select_js().'">
<input type="button" value="'._Filter_Button_Delete.'" onclick="'.$this->filter_delete_js().'">
</strong></div>
</td>
</tr>
<tr>
<td style="vertical-align:top"><input type="hidden" name="'.$this->dataelement.'" id="'.$this->dataelement.'" value="'.$this->datavalue.'"><div style="text-align:right">'._Filter_CurrentlySelected.':</div></td>
<td style="vertical-align:top">
<div style="width:200px;vertical-align:top" id="'.$this->textelement.'">'.$this->textvalue.'</div>
</td>
</tr>
';
}
return $html;
}
function filter_html_view_only() {
$this->init_filter();
if ($this->errmsg) {
$html='
<tr>
<td class="infoHeader"><div align="left"><strong>'.$this->filter_title().'</strong></div></td>
<td class="infoHeader">ERROR: '.$this->errmsg.'</td>
</tr>
';
}else {
$html='
<tr>
<td class="infoHeader" valign="bottom" height="24"><div align="left"><strong>'.$this->filter_title().'
</strong></div>
</td>
</tr>
<tr>
<td style="vertical-align:top">
<div align="left">'.$this->textvalue.'</div>
</td>
</tr>
';
}
return array($this->datakey,$html);
}
function filter_delete_js() {
$js="
var el1=document.getElementById('$this->textelement');
if (el1) {
el1.innerHTML='$this->msg_no_data_selected';
}
var el2=document.getElementById('$this->dataelement');
if (el2) {
el2.value='';
}";
return $js;
}
function filter_select_js() {
global $moduleRelPath;
$baseurl=$moduleRelPath.'/GUI_EXT_filterdialog.php?datakey='.$this->datakey.'&filtername='.$this->dataelement;
if ($this->datakey=='nacclub') {
$baseurl.='&NACid='.$this->nacid;
}
$js="
var el=document.getElementById('$this->dataelement');
if (el) {
var url='$baseurl&$this->selecteddata_key='+el.value;
popupwindow(url, $this->dialog_width, $this->dialog_height);
}";
return $js;
}
function filter_js_functions() {
static $callcount=0;
if ($callcount==0) {
$callcount++;
$js="
<script type=\"text/javascript\">
<!--
function popupwindow(url, winwidth,winheight){
if (url){
var newheight=winheight;
if (newheight==0) newheight=screen.availHeight*0.9;
var newwidth=winwidth;
var newtop=0;
if (winheight==0) {
newtop=screen.availHeight*0.1;
} else {
newtop=( screen.availHeight-newheight )/2 ;
}
if (newtop <0) newtop=0;
var newleft=screen.availWidth-winwidth;
var optionstring='height='+Math.round(newheight);
optionstring+=',width='+Math.round(newwidth);
optionstring+=',top='+Math.round(newtop);
optionstring+=',left='+Math.round(newleft);
optionstring+=',dependent=yes,resizable=yes,scrollbars=yes,status=yes ';
window.open(url, '_blank', optionstring, false);
//newwin.focus()
}
}// -->
</script>
";
}else {
$js='';
}
return $js;
}
/**
* Returns a lang specific designation for the kind of data queried
*
* @return string
*/
function items_title() {
$items=constant('_Filter_Items_'.$this->datakey);
if ($this->datakey=='nacclub') {
$items=$this->nacname.'-'.$items;
}
return $items;
}
/**
* Label-Text for filter-display
*
* @return string
*/
function filter_title() {
$items=$this->items_title();
if ($this->inclusive) {
//eval('$title="'.str_replace('[items]',$items,_Filter_FilterTitleIncluding).'";');
$title=str_replace('[items]',$items,_Filter_FilterTitleIncluding);
}else {
// eval('$title="'._Filter_FilterTitleExcluding.'";');
$title=str_replace('[items]',$items,_Filter_FilterTitleExcluding);
}
return $title;
}
/**
* Initialisation for dialog-mode
*/
function init_dialog() {
if (!$this->dialog_initialised) {
if (!$this->errmsg) {
$selecteddatastring=isset($_GET[$this->selecteddata_key]) ? urldecode($_GET[$this->selecteddata_key]) : '';
$parts=explode(',', $selecteddatastring);
$newparts=array();
foreach ($parts as $part) {
$part=trim($part);
if ($part) $newparts[]=$part;
}
$this->selecteddatavalues=$newparts;
$this->query_data();
}
$this->dialog_initialised=true;
}
}
function dialog_html() {
$this->init_dialog();
if ($this->errmsg) {
$html='ERROR: '.$this->errmsg;
}else {
$options='<option value=""></option>
';
foreach ($this->data as $key=>$value) {
$selected=in_array($key, $this->selecteddatavalues) ? ' selected ' : '';
$options.='<option value="'.$key.'" '.$selected.'>'.$value.'</option>
';
}
$html='
<table border="0" align="center" cellpadding="2" class="shadowBox main_text" style="height:95%;width:90%">
<tr>
<td width="85%" bgcolor="#CFE2CF">
<div align="center" style="height:100%">
<select name="dialogdata" id="dialogdata" size="2" style="height:85%" multiple>
'.$options.'
</select>
<br>
<br>
<b>'._Filter_DialogMultiSelectInfo.'</b>
<br>
<br>
<input type="button" value="'._Filter_Button_Accept.'" onclick="setValToMaster()"/>
<input type="button" value="'._Filter_Button_Cancel.'" onclick="window.close();" />
</div>
</td>
</tr>
</table>
';
}
return $html;
}
function dialog_title() {
$items=$this->items_title();
if ($this->inclusive) {
// eval('$title="'._Filter_DialogTitleIncluding.'";');
$title=str_replace('[items]',$items,_Filter_DialogTitleIncluding);
}else {
// eval('$title="'._Filter_DialogTitleExcluding.'";');
$title=str_replace('[items]',$items,_Filter_DialogTitleExcluding);
}
return $title;
}
function dialog_js_functions() {
$js="
<script type=\"text/javascript\">
<!--
function setValToMaster(){
if (!window.opener) {
alert('No parent page.');
}else {
var el=document.getElementById('dialogdata');
if (el) {
var masterDoc=window.opener.document;
var dataEl=masterDoc.getElementById('$this->dataelement');
var textEl=masterDoc.getElementById('$this->textelement');
if (!dataEl) {
alert('Error: Cannot find target Element.');
}else {
var selData='';
var selText='';
for (var i=0; i<el.options.length; i++) {
if (el.options[i].selected) {
selData+=el.options[i].value+',';
selText+=el.options[i].text+'<br/>';
}
}
if (selData.length==0) {
dataEl.value='';
textEl.innerHTML='$this->msg_no_data_selected';
}else {
dataEl.value=selData;
textEl.innerHTML=selText;
}
window.close();
}
}
}
}
// -->
</script>
";
return $js;
}
function getPilotList($in_string='') {
global $db;
global $flightsTable, $pilotsTable;
global $CONF, $PREFS;
if ($PREFS->nameOrder==1) {
#$nOrder='FirstName,LastName';
$nameSql="IF(FirstName='' AND LastName='', username, IF(FirstName='', LastName, CONCAT(FirstName, ' ', LastName)))";
}else {
#$nOrder='LastName,FirstName';
$nameSql="IF(LastName='' AND FirstName='', username, IF(LastName='', FirstName, CONCAT(LastName, ' ', FirstName)))";
}
# Martin Jursa 06.04.2009: using this instead of {$prefix}_users; because of bug with opMode=3
$users_table=$CONF['userdb']['users_table'];
$sql="SELECT DISTINCT userID, $nameSql AS Name
FROM
$users_table u
INNER JOIN $flightsTable f ON f.userID=u.user_id
INNER JOIN $pilotsTable p ON p.pilotID=u.user_id
WHERE userID>0";
if ($in_string) {
$sql.=" AND userID IN ($in_string)";
}
$sql.=" GROUP BY userID, $nameSql
ORDER BY $nameSql";
$pnames=array();
$res=$db->sql_query($sql);
//echo $sql;
if($res){
while (false!==$row=$db->sql_fetchrow($res)) {
$pnames[$row['userID']]=$row['Name'];
}
}
return $pnames;
}
/* Verion using getPilotRealName
Performance very bad in case of english, but good in case of currentlang==german (??!!)
function getPilotList($in_string='') {
global $db;
global $flightsTable;
global $prefix;
$sql="SELECT DISTINCT userID
FROM
{$prefix}_users u
INNER JOIN $flightsTable f ON f.userID=u.user_id
WHERE userID>0";
if ($in_string) {
$sql.=" AND userID IN ($in_string)";
}
$pnames=array();
$res=$db->sql_query($sql);
if($res){
while (false!==$row=$db->sql_fetchrow($res)) {
$name=getPilotRealName($row['userID']);
$pnames[$row['userID']]=$name;
}
}
if (!empty($pnames)) {
asort($pnames);
}
return $pnames;
}
*/
function getCountriesList($in_string='') {
global $db;
global $flightsTable, $waypointsTable, $countries;
$countriesList=array();
$where_clause='';
if ($in_string!='') $where_clause.=' AND countryCode IN ('.$in_string.')';
$sql="SELECT DISTINCT countryCode
FROM
$flightsTable f
INNER JOIN $waypointsTable w ON f.takeoffID=w.ID
WHERE
f.userID<>0 $where_clause
";
$res= $db->sql_query($sql);
if($res){
while (false!==$row=$db->sql_fetchrow($res)) {
$cCode=$row['countryCode'];
$countriesList[$cCode]=$countries[$cCode];
}
}
if (!empty($countriesList) ){
asort($countriesList);
}
return $countriesList;
}
function getNationalitiesList($in_string='') {
global $db;
global $pilotsTable, $countries;
$countriesList=array();
$where_clause='';
if ($in_string!='') $where_clause.=' AND countryCode IN ('.$in_string.')';
$sql="SELECT DISTINCT countryCode FROM $pilotsTable WHERE countryCode<>'' $where_clause ";
$res= $db->sql_query($sql);
if($res){
while (false!==$row=$db->sql_fetchrow($res)) {
$cCode=strtoupper($row['countryCode']);
if ($cCode)
$countriesList[$cCode]=$countries[$cCode];
}
}
if (!empty($countriesList) ){
asort($countriesList);
}
return $countriesList;
}
function getServerList($in_string='') {
global $CONF;
// echo "$in_string";
if ($in_string) {
$parts=split(',',$in_string);
// print_r($parts);
foreach ($CONF['servers']['list'] as $serverID=>$serverInfo) {
if (!$serverInfo['is_active']) continue;
if ($serverInfo['id_filter'])
$serverID=$serverInfo['id_filter'];
if ($serverID==0) $serverID=-1;
if ( in_array($serverID,$parts) ) {
$servers[$serverID]=$serverInfo['name_filter']?$serverInfo['name_filter']:$serverInfo['name'];
}
}
return $servers;
} else {
foreach ($CONF['servers']['list'] as $serverID=>$serverInfo) {
if (!$serverInfo['is_active']) continue;
if ($serverInfo['id_filter'])
$serverID=$serverInfo['id_filter'];
if ($serverID==0) $serverID=-1;
$servers[$serverID]=$serverInfo['name_filter']?$serverInfo['name_filter']:$serverInfo['name'];
}
return $servers;
}
}
function getTakeoffList($in_string='') {
global $db;
global $flightsTable;
$takeoffs=array();
$sql="SELECT DISTINCT takeoffID FROM $flightsTable WHERE takeoffID<>0";
if ($in_string) {
$sql.=' AND takeoffID IN ('.$in_string.')';
}
$res= $db->sql_query($sql);
if($res){
while (false!==$row=$db->sql_fetchrow($res)) {
$takeoffs[$row['takeoffID']]=getWaypointName($row['takeoffID'], -1, 1);
}
if (!empty($takeoffs)) {
asort($takeoffs);
}
}
return $takeoffs;
}
function getClubs($in_string='') {
global $db, $flightsTable, $NACclubsTable;
$NACclubList=array();
// Martin Jursa 06.04.2009: the CONCAT statement leads to funny characters with utf 8
//$sql="SELECT DISTINCT clubID, CONCAT(clubName, ' (', clubID, ')') AS NACclubName
$sql="SELECT DISTINCT clubID, clubName, clubID
FROM
$flightsTable f
INNER JOIN $NACclubsTable c ON c.NAC_ID=f.NACid AND c.clubID=f.NACclubID
WHERE f.NACid=$this->nacid";
if ($in_string) {
$sql.=' AND clubID IN ('.$in_string.')';
}
$sql.=' ORDER BY clubName';
$res=$db->sql_query($sql);
if($res){
while (false!==$row=$db->sql_fetchrow($res)) {
$NACclubList[$row['clubID']]=$row['clubName'].' ('.$row['clubID'].')';
}
}
return $NACclubList;
}
/* for testing only:
function getClubs($in_string='') {
global $db, $NACclubsTable;
$NACclubList=array();
$sql="SELECT clubID, CONCAT(clubName, ' (', clubID, ')') AS clubName FROM $NACclubsTable WHERE NAC_ID=$this->nacid";
if ($in_string) {
$sql.=' AND clubID IN ('.$in_string.')';
}
$sql.=' ORDER BY clubName';
$res=$db->sql_query($sql);
if($res){
while (false!==$row=$db->sql_fetchrow($res)) {
$NACclubList[$row['clubID']]=$row['clubName'];
}
}
return $NACclubList;
}*/
}
?>