-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLinkedCustomFields.php
232 lines (211 loc) · 6.33 KB
/
LinkedCustomFields.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
<?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/>.
*
* @noinspection PhpMissingParamTypeInspection, PhpMissingReturnTypeInspection
*/
class LinkedCustomFieldsPlugin extends MantisPlugin {
/**
* Error strings
*/
const ERROR_ALREADY_LINKED = 'error_already_linked';
public function register() {
$this->name = plugin_lang_get( "title" );
$this->description = plugin_lang_get( "description" );
$this->version = "2.0.2";
$this->requires = array(
"MantisCore" => "2.3.0",
);
$this->author = "Robert Munteanu, Damien Regad";
$this->contact = "[email protected],[email protected]";
$this->url = "https://github.com/mantisbt-plugins/LinkedCustomFields";
}
public function hooks() {
return array(
'EVENT_MENU_MANAGE' => 'manage_custom_field_links',
'EVENT_LAYOUT_RESOURCES' => 'resources',
'EVENT_REST_API_ROUTES' => 'routes',
);
}
function errors() {
return array(
self::ERROR_ALREADY_LINKED => plugin_lang_get( self::ERROR_ALREADY_LINKED ),
);
}
/**
* Hook for EVENT_MENU_MANAGE.
*
* @noinspection PhpUnused
*/
public function manage_custom_field_links() {
if( !access_has_global_level( config_get( 'manage_custom_fields_threshold' ) ) ) {
return [];
}
return array(
'<a href="' . plugin_page( 'configure_links' ) . '">'
. plugin_lang_get( 'configure_custom_field_links')
. '</a>',
);
}
/**
* Hook for EVENT_LAYOUT_RESOURCES.
*
* @return string
*/
function resources() {
$t_bug_id = gpc_get_int( 'bug_id', -1 );
$t_m_id = gpc_get_int( 'm_id', 0 );
if( $t_bug_id == -1
&& basename( $_SERVER['SCRIPT_NAME'] ) == 'bug_report_page.php'
) {
$t_bug_id = 0;
}
if( $t_bug_id != -1 ) {
return '<script type="text/javascript" src="'
. plugin_page( 'bug_page_custom_field_links.php' )
. '&bug_id=' . $t_bug_id . '&m_id=' . $t_m_id
. '"></script>';
}
return '';
}
public function init() {
require_once 'LinkedCustomFields.API.php';
}
public function schema() {
return array(
array( 'CreateTableSQL',
array( plugin_table( 'data' ), "
custom_field_id I NOTNULL,
custom_field_value_order I NOTNULL,
custom_field_value C(255) NOTNULL DEFAULT \" '' \",
target_field_id I NOTNULL,
target_field_values C(255) NOTNULL DEFAULT \" '' \"
"),
),
array( 'AlterColumnSQL',
array( plugin_table( 'data' ), " custom_field_value XL, target_field_values XL")
)
);
}
/**
* Hook for EVENT_REST_API_ROUTES.
*
* Add the RESTful routes handled by this plugin.
*
* @param string $p_event_name The event name
* @param array $p_event_args The event arguments
*
* @return void
*
* @noinspection PhpUnused, PhpUnusedParameterInspection
* @noinspection PhpVariableIsUsedOnlyInClosureInspection Using $this inside closure does not work
*/
public function routes( $p_event_name, $p_event_args ) {
$t_app = $p_event_args['app'];
$t_plugin = $this;
$t_app->group(
plugin_route_group(),
function() use ( $t_app, $t_plugin ) {
$t_app->get( '/values/{field_id}', [$t_plugin, 'route_values'] );
$t_app->get( '/mapping/{field_id}', [$t_plugin, 'route_mapping'] );
}
);
}
/**
* RESTful route for Custom Fields values.
*
* Returned JSON structure:
* - {array} List of possible values for given custom field
*
* @param Slim\Http\Request $request
* @param Slim\Http\Response $response
* @param array $args [bug_id = Bug Id for patterns replacement]
* @return Slim\Http\Response
*
* @noinspection PhpUnusedParameterInspection
*/
public function route_values( $request, $response, $args ) {
# Set the reference Bug Id for placeholders replacements
if( isset( $args['field_id'] ) ) {
$t_field_id = (int)$args['field_id'];
# Retrieve Custom Field definition
$t_custom_field_def = custom_field_cache_row( $t_field_id, false );
} else {
$t_custom_field_def = false;
}
# Make sure Custom Field exists and its type is supported
if( !$t_custom_field_def ) {
return $response->withStatus(
HTTP_STATUS_BAD_REQUEST,
"Invalid Custom Field Id"
);
}
switch( $t_custom_field_def['type'] ) {
case CUSTOM_FIELD_TYPE_ENUM:
case CUSTOM_FIELD_TYPE_MULTILIST:
break;
default:
return $response->withStatus(
HTTP_STATUS_BAD_REQUEST,
"Unsupported Custom Field Type"
);
}
# Return possible values
return $response
->withStatus( HTTP_STATUS_SUCCESS )
->withJson(
explode( '|', $t_custom_field_def['possible_values'] )
);
}
/**
* RESTful route for Custom Fields link mappings.
*
* Returned JSON structure:
* - {array} List of CF link mappings, with structure
* - {array} CF mapping
* - {string} Source CF value
* - {array} Allowed target CF values
*
* @param Slim\Http\Request $request
* @param Slim\Http\Response $response
* @param array $args [bug_id = Bug Id for patterns replacement]
* @return Slim\Http\Response
*
* @noinspection PhpUnusedParameterInspection
*/
public function route_mapping( $request, $response, $args ) {
# Set the reference Bug Id for placeholders replacements
if( isset( $args['field_id'] ) ) {
$t_field_id = (int)$args['field_id'];
plugin_push_current( $this->basename );
$t_map = LinkedCustomFieldsDao::getLinkedValuesMap( $t_field_id );
plugin_pop_current();
} else {
return $response->withStatus(
HTTP_STATUS_BAD_REQUEST,
"Invalid Custom Field Id"
);
}
# Return possible values
return $response
->withStatus( HTTP_STATUS_SUCCESS )
->withJson( $t_map );
}
}