/
sponsorship_api.php
364 lines (288 loc) · 11.1 KB
/
sponsorship_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
<?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: sponsorship_api.php,v 1.6.16.1 2007-10-13 22:35:43 giallu Exp $
# --------------------------------------------------------
$t_core_dir = dirname( __FILE__ ).DIRECTORY_SEPARATOR;
require_once( $t_core_dir . 'email_api.php' );
require_once( $t_core_dir . 'bug_api.php' );
require_once( $t_core_dir . 'history_api.php' );
#=========================================
# Sponsorship Data Structure Definition
#===================================
class SponsorshipData {
var $id = 0;
var $bug_id = 0;
var $user_id = 0;
var $amount = 0;
var $logo = '';
var $url = '';
var $paid = 0;
var $date_submitted = '';
var $last_updated = '';
}
#########################################
# SECURITY NOTE: cache globals are initialized here to prevent them
# being spoofed if register_globals is turned on
$g_cache_sponsorships = array();
# --------------------
# Cache a sponsorship row if necessary and return the cached copy
# If the second parameter is true (default), trigger an error
# if the sponsorship can't be found. If the second parameter is
# false, return false if the sponsorship can't be found.
function sponsorship_cache_row( $p_sponsorship_id, $p_trigger_errors = true ) {
global $g_cache_sponsorships;
$c_sponsorship_id = db_prepare_int( $p_sponsorship_id );
$t_sponsorship_table = config_get( 'mantis_sponsorship_table' );
if ( isset( $g_cache_sponsorships[$c_sponsorship_id] ) ) {
return $g_cache_sponsorships[$c_sponsorship_id];
}
$query = "SELECT *
FROM $t_sponsorship_table
WHERE id='$c_sponsorship_id'";
$result = db_query( $query );
if ( 0 == db_num_rows( $result ) ) {
$g_cache_sponsorships[$c_sponsorship_id] = false;
if ( $p_trigger_errors ) {
error_parameters( $p_sponsorship_id );
trigger_error( ERROR_SPONSORSHIP_NOT_FOUND, ERROR );
} else {
return false;
}
}
$row = db_fetch_array( $result );
$row['date_submitted'] = db_unixtimestamp( $row['date_submitted'] );
$row['last_updated'] = db_unixtimestamp( $row['last_updated'] );
$g_cache_sponsorships[$c_sponsorship_id] = $row;
return $row;
}
# --------------------
# Clear the sponsorship cache (or just the given id if specified)
function sponsorship_clear_cache( $p_sponsorship_id = null ) {
global $g_cache_sponsorships;
if ( $p_sponsorship_id === null ) {
$g_cache_sponsorships = array();
} else {
$c_sponsorship_id = db_prepare_int( $p_sponsorship_id );
unset( $g_cache_sponsorships[$c_sponsorship_id] );
}
}
# --------------------
# check to see if sponsorship exists by id
# return true if it does, false otherwise
function sponsorship_exists( $p_sponsorship_id ) {
return sponsorship_cache_row( $p_sponsorship_id, false ) !== false;
}
# --------------------
# return false if not found
# otherwise returns sponsorship id
function sponsorship_get_id( $p_bug_id, $p_user_id = null ) {
$c_bug_id = db_prepare_int( $p_bug_id );
if ( $p_user_id === null ) {
$c_user_id = auth_get_current_user_id();
} else {
$c_user_id = db_prepare_int( $p_user_id );
}
$t_sponsorship_table = config_get( 'mantis_sponsorship_table' );
$query = "SELECT id FROM $t_sponsorship_table WHERE bug_id = '$c_bug_id' AND user_id = '$c_user_id'";
$t_result = db_query( $query, 1 );
if ( db_num_rows( $t_result ) == 0 ) {
return false;
}
$row = db_fetch_array( $t_result );
return (integer)$row['id'];
}
# --------------------
# get information about a sponsorship given its id
function sponsorship_get( $p_sponsorship_id ) {
$row = sponsorship_cache_row( $p_sponsorship_id );
$t_sponsorship_data = new SponsorShipData;
$t_row_keys = array_keys( $row );
$t_vars = get_object_vars( $t_sponsorship_data );
# Check each variable in the class
foreach ( $t_vars as $var => $val ) {
# If we got a field from the DB with the same name
if ( in_array( $var, $t_row_keys, true ) ) {
# Store that value in the object
$t_sponsorship_data->$var = $row[$var];
}
}
return $t_sponsorship_data;
}
# --------------------
# Return an array of Sponsorships associated with the specified bug id
function sponsorship_get_all_ids( $p_bug_id ) {
$c_bug_id = db_prepare_int( $p_bug_id );
$t_sponsorship_table = config_get( 'mantis_sponsorship_table' );
$query = "SELECT id FROM $t_sponsorship_table
WHERE bug_id = '$c_bug_id'";
$t_result = db_query( $query );
$t_sponsorship_ids = array();
while ( $row = db_fetch_array( $t_result ) ) {
$t_sponsorship_ids[] = $row['id'];
}
return $t_sponsorship_ids;
}
# --------------------
# Get the amount of sponsorships for the specified id(s)
# handles the case where $p_sponsorship_id is an array or an id.
function sponsorship_get_amount( $p_sponsorship_id ) {
if ( is_array( $p_sponsorship_id ) ) {
$t_total = 0;
foreach( $p_sponsorship_id as $id ) {
$t_total += sponsorship_get_amount( $id );
}
return $t_total;
} else {
$sponsorship = sponsorship_get( $p_sponsorship_id );
return $sponsorship->amount;
}
}
# --------------------
# Return the currency used for all sponsorships
function sponsorship_get_currency() {
return config_get( 'sponsorship_currency' );
}
# --------------------
# This function should return the string in a globalized format.
function sponsorship_format_amount( $amount ) {
# @@@ add some currency formating in the future
$t_currency = sponsorship_get_currency();
return "$t_currency $amount";
}
# --------------------
# Update bug to reflect sponsorship change
# This is to be called after adding/updating/deleting sponsorships
function sponsorship_update_bug( $p_bug_id ) {
$t_total_amount = sponsorship_get_amount( sponsorship_get_all_ids( $p_bug_id ) );
bug_set_field( $p_bug_id, 'sponsorship_total', $t_total_amount );
bug_update_date( $p_bug_id );
}
# --------------------
# if sponsorship contains a non-zero id, then update the corresponding record.
# if sponsorship contains a zero id, search for bug_id/user_id, if found, then update the entry
# otherwise add a new entry
function sponsorship_set( $p_sponsorship ) {
$t_min_sponsorship = config_get( 'minimum_sponsorship_amount' );
if ( $p_sponsorship->amount < $t_min_sponsorship ) {
error_parameters( $p_sponsorship->amount, $t_min_sponsorship );
trigger_error( ERROR_SPONSORSHIP_AMOUNT_TOO_LOW, ERROR );
}
# if id == 0, check if the specified user is already sponsoring the bug, if so, overwrite
if ( $p_sponsorship->id == 0 ) {
$t_sponsorship_id = sponsorship_get_id( $p_sponsorship->bug_id, $p_sponsorship->user_id );
if ( $t_sponsorship_id !== false ) {
$p_sponsorship->id = $t_sponsorship_id;
}
}
$t_sponsorship_table = config_get( 'mantis_sponsorship_table' );
$c_id = db_prepare_int( $p_sponsorship->id );
$c_bug_id = db_prepare_int( $p_sponsorship->bug_id );
$c_user_id = db_prepare_int( $p_sponsorship->user_id );
$c_amount = db_prepare_int( $p_sponsorship->amount );
$c_logo = db_prepare_string( $p_sponsorship->logo );
$c_url = db_prepare_string( $p_sponsorship->url );
$c_now = db_now();
# if new sponsorship
if ( $c_id == 0 ) {
# Insert
$query = "INSERT INTO $t_sponsorship_table
( bug_id, user_id, amount, logo, url, date_submitted, last_updated )
VALUES
( '$c_bug_id', '$c_user_id', '$c_amount', '$c_logo', '$c_url', $c_now, $c_now )";
db_query( $query );
$t_sponsorship_id = db_insert_id( $t_sponsorship_table );
history_log_event_special( $c_bug_id, BUG_ADD_SPONSORSHIP, $c_user_id, $c_amount );
} else {
$t_old_amount = sponsorship_get_amount( $c_id );
$t_sponsorship_id = $c_id;
if ( $t_old_amount == $c_amount ) {
return $t_sponsorship_id;
}
# Update
$query = "UPDATE $t_sponsorship_table
SET bug_id = '$c_bug_id',
user_id = '$c_user_id',
amount = '$c_amount',
logo = '$c_logo',
url = '$c_url',
last_updated = $c_now
WHERE id = '$c_id'";
sponsorship_clear_cache( $c_id );
db_query( $query );
history_log_event_special( $c_bug_id, BUG_UPDATE_SPONSORSHIP, $c_user_id, $c_amount );
}
sponsorship_update_bug( $c_bug_id );
bug_monitor( $c_bug_id, $c_user_id );
if ( $c_id == 0 ) {
email_sponsorship_added( $c_bug_id );
} else {
email_sponsorship_updated( $c_bug_id );
}
return $t_sponsorship_id;
}
# --------------------
# delete a sponsorship given its id
# id can be an array of ids or just an id.
function sponsorship_delete( $p_sponsorship_id ) {
# handle the case of array of ids
if ( is_array( $p_sponsorship_id ) ) {
foreach( $p_sponsorship_id as $id ) {
sponsorship_delete( $id );
}
return;
}
$c_sponsorship_id = db_prepare_int( $p_sponsorship_id );
$t_sponsorship = sponsorship_get( $c_sponsorship_id );
$t_sponsorship_table = config_get( 'mantis_sponsorship_table' );
# Delete the bug entry
$query = "DELETE FROM $t_sponsorship_table
WHERE id='$c_sponsorship_id'";
db_query( $query );
sponsorship_clear_cache( $p_sponsorship_id );
history_log_event_special( $t_sponsorship->bug_id, BUG_DELETE_SPONSORSHIP, $t_sponsorship->user_id, $t_sponsorship->amount );
sponsorship_update_bug( $t_sponsorship->bug_id );
email_sponsorship_deleted( $t_sponsorship->bug_id );
}
# --------------------
# updates the paid field
function sponsorship_update_paid( $p_sponsorship_id, $p_paid ) {
$c_sponsorship_id = db_prepare_int( $p_sponsorship_id );
$t_sponsorship = sponsorship_get( $c_sponsorship_id );
$c_paid = db_prepare_int( $p_paid );
$t_sponsorship_table = config_get( 'mantis_sponsorship_table' );
$query = "UPDATE $t_sponsorship_table
SET last_updated= " . db_now() . ", paid=$c_paid
WHERE id='$c_sponsorship_id'";
db_query( $query );
history_log_event_special( $t_sponsorship->bug_id, BUG_PAID_SPONSORSHIP, $t_sponsorship->user_id, $p_paid );
sponsorship_clear_cache( $p_sponsorship_id );
return true;
}
# --------------------
# updates the last_updated field
function sponsorship_update_date( $p_sponsorship_id ) {
$c_sponsorship_id = db_prepare_int( $p_sponsorship_id );
$t_sponsorship_table = config_get( 'mantis_sponsorship_table' );
$query = "UPDATE $t_sponsorship_table
SET last_updated= " . db_now() . "
WHERE id='$c_sponsorship_id'";
db_query( $query );
sponsorship_clear_cache( $p_sponsorship_id );
return true;
}
?>