forked from leonardoxc/leonardoxc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CL_filter.php
869 lines (684 loc) · 23.9 KB
/
CL_filter.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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
<?
//************************************************************************
// 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_filter.php,v 1.9 2010/03/21 22:51:57 manolis Exp $
//
//************************************************************************
/*
this class compiles - decompiles a filter URL
to a more compact (binary?) format
FILTER_YEAR_select_op=%3E%3D
&FILTER_YEAR_select=2007
&FILTER_MONTH_YEAR_select_op=%3D
&FILTER_MONTH_YEAR_select_MONTH=02
&FILTER_MONTH_YEAR_select_YEAR=2010
&FILTER_dateType=DATE_RANGE
&FILTER_from_day_text=01.02.2010
&FILTER_to_day_text=15.02.2010
&FILTER_pilots_incl=3451%2C4220%2C4026%2C5717%2C2266%2C80%2C2784%2C7501%2C8803%2C
&FILTER_nacclubs101_incl=118%2C
&FILTER_countries_incl=DE%2CGR
&FILTER_takeoffs_incl=9705%2C9334%2C9888%2C9165%2C9014%2C9758%2C
&FILTER_nationality_incl=BB%2CBR%2CHR%2CFI%2CGR%2C
&FILTER_server_incl=1%2C2%2C4%2C8%2C
&FILTER_sex=M
&FILTER_cat1=1
&FILTER_cat2=2
&FILTER_linear_distance_op=%3E%3D
&FILTER_linear_distance_select=10
&FILTER_olc_distance_op=%3C%3D
&FILTER_olc_distance_select=100
&FILTER_olc_score_op=%3E%3D
&FILTER_olc_score_select=50
&FILTER_olc_type=FREE_FLIGHT
&FILTER_duration_op=%3E%3D
&FILTER_duration_hours_select=23
&FILTER_duration_minutes_select=12
add also
- Extend Filter with checkbox categories for Paraglider LTF Class (LTF 1, 1-2, 2, 2-3, A, B, C, D, E, Open)
- Extend Filter with checkbox category for Hanggliders with Kingposts.
- Extend Filter page with Pilot Birthdate.
- Extend Filter page for Start type: foot, winch, microlight tow or e-motor (is to be included on GUI_flight_submit.php)
Integrate RSS Feed in Filterpage, to generate Feeds according to filter settings.
Ensure Filter bookmarks function correctly (check seasons mod).
Ensure Filter settings function correctly on all scoring lists.
*/
$filterOps=array(
/*
the opID is a byte
4 left most bits for the type (0-15)
+ 5 bits for the id ( 0 - 15 )
we have
0000 -> reserved
0010 0x20 (+32) -> date related
0100 0x40 (+64) -> multiple items
0110 0x60 (+96) -> simple integer select
1000 0x80 (+128)-> greater then, less than , equal items
*/
// date related filter
// greater then, less than , equal items
/* opID + 1 byte for operator + 2 bytes per date
the 2 leftmost bits are the operator
00 -> = (equal)
01 -> <= (less than)
10 -> >= (greater then )
11 -> between (needs 2 extra bytes
year -> 8 bits ( 1900 + value we can get up to year 2256 .. this should be enough ?!)
month -> 4 bits ( 0-15 )
day -> 5 bits ( 0-31 )
total 19 bits -> 3 bytes
*/
0x20=>array("FILTER_DATE"),
0x22=>array("FILTER_PilotBirthdate"),
// multiple items inclusive
/*
opID
nationality and countries -> country code ie GRiBGiDE -> GR BG DE
pilots -> pilotID +i +pilotID +i .... + s
takeoffs -> takeoffID +i+ takeoffID +r+ takeoffID ... + s
server -> takeoffID +i+ takeoffID +i+ takeoffID ...2 bytes
nacclubs ->...
*/
0x40=>array("FILTER_pilots_incl", 'pilot', 'userID'),
0x41=>array("FILTER_countries_incl", 'country', $waypointsTable.'.countryCode'),
0x42=>array("FILTER_takeoffs_incl", 'takeoff', 'takeoffID'),
0x43=>array("FILTER_nationality_incl", 'nationality',$pilotsTable.'.countryCode'),
0x44=>array("FILTER_server_incl", 'server', $flightsTable.'.serverID'),
0x45=>array("FILTER_nacclubs_incl", 'nacclub', $flightsTable.'.NACclubID'),
// simple select , or multiple choices
/* opID + 2 bytes (32 bits) range
*/
0x60=>array("FILTER_sex"),
0x61=>array("FILTER_cat"), // category pg,hg etc...
0x62=>array("FILTER_class"), // class -> open, sport tandem etc...
0x63=>array("FILTER_olc_type"),
0x64=>array("FILTER_glider_cert"), //
0x65=>array("FILTER_start_type"), // Start type
0x66=>array("FILTER_WEEKEND"), // either 0 or 1
// greater then, less than , equal items
/* opID + 2 bytes (30 bits) range (0-16384) the 2 leftmost bits
are the operator
00 -> = (equal)
01 -> <= (less than)
10 -> >= (greater then )
*/
0x80=>array("FILTER_linear_distance"),
0x81=>array("FILTER_olc_distance"),
0x82=>array("FILTER_olc_score"),
0x83=>array("FILTER_duration"), // (in minutes)
);
if (0) {
//echo leonardoFilter::getShort("10004",0);
$filter=new LeonardoFilter();
$filterString=$_GET['f'];
if (!$filterString) $filterString="80000260000820C100A64100A741gr_de_us_fr.402466_4672_17282_13461.".
"45101_234_235_271.";
$filter->parseFilterString($filterString);
echo "<PRE>";
print_r($filter->filterArray);
echo "</PRE>";
echo $filter->makeClause();
echo "<HR>".$filterString. "<HR>".$filter->makeFilterString();
}
class leonardoFilter {
var $filterArray=array();
var $filterTextual="";
function leonardoFilter() {
}
function getByte($filterStr,$start) {
if ( strlen($filterStr) < ($start +2) ) return -1;
$valHex=substr($filterStr,$start,2);
if( preg_match("/[^0-9ABCDEF]+/i", $valHex) ) return -1;
$val=hexdec($valHex);
return $val;
}
function getShort($filterStr,$start){
if ( strlen($filterStr) < ($start +4) ) return -1;
$valHex=substr($filterStr,$start,4);
if( preg_match("/[^0-9ABCDEF]+/i", $valHex) ) return -1;
$val=hexdec($valHex);
return $val;
}
function parseOp($filterStr,$start){
global $filterOps;
$op=$this->getByte($filterStr,$start);
if ($op<0 ) return 0; // something is wrtong with the data, stop here
// echo "#".dechex($op)."#";
if ( ($op & 0xf0) == 0x40) {
// multiple items inclusive
/*
opID
nationality and countries -> country code ie GR_BG_DE. -> GR BG DE
pilots -> pilotID _ pilotID . .... + .
takeoffs -> takeoffID _ takeoffID _ takeoffID + .
server -> takeoffID _ takeoffID _ takeoffID
nacclubs ->...
*/
$i=2;
$end=false;
$values=array();
$tmpStr='';
while (!$end) {
$chr=$filterStr[$start+$i];
if ($chr=='&' || ($start+$i) >= strlen($filterStr) ) $end=true;
if ($chr=='_' || $chr=='.' || $chr=='&' || ($start+$i) >= strlen($filterStr) ) {
if( preg_match("/[^0-9A-Z]+/i", $tmpStr) ) {
return -1;
}
if ($chr=='.' || $chr=='&' || ($start+$i) >= strlen($filterStr) ) $end=true;
$values[]=$tmpStr;
$tmpStr='';
$i++;
continue;
}
$i++;
$tmpStr.=$chr;
}
// echo "i=$i ***<BR>";
if ( $op == 0x45 ) {
$nacid= array_shift($values);
$this->filterArray[]=array($op,$filterOps[$op][0],$values,$nacid);
} else {
$this->filterArray[]=array($op,$filterOps[$op][0],$values);
}
return $i;
} else if ( ($op & 0xf0) == 0x80) {
//opID + 2 bytes (30 bits) range (0-16384) the 2 leftmost bits are the operator
/* 00 -> = (equal)
01 -> <= (less than)
10 -> >= (greater then ) */
$val=$this->getShort($filterStr,$start+2);
if ($val<0) return -1;
$value= $val&0x3fff;
$operand=($val&0xC000 )>>14;
//echo $operand."**";
if ( $operand==0) $operand="=";
else if ( $operand==1) $operand="<=";
else if ( $operand==2) $operand=">=";
else return 0; // error
$this->filterArray[]=array($op,$filterOps[$op][0],$operand,$value);
return 6;
}
if ( ($op & 0xf0) == 0x60) {
// simple select , or multiple choices
/* opID + 2 bytes (32 bits) range
*/
$val=$this->getShort($filterStr,$start+2);
if ($val<0) return -1;
$value= $val& 0x3fff;
$this->filterArray[]=array($op,$filterOps[$op][0],$val);
return 6;
}
// date related filter
// greater then, less than , equal items
/* opID + 1 byte for operator + 3 bytes per date
the 2 leftmost bits are the operator
00 -> = (equal)
01 -> <= (less than)
10 -> >= (greater then )
11 -> between (needs 2 extra bytes
year -> 8 bits ( 1900 + value we can get up to year 2256 .. this should be enough ?!)
month -> 4 bits ( 0-15 )
day -> 5 bits ( 0-31 )
total 19 bits -> 3 bytes
12345678 1234 12345
10000000 0101 00110 -> 1 0000 0000 1010 0110 -> 1 00 A6 + less then (01) -> 4100A6
0100 0000 0000 0000 0000 0000
1 0000 000 0 1010 0110
0100 0001 0000 0000 1010 0110
128->2028 5 6
*/
if ( ($op & 0xf0) == 0x20) {
$val1=$this->getShort($filterStr,$start+2);
$val2=$this->getByte($filterStr,$start+6);
if ($val1<0 || $val2<0) return -1;
$val=($val1<<8 )+$val2;
$value=$val & 0x3fffff;
$year=1900+ ($value >>9) ;
$month=($value >>5) & 0x00000f;
$day = $value & 0x1F;
$resLen=8;
$operand=($val&0x00C00000) >> 22;
//echo "####***********####".$operand;
if ( $operand==0) $operand="=";
else if ( $operand==1) $operand="<=";
else if ( $operand==2) $operand=">=";
else {
$operand="between";
$val1=$this->getShort($filterStr,$start+8);
$val2=$this->getByte($filterStr,$start+12);
if ($val1<0 || $val2<0) return -1;
$val=($val1<<8 )+$val2;
$value=$val & 0x3fffff;
$year2=$value >>9 ;
$month2=$value >>5 & 0x0f;
$day2 = $value & 0x1F;
$year2=1900+ ($value >>9) ;
$month2=($value >>5) & 0x00000f;
$day2 = $value & 0x1F;
$resLen=14;
}
$this->filterArray[]=array($op,$filterOps[$op][0],$operand,$year,$month,$day,$year2,$month2,$day2);
return $resLen;
}
// all else failed
return 0;
}
function parseFilterString($filterStr) {
if (!$filterStr) return;
$filterStr=strtoupper($filterStr);
$start=0;
$filterStrLen=strlen($filterStr);
do {
$opLen=$this->parseOp($filterStr,$start);
// echo "opLen= $opLen <BR>";
$start+=$opLen;
if ($opLen<0) {
$start=0;
$this->filterArray=array();
break;
}
} while ($start <$filterStrLen && $opLen>0) ;
}
function filterImport($arrayName) {
if ($arrayName=='_REQUEST') {
$variablesArray=$_REQUEST;
} else {
$variablesArray=${$arrayName};
global $variablesArray;
}
global $filterOps;
global $pilotsTable,$flightsTable;
$this->filterArray=array();
foreach ($variablesArray as $key=>$value) {
if (substr($key,0,7)!="FILTER_") continue;
if (substr($key,-3)=="_op") continue; // we take care of these in the _select counterparts of them
if ( preg_match("/YEAR/",$key ) || preg_match("/day_text/",$key ) || preg_match("/day_text/",$key ) ) continue;
if ($key=="FILTER_PilotBirthdate_text") {
if (!$value) continue;
$op='FILTER_PilotBirthdate';
$opID=0x22;
$year1=substr($variablesArray['FILTER_PilotBirthdate_text'],6,4);;// 23.02.2008
$month1=substr($variablesArray['FILTER_PilotBirthdate_text'],3,2);
$day1=substr($variablesArray['FILTER_PilotBirthdate_text'],0,2);
$operand=$variablesArray['FILTER_PilotBirthdate_op'];
$this->filterArray[]=array($opID,$op,$operand,$year1,$month1,$day1,0,0,0);
continue;
}
if ($key=="FILTER_dateType") {
$opID=0x20;
$op='FILTER_DATE';
if ($value=="DATE_RANGE") {
$year1=substr($variablesArray['FILTER_from_day_text'],6,4);;// 23.02.2008
$month1=substr($variablesArray['FILTER_from_day_text'],3,2);
$day1=substr($variablesArray['FILTER_from_day_text'],0,2);
$year2=substr($variablesArray['FILTER_to_day_text'],6,4);
$month2=substr($variablesArray['FILTER_to_day_text'],3,2);
$day2=substr($variablesArray['FILTER_to_day_text'],0,2);
$this->filterArray[]=array($opID,$op,'between',$year1,$month1,$day1,$year2,$month2,$day2);
} else if ($value=="MONTH_YEAR") {
$year1=$variablesArray['FILTER_MONTH_YEAR_select_YEAR'];
$month1=$variablesArray['FILTER_MONTH_YEAR_select_MONTH'];
$operand=$variablesArray['FILTER_MONTH_YEAR_select_op'];
$this->filterArray[]=array($opID,$op,$operand,$year1,$month1,0);
} else if ($value=="YEAR") {
$year1=$variablesArray['FILTER_YEAR_select'];
$operand=$variablesArray['FILTER_YEAR_select_op'];
$this->filterArray[]=array($opID,$op,$operand,$year1,0,0);
}
continue;
}
if (substr($key,-7)=="_select") {
if (!$value) continue;
$op=substr($key,0,-7);
$opID=array_search_value($op, $filterOps);
$operand=$variablesArray[$op.'_op'];
$this->filterArray[]=array($opID,$op,$operand,$value);
continue;
}
if (substr($key,-5)=="_incl" ) {
if (!$value) continue;
$value=trim($value,",");
if ( preg_match("/FILTER_nacclubs(\d+)_incl/",$key,$matches) ) {
$key="FILTER_nacclubs_incl";
$opID=array_search_value($key, $filterOps);
$valueArray=explode(",",$value);
$this->filterArray[]=array($opID,$key,$valueArray,$matches[1]);
} else {
$opID=array_search_value($key, $filterOps);
$valueArray=explode(",",$value);
$this->filterArray[]=array($opID,$key,$valueArray);
}
continue;
}
// if nothing else matches , is a simple var
if (!$value) continue;
$op=$key;
$opID=array_search_value($op, $filterOps);
// echo "Importing $opID,$op,$value <BR>";
$this->filterArray[]=array($opID,$op,$value);
continue;
} // foreach
// echo "$filterArray<HR><PRE>"; print_r($this->filterArray); echo "</PRE>".$value;
}
// constructs the correct $_SESSION variables that the old GUI_filter code needs!
function filterExport($arrayName) {
if ($arrayName=='_REQUEST') {
$variablesArray=$_REQUEST;
} else {
global ${$arrayName};
$variablesArray=&${$arrayName};
}
global $filterOps;
global $pilotsTable,$flightsTable;
foreach ($this->filterArray as $i=>$item) {
$op=$item[0];
$opName=$item[1];
if ( ($op & 0xf0) == 0x40 ) {// multi values
$varName=$filterOps[$op][0];
$value=implode(",",$item[2]);
if ($op==0x45) {
// FILTER_nacclubs_incl ->FILTER_nacclubs24_incl
$varName=substr($varName,0,-5).$item[3].substr($varName,-5);
}
$variablesArray[$varName]=$value;
continue;
}
if ( ($op & 0xf0) == 0x60) {
$varName=$filterOps[$op][0];
$value=$item[2];
$variablesArray[$varName]=$value;
continue;
}
if ( ($op & 0xf0) == 0x80) {
$varName=$filterOps[$op][0];
$value=$item[3];
$operand=$item[2];
$variablesArray[$varName.'_select']=$value;
$variablesArray[$varName.'_op']=$operand;
continue;
}
if ( $op == 0x20) {
/* [0] => 32
[1] => FILTER_DATE
[2] => between
[3] => 2028
[4] => 5
[5] => 6
[6] => 2028
[7] => 5
[8] => 7 */
$operand=$item[2];
if ($operand=='between') {
$variablesArray['FILTER_dateType']='DATE_RANGE';
$variablesArray['FILTER_from_day_text']= sprintf("%02d.%02d.%04d",$item[5],$item[4],$item[3]); // 23.02.2010
$variablesArray['FILTER_to_day_text']=sprintf("%02d.%02d.%04d",$item[8],$item[7],$item[6]);
} else if ( $item[3] && $item[4]) {
$variablesArray['FILTER_dateType']='MONTH_YEAR';
$variablesArray['FILTER_MONTH_YEAR_select_op']= $operand;
$variablesArray['FILTER_MONTH_YEAR_select_YEAR']= $item[3];
$variablesArray['FILTER_MONTH_YEAR_select_MONTH']= $item[4];
} else if ( $item[3] ) {
$variablesArray['FILTER_dateType']='YEAR';
$variablesArray['FILTER_YEAR_select_op']= $operand;
$variablesArray['FILTER_YEAR_select']= $item[3];
}else {
}
continue;
}
if ( $op == 0x21) {
if ($operand=='WEEKEND'){
$variablesArray['FILTER_WEEKEND']= $operand;
}
}
if ( $op == 0x22) {
/* [0] => 32
[1] => FILTER_PilotBirthdate
[2] => >=
[3] => 2028
[4] => 5
[5] => 6
[6] => 0
[7] => 0
[8] => 0 */
$operand=$item[2];
$variablesArray['FILTER_PilotBirthdate_op']= $operand;
$variablesArray['FILTER_PilotBirthdate_text']= sprintf("%02d.%02d.%04d",$item[5],$item[4],$item[3]); // 23.02.2010
continue;
}
} // foreach
// done if $variablesArray['FILTER_dateType']='ALL'; is not set then set it hereL
if (!$variablesArray['FILTER_dateType']) $variablesArray['FILTER_dateType']='ALL';
// echo "<PRE>"; print_r($variablesArray); echo "</PRE>";
}
function makeFilterString() {
global $filterOps;
global $pilotsTable,$flightsTable;
$filterStr='';
foreach ($this->filterArray as $i=>$item) {
$op=$item[0];
$opName=$item[1];
if ( ($op & 0xf0) == 0x40 ) {// multi values
//print_r($item);
$filterStr.=sprintf("%02X",$op);
if ($op==0x45) $filterStr.=$item[3].'_';
foreach($item[2] as $item1) {
$filterStr.=$item1.'_';
}
$filterStr=substr($filterStr,0,-1).'.';
continue;
}
if ( ($op & 0xf0) == 0x60) {
$filterStr.=sprintf("%02X%04X",$op,$item[2]);
continue;
}
if ( ($op & 0xf0) == 0x80) {
$operand=$item[2];
if ( $operand=='=') $operand=0;
else if ( $operand=='<=') $operand=1;
else if ( $operand=='>=') $operand=2;
$operand=($operand<<14 )&0xC000;
$value= $item[3]&0x3fff;
$filterStr.=sprintf("%02X%04X",$op,$operand|$value);
continue;
}
if ( ($op & 0xf0) == 0x20) {
/*
[0] => 32
[1] => FILTER_DATE
[2] => between
[3] => 2028
[4] => 5
[5] => 6
[6] => 2028
[7] => 5
[8] => 7
*/
$operand=$item[2];
if ( $operand=='=') $operand=0;
else if ( $operand=='<=') $operand=1;
else if ( $operand=='>=') $operand=2;
else if ( $operand=='between') $operand=3;
$operand=($operand<<22 )&0x00C00000;
$year = ( ( $item[3]-1900) << 9 ) | 0x00000000 ;
$month= ($item[4] << 5 ) | 0x00000000 ;
$day = $item[5] | 0x00000000 ;
$filterStr.=sprintf("%02X%06X",$op,$operand|$year|$month|$day);
if ( $item[2]=='between') {
$year = ( ( $item[6]-1900) << 9 ) | 0x00000000 ;
$month= ($item[7] << 5 ) | 0x00000000 ;
$day = $item[8] | 0x00000000 ;
$filterStr.=sprintf("%06X",$year|$month|$day);
}
/* if ( $item[2]=='WEEKEND') {
$year = ( ( $item[6]-1900) << 9 ) | 0x00000000 ;
$month= ($item[7] << 5 ) | 0x00000000 ;
$day = $item[8] | 0x00000000 ;
$filterStr.=sprintf("%06X",$year|$month|$day);
} */
continue;
}
}
if ( ($op & 0xf0) == 0x21) {
//$filterStr.=sprintf("%06X",$year|$month|$day);
}
return $filterStr;
}
function makeClause() {
global $filterOps;
global $pilotsTable,$flightsTable;
$filter_clause="";
$filterTextual="";
$nacclub_clauses=array();
$AND_clauses=array();
// print_r($this->filterArray);
foreach ($this->filterArray as $i=>$item) {
$op=$item[0];
$opName=$item[1];
if ($opName=='FILTER_DATE') {
$opType=$item[2];
if ($opType=="between"){ // RANGE
$date1=sprintf("%04d%02d%02d",$item[3],$item[4],$item[5]);
$date2=sprintf("%04d%02d%02d",$item[6],$item[7],$item[8]);
$filter_clause.=" AND ( DATE_FORMAT(DATE,'%Y%m%d') >= '$date1' AND DATE_FORMAT(DATE,'%Y%m%d') <= '$date2' ) ";
$date1=sprintf("%04d.%02d%.02d",$item[3],$item[4],$item[5]);
$date2=sprintf("%04d.%02d.%02d",$item[6],$item[7],$item[8]);
$filterTextual.=" AND The date is >= $date1 and <= $date2 \n";
} else if ( $item[3] && !$item[4] && !$item[5] ) {
$filter_clause.=" AND DATE_FORMAT(DATE,'%Y') $opType '".$item[3]."' ";
$filterTextual.=" AND The Year is $opType ".$item[3]." \n";
} else if ( $item[3] && $item[4] ) {
$filter_clause.=" AND ( DATE_FORMAT(DATE,'%Y%m') $opType '".sprintf("%04d%02d",$item[3],$item[4])."' ) ";
$filterTextual.=" AND The date $opType ".sprintf("%04d.%02d",$item[3],$item[4])." \n";
}
continue;
}
if ($opName=="FILTER_WEEKEND"){
$filter_clause.=" AND (DAYOFWEEK(DATE)=1 OR DAYOFWEEK(DATE)=7) ";
$filterTextual.=" AND at the weekend";
}
if ($opName=='FILTER_PilotBirthdate') {
$opType=$item[2];
//$date1=sprintf("%02d.%02d.%04d",$item[5],$item[4],$item[3]);
//$date2=sprintf("%02d.%02d.%04d",$item[8],$item[7],$item[6]);
//Changed 20.11.14 P. Wild
$date1=sprintf("%04d-%02d-%02d",$item[3],$item[4],$item[5]);
$date2=sprintf("%04d-%02d-%02d",$item[6],$item[7],$item[8]);
if ($opType=="between"){ // RANGE
$filter_clause.=" AND ( $pilotsTable.Birthdate >= '$date1' AND $pilotsTable.Birthdate <= '$date2' ) ";
} else {
$filter_clause.=" AND $pilotsTable.Birthdate $opType '$date1' ";
}
$filter_clause.=" AND $pilotsTable.Birthdate <> '' ";
continue;
}
if ( ($op & 0xf0) == 0x40 ) {// multi values
//print_r($item);
$clause='';
$in_string='';
$filterTextual.="<li>".$filterOps[$op][1].": ";
foreach($item[2] as $item1) {
$in_string.="'$item1',";
$filterTextual.="$item1 ";
}
$in_string=substr($in_string,0,-1);
$filterTextual.="\n";
$clause= $filterOps[$op][2].' IN ('.$in_string.') ';
if ($filterOps[$op][1]=='nacclub') {
$clause=" ($flightsTable.NACid=".$item[3]." AND $clause)";
$nacclub_clauses[]='('.$clause.')';
} else {
$AND_clauses[]=$clause;
}
continue;
}
if ($opName=='FILTER_sex' && $item[2]) {
if ($item[2]==2) $val="F";
else $val="M";
$filter_clause.=" AND $pilotsTable.Sex='".$val."' ";
$filterTextual.="<li>Pilot Gender is ".(($item[2]!=2) ? "Male":"Female")."\n";
continue;
}
if ($opName=='FILTER_olc_type') {
$olc_type=$item[2];
if ($olc_type==1) $olc_type="FREE_FLIGHT";
if ($olc_type==2) $olc_type="FREE_TRIANGLE";
if ($olc_type==4) $olc_type="FAI_TRIANGLE";
$filter_clause.=" AND BEST_FLIGHT_TYPE='".$olc_type."' ";
$filterTextual.="<li>Type of XC flight is ".str_replace("_"," ",$olc_type)."\n";
continue;
}
if ($opName=='FILTER_cat') {
$filter_clause.=" AND $flightsTable.cat & ".$item[2]." ";
continue;
}
if ($opName=='FILTER_class') {
$filter_clause.=" AND $flightsTable.category = ".$item[2]." ";
continue;
}
if ($opName=='FILTER_glider_cert') {
$filter_clause.=" AND $flightsTable.gliderCertCategory & ".$item[2]." ";
continue;
}
if ($opName=='FILTER_start_type') {
$filter_clause.=" AND $flightsTable.startType & ".$item[2]." ";
continue;
}
if ($opName=='FILTER_linear_distance') {
$filter_clause.=" AND LINEAR_DISTANCE ".$item[2]." ".($item[3]*1000)." ";
$filterTextual.="<li>Straight Distance ".$item[2]." ".$item[3]." \n";
}
if ($opName=='FILTER_olc_distance') {
$filter_clause.=" AND FLIGHT_KM ".$item[2]." ".($item[3]*1000)." ";
$filterTextual.="<li>XC km ".$item[2]." ".$item[3]." \n";
}
if ($opName=='FILTER_olc_score') {
$filter_clause.=" AND FLIGHT_POINTS ".$item[2]." ".$item[3]." ";
$filterTextual.="<li>XC SCORE ".$item[2]." ".$item[3]." \n";
}
if ($opName=='FILTER_duration') {
$filter_clause.=" AND DURATION ".$item[2]." ".($item[3]*60)." ";
$filterTextual.="<li>Flight Duration ".$item[2]." ".sprintf("%2d:%02d",$item[3]/60,$item[3]%60)." \n";
}
} // foreach
//print_r($AND_clauses);
//print_r($nacclub_clauses);
if (count($nacclub_clauses)>0) {
$AND_clauses[]='('.implode(' OR ', $nacclub_clauses).')';
}
if (count($AND_clauses)>0) {
$filter_clause.=' AND '.implode(' AND ', $AND_clauses);
}
$this->filterTextual=$filterTextual;
//echo "<HR>SQL: $filter_clause <BR>";
return $filter_clause;
} // function
}
function array_search_value($needle,$haystack,$arraykey=FALSE) {
if (!is_array($haystack) ) return false;
foreach($haystack as $key=>$value) {
$current_key=$key;
if($arraykey){
if($needle == $value[$arraykey]){
return $value['id'];
}
if(array_search_value($needle,$value[$arraykey]) == true) {
return $current_key;
}
}else{
if($needle == $value) return $value;
if(array_search_value($needle,$value) == true) {
return $current_key;
}
}
}
return false;
}
?>