/
bugnote_api.php
303 lines (232 loc) · 8.31 KB
/
bugnote_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
<?php
# Mantis - a php based bugtracking system
# Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
# Copyright (C) 2002 - 2003 Mantis Team - mantisbt-dev@lists.sourceforge.net
# This program is distributed under the terms and conditions of the GPL
# See the README and LICENSE files for details
# --------------------------------------------------------
# $Id: bugnote_api.php,v 1.13 2003-01-25 18:21:08 jlatour Exp $
# --------------------------------------------------------
$t_core_dir = dirname( __FILE__ ).DIRECTORY_SEPARATOR;
require_once( $t_core_dir . 'current_user_api.php' );
require_once( $t_core_dir . 'email_api.php' );
require_once( $t_core_dir . 'history_api.php' );
require_once( $t_core_dir . 'bug_api.php' );
###########################################################################
# Bugnote API
###########################################################################
#===================================
# Boolean queries and ensures
#===================================
# --------------------
# Check if a bugnote with the given ID exists
#
# return true if the bugnote exists, false otherwise
function bugnote_exists( $p_bugnote_id ) {
$c_bugnote_id = db_prepare_int( $p_bugnote_id );
$t_bugnote_table = config_get( 'mantis_bugnote_table' );
$query ="SELECT COUNT(*) ".
"FROM $t_bugnote_table ".
"WHERE id='$c_bugnote_id'";
$result = db_query( $query );
if ( 0 == db_result( $result ) ) {
return false;
} else {
return true;
}
}
# --------------------
# Check if a bugnote with the given ID exists
#
# return true if the bugnote exists, raise an error if not
function bugnote_ensure_exists( $p_bugnote_id ) {
if ( ! bugnote_exists( $p_bugnote_id ) ) {
trigger_error( ERROR_BUGNOTE_NOT_FOUND, ERROR );
}
}
#===================================
# Creation / Deletion / Updating
#===================================
# --------------------
# Add a bugnote to a bug
#
# return the ID of the new bugnote
function bugnote_add ( $p_bug_id, $p_bugnote_text, $p_private=false )
{
$c_bug_id = db_prepare_int( $p_bug_id );
$c_bugnote_text = db_prepare_string( $p_bugnote_text );
$c_private = db_prepare_bool( $p_private );
$t_bugnote_text_table = config_get( 'mantis_bugnote_text_table' );
$t_bugnote_table = config_get( 'mantis_bugnote_table' );
# insert bugnote text
$query = "INSERT
INTO $t_bugnote_text_table
( id, note )
VALUES
( null, '$c_bugnote_text' )";
db_query( $query );
# retrieve bugnote text id number
$t_bugnote_text_id = db_insert_id();
# Check for private bugnotes.
if ( $p_private && access_level_check_greater_or_equal( config_get( 'private_bugnote_threshold' ) ) ) {
$t_view_state = PRIVATE;
} else {
$t_view_state = PUBLIC;
}
# get user information
$t_user_id = current_user_get_field( 'id' );
# insert bugnote info
$query = "INSERT
INTO $t_bugnote_table
( id, bug_id, reporter_id, bugnote_text_id, view_state, date_submitted, last_modified )
VALUES
( null, '$c_bug_id', '$t_user_id','$t_bugnote_text_id', '$t_view_state', NOW(), NOW() )";
db_query( $query );
# get bugnote id
$t_bugnote_id = db_insert_id();
# update bug last updated
bug_update_date( $p_bug_id );
# log new bug
history_log_event_special( $p_bug_id, BUGNOTE_ADDED, bugnote_format_id( $t_bugnote_id ) );
email_bugnote_add( $p_bug_id );
return $t_bugnote_id;
}
# --------------------
# Delete a bugnote
function bugnote_delete( $p_bugnote_id ) {
$c_bugnote_id = db_prepare_int( $p_bugnote_id );
$t_bug_id = bugnote_get_field( $p_bugnote_id, 'bug_id' );
$t_bugnote_text_id = bugnote_get_field( $p_bugnote_id, 'bugnote_text_id' );
$t_bugnote_text_table = config_get( 'mantis_bugnote_text_table' );
$t_bugnote_table = config_get( 'mantis_bugnote_table' );
# Remove the bugnote
$query = "DELETE
FROM $t_bugnote_table
WHERE id='$c_bugnote_id'";
db_query( $query );
# Remove the bugnote text
$query = "DELETE
FROM $t_bugnote_text_table
WHERE id='$t_bugnote_text_id'";
db_query( $query );
# log deletion of bug
history_log_event_special( $t_bug_id, BUGNOTE_DELETED, bugnote_format_id( $p_bugnote_id ) );
return true;
}
# --------------------
# delete all bugnotes associated with the given bug
function bugnote_delete_all( $p_bug_id ) {
$c_bug_id = db_prepare_int( $p_bug_id );
$t_bugnote_table = config_get( 'mantis_bugnote_table' );
$t_bugnote_text_table = config_get( 'mantis_bugnote_text_table' );
# Delete the bugnote text items
$query = "SELECT bugnote_text_id
FROM $t_bugnote_table
WHERE bug_id='$c_bug_id'";
$result = db_query($query);
$bugnote_count = db_num_rows( $result );
for ( $i = 0 ; $i < $bugnote_count ; $i++ ) {
$row = db_fetch_array( $result );
$t_bugnote_text_id = $row['bugnote_text_id'];
# Delete the corresponding bugnote texts
$query = "DELETE
FROM $t_bugnote_text_table
WHERE id='$t_bugnote_text_id'";
$result = db_query( $query );
}
# Delete the corresponding bugnotes
$query = "DELETE
FROM $t_bugnote_table
WHERE bug_id='$c_bug_id'";
$result = db_query($query);
# db_query() errors on failure so:
return true;
}
#===================================
# Data Access
#===================================
# --------------------
# Get the text associated with the bugnote
function bugnote_get_text( $p_bugnote_id ) {
$t_bugnote_text_id = bugnote_get_field( $p_bugnote_id, 'bugnote_text_id' );
$t_bugnote_text_table = config_get( 'mantis_bugnote_text_table' );
# grab the bugnote text
$query = "SELECT note
FROM $t_bugnote_text_table
WHERE id='$t_bugnote_text_id'";
$result = db_query( $query );
return db_result( $result );
}
# --------------------
# Get a field for the given bugnote
function bugnote_get_field( $p_bugnote_id, $p_field_name ) {
$c_bugnote_id = db_prepare_int( $p_bugnote_id );
$c_field_name = db_prepare_string( $p_field_name );
$t_bugnote_table = config_get( 'mantis_bugnote_table' );
$query ="SELECT $c_field_name ".
"FROM $t_bugnote_table ".
"WHERE id='$c_bugnote_id' ".
"LIMIT 1";
$result = db_query( $query );
return db_result( $result );
}
#===================================
# Data Modification
#===================================
# --------------------
# Update the last_modified field of the bugnote
function bugnote_date_update( $p_bugnote_id ) {
$c_bugnote_id = db_prepare_int( $p_bugnote_id );
$t_bugnote_table = config_get( 'mantis_bugnote_table' );
$query = "UPDATE $t_bugnote_table
SET last_modified=NOW()
WHERE id='$c_bugnote_id'";
db_query( $query );
# db_query() errors if there was a problem so:
return true;
}
# --------------------
# Set the bugnote text
function bugnote_set_text( $p_bugnote_id, $p_bugnote_text ) {
$c_bugnote_text = db_prepare_string( $p_bugnote_text );
$t_bug_id = bugnote_get_field( $p_bugnote_id, 'bug_id' );
$t_bugnote_text_id = bugnote_get_field( $p_bugnote_id, 'bugnote_text_id' );
$t_bugnote_text_table = config_get( 'mantis_bugnote_text_table' );
$query = "UPDATE $t_bugnote_text_table
SET note='$c_bugnote_text'
WHERE id='$t_bugnote_text_id'";
db_query( $query );
# updated the last_updated date
bugnote_date_update( $p_bugnote_id );
# log new bugnote
history_log_event_special( $t_bug_id, BUGNOTE_UPDATED, bugnote_format_id( $p_bugnote_id ) );
return true;
}
# --------------------
# Set the view state of the bugnote
function bugnote_set_view_state( $p_bugnote_id, $p_private ) {
$c_bugnote_id = db_prepare_int( $p_bugnote_id );
$t_bug_id = bugnote_get_field( $p_bugnote_id, 'bug_id' );
if ( $p_private ) {
$t_view_state = PRIVATE;
} else {
$t_view_state = PUBLIC;
}
$t_bugnote_table = config_get( 'mantis_bugnote_table' );
# update view_state
$query = "UPDATE $t_bugnote_table
SET view_state='$t_view_state'
WHERE id='$c_bugnote_id'";
db_query( $query );
history_log_event_special( $t_bug_id, BUGNOTE_STATE_CHANGED, bugnote_format_id( $t_view_state ), $p_bugnote_id );
return true;
}
#===================================
# Other
#===================================
# --------------------
# Pad the bugnote id with the appropriate number of zeros for printing
function bugnote_format_id( $p_bug_id ) {
return( str_pad( $p_bug_id, 7, '0', STR_PAD_LEFT ) );
}
?>