[ 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/ -> user_pref_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: user_pref_api.php,v 1.32.14.1 2007-10-13 22:35:47 giallu Exp $
  22      # --------------------------------------------------------
  23  
  24      ### User Preferences API ###
  25      $g_default_mapping = array(
  26          'default_profile' => 'default_profile',
  27          'default_project' => 'default_project',
  28          'advanced_report' => 'default_advanced_report',
  29          'advanced_view' => 'default_advanced_view',
  30          'advanced_update' => 'default_advanced_update',
  31          'refresh_delay' => 'default_refresh_delay',
  32          'redirect_delay' => 'default_redirect_delay',
  33          'bugnote_order' => 'default_bugnote_order',
  34          'email_on_new' => 'default_email_on_new',
  35          'email_on_assigned' => 'default_email_on_assigned',
  36          'email_on_feedback' => 'default_email_on_feedback',
  37          'email_on_resolved' => 'default_email_on_resolved',
  38          'email_on_closed' => 'default_email_on_closed',
  39          'email_on_reopened' => 'default_email_on_reopened',
  40          'email_on_bugnote' => 'default_email_on_bugnote',
  41          'email_on_status' => 'default_email_on_status',
  42          'email_on_priority' => 'default_email_on_priority',
  43          'email_on_new_min_severity' => 'default_email_on_new_minimum_severity',
  44          'email_on_assigned_min_severity' => 'default_email_on_assigned_minimum_severity',
  45          'email_on_feedback_min_severity' => 'default_email_on_feedback_minimum_severity',
  46          'email_on_resolved_min_severity' => 'default_email_on_resolved_minimum_severity',
  47          'email_on_closed_min_severity' => 'default_email_on_closed_minimum_severity',
  48          'email_on_reopened_min_severity' => 'default_email_on_reopened_minimum_severity',
  49          'email_on_bugnote_min_severity' => 'default_email_on_bugnote_minimum_severity',
  50          'email_on_status_min_severity' => 'default_email_on_status_minimum_severity',
  51          'email_on_priority_min_severity' => 'default_email_on_priority_minimum_severity',
  52          'email_bugnote_limit' => 'default_email_bugnote_limit',
  53          'language' => 'default_language'
  54          );
  55  
  56      #===================================
  57      # Preference Structure Definition
  58      #===================================
  59      class UserPreferences {
  60          var $default_profile = NULL;
  61          var $default_project = NULL;
  62          var $advanced_report = NULL;
  63          var $advanced_view = NULL;
  64          var $advanced_update = NULL;
  65          var $refresh_delay = NULL;
  66          var $redirect_delay = NULL;
  67          var $bugnote_order = NULL;
  68          var $email_on_new = NULL;
  69          var $email_on_assigned = NULL;
  70          var $email_on_feedback = NULL;
  71          var $email_on_resolved = NULL;
  72          var $email_on_closed = NULL;
  73          var $email_on_reopened = NULL;
  74          var $email_on_bugnote = NULL;
  75          var $email_on_status = NULL;
  76          var $email_on_priority = NULL;
  77          var $email_on_new_min_severity = NULL;
  78          var $email_on_assigned_min_severity = NULL;
  79          var $email_on_feedback_min_severity = NULL;
  80          var $email_on_resolved_min_severity = NULL;
  81          var $email_on_closed_min_severity = NULL;
  82          var $email_on_reopened_min_severity = NULL;
  83          var $email_on_bugnote_min_severity = NULL;
  84          var $email_on_status_min_severity = NULL;
  85          var $email_on_priority_min_severity = NULL;
  86          var $email_bugnote_limit = NULL;
  87          var $language = NULL;
  88  
  89  		function UserPreferences() {
  90              $this->default_profile                       = 0;
  91              $this->default_project                       = ALL_PROJECTS;
  92          }
  93  
  94  		function Get( $t_string ) {
  95              global $g_default_mapping;
  96              if( is_null( $this->{$t_string} ) ) {
  97                  $this->{$t_string} = config_get( $g_default_mapping[$t_string] );
  98              }
  99              return $this->{$t_string} ;
 100           }
 101      }
 102  
 103      #===================================
 104      # Caching
 105      #===================================
 106  
 107      #########################################
 108      # SECURITY NOTE: cache globals are initialized here to prevent them
 109      #   being spoofed if register_globals is turned on
 110  
 111      $g_cache_user_pref = array();
 112  
 113      # --------------------
 114      # Cache a user preferences row if necessary and return the cached copy
 115      #  If the third parameter is true (default), trigger an error
 116      #  if the preferences can't be found.  If the second parameter is
 117      #  false, return false if the preferences can't be found.
 118  	function user_pref_cache_row( $p_user_id, $p_project_id = ALL_PROJECTS, $p_trigger_errors = true) {
 119          global $g_cache_user_pref;
 120  
 121          $c_user_id        = db_prepare_int( $p_user_id );
 122          $c_project_id    = db_prepare_int( $p_project_id );
 123  
 124          if ( isset ( $g_cache_user_pref[$c_user_id][$c_project_id] ) ) {
 125              return $g_cache_user_pref[$c_user_id][$c_project_id];
 126          }
 127  
 128          $t_user_pref_table = config_get( 'mantis_user_pref_table' );
 129  
 130          $query = "SELECT *
 131                    FROM $t_user_pref_table
 132                    WHERE user_id='$c_user_id' AND project_id='$c_project_id'";
 133          $result = db_query( $query );
 134  
 135          if ( 0 == db_num_rows( $result ) ) {
 136              if ( $p_trigger_errors ) {
 137                  trigger_error( ERROR_USER_PREFS_NOT_FOUND, ERROR );
 138              } else {
 139                  $g_cache_user_pref[$c_user_id][$c_project_id] = false;
 140                  return false;
 141              }
 142          }
 143  
 144          $row = db_fetch_array( $result );
 145  
 146          if ( !isset( $g_cache_user_pref[$c_user_id] ) ) {
 147              $g_cache_user_pref[$c_user_id] = array();
 148          }
 149  
 150          $g_cache_user_pref[$c_user_id][$c_project_id] = $row;
 151  
 152          return $row;
 153      }
 154  
 155      # --------------------
 156      # Clear the user preferences cache (or just the given id if specified)
 157  	function user_pref_clear_cache( $p_user_id = null, $p_project_id = null ) {
 158          global $g_cache_user_pref;
 159  
 160          $c_user_id        = db_prepare_int( $p_user_id );
 161          $c_project_id    = db_prepare_int( $p_project_id );
 162  
 163          if ( null === $p_user_id ) {
 164              $g_cache_user_pref = array();
 165          } else if ( null === $p_project_id ) {
 166              unset( $g_cache_user_pref[$c_user_id] );
 167          } else {
 168              unset( $g_cache_user_pref[$c_user_id][$c_project_id] );
 169          }
 170  
 171          return true;
 172      }
 173  
 174      #===================================
 175      # Boolean queries and ensures
 176      #===================================
 177  
 178      # --------------------
 179      # return true if the user has prefs assigned for the given project,
 180      #  false otherwise
 181      #
 182      # Trying to get the row shouldn't be any slower in the DB than getting COUNT(*)
 183      #  and the transfer time is negligable.  So we try to cache the row - it could save
 184      #  us another query later.
 185  	function user_pref_exists( $p_user_id, $p_project_id = ALL_PROJECTS ) {
 186          if ( false === user_pref_cache_row( $p_user_id, $p_project_id, false ) ) {
 187              return false;
 188          } else {
 189              return true;
 190          }
 191      }
 192  
 193      #===================================
 194      # Creation / Deletion / Updating
 195      #===================================
 196  
 197      # --------------------
 198      # perform an insert of a preference object into the DB
 199      #
 200      # Also see the higher level user_pref_set() and user_pref_set_default()
 201  	function user_pref_insert( $p_user_id, $p_project_id, $p_prefs ) {
 202          $c_user_id         = db_prepare_int( $p_user_id );
 203          $c_project_id     = db_prepare_int( $p_project_id );
 204  
 205          user_ensure_unprotected( $p_user_id );
 206  
 207          $t_user_pref_table     = config_get( 'mantis_user_pref_table' );
 208  
 209          $t_vars        = get_object_vars( $p_prefs );
 210          $t_values    = array();
 211  
 212          foreach ( $t_vars as $var => $val ) {
 213              array_push( $t_values, '\'' . db_prepare_string( $p_prefs->Get( $var ) ) . '\'' );
 214          }
 215  
 216          $t_vars_string        = implode( ', ', array_keys( $t_vars ) );
 217          $t_values_string    = implode( ', ', $t_values );
 218  
 219          $query = "INSERT INTO $t_user_pref_table
 220                      (user_id, project_id, $t_vars_string)
 221                    VALUES
 222                      ('$c_user_id', '$c_project_id', $t_values_string)";
 223          db_query( $query );
 224  
 225          # db_query() errors on failure so:
 226          return true;
 227      }
 228  
 229      # --------------------
 230      # perform an update of a preference object into the DB
 231      #
 232      # Also see the higher level user_pref_set() and user_pref_set_default()
 233  	function user_pref_update( $p_user_id, $p_project_id, $p_prefs ) {
 234          $c_user_id         = db_prepare_int( $p_user_id );
 235          $c_project_id     = db_prepare_int( $p_project_id );
 236  
 237          user_ensure_unprotected( $p_user_id );
 238  
 239          $t_user_pref_table    = config_get( 'mantis_user_pref_table' );
 240          $t_vars                = get_object_vars( $p_prefs );
 241  
 242          $t_pairs = array();
 243  
 244          foreach ( $t_vars as $var => $val ) {
 245              if( is_bool( $p_prefs->$var ) ) {
 246                  array_push( $t_pairs, "$var = " . db_prepare_bool( $p_prefs->Get( $var ) ) );
 247              } else if( is_int( $p_prefs->$var ) ) {
 248                  array_push( $t_pairs, "$var = " . db_prepare_int( $p_prefs->Get( $var ) ) );
 249              } else {
 250                  array_push( $t_pairs, "$var = '" . db_prepare_string( $p_prefs->Get( $var ) ) . '\'' );
 251              }
 252          }
 253  
 254          $t_pairs_string = implode( ', ', $t_pairs );
 255  
 256          $query = "UPDATE $t_user_pref_table
 257                    SET $t_pairs_string
 258                    WHERE user_id=$c_user_id AND project_id=$c_project_id";
 259          db_query( $query );
 260  
 261          user_pref_clear_cache( $p_user_id, $p_project_id );
 262  
 263          # db_query() errors on failure so:
 264          return true;
 265      }
 266  
 267      # --------------------
 268      # delete a preferencess row
 269      # returns true if the prefs were successfully deleted
 270  	function user_pref_delete( $p_user_id, $p_project_id = ALL_PROJECTS ) {
 271          $c_user_id        = db_prepare_int( $p_user_id );
 272          $c_project_id    = db_prepare_int( $p_project_id );
 273  
 274          user_ensure_unprotected( $p_user_id );
 275  
 276          $t_user_pref_table = config_get( 'mantis_user_pref_table' );
 277  
 278          $query = "DELETE FROM $t_user_pref_table
 279                    WHERE user_id='$c_user_id' AND
 280                            project_id='$c_project_id'";
 281          db_query( $query );
 282  
 283          user_pref_clear_cache( $p_user_id, $p_project_id );
 284  
 285          # db_query() errors on failure so:
 286          return true;
 287      }
 288  
 289      # --------------------
 290      # delete all preferences for a user in all projects
 291      # returns true if the prefs were successfully deleted
 292      #
 293      # It is far more efficient to delete them all in one query than to
 294      #  call user_pref_delete() for each one and the code is short so that's
 295      #  what we do
 296  	function user_pref_delete_all( $p_user_id ) {
 297          $c_user_id = db_prepare_int( $p_user_id );
 298  
 299          user_ensure_unprotected( $p_user_id );
 300  
 301          $t_user_pref_table = config_get( 'mantis_user_pref_table' );
 302  
 303          $query = "DELETE FROM $t_user_pref_table
 304                    WHERE user_id='$c_user_id'";
 305          db_query( $query );
 306  
 307          user_pref_clear_cache( $p_user_id );
 308  
 309          # db_query() errors on failure so:
 310          return true;
 311      }
 312  
 313      # --------------------
 314      # delete all preferences for a project for all users (part of deleting the project)
 315      # returns true if the prefs were successfully deleted
 316      #
 317      # It is far more efficient to delete them all in one query than to
 318      #  call user_pref_delete() for each one and the code is short so that's
 319      #  what we do
 320  	function user_pref_delete_project( $p_project_id ) {
 321          $c_project_id = db_prepare_int( $p_project_id );
 322  
 323          $t_user_pref_table = config_get( 'mantis_user_pref_table' );
 324  
 325          $query = "DELETE FROM $t_user_pref_table
 326                    WHERE project_id='$c_project_id'";
 327          db_query( $query );
 328  
 329          # db_query() errors on failure so:
 330          return true;
 331      }
 332  
 333  
 334      #===================================
 335      # Data Access
 336      #===================================
 337  
 338      # --------------------
 339      # return the user's preferences
 340      # @@@ (this should be a private interface as it doesn't have the benefit of applying
 341      #  global defaults before returning values.
 342  	function user_pref_get_row( $p_user_id, $p_project_id = ALL_PROJECTS ) {
 343          return user_pref_cache_row( $p_user_id, $p_project_id );
 344      }
 345  
 346      # --------------------
 347      # return the user's preferences in a UserPreferences object
 348  	function user_pref_get( $p_user_id, $p_project_id = ALL_PROJECTS ) {
 349          global $g_default_mapping;
 350  
 351          $t_prefs = new UserPreferences;
 352  
 353          $row = user_pref_cache_row( $p_user_id, $p_project_id, false );
 354  
 355          # If the user has no preferences for the given project
 356          if ( false === $row ) {
 357              if ( ALL_PROJECTS != $p_project_id ) {
 358                  # Try to get the prefs for ALL_PROJECTS (the defaults)
 359                  $row = user_pref_cache_row( $p_user_id, ALL_PROJECTS, false );
 360              }
 361  
 362              # If $row is still false (the user doesn't have default preferences)
 363              if ( false === $row ) {
 364                  # We use an empty array
 365                  $row = array();
 366              }
 367          }
 368  
 369          $t_row_keys    = array_keys( $row );
 370          $t_vars        = get_object_vars( $t_prefs );
 371  
 372          # Check each variable in the class
 373          foreach ( $t_vars as $var => $val ) {
 374              # If we got a field from the DB with the same name
 375              if ( in_array( $var, $t_row_keys, true ) ) {
 376                  # Store that value in the object
 377                  $t_prefs->$var = $row[$var];
 378              } else {
 379                  $t_prefs->$var = $t_prefs->Get( $var );
 380              }
 381          }
 382  
 383          return $t_prefs;
 384      }
 385  
 386      # --------------------
 387      # Return the specified preference field for the user id
 388      # If the preference can't be found try to return a defined default
 389      # If that fails, trigger a WARNING and return ''
 390  	function user_pref_get_pref( $p_user_id, $p_pref_name, $p_project_id = ALL_PROJECTS ) {
 391          $t_prefs = user_pref_get( $p_user_id, $p_project_id );
 392  
 393          $t_vars = get_object_vars( $t_prefs );
 394  
 395          if ( in_array( $p_pref_name, array_keys( $t_vars ), true ) ) {
 396              return $t_prefs->Get( $p_pref_name );
 397          } else {
 398              error_parameters( $p_pref_name );
 399              trigger_error( ERROR_DB_FIELD_NOT_FOUND, WARNING );
 400              return '';
 401          }
 402      }
 403  
 404      # --------------------
 405      # returns user language
 406  	function user_pref_get_language( $p_user_id, $p_project_id = ALL_PROJECTS ) {
 407          $t_prefs = user_pref_get( $p_user_id, $p_project_id );
 408          return $t_prefs->language;
 409      }
 410  
 411      #===================================
 412      # Data Modification
 413      #===================================
 414  
 415      # --------------------
 416      # Set a user preference
 417      #
 418      # By getting the prefs for the project first we deal fairly well with defaults.
 419      #  If there are currently no prefs for that project, the ALL_PROJECTS prefs will
 420      #  be returned so we end up storing a new set of prefs for the given project
 421      #  based on the prefs for ALL_PROJECTS.  If there isn't even an entry for
 422      #  ALL_PROJECTS, we'd get returned a default UserPreferences object to modify.
 423  	function user_pref_set_pref( $p_user_id, $p_pref_name, $p_pref_value, $p_project_id = ALL_PROJECTS ) {
 424          $c_user_id        = db_prepare_int( $p_user_id );
 425          $c_pref_name    = db_prepare_string( $p_pref_name );
 426          $c_pref_value    = db_prepare_string( $p_pref_value );
 427          $c_project_id    = db_prepare_int( $p_project_id );
 428  
 429          $t_prefs = user_pref_get( $p_user_id, $p_project_id );
 430  
 431          $t_prefs->$p_pref_name = $p_pref_value;
 432  
 433          user_pref_set( $p_user_id, $t_prefs, $p_project_id );
 434  
 435          return true;
 436      }
 437  
 438      # --------------------
 439      # set the user's preferences for the project from the given preferences object
 440      #  Do the work by calling user_pref_update() or user_pref_insert() as appropriate
 441  	function user_pref_set( $p_user_id, $p_prefs, $p_project_id = ALL_PROJECTS ) {
 442          if ( user_pref_exists( $p_user_id, $p_project_id ) ) {
 443              return user_pref_update( $p_user_id, $p_project_id, $p_prefs );
 444          } else {
 445              return user_pref_insert( $p_user_id, $p_project_id, $p_prefs );
 446          }
 447      }
 448  
 449      # --------------------
 450      # create a set of default preferences for the project
 451  	function user_pref_set_default( $p_user_id, $p_project_id = ALL_PROJECTS ) {
 452          # get a default preferences object
 453          $t_prefs = new UserPreferences();
 454  
 455          return user_pref_set( $p_user_id, $t_prefs, $p_project_id );
 456      }
 457  ?>


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