-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbug_page_custom_field_links.php
156 lines (125 loc) · 4.87 KB
/
bug_page_custom_field_links.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
<?php
/**
* Linked custom fields plugin for MantisBT
*
* Copyright (c) 2011 Robert Munteanu ([email protected])
* Copyright (c) 2018, 2022 Damien Regad
*
* Linked custom fields for 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.
*
* Linked custom fields plugin for 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 Linked custom fields plugin for MantisBT.
* If not, see <http://www.gnu.org/licenses/>.
*/
header( "Content-Type: text/javascript" );
$t_bug_id = gpc_get_int( 'bug_id' );
$t_master_bug_id = gpc_get_int( 'm_id', 0 );
if( $t_bug_id == 0 ) {
if( $t_master_bug_id != 0 ) {
$t_project_id = bug_get_field( $t_master_bug_id, 'project_id' );
} else {
$t_project_id = helper_get_current_project();
}
} else {
$t_project_id = bug_get_field( $t_bug_id, 'project_id' );
}
$t_all_custom_field_ids = custom_field_get_linked_ids( $t_project_id );
?>
var linkedFieldValues = {};
var allFieldValues = {};
var bindings = {};
var savedValues = {};
<?php
foreach( $t_all_custom_field_ids as $t_custom_field_id ) {
if( $t_bug_id != 0 ) {
$t_has_write_access = custom_field_has_write_access( $t_custom_field_id, $t_bug_id );
} else {
$t_has_write_access = custom_field_has_write_access_to_project( $t_custom_field_id, $t_project_id );
}
if( ! $t_has_write_access ) {
continue;
}
$t_linked_values = LinkedCustomFieldsDao::getLinkedValuesMap( $t_custom_field_id );
if( count( $t_linked_values ) > 0 ) {
// values from $t_custom_field_id trigger filter values from $t_linked_field_id
$t_linked_field_id = LinkedCustomFieldsDao::getLinkedFieldId( $t_custom_field_id );
$t_linked_field = custom_field_get_definition( $t_linked_field_id );
$t_saved_values_source_id = $t_master_bug_id != 0 ? $t_master_bug_id : $t_bug_id;
$t_saved_values = $t_saved_values_source_id != 0 ? explode('|', custom_field_get_value( $t_linked_field_id, $t_saved_values_source_id ) ) : array();
echo 'savedValues["' . $t_linked_field_id .'"] = ' . JavascriptUtils::toJSArray( $t_saved_values ).";\n";
echo 'bindings["' . $t_custom_field_id.'"] = "'. $t_linked_field_id.'";'."\n";
echo 'allFieldValues["' .$t_custom_field_id.'"] = ' . JavascriptUtils::toJSArray( explode('|', $t_linked_field['possible_values']) ).";\n";
echo 'linkedFieldValues["'.$t_custom_field_id."\"] = {};\n";
foreach( $t_linked_values as $t_source_value => $t_target_values ) {
echo 'linkedFieldValues["' . $t_custom_field_id . '"]["' . addslashes( $t_source_value ) . '"] = '
. JavascriptUtils::toJSArray( $t_target_values ).";\n";
}
}
}
?>
var LinkedCustomFieldsUtil = {
removeDuplicates : function(arr) {
var uniques = [];
for(var i=arr.length;i--;){
var val = arr[i];
if(jQuery.inArray( val, uniques )===-1){
uniques.unshift(val);
}
}
return uniques;
},
findCustomFieldByFieldId: function(fieldId) {
var fieldRef = jQuery('[name="custom_field_' + fieldId +'"]');
if ( fieldRef.length == 0 ) {
fieldRef = jQuery('[name="custom_field_' + fieldId +'[]"]');
}
return fieldRef;
}
};
var refreshLinkedValues = function(fieldId, fieldValue) {
var targetValues = [];
if ( fieldValue instanceof Array) {
for ( var i = 0 ; i < fieldValue.length; i++) {
var singleValue = fieldValue[i];
var currentTargetValues = linkedFieldValues[fieldId][singleValue] || [];
for ( var j = 0 ; j < currentTargetValues.length; j++ ) {
targetValues.push(currentTargetValues[j]);
}
}
targetValues = LinkedCustomFieldsUtil.removeDuplicates(targetValues);
} else {
targetValues = linkedFieldValues[fieldId][fieldValue];
}
if ( ! targetValues || targetValues.length == 0 ) {
targetValues = allFieldValues[fieldId] ;
}
var targetFieldId = bindings[fieldId];
var targetFieldRef = LinkedCustomFieldsUtil.findCustomFieldByFieldId ( targetFieldId );
targetFieldRef.empty();
for ( var i = 0 ; i < targetValues.length; i++ ) {
var targetValue = targetValues[i];
targetFieldRef.append(jQuery('<option></option>').
attr('value', targetValue).
text(targetValue));
}
targetFieldRef.val(savedValues[targetFieldId]);
};
jQuery(document).ready(function() {
for ( var boundKey in bindings ) {
var applicable = linkedFieldValues[boundKey];
var customFieldRef = LinkedCustomFieldsUtil.findCustomFieldByFieldId( boundKey );
customFieldRef.data('fieldId', boundKey);
refreshLinkedValues(boundKey, customFieldRef.val());
customFieldRef.change(function() {
refreshLinkedValues(jQuery(this).data('fieldId'), jQuery(this).val());
});
}
});