/
category_api.php
320 lines (251 loc) · 10 KB
/
category_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
<?php
# Mantis - a php based bugtracking system
# Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
# Copyright (C) 2002 - 2007 Mantis Team - mantisbt-dev@lists.sourceforge.net
# Mantis 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.
#
# Mantis 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 Mantis. If not, see <http://www.gnu.org/licenses/>.
# --------------------------------------------------------
# $Id$
# --------------------------------------------------------
### Category API ###
# Category data cache (to prevent excessive db queries)
$g_category_cache = array();
#===================================
# Boolean queries and ensures
#===================================
# --------------------
# Check whether the category exists in the project
# Return true if the category exists, false otherwise
function category_exists( $p_category_id ) {
global $g_category_cache;
if ( isset( $g_category_cache[$p_category_id] ) ) {
return true;
}
$c_category_id = db_prepare_int( $p_category_id );
$t_category_table = db_get_table( 'mantis_category_table' );
$query = "SELECT COUNT(*) FROM $t_category_table
WHERE id=" . db_param(0);
$count = db_result( db_query_bound( $query, array( $c_category_id ) ) );
if ( 0 < $count ) {
return true;
} else {
return false;
}
}
# --------------------
# Check whether the category exists in the project
# Trigger an error if it does not
function category_ensure_exists( $p_category_id ) {
if ( !category_exists( $p_category_id ) ) {
trigger_error( ERROR_CATEGORY_NOT_FOUND, ERROR );
}
}
# --------------------
# Check whether the category is unique within a project
# Returns true if the category is unique, false otherwise
function category_is_unique( $p_project_id, $p_name ) {
$c_project_id = db_prepare_int( $p_project_id );
$t_category_table = db_get_table( 'mantis_category_table' );
$query = "SELECT COUNT(*) FROM $t_category_table
WHERE project_id=" . db_param(0) . " AND " . db_helper_like( 'name', 1 );
$count = db_result( db_query_bound( $query, array( $c_project_id, $p_name ) ) );
if ( 0 < $count ) {
return false;
} else {
return true;
}
}
# --------------------
# Check whether the category is unique within a project
# Trigger an error if it is not
function category_ensure_unique( $p_project_id, $p_name ) {
if ( !category_is_unique( $p_project_id, $p_name ) ) {
trigger_error( ERROR_CATEGORY_DUPLICATE, ERROR );
}
}
#===================================
# Creation / Deletion / Updating
#===================================
# --------------------
# Add a new category to the project
function category_add( $p_project_id, $p_name ) {
$c_project_id = db_prepare_int( $p_project_id );
category_ensure_unique( $p_project_id, $p_name );
$t_category_table = db_get_table( 'mantis_category_table' );
$query = "INSERT INTO $t_category_table
( project_id, name )
VALUES
( " . db_param(0) . ', ' . db_param(1) . ' )';
db_query_bound( $query, array( $c_project_id, $p_name ) );
# db_query errors on failure so:
return db_insert_id( $t_category_table );
}
# --------------------
# Update the name and user associated with the category
function category_update( $p_category_id, $p_name, $p_assigned_to ) {
$t_old_category = category_get_row( $p_category_id );
$c_category_id = db_prepare_int( $p_category_id );
$c_name = db_prepare_string( $p_name );
$c_assigned_to = db_prepare_int( $p_assigned_to );
$t_category_table = db_get_table( 'mantis_category_table' );
$t_bug_table = db_get_table( 'mantis_bug_table' );
$query = "UPDATE $t_category_table
SET name=" . db_param(0) . ',
user_id=' . db_param(1) . '
WHERE id=' . db_param(2);
db_query_bound( $query, array( $c_name, $c_assigned_to, $c_category_id ) );
# Add bug history entries if we update the category's name
if ( $t_old_category['name'] != $c_name ) {
$query = "SELECT id FROM $t_bug_table WHERE category_id=" . db_param(0);
$t_result = db_query_bound( $query, array( $c_category_id ) );
while ( $t_bug_row = db_fetch_array( $t_result ) ) {
history_log_event_direct( $t_bug_row['id'], 'category', $t_old_category['name'], $c_name );
}
}
# db_query errors on failure so:
return true;
}
# --------------------
# Remove a category from the project
function category_remove( $p_category_id, $p_new_category_id = 1 ) {
$t_category_row = category_get_row( $p_category_id );
$c_category_id = db_prepare_int( $p_category_id );
$c_new_category_id = db_prepare_int( $p_new_category_id );
category_ensure_exists( $p_category_id );
if ( 0 != $p_new_category_id ) {
category_ensure_exists( $p_new_category_id );
}
$t_category_table = db_get_table( 'mantis_category_table' );
$t_bug_table = db_get_table( 'mantis_bug_table' );
$query = "DELETE FROM $t_category_table
WHERE id=" . db_param(0);
db_query_bound( $query, array( $c_category_id ) );
# update bug history entries
$query = "SELECT id FROM $t_bug_table WHERE category_id=" . db_param(0);
$t_result = db_query_bound( $query, array( $c_category_id ) );
while ( $t_bug_row = db_fetch_array( $t_result ) ) {
var_dump( $t_bug_row );
history_log_event_direct( $t_bug_row['id'], 'category', $t_category_row['name'], category_full_name( $p_new_category_id, false ) );
}
# update bug data
$query = "UPDATE $t_bug_table
SET category_id=" . db_param(0) . "
WHERE category_id=" . db_param(1);
db_query_bound( $query, array( $c_new_category_id, $c_category_id ) );
# db_query errors on failure so:
return true;
}
# --------------------
# Remove all categories associated with a project
function category_remove_all( $p_project_id, $p_new_category_id = 1 ) {
$c_project_id = db_prepare_int( $p_project_id );
$c_new_category_id = db_prepare_int( $p_new_category_id );
project_ensure_exists( $p_project_id );
if ( 0 != $p_new_category_id ) {
category_ensure_exists( $p_new_category_id );
}
$t_category_table = db_get_table( 'mantis_category_table' );
$t_bug_table = db_get_table( 'mantis_bug_table' );
$query = "DELETE FROM $t_category_table
WHERE project_id=" . db_param(0);
db_query_bound( $query, array( $c_project_id ) );
# cache category names
category_get_all_rows();
# update bug history entries
$query = "SELECT id, category_id FROM $t_bug_table WHERE project_id=" . db_param(0);
$t_result = db_query_bound( $query, array( $c_project_id ) );
while ( $t_bug_row = db_fetch_array( $t_result ) ) {
var_dump( $t_bug_row );
history_log_event_direct( $t_bug_row['id'], 'category', category_full_name( $t_bug_row['category_id'], false ), category_full_name( $p_new_category_id, false ) );
}
# update bug data
$query = "UPDATE $t_bug_table
SET category=" . db_param(0) . '
WHERE project_id=' . db_param(1);
db_query_bound( $query, array( $c_new_category_id, $c_project_id ) );
# db_query errors on failure so:
return true;
}
#===================================
# Data Access
#===================================
# --------------------
# Return the definition row for the category
function category_get_row( $p_category_id ) {
global $g_category_cache;
if ( isset( $g_category_cache[$p_category_id] ) ) {
return $g_category_cache[$p_category_id];
}
$c_category_id = db_prepare_int( $p_category_id );
$t_category_table = db_get_table( 'mantis_category_table' );
$t_project_table = db_get_table( 'mantis_project_table' );
$query = "SELECT * FROM $t_category_table
WHERE id=" . db_param(0);
$result = db_query_bound( $query, array( $c_category_id ) );
$count = db_num_rows( $result );
if ( 0 == $count ) {
trigger_error( ERROR_CATEGORY_NOT_FOUND, ERROR );
}
$row = db_fetch_array( $result );
$g_category_cache[$p_category_id] = $row;
return $row;
}
# --------------------
# Return all categories for the specified project id
function category_get_all_rows( $p_project_id ) {
global $g_category_cache;
$c_project_id = db_prepare_int( $p_project_id );
$t_category_table = db_get_table( 'mantis_category_table' );
$t_project_table = db_get_table( 'mantis_project_table' );
$t_project_where = helper_project_specific_where( $c_project_id );
$query = "SELECT c.*, p.name AS project_name FROM $t_category_table AS c
JOIN $t_project_table AS p
ON c.project_id=p.id
WHERE $t_project_where
ORDER BY c.name ";
$result = db_query_bound( $query );
$count = db_num_rows( $result );
$rows = array();
for ( $i = 0 ; $i < $count ; $i++ ) {
$row = db_fetch_array( $result );
$rows[] = $row;
$g_category_cache[$row['id']] = $row;
}
return $rows;
}
# --------------------
# Given a category id and a field name, this function returns the field value.
# An error will be triggered for a non-existent category id or category id = 0.
function category_get_field( $p_category_id, $p_field_name ) {
$t_row = category_get_row( $p_category_id );
return $t_row[$p_field_name];
}
# --------------------
# Given a category id, this function returns the category name.
# An error will be triggered for a non-existent category id or category id = 0.
function category_get_name( $p_category_id ) {
return category_get_field( $p_category_id, 'name' );
}
# Helpers
function category_full_name( $p_category_id, $p_show_project=true ) {
if ( 0 == $p_category_id ) { # No Category
return lang_get( 'no_category' );
} else {
$t_row = category_get_row( $p_category_id );
$t_project_id = $t_row['project_id'];
if ( $p_show_project && ALL_PROJECTS != $t_project_id ) {
return '[' . project_get_name( $t_project_id ) . '] ' . $t_row['name'];
}
return $t_row['name'];
}
}