/
BugFilterQuery.class.php
1211 lines (1068 loc) · 44.9 KB
/
BugFilterQuery.class.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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class BugFilterQuery extends DbQuery {
const QUERY_TYPE_LIST = 0;
const QUERY_TYPE_COUNT = 1;
const QUERY_TYPE_IDS = 2;
const QUERY_TYPE_DISTINCT_IDS = 3;
# properties used to build the query
public $filter;
public $project_id;
public $user_id;
public $use_sticky;
# internal storage for intermediate data
protected $parts_select = array();
protected $parts_from = array();
protected $parts_join = array();
protected $parts_where = array(); # these are joined by the filter operator
protected $parts_order = array();
protected $fixed_where = array(); # these are joined always by AND
protected $filter_operator;
# runtime variables for building the filter query
protected $rt_stop_build;
protected $rt_included_projects;
protected $rt_table_alias_cf;
protected $table_alias_bugnote = null;
/**
* $p_config can be either:
* - A single type constant, for easy object creation using default attributes
* - An array of options, for more advanced configuration.
*
* Option array uses "option => value" pairs, supported as:
* - 'query_type': Any of QUERY_TYPE_xxx class constants, meaning.
* QUERY_TYPE_LIST, query listing all fields of matched bugs. This is the default.
* QUERY_TYPE_COUNT, query to return number of matched bugs.
* QUERY_TYPE_IDS, query to return only matched bug ids, which may not
* be unique, but may be faster and convenient for use as a subquery.
* QUERY_TYPE_DISTINCT_IDS, query to return unique matched bug ids .
* - 'project_id': (integer) A project id to be used, if needed by the filer. By default,
* current project is used.
* - 'user_id': (integer) A user id to be used to determine visibility for the filter.
* By default current user is used.
* - 'use_sticky': (boolean) Whether to allow returning the bug list sorted so that sticky
* bugs are placed first in the result order. This is false by default.
*
* @param type $p_filter
* @param type $p_config
*/
public function __construct( $p_filter, $p_config = self::QUERY_TYPE_LIST ) {
# defaults
$t_query_type = self::QUERY_TYPE_LIST;
$this->filter = $p_filter;
$this->use_sticky = false;
$this->project_id = helper_get_current_project();
$this->user_id = auth_get_current_user_id();
# $p_config can be an array or an integer
if( is_array( $p_config ) ) {
foreach( $p_config as $t_option => $t_value ) {
if( null === $t_value ) {
continue;
}
switch( $t_option ) {
case 'query_type':
$t_query_type = (int)$t_value;
break;
case 'project_id':
$this->project_id = (int)$t_value;
break;
case 'user_id':
$this->user_id = (int)$t_value;
break;
case 'use_sticky':
$this->use_sticky = (boolean)$t_value;
break;
}
}
} else {
$t_query_type = $p_config;
}
# The query string must be built here to have a valid DbQuery object ready for use
$this->build_main();
$this->set_query_type( $t_query_type );
}
public function set_query_type( $p_query_type ) {
switch( $p_query_type ) {
case self::QUERY_TYPE_COUNT:
$this->sql( $this->string_query_count() );
break;
case self::QUERY_TYPE_IDS:
$this->sql( $this->string_query_ids() );
break;
case self::QUERY_TYPE_DISTINCT_IDS:
$this->sql( $this->string_query_dinstinct_ids() );
break;
case self::QUERY_TYPE_LIST:
default:
$this->sql( $this->string_query_list() );
break;
}
$this->db_result = null;
}
public function get_bug_count() {
# create a copy from current query
$t_query_count = clone $this;
# rebuild clauses for count query type
$t_query_count->set_query_type( self::QUERY_TYPE_COUNT );
# set defaults
$t_query_count->set_limit();
$t_query_count->set_offset();
$t_query_count->execute();
return $t_query_count->value();
}
public function add_select( $p_string ) {
$this->parts_select[] = $p_string;
}
public function add_from( $p_string ) {
$this->parts_from[] = $p_string;
}
public function add_join( $p_string ) {
$this->parts_join[] = $p_string;
}
public function add_where( $p_string ) {
$this->parts_where[] = $p_string;
}
public function add_fixed_where( $p_string ) {
$this->fixed_where[] = $p_string;
}
public function add_order( $p_string ) {
$this->parts_order[] = $p_string;
}
protected function helper_string_query_inner() {
$t_from_string = ' FROM ' . implode( ', ', $this->parts_from );
$t_join_string = count( $this->parts_join ) > 0 ? ' ' . implode( ' ', $this->parts_join ) : '';
$t_where_string = ' WHERE '. implode( ' AND ', $this->fixed_where );
if( count( $this->parts_where ) > 0 ) {
$t_where_string .= ' AND ( ';
$t_where_string .= implode( $this->filter_operator, $this->parts_where );
$t_where_string .= ' )';
}
return $t_from_string . $t_join_string . $t_where_string;
}
protected function string_query_list() {
if( empty( $this->parts_order ) ) {
$this->build_order_by();
}
$t_select_string = 'SELECT DISTINCT ' . implode( ', ', $this->parts_select );
$t_order_string = ' ORDER BY ' . implode( ', ', $this->parts_order );
return $t_select_string . $this->helper_string_query_inner() . $t_order_string;
}
protected function string_query_count() {
$t_select_string = 'SELECT COUNT( DISTINCT {bug}.id )';
return $t_select_string . $this->helper_string_query_inner();
}
protected function string_query_ids() {
$t_select_string = 'SELECT {bug}.id';
return $t_select_string . $this->helper_string_query_inner();
}
protected function string_query_dinstinct_ids() {
$t_select_string = 'SELECT DISTINCT {bug}.id';
return $t_select_string . $this->helper_string_query_inner();
}
protected function build_main() {
$this->rt_stop_build = false;
$this->add_from( '{bug}' );
$this->add_select( '{bug}.*' );
if( $this->filter[FILTER_PROPERTY_MATCH_TYPE] == FILTER_MATCH_ANY ) {
$this->filter_operator = ' OR ';
} else {
$this->filter_operator = ' AND ';
}
$this->build_projects();
# if no projects where found, stop here
if( $this->rt_stop_build ) {
return;
}
foreach( $this->filter as $t_prop => $t_value ) {
# These are the main entries for filter properties
switch( $t_prop ) {
case FILTER_PROPERTY_REPORTER_ID:
$this->build_prop_reporter();
break;
case FILTER_PROPERTY_HANDLER_ID:
$this->build_prop_handler();
break;
case FILTER_PROPERTY_MONITOR_USER_ID:
$this->build_prop_monitor_by();
break;
case FILTER_PROPERTY_NOTE_USER_ID:
$this->build_prop_note_by();
break;
case FILTER_PROPERTY_FILTER_BY_DATE_SUBMITTED:
$this->build_prop_date_created();
break;
case FILTER_PROPERTY_FILTER_BY_LAST_UPDATED_DATE:
$this->build_prop_date_updated();
break;
case FILTER_PROPERTY_BUILD:
$this->build_prop_build();
break;
case FILTER_PROPERTY_VERSION:
$this->build_prop_version();
break;
case FILTER_PROPERTY_FIXED_IN_VERSION:
$this->build_prop_fixed_version();
break;
case FILTER_PROPERTY_TARGET_VERSION:
$this->build_prop_target_version();
break;
case FILTER_PROPERTY_VIEW_STATE:
$this->build_prop_view_state();
break;
case FILTER_PROPERTY_CATEGORY_ID:
$this->build_prop_category();
break;
case FILTER_PROPERTY_SEVERITY:
$this->build_prop_severity();
break;
case FILTER_PROPERTY_RESOLUTION:
$this->build_prop_resolution();
break;
case FILTER_PROPERTY_PRIORITY:
$this->build_prop_priority();
break;
case FILTER_PROPERTY_PROFILE_ID:
$this->build_prop_profile();
break;
case FILTER_PROPERTY_PLATFORM:
$this->build_prop_platform();
break;
case FILTER_PROPERTY_OS:
$this->build_prop_os();
break;
case FILTER_PROPERTY_OS_BUILD:
$this->build_prop_os_build();
break;
case FILTER_PROPERTY_SEARCH:
$this->build_prop_search();
break;
case FILTER_PROPERTY_RELATIONSHIP_TYPE:
$this->build_prop_relationship();
break;
}
}
# these have several properties that must be built only once
if( isset( $this->filter[FILTER_PROPERTY_TAG_STRING] )
|| isset( $this->filter[FILTER_PROPERTY_TAG_SELECT] ) ) {
$this->build_prop_tags();
}
if( isset( $this->filter['custom_fields'] ) ) {
$this->build_prop_custom_fields();
}
if( isset( $this->filter[FILTER_PROPERTY_HIDE_STATUS] )
|| isset( $this->filter[FILTER_PROPERTY_STATUS] )) {
$this->build_prop_status();
}
$this->build_prop_plugin_filters();
$this->parts_select = array_unique( $this->parts_select );
$this->parts_from = array_unique( $this->parts_from );
$this->parts_join = array_unique( $this->parts_join );
$this->parts_order = array_unique( $this->parts_order );
}
protected function build_projects() {
$this->add_join( 'JOIN {project} ON {project}.id = {bug}.project_id' );
$this->add_fixed_where( '{project}.enabled =' . $this->param( true ) );
$t_user_id = $this->user_id;
$t_project_id = $this->project_id;
$t_projects_query_required = true;
$t_included_project_ids = filter_get_included_projects( $this->filter, $t_project_id, $t_user_id, true /* return ALL_PROJECTS */ );
if( ALL_PROJECTS == $t_included_project_ids ) {
# The list of expanded projects is needed later even if project_query is not required
$t_included_project_ids = filter_get_included_projects( $this->filter, $t_project_id, $t_user_id, false /* return ALL_PROJECTS */ );
# this special case can skip the projects query clause:
if( user_is_administrator( $t_user_id ) ) {
log_event( LOG_FILTERING, 'all projects + administrator, hence no project filter.' );
$t_projects_query_required = false;
}
}
$this->rt_included_projects = $t_included_project_ids;
if( $t_projects_query_required ) {
# if no projects are accessible, then stop here
if( count( $t_included_project_ids ) == 0 ) {
log_event( LOG_FILTERING, 'no accessible projects' );
$this->add_fixed_where( '{project}.id =' . $this->param( 0 ) );
$this->rt_stop_build = true;
return;
}
$t_limit_reporters = config_get( 'limit_reporters' );
# this array is to be populated with project ids for which we only want to show public issues. This is due to the limited
# access of the current user.
$t_public_only_project_ids = array();
# this array is populated with project ids that the current user has full access to.
$t_private_and_public_project_ids = array();
# this array is populated with projects where the user can view only bugs for which he is the reporter user
$t_limit_reporter_project_ids = array();
# make sure the project rows are cached, as they will be used to check access levels.
project_cache_array_rows( $t_included_project_ids );
foreach( $t_included_project_ids as $t_pid ) {
if( ( ON === $t_limit_reporters ) && ( !access_has_project_level( access_threshold_min_level( config_get( 'report_bug_threshold', null, $t_user_id, $t_pid ) ) + 1, $t_pid, $t_user_id ) ) ) {
# project is limited, only view own reported bugs
$t_limit_reporter_project_ids[] = $t_pid;
# as we will check the user is reporter for each bug, and reporter can view his own private bugs, there's no need to check for private bug access
continue;
}
$t_access_required_to_view_private_bugs = config_get( 'private_bug_threshold', null, null, $t_pid );
if( access_has_project_level( $t_access_required_to_view_private_bugs, $t_pid, $t_user_id ) ) {
$t_private_and_public_project_ids[] = $t_pid;
} else {
$t_public_only_project_ids[] = $t_pid;
}
}
log_event( LOG_FILTERING, 'project_ids (with access to public/private issues) = @P' . implode( ', @P', $t_private_and_public_project_ids ) );
log_event( LOG_FILTERING, 'project_ids (with access limited to public issues) = @P' . implode( ', @P', $t_public_only_project_ids ) );
log_event( LOG_FILTERING, 'project_ids (with access limited to own issues) = @P' . implode( ', @P', $t_limit_reporter_project_ids ) );
$t_query_projects_or = array();
# for projects with total visibility
if( !empty( $t_private_and_public_project_ids ) ) {
$t_query_projects_or[] = $this->sql_in( '{bug}.project_id', $t_private_and_public_project_ids );
}
# for projects with public visibility, public issues can be shown
if( !empty( $t_public_only_project_ids ) ) {
$t_query_projects_or[] = $this->sql_in( '{bug}.project_id', $t_public_only_project_ids ) . ' AND {bug}.view_state = ' . $this->param( VS_PUBLIC );
}
# for projects with public visibility, the issue can be shown if the user is the reporter, regardless of public/private issue
# also, for projects limited to reporters, the same condition applies
# combine both arrays for this condition
$t_projects_for_reporter_visibility = array_merge( $t_public_only_project_ids, $t_limit_reporter_project_ids );
if( !empty( $t_projects_for_reporter_visibility ) ) {
$t_query_projects_or[] = $this->sql_in( '{bug}.project_id', $t_projects_for_reporter_visibility ) . ' AND {bug}.reporter_id = ' . $this->param( $t_user_id );
}
$t_project_query = '(' . implode( ' OR ', $t_query_projects_or ) . ')';
log_event( LOG_FILTERING, 'project query = ' . $t_project_query );
$this->add_fixed_where( $t_project_query );
}
}
protected function build_prop_status() {
# take a list of all available statuses then remove the ones that we want hidden, then make sure
# the ones we want shown are still available
$t_desired_statuses = $this->filter[FILTER_PROPERTY_STATUS];
# simple filtering: restrict by the hide status value if present
if( FILTER_VIEW_TYPE_SIMPLE == $this->filter['_view_type'] ) {
if( isset( $this->filter[FILTER_PROPERTY_HIDE_STATUS][0] ) && !filter_field_is_none( $this->filter[FILTER_PROPERTY_HIDE_STATUS][0] ) ) {
$t_selected_status_array = $this->filter[FILTER_PROPERTY_STATUS];
# if we have metavalue for "any", expand to all status, to filter them
if( filter_field_is_any( $t_selected_status_array ) ) {
$t_selected_status_array = MantisEnum::getValues( config_get( 'status_enum_string' ) );
}
$t_hide_status = $this->filter[FILTER_PROPERTY_HIDE_STATUS][0];
# Filter out status that must be hidden
$t_desired_statuses = array();
foreach( $t_selected_status_array as $t_this_status ) {
if( $t_hide_status > $t_this_status ) {
$t_desired_statuses[] = $t_this_status;
}
}
}
}
# advanced filtering: ignore hide_status, do nothing.
# if show_status is "any", empty the array, to not include any condition on status.
if( filter_field_is_any( $t_desired_statuses ) ) {
$t_desired_statuses = array();
}
if( count( $t_desired_statuses ) > 0 ) {
$t_clauses = $this->helper_array_map_int( $t_desired_statuses );
$this->add_where( $this->sql_in( '{bug}.status', $t_clauses ) );
}
}
protected function build_prop_date_created() {
if( ( gpc_string_to_bool( $this->filter[FILTER_PROPERTY_FILTER_BY_DATE_SUBMITTED] ) )
&& is_numeric( $this->filter[FILTER_PROPERTY_DATE_SUBMITTED_START_MONTH] )
&& is_numeric( $this->filter[FILTER_PROPERTY_DATE_SUBMITTED_START_DAY] )
&& is_numeric( $this->filter[FILTER_PROPERTY_DATE_SUBMITTED_START_YEAR] )
&& is_numeric( $this->filter[FILTER_PROPERTY_DATE_SUBMITTED_END_MONTH] )
&& is_numeric( $this->filter[FILTER_PROPERTY_DATE_SUBMITTED_END_DAY] )
&& is_numeric( $this->filter[FILTER_PROPERTY_DATE_SUBMITTED_END_YEAR] )
) {
$t_start_string = $this->filter[FILTER_PROPERTY_DATE_SUBMITTED_START_YEAR]
. '-' . $this->filter[FILTER_PROPERTY_DATE_SUBMITTED_START_MONTH]
. '-' . $this->filter[FILTER_PROPERTY_DATE_SUBMITTED_START_DAY]
. ' 00:00:00';
$t_end_string = $this->filter[FILTER_PROPERTY_DATE_SUBMITTED_END_YEAR]
. '-' . $this->filter[FILTER_PROPERTY_DATE_SUBMITTED_END_MONTH]
. '-' . $this->filter[FILTER_PROPERTY_DATE_SUBMITTED_END_DAY]
. ' 23:59:59';
$t_query_created_at = '{bug}.date_submitted BETWEEN '
. $this->param( strtotime( $t_start_string ) ) . ' AND '
. $this->param( strtotime( $t_end_string ) ) ;
$this->add_fixed_where( $t_query_created_at );
}
}
protected function build_prop_date_updated() {
if( ( gpc_string_to_bool( $this->filter[FILTER_PROPERTY_FILTER_BY_LAST_UPDATED_DATE] ) )
&& is_numeric( $this->filter[FILTER_PROPERTY_LAST_UPDATED_START_MONTH] )
&& is_numeric( $this->filter[FILTER_PROPERTY_LAST_UPDATED_START_DAY] )
&& is_numeric( $this->filter[FILTER_PROPERTY_LAST_UPDATED_START_YEAR] )
&& is_numeric( $this->filter[FILTER_PROPERTY_LAST_UPDATED_END_MONTH] )
&& is_numeric( $this->filter[FILTER_PROPERTY_LAST_UPDATED_END_DAY] )
&& is_numeric( $this->filter[FILTER_PROPERTY_LAST_UPDATED_END_YEAR] )
) {
$t_start_string = $this->filter[FILTER_PROPERTY_LAST_UPDATED_START_YEAR]
. '-' . $this->filter[FILTER_PROPERTY_LAST_UPDATED_START_MONTH]
. '-' . $this->filter[FILTER_PROPERTY_LAST_UPDATED_START_DAY]
. ' 00:00:00';
$t_end_string = $this->filter[FILTER_PROPERTY_LAST_UPDATED_END_YEAR]
. '-' . $this->filter[FILTER_PROPERTY_LAST_UPDATED_END_MONTH]
. '-' . $this->filter[FILTER_PROPERTY_LAST_UPDATED_END_DAY]
. ' 23:59:59';
$t_query_updated_at = '{bug}.last_updated BETWEEN '
. $this->param( strtotime( $t_start_string ) ) . ' AND '
. $this->param( strtotime( $t_end_string ) ) ;
$this->add_fixed_where( $t_query_updated_at );
}
}
protected function build_prop_view_state() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_VIEW_STATE] ) ) {
return;
}
$t_view_state = (int)$this->filter[FILTER_PROPERTY_VIEW_STATE];
$t_view_state_query = '{bug}.view_state = ' . $this->param( $t_view_state ) ;
log_event( LOG_FILTERING, 'view_state query = ' . $t_view_state_query );
$this->add_where( $t_view_state_query );
}
protected function helper_process_users_property( $p_users_array ) {
$t_new_array = array();
foreach( $p_users_array as $t_user ) {
if( filter_field_is_none( $t_user ) ) {
$t_new_array[] = 0;
} else {
$c_user_id = (int)$t_user;
if( filter_field_is_myself( $c_user_id ) ) {
$t_new_array[] = $this->user_id;
} else {
$t_new_array[] = $c_user_id;
}
}
}
return $t_new_array;
}
protected function build_prop_reporter() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_REPORTER_ID] ) ) {
return;
}
$t_user_ids = $this->helper_process_users_property( $this->filter[FILTER_PROPERTY_REPORTER_ID] );
$t_users_query = $this->sql_in( '{bug}.reporter_id', $t_user_ids );
log_event( LOG_FILTERING, 'reporter query = ' . $t_users_query );
$this->add_where( $t_users_query );
}
protected function build_prop_handler() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_HANDLER_ID] ) ) {
return;
}
$t_user_ids = $this->helper_process_users_property( $this->filter[FILTER_PROPERTY_HANDLER_ID] );
$t_users_query = $this->sql_in( '{bug}.handler_id', $t_user_ids );
log_event( LOG_FILTERING, 'handler query = ' . $t_users_query );
$this->add_where( $t_users_query );
}
protected function build_prop_category() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_CATEGORY_ID] ) ) {
return;
}
$t_names = array();
$t_use_none = false;
foreach( $this->filter[FILTER_PROPERTY_CATEGORY_ID] as $t_filter_member ) {
if( filter_field_is_none( $t_filter_member ) ) {
$t_use_none = true;
} else {
$t_names[] = $t_filter_member;
}
}
$t_query_or = array();
if( !empty( $t_names ) ) {
$t_subselect = 'SELECT id FROM {category} WHERE ' . $this->sql_in( 'name', $t_names );
$t_query_names = '{bug}.category_id IN ( '. $t_subselect . ')';
$t_query_or[] = $t_query_names;
}
if( $t_use_none ) {
$t_query_or[] = '{bug}.category_id = ' . $this->param( 0 );
}
$t_query_category = '(' . implode( ' OR ', $t_query_or ) . ')';
log_event( LOG_FILTERING, 'category query = ' . $t_query_category );
$this->add_where( $t_query_category );
}
protected function build_prop_severity() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_SEVERITY] ) ) {
return;
}
$t_query = $this->sql_in( '{bug}.severity', $this->filter[FILTER_PROPERTY_SEVERITY] );
$this->add_where( $t_query );
}
protected function build_prop_resolution() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_RESOLUTION] ) ) {
return;
}
$t_query = $this->sql_in( '{bug}.resolution', $this->filter[FILTER_PROPERTY_RESOLUTION] );
$this->add_where( $t_query );
}
protected function build_prop_priority() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_PRIORITY] ) ) {
return;
}
$t_query = $this->sql_in( '{bug}.priority', $this->filter[FILTER_PROPERTY_PRIORITY] );
$this->add_where( $t_query );
}
protected function helper_process_string_property( $p_array ) {
$t_new_array = array();
foreach( $p_array as $t_elem ) {
if( filter_field_is_none( $t_elem ) ) {
$t_new_array[] = '';
} else {
$t_new_array[] = $t_elem;
}
}
return $t_new_array;
}
protected function build_prop_build() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_BUILD] ) ) {
return;
}
$t_array = $this->helper_process_string_property( $this->filter[FILTER_PROPERTY_BUILD] );
$t_query = $this->sql_in( '{bug}.build', $t_array );
$this->add_where( $t_query );
}
protected function build_prop_version() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_VERSION] ) ) {
return;
}
$t_array = $this->helper_process_string_property( $this->filter[FILTER_PROPERTY_VERSION] );
$t_query = $this->sql_in( '{bug}.version', $t_array );
$this->add_where( $t_query );
}
protected function helper_process_id_property( $p_array ) {
$t_new_array = array();
foreach( $p_array as $t_elem ) {
if( filter_field_is_none( $t_elem ) ) {
$t_new_array[] = 0;
} else {
$t_new_array[] = $t_elem;
}
}
return $t_new_array;
}
protected function build_prop_profile() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_PROFILE_ID] ) ) {
return;
}
$t_array = $this->helper_process_id_property( $this->filter[FILTER_PROPERTY_PROFILE_ID] );
$t_query = $this->sql_in( '{bug}.profile_id', $t_array );
$this->add_where( $t_query );
}
protected function build_prop_platform() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_PLATFORM] ) ) {
return;
}
$t_array = $this->helper_process_string_property( $this->filter[FILTER_PROPERTY_PLATFORM] );
$t_query = $this->sql_in( '{bug}.platform', $t_array );
$this->add_where( $t_query );
}
protected function build_prop_os() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_OS] ) ) {
return;
}
$t_array = $this->helper_process_string_property( $this->filter[FILTER_PROPERTY_OS] );
$t_query = $this->sql_in( '{bug}.os', $t_array );
$this->add_where( $t_query );
}
protected function build_prop_os_build() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_OS_BUILD] ) ) {
return;
}
$t_array = $this->helper_process_string_property( $this->filter[FILTER_PROPERTY_OS_BUILD] );
$t_query = $this->sql_in( '{bug}.os_build', $t_array );
$this->add_where( $t_query );
}
protected function build_prop_fixed_version() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_FIXED_IN_VERSION] ) ) {
return;
}
$t_array = $this->helper_process_string_property( $this->filter[FILTER_PROPERTY_FIXED_IN_VERSION] );
$t_query = $this->sql_in( '{bug}.fixed_in_version', $t_array );
$this->add_where( $t_query );
}
protected function build_prop_target_version() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_TARGET_VERSION] ) ) {
return;
}
$t_array = $this->helper_process_string_property( $this->filter[FILTER_PROPERTY_TARGET_VERSION] );
$t_query = $this->sql_in( '{bug}.target_version', $t_array );
$this->add_where( $t_query );
}
protected function build_prop_monitor_by() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_MONITOR_USER_ID] ) ) {
return;
}
$t_user_ids = $this->helper_process_users_property( $this->filter[FILTER_PROPERTY_MONITOR_USER_ID] );
$t_use_none = ( in_array( 0, $t_user_ids ) );
# @TODO can this user view monitoring users?
$this->add_join( 'LEFT JOIN {bug_monitor} ON {bug}.id = {bug_monitor}.bug_id' );
if( $t_use_none ) {
$t_expr = 'COALESCE( {bug_monitor}.user_id, 0 )';
} else {
$t_expr = '{bug_monitor}.user_id';
}
$t_where = $this->sql_in( $t_expr, $t_user_ids );
$this->add_where( $t_where );
}
protected function helper_table_alias_for_bugnote() {
if( $this->table_alias_bugnote ) {
return $this->table_alias_bugnote;
}
# Build a condition for determining note visibility, the user can view:
# - public notes
# - his own private notes
# - private notes if meets access level for 'private_bugnote_threshold'
$t_projects_can_view_private = $this->helper_filter_projects_using_access( 'private_bugnote_threshold' );
$t_table_alias = 'visible_bugnote';
if( ALL_PROJECTS == $t_projects_can_view_private ) {
$t_view_condition = null;
} else {
$t_view_condition = $t_table_alias . '.view_state = ' . $this->param( VS_PUBLIC )
. ' OR ' . $t_table_alias . '.reporter_id = ' . $this->param( $this->user_id );
if( !empty( $t_projects_can_view_private ) ) {
$t_view_condition .= ' OR ' . $this->sql_in( '{bug}.project_id', $t_projects_can_view_private );
}
}
if( $t_view_condition ) {
$t_view_condition = ' AND (' . $t_view_condition . ')';
}
$t_join = 'LEFT JOIN {bugnote} ' . $t_table_alias
. ' ON {bug}.id = ' . $t_table_alias . '.bug_id'
. $t_view_condition;
$this->add_join( $t_join );
$this->table_alias_bugnote = $t_table_alias;
return $this->table_alias_bugnote;
}
protected function build_prop_note_by() {
if( filter_field_is_any( $this->filter[FILTER_PROPERTY_NOTE_USER_ID] ) ) {
return;
}
$t_user_ids = $this->helper_process_users_property( $this->filter[FILTER_PROPERTY_NOTE_USER_ID] );
$t_use_none = ( in_array( 0, $t_user_ids ) );
$t_table_alias = $this->helper_table_alias_for_bugnote();
if( $t_use_none ) {
$t_alias = 'COALESCE( ' . $t_table_alias . '.reporter_id, 0 )';
} else {
$t_alias = $t_table_alias . '.reporter_id';
}
$t_where = $this->sql_in( $t_alias, $t_user_ids );
$this->add_where( $t_where );
}
protected function build_prop_relationship() {
$t_any_found = false;
$c_rel_type = $this->filter[FILTER_PROPERTY_RELATIONSHIP_TYPE];
$c_rel_bug = $this->filter[FILTER_PROPERTY_RELATIONSHIP_BUG];
if( -1 == $c_rel_type || 0 == $c_rel_bug ) {
return;
}
# use the complementary type
$t_comp_type = relationship_get_complementary_type( $c_rel_type );
$t_table_dst = 'rel_dst';
$t_table_src = 'rel_src';
$this->add_join( 'LEFT JOIN {bug_relationship} ' . $t_table_dst . ' ON ' . $t_table_dst . '.destination_bug_id = {bug}.id' );
$this->add_join( 'LEFT JOIN {bug_relationship} ' . $t_table_src . ' ON ' . $t_table_src . '.source_bug_id = {bug}.id' );
# get reverse relationships
$t_clauses = array();
$t_clauses[] = '(' . $t_table_dst . '.relationship_type=' . $this->param( $t_comp_type )
. ' AND ' . $t_table_dst . '.source_bug_id=' . $this->param( $c_rel_bug ) . ')';
$t_clauses[] = '(' . $t_table_src . '.relationship_type=' . $this->param( $c_rel_type )
. ' AND ' . $t_table_src . '.destination_bug_id=' . $this->param( $c_rel_bug ) . ')';
$this->add_where( '(' . implode( ' OR ', $t_clauses ) . ')' );
}
protected function helper_filter_projects_using_access( $p_access ) {
$t_filtered_projects = access_project_array_filter( $p_access, $this->rt_included_projects, $this->user_id );
$t_diff = array_diff( $this->rt_included_projects, $t_filtered_projects );
if( empty( $t_diff ) ) {
return ALL_PROJECTS;
} else {
return $t_filtered_projects;
}
}
protected function helper_array_map_int( $p_array ) {
$t_new_array = array();
foreach( $p_array as $t_elem ) {
$t_new_array[] = (int)$t_elem;
}
return $t_new_array;
}
protected function build_prop_tags() {
$c_tag_string = trim( $this->filter[FILTER_PROPERTY_TAG_STRING] );
$c_tag_select = (int)$this->filter[FILTER_PROPERTY_TAG_SELECT];
if( is_blank( $c_tag_string ) && $c_tag_select == 0 ) {
# shortcut exit
return;
}
$t_tags = tag_parse_filters( $c_tag_string );
if( empty( $t_tags ) && $c_tag_select == 0 ) {
# shortcut exit
return;
}
$t_projects_can_view_tags = $this->helper_filter_projects_using_access( 'tag_view_threshold' );
if( ALL_PROJECTS == $t_projects_can_view_tags ) {
$t_tag_projects_clause = '';
} else {
if( empty( $t_projects_can_view_tags ) ) {
# if cant view tags in any project, exit
log_event( LOG_FILTERING, 'tags query, no accesible projects ' );
return;
} else {
$t_tag_projects_clause = ' AND ' . $this->sql_in( '{bug}.project_id', $t_projects_can_view_tags );
log_event( LOG_FILTERING, 'tags query, accesible projects = @P' . implode( ', @P', $t_projects_can_view_tags ) );
}
}
$t_tags_always = array();
$t_tags_any = array();
$t_tags_never = array();
# @TODO, use constants for tag modifiers
foreach( $t_tags as $t_tag_row ) {
switch( $t_tag_row['filter'] ) {
case 1:
# A matched issue must always have this tag
$t_tags_always[] = $t_tag_row;
break;
case 0:
# A matched issue may have this tag
$t_tags_any[] = $t_tag_row;
break;
case -1:
# A matched must never have this tag
$t_tags_never[] = $t_tag_row;
break;
}
}
# Consider those tags that must always match, to also be part of those that can be
# optionally matched. This solves the scenario for an issue that matches one tag
# from the "always" group, and none from the "any" group.
if( !empty( $t_tags_always ) && !empty( $t_tags_any ) ) {
$t_tags_any = array_merge( $t_tags_any, $t_tags_always );
}
# Add the tag id to the array, from filter field "tag_select"
if( 0 < $c_tag_select && tag_exists( $c_tag_select ) ) {
$t_tags_any[] = tag_get( $c_tag_select );
}
$t_where = array();
if( count( $t_tags_always ) ) {
foreach( $t_tags_always as $t_tag_row ) {
$t_tag_alias = 'bug_tag_alias_alw_' . $t_tag_row['id'];
$t_join_inc = 'LEFT JOIN {bug_tag} ' . $t_tag_alias . ' ON ' . $t_tag_alias . '.bug_id = {bug}.id'
. ' AND ' . $t_tag_alias . '.tag_id = ' . $this->param( (int)$t_tag_row['id'] )
. $t_tag_projects_clause;
$this->add_join( $t_join_inc );
$t_where[] = $t_tag_alias . '.tag_id IS NOT NULL';
}
}
if( count( $t_tags_any ) ) {
$t_tag_alias = 'bug_tag_alias_any';
$t_tag_ids = $this->helper_array_map_int( array_column( $t_tags_any, 'id' ) );
$t_join_any = 'LEFT JOIN {bug_tag} ' . $t_tag_alias . ' ON ' . $t_tag_alias . '.bug_id = {bug}.id'
. ' AND ' . $this->sql_in( $t_tag_alias . '.tag_id', $t_tag_ids )
. $t_tag_projects_clause;
$this->add_join( $t_join_any );
$t_where[] = $t_tag_alias . '.tag_id IS NOT NULL';
}
if( count( $t_tags_never ) ) {
$t_tag_alias = 'bug_tag_alias_nev';
$t_tag_ids = $this->helper_array_map_int( array_column( $t_tags_never, 'id' ) );
$t_join_exc = 'LEFT JOIN {bug_tag} ' . $t_tag_alias . ' ON ' . $t_tag_alias . '.bug_id = {bug}.id'
. ' AND ' . $this->sql_in( $t_tag_alias . '.tag_id', $t_tag_ids )
. $t_tag_projects_clause;
$this->add_join( $t_join_exc );
$t_where[] = $t_tag_alias . '.tag_id IS NULL';
}
if( !empty( $t_where ) ) {
$this->add_where( implode( ' AND ', $t_where ) );
}
}
protected function build_prop_custom_fields() {
if( ON != config_get( 'filter_by_custom_fields' ) ) {
log_event( LOG_FILTERING, 'filter custom fields is globally disabled, skip' );
return;
}
$t_custom_fields = custom_field_get_linked_ids( $this->rt_included_projects );
foreach( $t_custom_fields as $t_cfid ) {
$t_field_info = custom_field_cache_row( $t_cfid, true );
if( !$t_field_info['filter_by'] ) {
# skip this custom field if it shouldn't be filterable
log_event( LOG_FILTERING, 'filter custom fields, field_id=' . $t_cfid . ' is not valid for filtering');
continue;
}
$t_field = $this->filter['custom_fields'][$t_cfid];
if( filter_field_is_any( $t_field ) ) {
# Ignore all custom filters that are not set, or that are set to '' or "any"
continue;
}
$t_custom_where_clause = '';
$t_def = custom_field_get_definition( $t_cfid );
# skip date custom fields with value of "any"
if( $t_def['type'] == CUSTOM_FIELD_TYPE_DATE && $t_field[0] == CUSTOM_FIELD_DATE_ANY ) {
continue;
}
$t_table_name = 'cf_alias_' . $t_cfid;
$t_cf_join_clause = 'LEFT OUTER JOIN {custom_field_string} ' . $t_table_name . ' ON {bug}.id = ' . $t_table_name . '.bug_id AND ' . $t_table_name . '.field_id = ' . $this->param( (int)$t_cfid );
# get which projects are valid for this custom field
$t_searchable_projects = array_intersect( $this->rt_included_projects, custom_field_get_project_ids( $t_cfid ) );
# and for which of those projects the user have read access to this field
$t_projects_can_view_field = access_project_array_filter( (int)$t_def['access_level_r'], $t_searchable_projects, $this->user_id );
if( empty( $t_projects_can_view_field ) ) {
# This field cant be viewed in any project, skip this field
# @TODO if the field cant be viewed in any project, should still the join be made to match for META_FILTER_NONE?
continue;
}
# This diff will contain those included projects that can't view this custom field
$t_diff = array_diff( $this->rt_included_projects, $t_projects_can_view_field );
# If not empty, it means there are some projects that can't view the field values,
# so a project filter must be used to not include values from those projects
if( !empty( $t_diff ) ) {
$t_cf_join_clause .= ' AND ' . $this->sql_in( '{bug}.project_id', $t_projects_can_view_field );
}
$this->rt_table_alias_cf[$t_cfid] = $t_table_name;
$this->add_join( $t_cf_join_clause );
if( $t_def['type'] == CUSTOM_FIELD_TYPE_DATE ) {
# Define the value field with type cast to integer
$t_value_field = 'CAST(COALESCE(NULLIF(' . $t_table_name . '.value, \'\'), \'0\') AS DECIMAL)';
switch( $t_field[0] ) {
# Closing parenthesis intentionally omitted, will be added later on
# CUSTOM_FIELD_DATE_ANY can't appear here, it was previously skipped
case CUSTOM_FIELD_DATE_NONE:
$t_custom_where_clause = '( ' . $t_table_name . '.bug_id IS NULL OR ' . $t_value_field . ' = 0 ';
break;
case CUSTOM_FIELD_DATE_BEFORE:
$t_custom_where_clause = '( ' . $t_value_field . ' != 0 AND ' . $t_value_field . ' < ' . $this->param( $t_field[2] );
break;
case CUSTOM_FIELD_DATE_AFTER:
$t_custom_where_clause = '( ' . $t_value_field . ' > ' . $this->param( $t_field[1] + 1 );
break;
default:
$t_custom_where_clause = '( ' . $t_value_field . ' BETWEEN ' . $this->param( $t_field[1] ) . ' AND ' . $this->param( $t_field[2] );
break;
}
} else {
$t_filter_array = array();
foreach( $t_field as $t_filter_member ) {
$t_filter_member = stripslashes( $t_filter_member );
if( filter_field_is_none( $t_filter_member ) ) {
# coerce filter value if selecting META_FILTER_NONE so it will match empty fields
$t_filter_member = '';
# but also add those _not_ present in the custom field string table
array_push( $t_filter_array, $t_table_name . '.value IS NULL' );
}
switch( $t_def['type'] ) {
case CUSTOM_FIELD_TYPE_CHECKBOX:
case CUSTOM_FIELD_TYPE_MULTILIST:
$t_filter_array[] = $this->sql_like( $t_table_name . '.value', '%|' . $t_filter_member . '|%' );
break;
case CUSTOM_FIELD_TYPE_TEXTAREA:
$t_filter_array[] = $this->sql_like( $t_table_name . '.text', '%' . $t_filter_member . '%' );
break;
default:
$t_filter_array[] = $t_table_name . '.value = ' . $this->param( $t_filter_member );
}
}
$t_custom_where_clause .= '(' . implode( ' OR ', $t_filter_array );
}
if( !is_blank( $t_custom_where_clause ) ) {
$this->add_where( $t_custom_where_clause . ')' );
}
} # foreach cf
}
protected function build_prop_search() {
if( is_blank( $this->filter[FILTER_PROPERTY_SEARCH] ) ) {
return;
}
# break up search terms by spacing or quoting
preg_match_all( "/-?([^'\"\s]+|\"[^\"]+\"|'[^']+')/", $this->filter[FILTER_PROPERTY_SEARCH], $t_matches, PREG_SET_ORDER );
# organize terms without quoting, paying attention to negation
$t_search_terms = array();
foreach( $t_matches as $t_match ) {
$t_search_terms[trim( $t_match[1], "\'\"" )] = ( $t_match[0][0] == '-' );
}
$t_bugnote_table = $this->helper_table_alias_for_bugnote();
# build a big where-clause and param list for all search terms, including negations
$t_first = true;
$t_textsearch_where_clause = '( ';
foreach( $t_search_terms as $t_search_term => $t_negate ) {
if( !$t_first ) {
$t_textsearch_where_clause .= ' AND ';
}
if( $t_negate ) {
$t_textsearch_where_clause .= 'NOT ';
}