PageRenderTime 24ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/mantisbt-1.2.8/core/sponsorship_api.php

#
PHP | 426 lines | 219 code | 77 blank | 130 comment | 28 complexity | e62cf4dd6043e8290b48e2a0a224846e MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. # MantisBT - a php based bugtracking system
  3. # MantisBT is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # MantisBT is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Sponsorship API
  17. * @package CoreAPI
  18. * @subpackage SponsorshipAPI
  19. * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
  20. * @copyright Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  21. * @link http://www.mantisbt.org
  22. */
  23. /**
  24. * requires email_api
  25. */
  26. require_once( 'email_api.php' );
  27. /**
  28. * requires bug_api
  29. */
  30. require_once( 'bug_api.php' );
  31. /**
  32. * requires history_api
  33. */
  34. require_once( 'history_api.php' );
  35. /**
  36. * Sponsorship Data Structure Definition
  37. * @package MantisBT
  38. * @subpackage classes
  39. */
  40. class SponsorshipData {
  41. var $id = 0;
  42. var $bug_id = 0;
  43. var $user_id = 0;
  44. var $amount = 0;
  45. var $logo = '';
  46. var $url = '';
  47. var $paid = 0;
  48. var $date_submitted = '';
  49. var $last_updated = '';
  50. }
  51. # ########################################
  52. # SECURITY NOTE: cache globals are initialized here to prevent them
  53. # being spoofed if register_globals is turned on
  54. $g_cache_sponsorships = array();
  55. /**
  56. * Cache a sponsorship row if necessary and return the cached copy
  57. * If the second parameter is true (default), trigger an error
  58. * if the sponsorship can't be found. If the second parameter is
  59. * false, return false if the sponsorship can't be found.
  60. * @param int $p_sponsorship_id
  61. * @param bool $p_trigger_errors
  62. * @return array
  63. */
  64. function sponsorship_cache_row( $p_sponsorship_id, $p_trigger_errors = true ) {
  65. global $g_cache_sponsorships;
  66. $c_sponsorship_id = db_prepare_int( $p_sponsorship_id );
  67. $t_sponsorship_table = db_get_table( 'mantis_sponsorship_table' );
  68. if( isset( $g_cache_sponsorships[$c_sponsorship_id] ) ) {
  69. return $g_cache_sponsorships[$c_sponsorship_id];
  70. }
  71. $query = "SELECT *
  72. FROM $t_sponsorship_table
  73. WHERE id=" . db_param();
  74. $result = db_query_bound( $query, Array( $c_sponsorship_id ) );
  75. if( 0 == db_num_rows( $result ) ) {
  76. $g_cache_sponsorships[$c_sponsorship_id] = false;
  77. if( $p_trigger_errors ) {
  78. error_parameters( $p_sponsorship_id );
  79. trigger_error( ERROR_SPONSORSHIP_NOT_FOUND, ERROR );
  80. } else {
  81. return false;
  82. }
  83. }
  84. $row = db_fetch_array( $result );
  85. $g_cache_sponsorships[$c_sponsorship_id] = $row;
  86. return $row;
  87. }
  88. /**
  89. * Clear the sponsorship cache (or just the given id if specified)
  90. * @param int $p_sponsorship_id
  91. * @return null
  92. */
  93. function sponsorship_clear_cache( $p_sponsorship_id = null ) {
  94. global $g_cache_sponsorships;
  95. if( $p_sponsorship_id === null ) {
  96. $g_cache_sponsorships = array();
  97. } else {
  98. $c_sponsorship_id = db_prepare_int( $p_sponsorship_id );
  99. unset( $g_cache_sponsorships[$c_sponsorship_id] );
  100. }
  101. }
  102. /**
  103. * check to see if sponsorship exists by id
  104. * return true if it does, false otherwise
  105. * @param int $p_sponsorship_id
  106. * @return bool
  107. */
  108. function sponsorship_exists( $p_sponsorship_id ) {
  109. return sponsorship_cache_row( $p_sponsorship_id, false ) !== false;
  110. }
  111. /**
  112. * return false if not found
  113. * otherwise returns sponsorship id
  114. * @param int $p_bug_id
  115. * @param int $p_user_id
  116. * @return int|false
  117. */
  118. function sponsorship_get_id( $p_bug_id, $p_user_id = null ) {
  119. $c_bug_id = db_prepare_int( $p_bug_id );
  120. if( $p_user_id === null ) {
  121. $c_user_id = auth_get_current_user_id();
  122. } else {
  123. $c_user_id = db_prepare_int( $p_user_id );
  124. }
  125. $t_sponsorship_table = db_get_table( 'mantis_sponsorship_table' );
  126. $query = "SELECT id FROM $t_sponsorship_table WHERE bug_id = " . db_param() . " AND user_id = " . db_param();
  127. $t_result = db_query_bound( $query, Array( $c_bug_id, $c_user_id ), 1 );
  128. if( db_num_rows( $t_result ) == 0 ) {
  129. return false;
  130. }
  131. $row = db_fetch_array( $t_result );
  132. return (integer) $row['id'];
  133. }
  134. /**
  135. * get information about a sponsorship given its id
  136. * @param int $p_sponsorship_id
  137. * @return array
  138. */
  139. function sponsorship_get( $p_sponsorship_id ) {
  140. $row = sponsorship_cache_row( $p_sponsorship_id );
  141. $t_sponsorship_data = new SponsorShipData;
  142. $t_row_keys = array_keys( $row );
  143. $t_vars = get_object_vars( $t_sponsorship_data );
  144. # Check each variable in the class
  145. foreach( $t_vars as $var => $val ) {
  146. # If we got a field from the DB with the same name
  147. if( in_array( $var, $t_row_keys, true ) ) {
  148. # Store that value in the object
  149. $t_sponsorship_data->$var = $row[$var];
  150. }
  151. }
  152. return $t_sponsorship_data;
  153. }
  154. /**
  155. * Return an array of Sponsorships associated with the specified bug id
  156. * @param int $p_bug_id
  157. * @return array
  158. */
  159. function sponsorship_get_all_ids( $p_bug_id ) {
  160. global $g_cache_sponsorships;
  161. static $s_cache_sponsorship_bug_ids = array();
  162. $c_bug_id = db_prepare_int( $p_bug_id );
  163. if( isset( $s_cache_sponsorship_bug_ids[$c_bug_id] ) ) {
  164. return $s_cache_sponsorship_bug_ids[$c_bug_id];
  165. }
  166. $t_sponsorship_table = db_get_table( 'mantis_sponsorship_table' );
  167. $query = "SELECT * FROM $t_sponsorship_table
  168. WHERE bug_id = " . db_param();
  169. $t_result = db_query_bound( $query, Array( $c_bug_id ) );
  170. $t_sponsorship_ids = array();
  171. while( $row = db_fetch_array( $t_result ) ) {
  172. $t_sponsorship_ids[] = $row['id'];
  173. $g_cache_sponsorships[(int)$row['id']] = $row;
  174. }
  175. $s_cache_sponsorship_bug_ids[$c_bug_id] = $t_sponsorship_ids;
  176. return $t_sponsorship_ids;
  177. }
  178. /**
  179. * Get the amount of sponsorships for the specified id(s)
  180. * handles the case where $p_sponsorship_id is an array or an id.
  181. * @param int $p_sponsorship_id
  182. * @return int
  183. */
  184. function sponsorship_get_amount( $p_sponsorship_id ) {
  185. if( is_array( $p_sponsorship_id ) ) {
  186. $t_total = 0;
  187. foreach( $p_sponsorship_id as $id ) {
  188. $t_total += sponsorship_get_amount( $id );
  189. }
  190. return $t_total;
  191. } else {
  192. $sponsorship = sponsorship_get( $p_sponsorship_id );
  193. return $sponsorship->amount;
  194. }
  195. }
  196. /**
  197. * Return the currency used for all sponsorships
  198. * @return string
  199. */
  200. function sponsorship_get_currency() {
  201. return config_get( 'sponsorship_currency' );
  202. }
  203. /**
  204. * This function should return the string in a globalized format.
  205. * @param int $p_amount
  206. * @return string
  207. * @todo add some currency formating in the future
  208. */
  209. function sponsorship_format_amount( $p_amount ) {
  210. $t_currency = sponsorship_get_currency();
  211. return $t_currency . ' ' . $p_amount;
  212. }
  213. /**
  214. * Update bug to reflect sponsorship change
  215. * This is to be called after adding/updating/deleting sponsorships
  216. * @param int $p_bug_id
  217. * @return null
  218. */
  219. function sponsorship_update_bug( $p_bug_id ) {
  220. $t_total_amount = sponsorship_get_amount( sponsorship_get_all_ids( $p_bug_id ) );
  221. bug_set_field( $p_bug_id, 'sponsorship_total', $t_total_amount );
  222. bug_update_date( $p_bug_id );
  223. }
  224. /**
  225. * if sponsorship contains a non-zero id, then update the corresponding record.
  226. * if sponsorship contains a zero id, search for bug_id/user_id, if found, then update the entry
  227. * otherwise add a new entry
  228. * @param int $p_sponsorship
  229. * @return int
  230. */
  231. function sponsorship_set( $p_sponsorship ) {
  232. $t_min_sponsorship = config_get( 'minimum_sponsorship_amount' );
  233. if( $p_sponsorship->amount < $t_min_sponsorship ) {
  234. error_parameters( $p_sponsorship->amount, $t_min_sponsorship );
  235. trigger_error( ERROR_SPONSORSHIP_AMOUNT_TOO_LOW, ERROR );
  236. }
  237. # if id == 0, check if the specified user is already sponsoring the bug, if so, overwrite
  238. if( $p_sponsorship->id == 0 ) {
  239. $t_sponsorship_id = sponsorship_get_id( $p_sponsorship->bug_id, $p_sponsorship->user_id );
  240. if( $t_sponsorship_id !== false ) {
  241. $p_sponsorship->id = $t_sponsorship_id;
  242. }
  243. }
  244. $t_sponsorship_table = db_get_table( 'mantis_sponsorship_table' );
  245. $c_id = db_prepare_int( $p_sponsorship->id );
  246. $c_bug_id = db_prepare_int( $p_sponsorship->bug_id );
  247. $c_user_id = db_prepare_int( $p_sponsorship->user_id );
  248. $c_amount = db_prepare_int( $p_sponsorship->amount );
  249. $c_logo = $p_sponsorship->logo;
  250. $c_url = $p_sponsorship->url;
  251. $c_now = db_now();
  252. # if new sponsorship
  253. if( $c_id == 0 ) {
  254. # Insert
  255. $query = "INSERT INTO $t_sponsorship_table
  256. ( bug_id, user_id, amount, logo, url, date_submitted, last_updated )
  257. VALUES
  258. (" . db_param() . ',' . db_param() . ',' . db_param() . ',' . db_param() . ',' . db_param() . ',' . db_param() . ',' . db_param() . ')';
  259. db_query_bound( $query, Array( $c_bug_id, $c_user_id, $c_amount, $c_logo, $c_url, $c_now, $c_now ) );
  260. $t_sponsorship_id = db_insert_id( $t_sponsorship_table );
  261. history_log_event_special( $c_bug_id, BUG_ADD_SPONSORSHIP, $c_user_id, $c_amount );
  262. } else {
  263. $t_old_amount = sponsorship_get_amount( $c_id );
  264. $t_sponsorship_id = $c_id;
  265. if( $t_old_amount == $c_amount ) {
  266. return $t_sponsorship_id;
  267. }
  268. # Update
  269. $query = "UPDATE $t_sponsorship_table
  270. SET bug_id = " . db_param() . ",
  271. user_id = " . db_param() . ",
  272. amount = " . db_param() . ",
  273. logo = " . db_param() . ",
  274. url = " . db_param() . ",
  275. last_updated = " . db_param() . "
  276. WHERE id = " . db_param();
  277. sponsorship_clear_cache( $c_id );
  278. db_query_bound( $query, Array( $c_bug_id, $c_user_id, $c_amount, $c_logo, $c_url, $c_now, $c_id ) );
  279. history_log_event_special( $c_bug_id, BUG_UPDATE_SPONSORSHIP, $c_user_id, $c_amount );
  280. }
  281. sponsorship_update_bug( $c_bug_id );
  282. bug_monitor( $c_bug_id, $c_user_id );
  283. if( $c_id == 0 ) {
  284. email_sponsorship_added( $c_bug_id );
  285. } else {
  286. email_sponsorship_updated( $c_bug_id );
  287. }
  288. return $t_sponsorship_id;
  289. }
  290. /**
  291. * delete a sponsorship given its id
  292. * id can be an array of ids or just an id.
  293. * @param int $p_sponsorship_id
  294. * @return null
  295. */
  296. function sponsorship_delete( $p_sponsorship_id ) {
  297. # handle the case of array of ids
  298. if( is_array( $p_sponsorship_id ) ) {
  299. foreach( $p_sponsorship_id as $id ) {
  300. sponsorship_delete( $id );
  301. }
  302. return;
  303. }
  304. $c_sponsorship_id = db_prepare_int( $p_sponsorship_id );
  305. $t_sponsorship = sponsorship_get( $c_sponsorship_id );
  306. $t_sponsorship_table = db_get_table( 'mantis_sponsorship_table' );
  307. # Delete the bug entry
  308. $query = "DELETE FROM $t_sponsorship_table
  309. WHERE id=" . db_param();
  310. db_query_bound( $query, Array( $c_sponsorship_id ) );
  311. sponsorship_clear_cache( $p_sponsorship_id );
  312. history_log_event_special( $t_sponsorship->bug_id, BUG_DELETE_SPONSORSHIP, $t_sponsorship->user_id, $t_sponsorship->amount );
  313. sponsorship_update_bug( $t_sponsorship->bug_id );
  314. email_sponsorship_deleted( $t_sponsorship->bug_id );
  315. }
  316. /**
  317. * updates the paid field
  318. * @param int $p_sponsorship_id
  319. * @param int $p_paid
  320. * @return true
  321. */
  322. function sponsorship_update_paid( $p_sponsorship_id, $p_paid ) {
  323. $c_sponsorship_id = db_prepare_int( $p_sponsorship_id );
  324. $t_sponsorship = sponsorship_get( $c_sponsorship_id );
  325. $c_paid = db_prepare_int( $p_paid );
  326. $t_sponsorship_table = db_get_table( 'mantis_sponsorship_table' );
  327. $query = "UPDATE $t_sponsorship_table
  328. SET last_updated= " . db_param() . ", paid=" . db_param() . "
  329. WHERE id=" . db_param();
  330. db_query_bound( $query, Array( db_now(), $c_paid, $c_sponsorship_id ) );
  331. history_log_event_special( $t_sponsorship->bug_id, BUG_PAID_SPONSORSHIP, $t_sponsorship->user_id, $p_paid );
  332. sponsorship_clear_cache( $p_sponsorship_id );
  333. return true;
  334. }
  335. /**
  336. * updates the last_updated field
  337. * @param int $p_sponsorship_id
  338. * @return true
  339. */
  340. function sponsorship_update_date( $p_sponsorship_id ) {
  341. $c_sponsorship_id = db_prepare_int( $p_sponsorship_id );
  342. $t_sponsorship_table = db_get_table( 'mantis_sponsorship_table' );
  343. $query = "UPDATE $t_sponsorship_table
  344. SET last_updated= " . db_param() . "
  345. WHERE id=" . db_param();
  346. db_query_bound( $query, Array( db_now(), $c_sponsorship_id ) );
  347. sponsorship_clear_cache( $p_sponsorship_id );
  348. return true;
  349. }