/
custom_field_api.php
1479 lines (1301 loc) · 46.5 KB
/
custom_field_api.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
# MantisBT - A PHP based bugtracking system
# MantisBT 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, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* Custom Field API
*
* @package CoreAPI
* @subpackage CustomFieldAPI
* @copyright Copyright 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
* @copyright Copyright 2002 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*
* @uses access_api.php
* @uses authentication_api.php
* @uses bug_api.php
* @uses config_api.php
* @uses constant_inc.php
* @uses database_api.php
* @uses email_api.php
* @uses error_api.php
* @uses helper_api.php
* @uses history_api.php
* @uses project_api.php
* @uses string_api.php
* @uses utility_api.php
*/
require_api( 'access_api.php' );
require_api( 'authentication_api.php' );
require_api( 'bug_api.php' );
require_api( 'config_api.php' );
require_api( 'constant_inc.php' );
require_api( 'database_api.php' );
require_api( 'email_api.php' );
require_api( 'error_api.php' );
require_api( 'helper_api.php' );
require_api( 'history_api.php' );
require_api( 'project_api.php' );
require_api( 'string_api.php' );
require_api( 'utility_api.php' );
$g_custom_field_types[CUSTOM_FIELD_TYPE_STRING] = 'standard';
$g_custom_field_types[CUSTOM_FIELD_TYPE_TEXTAREA] = 'standard';
$g_custom_field_types[CUSTOM_FIELD_TYPE_NUMERIC] = 'standard';
$g_custom_field_types[CUSTOM_FIELD_TYPE_FLOAT] = 'standard';
$g_custom_field_types[CUSTOM_FIELD_TYPE_ENUM] = 'standard';
$g_custom_field_types[CUSTOM_FIELD_TYPE_EMAIL] = 'standard';
$g_custom_field_types[CUSTOM_FIELD_TYPE_CHECKBOX] = 'standard';
$g_custom_field_types[CUSTOM_FIELD_TYPE_LIST] = 'standard';
$g_custom_field_types[CUSTOM_FIELD_TYPE_MULTILIST] = 'standard';
$g_custom_field_types[CUSTOM_FIELD_TYPE_DATE] = 'standard';
foreach( $g_custom_field_types as $t_type ) {
require_once( config_get_global( 'core_path' ) . 'cfdefs/cfdef_' . $t_type . '.php' );
}
unset( $t_type );
/**
* Return true whether to display custom field
* @param integer $p_type Custom field type.
* @param string $p_display When to display.
* @return boolean
*/
function custom_field_allow_manage_display( $p_type, $p_display ) {
global $g_custom_field_type_definition;
if( isset( $g_custom_field_type_definition[$p_type]['#display_' . $p_display] ) ) {
return $g_custom_field_type_definition[$p_type]['#display_' . $p_display];
}
return false;
}
$g_cache_custom_field = array();
$g_cache_cf_list = null;
$g_cache_cf_linked = array();
$g_cache_name_to_id_map = array();
# Values are indexed by [ bug_id, field_id ]
# a non existant value will have a cached value of null
$g_cache_cf_bug_values = array();
/**
* Cache a custom field row if necessary and return the cached copy
* If the second parameter is true (default), trigger an error
* if the field can't be found. If the second parameter is
* false, return false if the field can't be found.
* @param integer $p_field_id Integer representing custom field id.
* @param boolean $p_trigger_errors Indicates whether to trigger an error if the field is not found.
* @return array array representing custom field
* @access public
*/
function custom_field_cache_row( $p_field_id, $p_trigger_errors = true ) {
global $g_cache_custom_field;
$c_field_id = (int)$p_field_id;
if( !isset( $g_cache_custom_field[$c_field_id] ) ) {
custom_field_cache_array_rows( array( $c_field_id ) );
}
# the cached index exist, even when not found
$t_cf_row = $g_cache_custom_field[$c_field_id];
if( !$t_cf_row ) {
if( $p_trigger_errors ) {
error_parameters( 'Custom ' . $p_field_id );
trigger_error( ERROR_CUSTOM_FIELD_NOT_FOUND, ERROR );
} else {
return false;
}
}
return $t_cf_row;
}
/**
* Cache custom fields contained within an array of field id's
* @param array $p_cf_id_array Array of custom field id's.
* @return void
* @access public
*/
function custom_field_cache_array_rows( array $p_cf_id_array ) {
global $g_cache_custom_field, $g_cache_name_to_id_map;
$c_cf_id_array = array();
foreach( $p_cf_id_array as $t_cf_id ) {
$c_id = (int)$t_cf_id;
if( !isset( $g_cache_custom_field[$c_id] ) ) {
$c_cf_id_array[$c_id] = $c_id;
}
}
if( empty( $c_cf_id_array ) ) {
return;
}
db_param_push();
$t_params = array();
$t_in_caluse_dbparams = array();
foreach( $c_cf_id_array as $t_id) {
$t_in_caluse_dbparams[] = db_param();
$t_params[] = $t_id;
}
$t_query = 'SELECT * FROM {custom_field} WHERE id IN (' . implode( ',', $t_in_caluse_dbparams ) . ')';
$t_result = db_query( $t_query, $t_params );
while( $t_row = db_fetch_array( $t_result ) ) {
$c_id = (int)$t_row['id'];
$g_cache_custom_field[$c_id] = $t_row;
$g_cache_name_to_id_map[$t_row['name']] = $c_id;
unset( $c_cf_id_array[$c_id] );
}
# set the remaining ids as not found
foreach( $c_cf_id_array as $t_id) {
$g_cache_custom_field[$t_id] = false;
}
return;
}
/**
* Load in cache values of custom fields, for given bugs and field ids.
* When a value for a given bug and field does not exist, fill the cached value as null
* @global array $g_cache_cf_bug_values
* @param array $p_bug_id_array
* @param array $p_field_id_array
* @return void
*/
function custom_field_cache_values( array $p_bug_id_array, array $p_field_id_array ) {
global $g_cache_cf_bug_values;
if( empty( $p_field_id_array ) ) {
return;
}
# clean fields ids
$t_fields_to_search = array();
$f_cf_defs = array();
foreach( $p_field_id_array as $t_field_id ) {
$c_field_id = (int)$t_field_id;
$t_fields_to_search[$c_field_id] = $c_field_id;
$f_cf_defs[$c_field_id] = custom_field_get_definition( $c_field_id );
}
# get bugs to fetch
$t_bugs_to_search = array();
foreach( $p_bug_id_array as $t_bug_id ) {
$c_bug_id = (int)$t_bug_id;
if( !isset( $g_cache_cf_bug_values[$c_bug_id] ) ) {
$t_bugs_to_search[] = $c_bug_id;
} else {
$t_diff = array_diff( $t_fields_to_search, array_keys( $g_cache_cf_bug_values[$c_bug_id] ) );
if( !empty( $t_diff ) ) {
$t_bugs_to_search[] = $c_bug_id;
}
}
}
if( empty( $t_bugs_to_search ) ) {
return;
}
db_param_push();
$t_params= array();
$t_query = 'SELECT B.id AS bug_id, CF.id AS field_id, CFS.value, CFS.text FROM {bug} B'
. ' LEFT OUTER JOIN {custom_field} CF ON 1 = 1'
. ' LEFT OUTER JOIN {custom_field_string} CFS ON ( B.id = CFS.bug_id AND CF.id = CFS.field_id )';
$t_bug_in_params = array();
foreach( $t_bugs_to_search as $t_bug_id ) {
$t_bug_in_params[] = db_param();
$t_params[] = $t_bug_id;
}
$t_query .= ' WHERE B.id IN (' . implode( ',', $t_bug_in_params ) . ')';
$t_field_in_params = array();
foreach( $t_fields_to_search as $t_field_id ) {
$t_field_in_params[] = db_param();
$t_params[] = $t_field_id;
}
$t_query .= ' AND CF.id IN (' . implode( ',', $t_field_in_params ) . ')';
$t_result = db_query( $t_query, $t_params );
# By having the left outer joins, non existant values are fetched as nulls,
# and can be stored in cache to mark them as not-found
while( $t_row = db_fetch_array( $t_result ) ) {
$c_bug_id = (int)$t_row['bug_id'];
# create a bug index if necessary
if( !isset( $g_cache_cf_bug_values[$c_bug_id] ) ) {
$g_cache_cf_bug_values[$c_bug_id] = array();
}
$c_field_id = (int)$t_row['field_id'];
$t_value_column = ( $f_cf_defs[$c_field_id]['type'] == CUSTOM_FIELD_TYPE_TEXTAREA ? 'text' : 'value' );
$t_value = $t_row[$t_value_column];
if( null !== $t_value ) {
$t_value = custom_field_database_to_value( $t_value, $f_cf_defs[$c_field_id]['type'] );
}
# non-existant will be stored as null
$g_cache_cf_bug_values[$c_bug_id][$c_field_id] = $t_value;
}
}
/**
* Clear the custom field values cache (or just the given bug id if specified)
* @global array $g_cache_cf_bug_values
* @param integer $p_bug_id Bug id
* @return void
*/
function custom_field_clear_cache_values( $p_bug_id = null ) {
global $g_cache_cf_bug_values;
if( null === $p_bug_id ) {
$g_cache_cf_bug_values = array();
} else {
if( isset( $g_cache_cf_bug_values[(int)$p_bug_id] ) ) {
unset( $g_cache_cf_bug_values[(int)$p_bug_id] );
}
}
}
/**
* Clear the custom field cache (or just the given id if specified)
* @param integer $p_field_id Custom field id.
* @return void
* @access public
*/
function custom_field_clear_cache( $p_field_id = null ) {
global $g_cache_custom_field, $g_cached_custom_field_lists;
$g_cached_custom_field_lists = null;
if( null === $p_field_id ) {
$g_cache_custom_field = array();
} else {
if( isset( $g_cache_custom_field[$p_field_id] ) ) {
unset( $g_cache_custom_field[$p_field_id] );
}
}
}
/**
* Check to see whether the field is included in the given project
* return true if the field is included, false otherwise
* @param integer $p_field_id Custom field id.
* @param integer $p_project_id Project id.
* @return boolean
* @access public
*/
function custom_field_is_linked( $p_field_id, $p_project_id ) {
global $g_cache_cf_linked;
if( isset( $g_cache_cf_linked[$p_project_id] ) ) {
if( in_array( $p_field_id, $g_cache_cf_linked[$p_project_id] ) ) {
return true;
}
return false;
}
# figure out if this bug_id/field_id combination exists
db_param_push();
$t_query = 'SELECT COUNT(*) FROM {custom_field_project}
WHERE field_id=' . db_param() . ' AND project_id=' . db_param();
$t_result = db_query( $t_query, array( $p_field_id, $p_project_id ) );
$t_count = db_result( $t_result );
if( $t_count > 0 ) {
return true;
} else {
return false;
}
}
/**
* Check to see whether the field id is defined
* return true if the field is defined, false otherwise
* @param integer $p_field_id Custom field id.
* @return boolean
* @access public
*/
function custom_field_exists( $p_field_id ) {
if( false == custom_field_cache_row( $p_field_id, false ) ) {
return false;
} else {
return true;
}
}
/**
* Return the type of a custom field if it exists.
* @param integer $p_field_id Custom field id.
* @return integer custom field type
* @access public
*/
function custom_field_type( $p_field_id ) {
$t_field = custom_field_cache_row( $p_field_id, false );
if( $t_field == false ) {
return - 1;
} else {
return $t_field['type'];
}
}
/**
* Check to see whether the field id is defined
* return true if the field is defined, error otherwise
* @param integer $p_field_id Custom field id.
* @return boolean
* @access public
*/
function custom_field_ensure_exists( $p_field_id ) {
if( custom_field_exists( $p_field_id ) ) {
return true;
} else {
error_parameters( 'Custom ' . $p_field_id );
trigger_error( ERROR_CUSTOM_FIELD_NOT_FOUND, ERROR );
}
}
/**
* Check to see whether the name is unique
* return false if a field with the name already exists, true otherwise
* if an id is specified, then the corresponding record is excluded from the
* uniqueness test.
* @param string $p_name Custom field name.
* @param integer $p_custom_field_id Custom field identifier.
* @return boolean
* @access public
*/
function custom_field_is_name_unique( $p_name, $p_custom_field_id = null ) {
db_param_push();
$t_query = 'SELECT COUNT(*) FROM {custom_field} WHERE name=' . db_param();
if( $p_custom_field_id !== null ) {
$t_query .= ' AND (id <> ' . db_param() . ')';
}
$t_result = db_query( $t_query, ( ($p_custom_field_id !== null) ? array( $p_name, $p_custom_field_id ) : array( $p_name ) ) );
$t_count = db_result( $t_result );
if( $t_count > 0 ) {
return false;
} else {
return true;
}
}
/**
* Check to see whether the name is unique
* return true if the name has not been used, error otherwise
* @param string $p_name Custom field name.
* @return boolean
* @access public
*/
function custom_field_ensure_name_unique( $p_name ) {
if( custom_field_is_name_unique( $p_name ) ) {
return true;
} else {
trigger_error( ERROR_CUSTOM_FIELD_NAME_NOT_UNIQUE, ERROR );
}
}
/**
* Return true if the user can read the value of the field for the given bug,
* false otherwise.
* @param integer $p_field_id Custom field identifier.
* @param integer $p_bug_id A bug identifier.
* @param integer $p_user_id User id.
* @return boolean
* @access public
*/
function custom_field_has_read_access( $p_field_id, $p_bug_id, $p_user_id = null ) {
custom_field_ensure_exists( $p_field_id );
if( null === $p_user_id ) {
$p_user_id = auth_get_current_user_id();
}
$t_access_level_r = custom_field_get_field( $p_field_id, 'access_level_r' );
$t_project_id = bug_get_field( $p_bug_id, 'project_id' );
return access_has_project_level( $t_access_level_r, $t_project_id, $p_user_id );
}
/**
* Return true if the user can read the value of the field for the given project,
* false otherwise.
* @param integer $p_field_id Custom field identifier.
* @param integer $p_project_id A project identifier.
* @param integer $p_user_id A user identifier.
* @return boolean
* @access public
*/
function custom_field_has_read_access_by_project_id( $p_field_id, $p_project_id, $p_user_id = null ) {
custom_field_ensure_exists( $p_field_id );
if( null === $p_user_id ) {
$p_user_id = auth_get_current_user_id();
}
$t_access_level_r = custom_field_get_field( $p_field_id, 'access_level_r' );
return access_has_project_level( $t_access_level_r, $p_project_id, $p_user_id );
}
/**
* Return true if the user can modify the value of the field for the given project,
* false otherwise.
* @param integer $p_field_id Custom field identifier.
* @param integer $p_project_id A project identifier.
* @param integer $p_user_id A user identifier.
* @return boolean
* @access public
*/
function custom_field_has_write_access_to_project( $p_field_id, $p_project_id, $p_user_id = null ) {
custom_field_ensure_exists( $p_field_id );
if( null === $p_user_id ) {
$p_user_id = auth_get_current_user_id();
}
$t_access_level_rw = custom_field_get_field( $p_field_id, 'access_level_rw' );
return access_has_project_level( $t_access_level_rw, $p_project_id, $p_user_id );
}
/**
* Return true if the user can modify the value of the field for the given bug,
* false otherwise.
* @param integer $p_field_id Custom field identifier.
* @param integer $p_bug_id A bug identifier.
* @param integer $p_user_id A user identifier.
* @return boolean
* @access public
*/
function custom_field_has_write_access( $p_field_id, $p_bug_id, $p_user_id = null ) {
$t_project_id = bug_get_field( $p_bug_id, 'project_id' );
return( custom_field_has_write_access_to_project( $p_field_id, $t_project_id, $p_user_id ) );
}
/**
* create a new custom field with the name $p_name
* the definition are the default values and can be changes later
* return the ID of the new definition
* @param string $p_name Custom field name.
* @return integer custom field id
* @access public
*/
function custom_field_create( $p_name ) {
$c_name = trim( $p_name );
if( is_blank( $c_name ) ) {
error_parameters( 'name' );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}
custom_field_ensure_name_unique( $c_name );
db_param_push();
$t_query = 'INSERT INTO {custom_field} ( name, possible_values )
VALUES ( ' . db_param() . ',' . db_param() . ')';
db_query( $t_query, array( $c_name, '' ) );
return db_insert_id( db_get_table( 'custom_field' ) );
}
/**
* Update the field definition
* return true on success, false on failure
* @param integer $p_field_id Custom field identifier.
* @param array $p_def_array Custom field definition.
* @return boolean
* @access public
*/
function custom_field_update( $p_field_id, array $p_def_array ) {
if( is_blank( $p_def_array['name'] ) ) {
error_parameters( 'name' );
trigger_error( ERROR_EMPTY_FIELD, ERROR );
}
if( $p_def_array['access_level_rw'] < $p_def_array['access_level_r'] ) {
error_parameters(
lang_get( 'custom_field_access_level_r' ) . ', ' .
lang_get( 'custom_field_access_level_rw' ) );
trigger_error( ERROR_CUSTOM_FIELD_INVALID_PROPERTY, ERROR );
}
if( $p_def_array['length_min'] < 0
|| ( $p_def_array['length_max'] != 0 && $p_def_array['length_min'] > $p_def_array['length_max'] )
) {
error_parameters( lang_get( 'custom_field_length_min' ) . ', ' . lang_get( 'custom_field_length_max' ) );
trigger_error( ERROR_CUSTOM_FIELD_INVALID_PROPERTY, ERROR );
}
if( !custom_field_is_name_unique( $p_def_array['name'], $p_field_id ) ) {
trigger_error( ERROR_CUSTOM_FIELD_NAME_NOT_UNIQUE, ERROR );
}
db_param_push();
# Build fields update statement
$t_update = '';
foreach( $p_def_array as $t_field => $t_value ) {
switch( $t_field ) {
case 'name':
case 'possible_values':
case 'default_value':
case 'valid_regexp':
# Possible values doesn't apply to textarea fields
if( $p_def_array['type'] == CUSTOM_FIELD_TYPE_TEXTAREA && $t_field == 'possible_values' ) {
$t_value = '';
}
$t_update .= $t_field . '=' . db_param() . ', ';
$t_params[] = (string)$t_value;
break;
case 'type':
case 'access_level_r':
case 'access_level_rw':
case 'length_min':
case 'length_max':
$t_update .= $t_field . '=' . db_param() . ', ';
$t_params[] = (int)$t_value;
break;
case 'filter_by':
case 'display_report':
case 'display_update':
case 'display_resolved':
case 'display_closed':
case 'require_report':
case 'require_update':
case 'require_resolved':
case 'require_closed':
$t_update .= $t_field . '=' . db_param() . ', ';
$t_params[] = (bool)$t_value;
break;
}
}
# If there are fields to update, execute SQL
if( $t_update !== '' ) {
$t_query = 'UPDATE {custom_field} SET ' . rtrim( $t_update, ', ' ) . ' WHERE id = ' . db_param();
$t_params[] = $p_field_id;
db_query( $t_query, $t_params );
custom_field_clear_cache( $p_field_id );
return true;
}
# Reset the parameter count manually since the query was not executed
db_param_pop();
return false;
}
/**
* Add a custom field to a project
* return true on success, false on failure or if already added
* @param integer $p_field_id Custom field identifier.
* @param integer $p_project_id Project identifier.
* @return boolean
* @access public
*/
function custom_field_link( $p_field_id, $p_project_id ) {
custom_field_ensure_exists( $p_field_id );
project_ensure_exists( $p_project_id );
if( custom_field_is_linked( $p_field_id, $p_project_id ) ) {
return false;
}
db_param_push();
$t_query = 'INSERT INTO {custom_field_project} ( field_id, project_id )
VALUES ( ' . db_param() . ', ' . db_param() . ')';
db_query( $t_query, array( $p_field_id, $p_project_id ) );
return true;
}
/**
* Remove a custom field from a project
* return true on success, false on failure
*
* The values for the custom fields are not deleted. This is to allow for the
* case where a bug is moved to another project that has the field, or the
* field is linked again to the project.
* @param integer $p_field_id Custom field identifier.
* @param integer $p_project_id Project identifier.
* @return void
* @access public
*/
function custom_field_unlink( $p_field_id, $p_project_id ) {
db_param_push();
$t_query = 'DELETE FROM {custom_field_project}
WHERE field_id = ' . db_param() . ' AND project_id = ' . db_param();
db_query( $t_query, array( $p_field_id, $p_project_id ) );
}
/**
* Delete the field definition and all associated values and project associations
* return true on success, false on failure
* @param integer $p_field_id Custom field identifier.
* @return void
* @access public
*/
function custom_field_destroy( $p_field_id ) {
# delete all values
db_param_push();
$t_query = 'DELETE FROM {custom_field_string} WHERE field_id=' . db_param();
db_query( $t_query, array( $p_field_id ) );
# delete all project associations
db_param_push();
$t_query = 'DELETE FROM {custom_field_project} WHERE field_id=' . db_param();
db_query( $t_query, array( $p_field_id ) );
# delete the definition
db_param_push();
$t_query = 'DELETE FROM {custom_field} WHERE id=' . db_param();
db_query( $t_query, array( $p_field_id ) );
custom_field_clear_cache( $p_field_id );
custom_field_clear_cache_values();
}
/**
* Delete all associations of custom fields to the specified project
* return true on success, false on failure
*
* To be called from within project_delete().
* @param integer $p_project_id A project identifier.
* @return void
* @access public
*/
function custom_field_unlink_all( $p_project_id ) {
# delete all project associations
db_param_push();
$t_query = 'DELETE FROM {custom_field_project} WHERE project_id=' . db_param();
db_query( $t_query, array( $p_project_id ) );
}
/**
* Delete all custom values associated with the specified bug.
*
* To be called from bug_delete().
* @param integer $p_bug_id A bug identifier.
* @return void
* @access public
*/
function custom_field_delete_all_values( $p_bug_id ) {
db_param_push();
$t_query = 'DELETE FROM {custom_field_string} WHERE bug_id=' . db_param();
db_query( $t_query, array( $p_bug_id ) );
custom_field_clear_cache_values( $p_bug_id );
}
/**
* Get the id of the custom field with the specified name.
* false is returned if no custom field found with the specified name.
* @param string $p_field_name Custom field name.
* @return boolean|integer false or custom field id
* @access public
*/
function custom_field_get_id_from_name( $p_field_name ) {
global $g_cache_name_to_id_map;
if( is_blank( $p_field_name ) ) {
return false;
}
if( isset( $g_cache_name_to_id_map[$p_field_name] ) ) {
return $g_cache_name_to_id_map[$p_field_name];
}
db_param_push();
$t_query = 'SELECT id FROM {custom_field} WHERE name=' . db_param();
$t_result = db_query( $t_query, array( $p_field_name ) );
$t_row = db_fetch_array( $t_result );
if( !$t_row ) {
$g_cache_name_to_id_map[$p_field_name] = false;
return false;
}
$g_cache_name_to_id_map[$p_field_name] = $t_row['id'];
return $t_row['id'];
}
/**
* Return an array of ids of custom fields bound to the specified project
*
* The ids will be sorted based on the sequence number associated with the binding
* @param integer $p_project_id A project identifier.
* @return array
* @access public
*/
function custom_field_get_linked_ids( $p_project_id = ALL_PROJECTS ) {
global $g_cache_cf_linked;
if( !isset( $g_cache_cf_linked[$p_project_id] ) ) {
db_param_push();
if( ALL_PROJECTS == $p_project_id ) {
$t_user_id = auth_get_current_user_id();
# Select only the ids of custom fields in projects the user has access to
# - all custom fields in public projects,
# - those in private projects where the user is listed
# - in private projects where the user is implicitly listed
$t_query = 'SELECT DISTINCT cft.id
FROM {custom_field} cft
JOIN {custom_field_project} cfpt ON cfpt.field_id = cft.id
JOIN {project} pt
ON pt.id = cfpt.project_id AND pt.enabled = ' . db_prepare_bool( true ) . '
LEFT JOIN {project_user_list} pult
ON pult.project_id = cfpt.project_id AND pult.user_id = ' . db_param() . '
, {user} ut
WHERE ut.id = ' . db_param() . '
AND ( pt.view_state = ' . VS_PUBLIC . '
OR pult.user_id = ut.id
';
$t_params = array( $t_user_id, $t_user_id );
# Add private access clause and related parameter
$t_private_access = config_get( 'private_project_threshold' );
if( is_array( $t_private_access ) ) {
if( 1 == count( $t_private_access ) ) {
$t_access_clause = '= ' . db_param();
$t_params[] = array_shift( $t_private_access );
} else {
$t_access_clause = 'IN (';
foreach( $t_private_access as $t_elem ) {
$t_access_clause .= db_param() . ',';
$t_params[] = $t_elem;
}
$t_access_clause = rtrim( $t_access_clause, ',' ) . ')';
}
} else {
$t_access_clause = '>=' . db_param();
$t_params[] = $t_private_access;
}
$t_query .= 'OR ( pult.user_id IS NULL AND ut.access_level ' . $t_access_clause . ' ) )';
} else {
if( is_array( $p_project_id ) ) {
if( 1 == count( $p_project_id ) ) {
$t_project_clause = '= ' . db_param();
$t_params[] = array_shift( $p_project_id );
} else {
$t_project_clause = 'IN (';
foreach( $p_project_id as $t_project ) {
$t_project_clause .= db_param() . ',';
$t_params[] = $t_project;
}
$t_project_clause = rtrim( $t_project_clause, ',' ) . ')';
}
} else {
$t_project_clause = '= ' . db_param();
$t_params[] = $p_project_id;
}
$t_query = 'SELECT cft.id
FROM {custom_field} cft
JOIN {custom_field_project} cfpt ON cfpt.field_id = cft.id
WHERE cfpt.project_id ' . $t_project_clause . '
ORDER BY sequence ASC, name ASC';
}
$t_result = db_query( $t_query, $t_params );
$t_ids = array();
while( $t_row = db_fetch_array( $t_result ) ) {
array_push( $t_ids, $t_row['id'] );
}
custom_field_cache_array_rows( $t_ids );
$g_cache_cf_linked[$p_project_id] = $t_ids;
} else {
$t_ids = $g_cache_cf_linked[$p_project_id];
}
return $t_ids;
}
/**
* Return an array all custom field ids sorted by name
* @return array
* @access public
*/
function custom_field_get_ids() {
global $g_cache_cf_list, $g_cache_custom_field;
if( $g_cache_cf_list === null ) {
$t_query = 'SELECT * FROM {custom_field} ORDER BY name ASC';
$t_result = db_query( $t_query );
$t_ids = array();
while( $t_row = db_fetch_array( $t_result ) ) {
$g_cache_custom_field[(int)$t_row['id']] = $t_row;
array_push( $t_ids, $t_row['id'] );
}
$g_cache_cf_list = $t_ids;
} else {
$t_ids = $g_cache_cf_list;
}
return $t_ids;
}
/**
* Return an array of ids of projects related to the specified custom field
* (the array may be empty)
* @param integer $p_field_id Custom field identifier.
* @return array
* @access public
*/
function custom_field_get_project_ids( $p_field_id ) {
db_param_push();
$t_query = 'SELECT project_id FROM {custom_field_project} WHERE field_id = ' . db_param();
$t_result = db_query( $t_query, array( $p_field_id ) );
$t_ids = array();
while( $t_row = db_fetch_array( $t_result ) ) {
array_push( $t_ids, $t_row['project_id'] );
}
return $t_ids;
}
/**
* Return a field definition row for the field or error if the field does not exist
* @param integer $p_field_id Custom field identifier.
* @return array custom field definition
* @access public
*/
function custom_field_get_definition( $p_field_id ) {
return custom_field_cache_row( $p_field_id );
}
/**
* Return a single database field from a custom field definition row for the field
* if the database field does not exist, display a warning and return ''
* @param integer $p_field_id Custom field identifier.
* @param integer $p_field_name Custom field name.
* @return string
* @access public
*/
function custom_field_get_field( $p_field_id, $p_field_name ) {
$t_row = custom_field_get_definition( $p_field_id );
if( isset( $t_row[$p_field_name] ) ) {
return $t_row[$p_field_name];
} else {
error_parameters( $p_field_name );
trigger_error( ERROR_DB_FIELD_NOT_FOUND, WARNING );
return '';
}
}
/**
* Return custom field name including localized name (if available)
*
* @param string $p_name Custom field's name.
* @return string CustomFieldName [(LocalizedName)]
* @access public
*/
function custom_field_get_display_name( $p_name ) {
$t_local_name = lang_get_defaulted( $p_name );
if( $t_local_name != $p_name ) {
$p_name .= ' (' . $t_local_name . ')';
}
return string_display( $p_name );
}
/**
* Get the value of a custom field for the given bug
* @todo return values are unclear... should we error when access is denied
* and provide an api to check whether it will be?
* @param integer $p_field_id Custom field id.
* @param integer $p_bug_id A bug identifier.
* @return mixed: value is defined, null: no value is defined, false: read access is denied
* @access public
*/
function custom_field_get_value( $p_field_id, $p_bug_id ) {
global $g_cache_cf_bug_values;
$c_bug_id = (int)$p_bug_id;
$c_field_id = (int)$p_field_id;
$t_row = custom_field_cache_row( $c_field_id );
$t_access_level_r = $t_row['access_level_r'];
# first check permissions
if( !custom_field_has_read_access( $c_field_id, $c_bug_id, auth_get_current_user_id() ) ) {
return false;
}
# A null value means a cached non existant value. It must be checked with care.
if( !isset( $g_cache_cf_bug_values[$c_bug_id] )
|| !array_key_exists( $c_field_id, $g_cache_cf_bug_values[$c_bug_id] ) ) {
custom_field_cache_values( array( $c_bug_id ), array( $c_field_id ) );
}
return $g_cache_cf_bug_values[$c_bug_id][$c_field_id];
}
/**
* Gets the custom fields array for the given bug readable by specified level.
* Array keys are custom field names. Array is sorted by custom field sequence number;
* Array items are arrays with the next keys:
* 'type', 'value', 'access_level_r'
* @param integer $p_bug_id A bug identifier.
* @param integer $p_user_access_level Access level.
* @return array
* @access public
*/
function custom_field_get_linked_fields( $p_bug_id, $p_user_access_level ) {
$t_custom_fields = custom_field_get_all_linked_fields( $p_bug_id );
# removing restricted fields
foreach( $t_custom_fields as $t_custom_field_name => $t_custom_field_data ) {
if( $p_user_access_level < $t_custom_field_data['access_level_r'] ) {
unset( $t_custom_fields[$t_custom_field_name] );
}
}
return $t_custom_fields;
}
/**
* Gets the custom fields array for the given bug. Array keys are custom field names.
* Array is sorted by custom field sequence number; Array items are arrays with the next keys:
* 'type', 'value', 'access_level_r'
* @param integer $p_bug_id A bug identifier.
* @return array
* @access public
*/
function custom_field_get_all_linked_fields( $p_bug_id ) {
global $g_cached_custom_field_lists;
if( !is_array( $g_cached_custom_field_lists ) ) {
$g_cached_custom_field_lists = array();
}
# is the list in cache ?
if( !array_key_exists( $p_bug_id, $g_cached_custom_field_lists ) ) {
$c_project_id = (int)( bug_get_field( $p_bug_id, 'project_id' ) );
db_param_push();
$t_query = 'SELECT f.name, f.type, f.access_level_r, f.default_value, s.value
FROM {custom_field_project} p
INNER JOIN {custom_field} f ON f.id = p.field_id
LEFT JOIN {custom_field_string} s
ON s.field_id = p.field_id AND s.bug_id = ' . db_param() . '
WHERE p.project_id = ' . db_param() . '
ORDER BY p.sequence ASC, f.name ASC';