2 # MantisBT - A PHP based bugtracking system
4 # MantisBT is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 2 of the License, or
7 # (at your option) any later version.
9 # MantisBT is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
21 * @subpackage SponsorshipAPI
22 * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
23 * @copyright Copyright (C) 2002 - 2010 MantisBT Team - mantisbt-dev@lists.sourceforge.net
24 * @link http://www.mantisbt.org
26 * @uses authentication_api.php
28 * @uses config_api.php
29 * @uses constant_inc.php
30 * @uses database_api.php
33 * @uses history_api.php
36 require_api( 'authentication_api.php' );
37 require_api( 'bug_api.php' );
38 require_api( 'config_api.php' );
39 require_api( 'constant_inc.php' );
40 require_api( 'database_api.php' );
41 require_api( 'email_api.php' );
42 require_api( 'error_api.php' );
43 require_api( 'history_api.php' );
46 * Sponsorship Data Structure Definition
50 class SponsorshipData
{
59 var $date_submitted = '';
60 var $last_updated = '';
63 # ########################################
64 # SECURITY NOTE: cache globals are initialized here to prevent them
65 # being spoofed if register_globals is turned on
67 $g_cache_sponsorships = array();
70 * Cache a sponsorship row if necessary and return the cached copy
71 * If the second parameter is true (default), trigger an error
72 * if the sponsorship can't be found. If the second parameter is
73 * false, return false if the sponsorship can't be found.
74 * @param int $p_sponsorship_id
75 * @param bool $p_trigger_errors
78 function sponsorship_cache_row( $p_sponsorship_id, $p_trigger_errors = true ) {
79 global $g_cache_sponsorships;
81 $c_sponsorship_id = db_prepare_int( $p_sponsorship_id );
82 $t_sponsorship_table = db_get_table( 'sponsorship' );
84 if( isset( $g_cache_sponsorships[$c_sponsorship_id] ) ) {
85 return $g_cache_sponsorships[$c_sponsorship_id];
89 FROM $t_sponsorship_table
90 WHERE id=" . db_param();
91 $result = db_query_bound( $query, Array( $c_sponsorship_id ) );
93 if( 0 == db_num_rows( $result ) ) {
94 $g_cache_sponsorships[$c_sponsorship_id] = false;
96 if( $p_trigger_errors ) {
97 error_parameters( $p_sponsorship_id );
98 trigger_error( ERROR_SPONSORSHIP_NOT_FOUND
, ERROR
);
104 $row = db_fetch_array( $result );
105 $g_cache_sponsorships[$c_sponsorship_id] = $row;
111 * Clear the sponsorship cache (or just the given id if specified)
112 * @param int $p_sponsorship_id
115 function sponsorship_clear_cache( $p_sponsorship_id = null ) {
116 global $g_cache_sponsorships;
118 if( $p_sponsorship_id === null ) {
119 $g_cache_sponsorships = array();
121 $c_sponsorship_id = db_prepare_int( $p_sponsorship_id );
122 unset( $g_cache_sponsorships[$c_sponsorship_id] );
127 * check to see if sponsorship exists by id
128 * return true if it does, false otherwise
129 * @param int $p_sponsorship_id
132 function sponsorship_exists( $p_sponsorship_id ) {
133 return sponsorship_cache_row( $p_sponsorship_id, false ) !== false;
137 * return false if not found
138 * otherwise returns sponsorship id
139 * @param int $p_bug_id
140 * @param int $p_user_id
143 function sponsorship_get_id( $p_bug_id, $p_user_id = null ) {
144 $c_bug_id = db_prepare_int( $p_bug_id );
146 if( $p_user_id === null ) {
147 $c_user_id = auth_get_current_user_id();
149 $c_user_id = db_prepare_int( $p_user_id );
152 $t_sponsorship_table = db_get_table( 'sponsorship' );
154 $query = "SELECT id FROM $t_sponsorship_table WHERE bug_id = " . db_param() . " AND user_id = " . db_param();
155 $t_result = db_query_bound( $query, Array( $c_bug_id, $c_user_id ), 1 );
157 if( db_num_rows( $t_result ) == 0 ) {
161 $row = db_fetch_array( $t_result );
163 return (integer) $row['id'];
167 * get information about a sponsorship given its id
168 * @param int $p_sponsorship_id
171 function sponsorship_get( $p_sponsorship_id ) {
172 $row = sponsorship_cache_row( $p_sponsorship_id );
174 $t_sponsorship_data = new SponsorShipData
;
175 $t_row_keys = array_keys( $row );
176 $t_vars = get_object_vars( $t_sponsorship_data );
178 # Check each variable in the class
179 foreach( $t_vars as $var => $val ) {
180 # If we got a field from the DB with the same name
181 if( in_array( $var, $t_row_keys, true ) ) {
182 # Store that value in the object
183 $t_sponsorship_data->$var = $row[$var];
187 return $t_sponsorship_data;
191 * Return an array of Sponsorships associated with the specified bug id
192 * @param int $p_bug_id
195 function sponsorship_get_all_ids( $p_bug_id ) {
196 global $g_cache_sponsorships;
197 static $s_cache_sponsorship_bug_ids = array();
199 $c_bug_id = db_prepare_int( $p_bug_id );
201 if( isset( $s_cache_sponsorship_bug_ids[$c_bug_id] ) ) {
202 return $s_cache_sponsorship_bug_ids[$c_bug_id];
205 $t_sponsorship_table = db_get_table( 'sponsorship' );
207 $query = "SELECT * FROM $t_sponsorship_table
208 WHERE bug_id = " . db_param();
209 $t_result = db_query_bound( $query, Array( $c_bug_id ) );
211 $t_sponsorship_ids = array();
212 while( $row = db_fetch_array( $t_result ) ) {
213 $t_sponsorship_ids[] = $row['id'];
214 $g_cache_sponsorships[(int)$row['id']] = $row;
217 $s_cache_sponsorship_bug_ids[$c_bug_id] = $t_sponsorship_ids;
219 return $t_sponsorship_ids;
223 * Get the amount of sponsorships for the specified id(s)
224 * handles the case where $p_sponsorship_id is an array or an id.
225 * @param int $p_sponsorship_id
228 function sponsorship_get_amount( $p_sponsorship_id ) {
229 if( is_array( $p_sponsorship_id ) ) {
232 foreach( $p_sponsorship_id as $id ) {
233 $t_total +
= sponsorship_get_amount( $id );
238 $sponsorship = sponsorship_get( $p_sponsorship_id );
239 return $sponsorship->amount
;
244 * Return the currency used for all sponsorships
247 function sponsorship_get_currency() {
248 return config_get( 'sponsorship_currency' );
252 * This function should return the string in a globalized format.
253 * @param int $p_amount
255 * @todo add some currency formating in the future
257 function sponsorship_format_amount( $p_amount ) {
258 $t_currency = sponsorship_get_currency();
259 return $t_currency . ' ' . $p_amount;
263 * Update bug to reflect sponsorship change
264 * This is to be called after adding/updating/deleting sponsorships
265 * @param int $p_bug_id
268 function sponsorship_update_bug( $p_bug_id ) {
269 $t_total_amount = sponsorship_get_amount( sponsorship_get_all_ids( $p_bug_id ) );
270 bug_set_field( $p_bug_id, 'sponsorship_total', $t_total_amount );
271 bug_update_date( $p_bug_id );
275 * if sponsorship contains a non-zero id, then update the corresponding record.
276 * if sponsorship contains a zero id, search for bug_id/user_id, if found, then update the entry
277 * otherwise add a new entry
278 * @param int $p_sponsorship
281 function sponsorship_set( $p_sponsorship ) {
282 $t_min_sponsorship = config_get( 'minimum_sponsorship_amount' );
283 if( $p_sponsorship->amount
< $t_min_sponsorship ) {
284 error_parameters( $p_sponsorship->amount
, $t_min_sponsorship );
285 trigger_error( ERROR_SPONSORSHIP_AMOUNT_TOO_LOW
, ERROR
);
288 # if id == 0, check if the specified user is already sponsoring the bug, if so, overwrite
289 if( $p_sponsorship->id
== 0 ) {
290 $t_sponsorship_id = sponsorship_get_id( $p_sponsorship->bug_id
, $p_sponsorship->user_id
);
291 if( $t_sponsorship_id !== false ) {
292 $p_sponsorship->id
= $t_sponsorship_id;
296 $t_sponsorship_table = db_get_table( 'sponsorship' );
298 $c_id = db_prepare_int( $p_sponsorship->id
);
299 $c_bug_id = db_prepare_int( $p_sponsorship->bug_id
);
300 $c_user_id = db_prepare_int( $p_sponsorship->user_id
);
301 $c_amount = db_prepare_int( $p_sponsorship->amount
);
302 $c_logo = $p_sponsorship->logo
;
303 $c_url = $p_sponsorship->url
;
309 $query = "INSERT INTO $t_sponsorship_table
310 ( bug_id, user_id, amount, logo, url, date_submitted, last_updated )
312 (" . db_param() . ',' . db_param() . ',' . db_param() . ',' . db_param() . ',' . db_param() . ',' . db_param() . ',' . db_param() . ')';
314 db_query_bound( $query, Array( $c_bug_id, $c_user_id, $c_amount, $c_logo, $c_url, $c_now, $c_now ) );
315 $t_sponsorship_id = db_insert_id( $t_sponsorship_table );
317 history_log_event_special( $c_bug_id, BUG_ADD_SPONSORSHIP
, $c_user_id, $c_amount );
319 $t_old_amount = sponsorship_get_amount( $c_id );
320 $t_sponsorship_id = $c_id;
322 if( $t_old_amount == $c_amount ) {
323 return $t_sponsorship_id;
327 $query = "UPDATE $t_sponsorship_table
328 SET bug_id = " . db_param() . ",
329 user_id = " . db_param() . ",
330 amount = " . db_param() . ",
331 logo = " . db_param() . ",
332 url = " . db_param() . ",
333 last_updated = " . db_param() . "
334 WHERE id = " . db_param();
336 sponsorship_clear_cache( $c_id );
338 db_query_bound( $query, Array( $c_bug_id, $c_user_id, $c_amount, $c_logo, $c_url, $c_now, $c_id ) );
340 history_log_event_special( $c_bug_id, BUG_UPDATE_SPONSORSHIP
, $c_user_id, $c_amount );
343 sponsorship_update_bug( $c_bug_id );
344 bug_monitor( $c_bug_id, $c_user_id );
347 email_sponsorship_added( $c_bug_id );
349 email_sponsorship_updated( $c_bug_id );
352 return $t_sponsorship_id;
356 * delete a sponsorship given its id
357 * id can be an array of ids or just an id.
358 * @param int $p_sponsorship_id
361 function sponsorship_delete( $p_sponsorship_id ) {
362 # handle the case of array of ids
363 if( is_array( $p_sponsorship_id ) ) {
364 foreach( $p_sponsorship_id as $id ) {
365 sponsorship_delete( $id );
370 $c_sponsorship_id = db_prepare_int( $p_sponsorship_id );
372 $t_sponsorship = sponsorship_get( $c_sponsorship_id );
374 $t_sponsorship_table = db_get_table( 'sponsorship' );
376 # Delete the bug entry
377 $query = "DELETE FROM $t_sponsorship_table
378 WHERE id=" . db_param();
379 db_query_bound( $query, Array( $c_sponsorship_id ) );
381 sponsorship_clear_cache( $p_sponsorship_id );
383 history_log_event_special( $t_sponsorship->bug_id
, BUG_DELETE_SPONSORSHIP
, $t_sponsorship->user_id
, $t_sponsorship->amount
);
384 sponsorship_update_bug( $t_sponsorship->bug_id
);
386 email_sponsorship_deleted( $t_sponsorship->bug_id
);
390 * updates the paid field
391 * @param int $p_sponsorship_id
395 function sponsorship_update_paid( $p_sponsorship_id, $p_paid ) {
396 $c_sponsorship_id = db_prepare_int( $p_sponsorship_id );
397 $t_sponsorship = sponsorship_get( $c_sponsorship_id );
399 $c_paid = db_prepare_int( $p_paid );
401 $t_sponsorship_table = db_get_table( 'sponsorship' );
403 $query = "UPDATE $t_sponsorship_table
404 SET last_updated= " . db_param() . ", paid=" . db_param() . "
405 WHERE id=" . db_param();
406 db_query_bound( $query, Array( db_now(), $c_paid, $c_sponsorship_id ) );
408 history_log_event_special( $t_sponsorship->bug_id
, BUG_PAID_SPONSORSHIP
, $t_sponsorship->user_id
, $p_paid );
409 sponsorship_clear_cache( $p_sponsorship_id );
415 * updates the last_updated field
416 * @param int $p_sponsorship_id
419 function sponsorship_update_date( $p_sponsorship_id ) {
420 $c_sponsorship_id = db_prepare_int( $p_sponsorship_id );
422 $t_sponsorship_table = db_get_table( 'sponsorship' );
424 $query = "UPDATE $t_sponsorship_table
425 SET last_updated= " . db_param() . "
426 WHERE id=" . db_param();
427 db_query_bound( $query, Array( db_now(), $c_sponsorship_id ) );
429 sponsorship_clear_cache( $p_sponsorship_id );