[ Index ]
 

Code source de Mantis 1.1.0rc3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/core/ -> sponsorship_api.php (source)

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


Généré le : Thu Nov 29 09:42:17 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics