| [ Index ] |
PHP Cross Reference of MantisBT |
[Summary view] [Print] [Text view]
1 <?php 2 # MantisBT - A PHP based bugtracking system 3 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. 8 # 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. 13 # 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/>. 16 17 /** 18 * Sponsorship API 19 * 20 * @package CoreAPI 21 * @subpackage SponsorshipAPI 22 * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org 23 * @copyright Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net 24 * @link http://www.mantisbt.org 25 * 26 * @uses authentication_api.php 27 * @uses bug_api.php 28 * @uses config_api.php 29 * @uses constant_inc.php 30 * @uses database_api.php 31 * @uses email_api.php 32 * @uses error_api.php 33 * @uses history_api.php 34 */ 35 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' ); 44 45 /** 46 * Sponsorship Data Structure Definition 47 * @package MantisBT 48 * @subpackage classes 49 */ 50 class SponsorshipData { 51 var $id = 0; 52 var $bug_id = 0; 53 var $user_id = 0; 54 var $amount = 0; 55 var $logo = ''; 56 var $url = ''; 57 var $paid = 0; 58 59 var $date_submitted = ''; 60 var $last_updated = ''; 61 } 62 63 # ######################################## 64 # SECURITY NOTE: cache globals are initialized here to prevent them 65 # being spoofed if register_globals is turned on 66 67 $g_cache_sponsorships = array(); 68 69 /** 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 76 * @return array 77 */ 78 function sponsorship_cache_row( $p_sponsorship_id, $p_trigger_errors = true ) { 79 global $g_cache_sponsorships; 80 81 $c_sponsorship_id = db_prepare_int( $p_sponsorship_id ); 82 $t_sponsorship_table = db_get_table( 'sponsorship' ); 83 84 if( isset( $g_cache_sponsorships[$c_sponsorship_id] ) ) { 85 return $g_cache_sponsorships[$c_sponsorship_id]; 86 } 87 88 $query = "SELECT * 89 FROM $t_sponsorship_table 90 WHERE id=" . db_param(); 91 $result = db_query_bound( $query, Array( $c_sponsorship_id ) ); 92 93 if( 0 == db_num_rows( $result ) ) { 94 $g_cache_sponsorships[$c_sponsorship_id] = false; 95 96 if( $p_trigger_errors ) { 97 error_parameters( $p_sponsorship_id ); 98 trigger_error( ERROR_SPONSORSHIP_NOT_FOUND, ERROR ); 99 } else { 100 return false; 101 } 102 } 103 104 $row = db_fetch_array( $result ); 105 $g_cache_sponsorships[$c_sponsorship_id] = $row; 106 107 return $row; 108 } 109 110 /** 111 * Clear the sponsorship cache (or just the given id if specified) 112 * @param int $p_sponsorship_id 113 * @return null 114 */ 115 function sponsorship_clear_cache( $p_sponsorship_id = null ) { 116 global $g_cache_sponsorships; 117 118 if( $p_sponsorship_id === null ) { 119 $g_cache_sponsorships = array(); 120 } else { 121 $c_sponsorship_id = db_prepare_int( $p_sponsorship_id ); 122 unset( $g_cache_sponsorships[$c_sponsorship_id] ); 123 } 124 } 125 126 /** 127 * check to see if sponsorship exists by id 128 * return true if it does, false otherwise 129 * @param int $p_sponsorship_id 130 * @return bool 131 */ 132 function sponsorship_exists( $p_sponsorship_id ) { 133 return sponsorship_cache_row( $p_sponsorship_id, false ) !== false; 134 } 135 136 /** 137 * return false if not found 138 * otherwise returns sponsorship id 139 * @param int $p_bug_id 140 * @param int $p_user_id 141 * @return int|false 142 */ 143 function sponsorship_get_id( $p_bug_id, $p_user_id = null ) { 144 $c_bug_id = db_prepare_int( $p_bug_id ); 145 146 if( $p_user_id === null ) { 147 $c_user_id = auth_get_current_user_id(); 148 } else { 149 $c_user_id = db_prepare_int( $p_user_id ); 150 } 151 152 $t_sponsorship_table = db_get_table( 'sponsorship' ); 153 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 ); 156 157 if( db_num_rows( $t_result ) == 0 ) { 158 return false; 159 } 160 161 $row = db_fetch_array( $t_result ); 162 163 return (integer) $row['id']; 164 } 165 166 /** 167 * get information about a sponsorship given its id 168 * @param int $p_sponsorship_id 169 * @return array 170 */ 171 function sponsorship_get( $p_sponsorship_id ) { 172 $row = sponsorship_cache_row( $p_sponsorship_id ); 173 174 $t_sponsorship_data = new SponsorShipData; 175 $t_row_keys = array_keys( $row ); 176 $t_vars = get_object_vars( $t_sponsorship_data ); 177 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]; 184 } 185 } 186 187 return $t_sponsorship_data; 188 } 189 190 /** 191 * Return an array of Sponsorships associated with the specified bug id 192 * @param int $p_bug_id 193 * @return array 194 */ 195 function sponsorship_get_all_ids( $p_bug_id ) { 196 global $g_cache_sponsorships; 197 static $s_cache_sponsorship_bug_ids = array(); 198 199 $c_bug_id = db_prepare_int( $p_bug_id ); 200 201 if( isset( $s_cache_sponsorship_bug_ids[$c_bug_id] ) ) { 202 return $s_cache_sponsorship_bug_ids[$c_bug_id]; 203 } 204 205 $t_sponsorship_table = db_get_table( 'sponsorship' ); 206 207 $query = "SELECT * FROM $t_sponsorship_table 208 WHERE bug_id = " . db_param(); 209 $t_result = db_query_bound( $query, Array( $c_bug_id ) ); 210 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; 215 } 216 217 $s_cache_sponsorship_bug_ids[$c_bug_id] = $t_sponsorship_ids; 218 219 return $t_sponsorship_ids; 220 } 221 222 /** 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 226 * @return int 227 */ 228 function sponsorship_get_amount( $p_sponsorship_id ) { 229 if( is_array( $p_sponsorship_id ) ) { 230 $t_total = 0; 231 232 foreach( $p_sponsorship_id as $id ) { 233 $t_total += sponsorship_get_amount( $id ); 234 } 235 236 return $t_total; 237 } else { 238 $sponsorship = sponsorship_get( $p_sponsorship_id ); 239 return $sponsorship->amount; 240 } 241 } 242 243 /** 244 * Return the currency used for all sponsorships 245 * @return string 246 */ 247 function sponsorship_get_currency() { 248 return config_get( 'sponsorship_currency' ); 249 } 250 251 /** 252 * This function should return the string in a globalized format. 253 * @param int $p_amount 254 * @return string 255 * @todo add some currency formating in the future 256 */ 257 function sponsorship_format_amount( $p_amount ) { 258 $t_currency = sponsorship_get_currency(); 259 return $t_currency . ' ' . $p_amount; 260 } 261 262 /** 263 * Update bug to reflect sponsorship change 264 * This is to be called after adding/updating/deleting sponsorships 265 * @param int $p_bug_id 266 * @return null 267 */ 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 ); 272 } 273 274 /** 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 279 * @return int 280 */ 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 ); 286 } 287 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; 293 } 294 } 295 296 $t_sponsorship_table = db_get_table( 'sponsorship' ); 297 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; 304 $c_now = db_now(); 305 306 # if new sponsorship 307 if( $c_id == 0 ) { 308 # Insert 309 $query = "INSERT INTO $t_sponsorship_table 310 ( bug_id, user_id, amount, logo, url, date_submitted, last_updated ) 311 VALUES 312 (" . db_param() . ',' . db_param() . ',' . db_param() . ',' . db_param() . ',' . db_param() . ',' . db_param() . ',' . db_param() . ')'; 313 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 ); 316 317 history_log_event_special( $c_bug_id, BUG_ADD_SPONSORSHIP, $c_user_id, $c_amount ); 318 } else { 319 $t_old_amount = sponsorship_get_amount( $c_id ); 320 $t_sponsorship_id = $c_id; 321 322 if( $t_old_amount == $c_amount ) { 323 return $t_sponsorship_id; 324 } 325 326 # Update 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(); 335 336 sponsorship_clear_cache( $c_id ); 337 338 db_query_bound( $query, Array( $c_bug_id, $c_user_id, $c_amount, $c_logo, $c_url, $c_now, $c_id ) ); 339 340 history_log_event_special( $c_bug_id, BUG_UPDATE_SPONSORSHIP, $c_user_id, $c_amount ); 341 } 342 343 sponsorship_update_bug( $c_bug_id ); 344 bug_monitor( $c_bug_id, $c_user_id ); 345 346 if( $c_id == 0 ) { 347 email_sponsorship_added( $c_bug_id ); 348 } else { 349 email_sponsorship_updated( $c_bug_id ); 350 } 351 352 return $t_sponsorship_id; 353 } 354 355 /** 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 359 * @return null 360 */ 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 ); 366 } 367 return; 368 } 369 370 $c_sponsorship_id = db_prepare_int( $p_sponsorship_id ); 371 372 $t_sponsorship = sponsorship_get( $c_sponsorship_id ); 373 374 $t_sponsorship_table = db_get_table( 'sponsorship' ); 375 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 ) ); 380 381 sponsorship_clear_cache( $p_sponsorship_id ); 382 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 ); 385 386 email_sponsorship_deleted( $t_sponsorship->bug_id ); 387 } 388 389 /** 390 * updates the paid field 391 * @param int $p_sponsorship_id 392 * @param int $p_paid 393 * @return true 394 */ 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 ); 398 399 $c_paid = db_prepare_int( $p_paid ); 400 401 $t_sponsorship_table = db_get_table( 'sponsorship' ); 402 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 ) ); 407 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 ); 410 411 return true; 412 } 413 414 /** 415 * updates the last_updated field 416 * @param int $p_sponsorship_id 417 * @return true 418 */ 419 function sponsorship_update_date( $p_sponsorship_id ) { 420 $c_sponsorship_id = db_prepare_int( $p_sponsorship_id ); 421 422 $t_sponsorship_table = db_get_table( 'sponsorship' ); 423 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 ) ); 428 429 sponsorship_clear_cache( $p_sponsorship_id ); 430 431 return true; 432 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Jul 28 15:48:31 2011 | Cross-referenced by PHPXref 0.7 |