[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/core/ -> project_api.php (source)

   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   * Project API
  19   *
  20   * @package CoreAPI
  21   * @subpackage ProjectAPI
  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 bug_api.php
  27   * @uses category_api.php
  28   * @uses config_api.php
  29   * @uses constant_inc.php
  30   * @uses custom_field_api.php
  31   * @uses database_api.php
  32   * @uses error_api.php
  33   * @uses file_api.php
  34   * @uses lang_api.php
  35   * @uses news_api.php
  36   * @uses project_hierarchy_api.php
  37   * @uses user_api.php
  38   * @uses user_pref_api.php
  39   * @uses utility_api.php
  40   * @uses version_api.php
  41   */
  42  
  43  require_api( 'bug_api.php' );
  44  require_api( 'category_api.php' );
  45  require_api( 'config_api.php' );
  46  require_api( 'constant_inc.php' );
  47  require_api( 'custom_field_api.php' );
  48  require_api( 'database_api.php' );
  49  require_api( 'error_api.php' );
  50  require_api( 'file_api.php' );
  51  require_api( 'lang_api.php' );
  52  require_api( 'news_api.php' );
  53  require_api( 'project_hierarchy_api.php' );
  54  require_api( 'user_api.php' );
  55  require_api( 'user_pref_api.php' );
  56  require_api( 'utility_api.php' );
  57  require_api( 'version_api.php' );
  58  
  59  # ## Project API ###
  60  # ===================================
  61  # Caching
  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_project = array();
  68  $g_cache_project_missing = array();
  69  $g_cache_project_all = false;
  70  
  71  # --------------------
  72  # Cache a project row if necessary and return the cached copy
  73  #  If the second parameter is true (default), trigger an error
  74  #  if the project can't be found.  If the second parameter is
  75  #  false, return false if the project can't be found.
  76  function project_cache_row( $p_project_id, $p_trigger_errors = true ) {
  77      global $g_cache_project, $g_cache_project_missing;
  78  
  79      if( $p_project_id == ALL_PROJECTS ) {
  80          return false;
  81      }
  82  
  83      if( isset( $g_cache_project[(int) $p_project_id] ) ) {
  84          return $g_cache_project[(int) $p_project_id];
  85      }
  86      else if( isset( $g_cache_project_missing[(int) $p_project_id] ) ) {
  87          return false;
  88      }
  89  
  90      $c_project_id = db_prepare_int( $p_project_id );
  91      $t_project_table = db_get_table( 'project' );
  92  
  93      $query = "SELECT *
  94                    FROM $t_project_table
  95                    WHERE id=" . db_param();
  96      $result = db_query_bound( $query, Array( $c_project_id ) );
  97  
  98      if( 0 == db_num_rows( $result ) ) {
  99          $g_cache_project_missing[(int) $p_project_id] = true;
 100  
 101          if( $p_trigger_errors ) {
 102              error_parameters( $p_project_id );
 103              trigger_error( ERROR_PROJECT_NOT_FOUND, ERROR );
 104          } else {
 105              return false;
 106          }
 107      }
 108  
 109      $row = db_fetch_array( $result );
 110  
 111      $g_cache_project[(int) $p_project_id] = $row;
 112  
 113      return $row;
 114  }
 115  
 116  function project_cache_array_rows( $p_project_id_array ) {
 117      global $g_cache_project, $g_cache_project_missing;
 118  
 119      $c_project_id_array = array();
 120  
 121      foreach( $p_project_id_array as $t_project_id ) {
 122          if( !isset( $g_cache_project[(int) $t_project_id] ) && !isset( $g_cache_project_missing[(int) $t_project_id] ) ) {
 123              $c_project_id_array[] = (int) $t_project_id;
 124          }
 125      }
 126  
 127      if( empty( $c_project_id_array ) ) {
 128          return;
 129      }
 130  
 131      $t_project_table = db_get_table( 'project' );
 132  
 133      $query = "SELECT *
 134                    FROM $t_project_table
 135                    WHERE id IN (" . implode( ',', $c_project_id_array ) . ')';
 136      $result = db_query_bound( $query );
 137  
 138      $t_projects_found = array();
 139      while( $row = db_fetch_array( $result ) ) {
 140          $g_cache_project[(int) $row['id']] = $row;
 141          $t_projects_found[(int) $row['id']] = true;
 142      }
 143  
 144      foreach ( $c_project_id_array as $c_project_id ) {
 145          if ( !isset( $t_projects_found[$c_project_id] ) ) {
 146              $g_cache_project_missing[(int) $c_project_id] = true;
 147          }
 148      }
 149  
 150      return;
 151  }
 152  
 153  # --------------------
 154  # Cache all project rows and return an array of them
 155  function project_cache_all() {
 156      global $g_cache_project, $g_cache_project_all;
 157  
 158      if( !$g_cache_project_all ) {
 159          $t_project_table = db_get_table( 'project' );
 160  
 161          $query = "SELECT *
 162                        FROM $t_project_table";
 163          $result = db_query_bound( $query );
 164          $count = db_num_rows( $result );
 165          for( $i = 0;$i < $count;$i++ ) {
 166              $row = db_fetch_array( $result );
 167  
 168              $g_cache_project[(int) $row['id']] = $row;
 169          }
 170  
 171          $g_cache_project_all = true;
 172      }
 173  
 174      return $g_cache_project;
 175  }
 176  
 177  # Clear the project cache (or just the given id if specified)
 178  function project_clear_cache( $p_project_id = null ) {
 179      global $g_cache_project, $g_cache_project_missing, $g_cache_project_all;
 180  
 181      if( null === $p_project_id ) {
 182          $g_cache_project = array();
 183          $g_cache_project_missing = array();
 184          $g_cache_project_all = false;
 185      } else {
 186          unset( $g_cache_project[(int) $p_project_id] );
 187          unset( $g_cache_project_missing[(int) $p_project_id] );
 188          $g_cache_project_all = false;
 189      }
 190  
 191      return true;
 192  }
 193  
 194  # ===================================
 195  # Boolean queries and ensures
 196  # ===================================
 197  # check to see if project exists by id
 198  # return true if it does, false otherwise
 199  function project_exists( $p_project_id ) {
 200  
 201      # we're making use of the caching function here.  If we
 202      #  succeed in caching the project then it exists and is
 203      #  now cached for use by later function calls.  If we can't
 204      #  cache it we return false.
 205      if( false == project_cache_row( $p_project_id, false ) ) {
 206          return false;
 207      } else {
 208          return true;
 209      }
 210  }
 211  
 212  # check to see if project exists by id
 213  # if it doesn't exist then error
 214  #  otherwise let execution continue undisturbed
 215  function project_ensure_exists( $p_project_id ) {
 216      if( !project_exists( $p_project_id ) ) {
 217          error_parameters( $p_project_id );
 218          trigger_error( ERROR_PROJECT_NOT_FOUND, ERROR );
 219      }
 220  }
 221  
 222  # check to see if project exists by name
 223  function project_is_name_unique( $p_name ) {
 224      $t_project_table = db_get_table( 'project' );
 225  
 226      $query = "SELECT COUNT(*)
 227                   FROM $t_project_table
 228                   WHERE name=" . db_param();
 229      $result = db_query_bound( $query, Array( $p_name ) );
 230  
 231      if( 0 == db_result( $result ) ) {
 232          return true;
 233      } else {
 234          return false;
 235      }
 236  }
 237  
 238  # check to see if project exists by id
 239  # if it doesn't exist then error
 240  #  otherwise let execution continue undisturbed
 241  function project_ensure_name_unique( $p_name ) {
 242      if( !project_is_name_unique( $p_name ) ) {
 243          trigger_error( ERROR_PROJECT_NAME_NOT_UNIQUE, ERROR );
 244      }
 245  }
 246  
 247  # check to see if the user/project combo already exists
 248  # returns true is duplicate is found, otherwise false
 249  function project_includes_user( $p_project_id, $p_user_id ) {
 250      $t_project_user_list_table = db_get_table( 'project_user_list' );
 251  
 252      $c_project_id = db_prepare_int( $p_project_id );
 253      $c_user_id = db_prepare_int( $p_user_id );
 254  
 255      $query = "SELECT COUNT(*)
 256                    FROM $t_project_user_list_table
 257                    WHERE project_id=" . db_param() . " AND
 258                          user_id=" . db_param();
 259      $result = db_query_bound( $query, Array( $c_project_id, $c_user_id ) );
 260  
 261      if( 0 == db_result( $result ) ) {
 262          return false;
 263      } else {
 264          return true;
 265      }
 266  }
 267  
 268  # =======================================
 269  # Creation / Deletion / Updating / Copy
 270  # =======================================
 271  
 272  # Create a new project
 273  function project_create( $p_name, $p_description, $p_status, $p_view_state = VS_PUBLIC, $p_file_path = '', $p_enabled = true, $p_inherit_global = true ) {
 274  
 275      $c_enabled = db_prepare_bool( $p_enabled );
 276      $c_inherit_global = db_prepare_bool( $p_inherit_global );
 277  
 278      if( is_blank( $p_name ) ) {
 279          trigger_error( ERROR_PROJECT_NAME_INVALID, ERROR );
 280      }
 281  
 282      project_ensure_name_unique( $p_name );
 283  
 284      if( !is_blank( $p_file_path ) ) {
 285          # Make sure file path has trailing slash
 286          $p_file_path = terminate_directory_path( $p_file_path );
 287          file_ensure_valid_upload_path( $p_file_path );
 288      }
 289  
 290      $t_project_table = db_get_table( 'project' );
 291  
 292      $query = "INSERT INTO $t_project_table
 293                      ( name, status, enabled, view_state, file_path, description, inherit_global )
 294                    VALUES
 295                      ( " . db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ')';
 296  
 297      db_query_bound( $query, Array( $p_name, (int) $p_status, $c_enabled, (int) $p_view_state, $p_file_path, $p_description, $c_inherit_global ) );
 298  
 299      # return the id of the new project
 300      return db_insert_id( $t_project_table );
 301  }
 302  
 303  # --------------------
 304  # Delete a project
 305  function project_delete( $p_project_id ) {
 306      $t_email_notifications = config_get( 'enable_email_notification' );
 307  
 308      # temporarily disable all notifications
 309      config_set_cache( 'enable_email_notification', OFF, CONFIG_TYPE_INT );
 310  
 311      $c_project_id = db_prepare_int( $p_project_id );
 312  
 313      $t_project_table = db_get_table( 'project' );
 314  
 315      # Delete the bugs
 316      bug_delete_all( $p_project_id );
 317  
 318      # Delete associations with custom field definitions.
 319      custom_field_unlink_all( $p_project_id );
 320  
 321      # Delete the project categories
 322      category_remove_all( $p_project_id );
 323  
 324      # Delete the project versions
 325      version_remove_all( $p_project_id );
 326  
 327      # Delete relations to other projects
 328      project_hierarchy_remove_all( $p_project_id );
 329  
 330      # Delete the project files
 331      project_delete_all_files( $p_project_id );
 332  
 333      # Delete the records assigning users to this project
 334      project_remove_all_users( $p_project_id );
 335  
 336      # Delete all news entries associated with the project being deleted
 337      news_delete_all( $p_project_id );
 338  
 339      # Delete project specific configurations
 340      config_delete_project( $p_project_id );
 341  
 342      # Delete any user prefs that are project specific
 343      user_pref_delete_project( $p_project_id );
 344  
 345      # Delete the project entry
 346      $query = "DELETE FROM $t_project_table
 347                    WHERE id=" . db_param();
 348  
 349      db_query_bound( $query, Array( $c_project_id ) );
 350  
 351      config_set_cache( 'enable_email_notification', $t_email_notifications, CONFIG_TYPE_INT );
 352  
 353      project_clear_cache( $p_project_id );
 354  
 355      # db_query errors on failure so:
 356      return true;
 357  }
 358  
 359  # --------------------
 360  # Update a project
 361  function project_update( $p_project_id, $p_name, $p_description, $p_status, $p_view_state, $p_file_path, $p_enabled, $p_inherit_global ) {
 362  
 363      $p_project_id = (int) $p_project_id;
 364      $c_enabled = db_prepare_bool( $p_enabled );
 365      $c_inherit_global = db_prepare_bool( $p_inherit_global );
 366  
 367      if( is_blank( $p_name ) ) {
 368          trigger_error( ERROR_PROJECT_NAME_INVALID, ERROR );
 369      }
 370  
 371      $t_old_name = project_get_field( $p_project_id, 'name' );
 372  
 373      if( strcasecmp( $p_name, $t_old_name ) != 0 ) {
 374          project_ensure_name_unique( $p_name );
 375      }
 376  
 377      if( !is_blank( $p_file_path ) ) {
 378          # Make sure file path has trailing slash
 379          $p_file_path = terminate_directory_path( $p_file_path );
 380          file_ensure_valid_upload_path( $p_file_path );
 381      }
 382  
 383      $t_project_table = db_get_table( 'project' );
 384  
 385      $query = "UPDATE $t_project_table
 386                    SET name=" . db_param() . ",
 387                      status=" . db_param() . ",
 388                      enabled=" . db_param() . ",
 389                      view_state=" . db_param() . ",
 390                      file_path=" . db_param() . ",
 391                      description=" . db_param() . ",
 392                      inherit_global=" . db_param() . "
 393                    WHERE id=" . db_param();
 394      db_query_bound( $query, Array( $p_name, (int) $p_status, $c_enabled, (int) $p_view_state, $p_file_path, $p_description, $c_inherit_global, $p_project_id ) );
 395  
 396      project_clear_cache( $p_project_id );
 397  
 398      # db_query errors on failure so:
 399      return true;
 400  }
 401  
 402  # Copy custom fields
 403  function project_copy_custom_fields( $p_destination_id, $p_source_id ) {
 404      $t_custom_field_ids = custom_field_get_linked_ids( $p_source_id );
 405      foreach( $t_custom_field_ids as $t_custom_field_id ) {
 406          if( !custom_field_is_linked( $t_custom_field_id, $p_destination_id ) ) {
 407              custom_field_link( $t_custom_field_id, $p_destination_id );
 408              $t_sequence = custom_field_get_sequence( $t_custom_field_id, $p_source_id );
 409              custom_field_set_sequence( $t_custom_field_id, $p_destination_id, $t_sequence );
 410          }
 411      }
 412  }
 413  
 414  # ===================================
 415  # Data Access
 416  # ===================================
 417  # Get the id of the project with the specified name
 418  function project_get_id_by_name( $p_project_name ) {
 419      $t_project_table = db_get_table( 'project' );
 420  
 421      $query = "SELECT id FROM $t_project_table WHERE name = " . db_param();
 422      $t_result = db_query_bound( $query, Array( $p_project_name ), 1 );
 423  
 424      if( db_num_rows( $t_result ) == 0 ) {
 425          return 0;
 426      } else {
 427          return db_result( $t_result );
 428      }
 429  }
 430  
 431  # Return the row describing the given project
 432  function project_get_row( $p_project_id, $p_trigger_errors = true ) {
 433      return project_cache_row( $p_project_id, $p_trigger_errors );
 434  }
 435  
 436  # Return all rows describing all projects
 437  function project_get_all_rows() {
 438      return project_cache_all();
 439  }
 440  
 441  # Return the specified field of the specified project
 442  function project_get_field( $p_project_id, $p_field_name, $p_trigger_errors = true ) {
 443      $row = project_get_row( $p_project_id, $p_trigger_errors );
 444  
 445      if( isset( $row[$p_field_name] ) ) {
 446          return $row[$p_field_name];
 447      } else if ( $p_trigger_errors ) {
 448          error_parameters( $p_field_name );
 449          trigger_error( ERROR_DB_FIELD_NOT_FOUND, WARNING );
 450      }
 451  
 452      return '';
 453  }
 454  
 455  # Return the name of the project
 456  # Handles ALL_PROJECTS by returning the internationalized string for All Projects
 457  function project_get_name( $p_project_id, $p_trigger_errors = true ) {
 458      if( ALL_PROJECTS == $p_project_id ) {
 459          return lang_get( 'all_projects' );
 460      } else {
 461          return project_get_field( $p_project_id, 'name', $p_trigger_errors );
 462      }
 463  }
 464  
 465  # Return the user's local (overridden) access level on the project or false
 466  #  if the user is not listed on the project
 467  function project_get_local_user_access_level( $p_project_id, $p_user_id ) {
 468      $p_project_id = (int) $p_project_id;
 469  
 470      if( ALL_PROJECTS == $p_project_id ) {
 471          return false;
 472      }
 473  
 474      $t_project_user_list_table = db_get_table( 'project_user_list' );
 475  
 476      $query = "SELECT access_level
 477                    FROM $t_project_user_list_table
 478                    WHERE user_id=" . db_param() . " AND project_id=" . db_param();
 479      $result = db_query_bound( $query, Array( (int) $p_user_id, $p_project_id ) );
 480  
 481      if( db_num_rows( $result ) > 0 ) {
 482          return db_result( $result );
 483      } else {
 484          return false;
 485      }
 486  }
 487  
 488  # return the descriptor holding all the info from the project user list
 489  # for the specified project
 490  function project_get_local_user_rows( $p_project_id ) {
 491      $t_project_user_list_table = db_get_table( 'project_user_list' );
 492  
 493      $query = "SELECT *
 494                  FROM $t_project_user_list_table
 495                  WHERE project_id=" . db_param();
 496  
 497      $result = db_query_bound( $query, Array( (int) $p_project_id ) );
 498  
 499      $t_user_rows = array();
 500      $t_row_count = db_num_rows( $result );
 501  
 502      for( $i = 0;$i < $t_row_count;$i++ ) {
 503          array_push( $t_user_rows, db_fetch_array( $result ) );
 504      }
 505  
 506      return $t_user_rows;
 507  }
 508  
 509  # Return an array of info about users who have access to the the given project
 510  # For each user we have 'id', 'username', and 'access_level' (overall access level)
 511  # If the second parameter is given, return only users with an access level
 512  #  higher than the given value.
 513  # if the first parameter is given as 'ALL_PROJECTS', return the global access level (without
 514  # any reference to the specific project
 515  function project_get_all_user_rows( $p_project_id = ALL_PROJECTS, $p_access_level = ANYBODY, $p_include_global_users = true ) {
 516      $c_project_id = db_prepare_int( $p_project_id );
 517  
 518      # Optimization when access_level is NOBODY
 519      if( NOBODY == $p_access_level ) {
 520          return array();
 521      }
 522  
 523      $t_user_table = db_get_table( 'user' );
 524      $t_project_user_list_table = db_get_table( 'project_user_list' );
 525      $t_project_table = db_get_table( 'project' );
 526  
 527      $t_on = ON;
 528      $t_users = array();
 529  
 530      $t_global_access_level = $p_access_level;
 531      if( $c_project_id != ALL_PROJECTS && $p_include_global_users ) {
 532  
 533          # looking for specific project
 534          if( VS_PRIVATE == project_get_field( $p_project_id, 'view_state' ) ) {
 535              /** @todo (thraxisp) this is probably more complex than it needs to be
 536               * When a new project is created, those who meet 'private_project_threshold' are added
 537               *  automatically, but don't have an entry in project_user_list_table.
 538               *  if they did, you would not have to add global levels.
 539               */
 540              $t_private_project_threshold = config_get( 'private_project_threshold' );
 541              if( is_array( $t_private_project_threshold ) ) {
 542                  if( is_array( $p_access_level ) ) {
 543                      # both private threshold and request are arrays, use intersection
 544                      $t_global_access_level = array_intersect( $p_access_level, $t_private_project_threshold );
 545                  } else {
 546                      # private threshold is an array, but request is a number, use values in threshold higher than request
 547                      $t_global_access_level = array();
 548                      foreach( $t_private_project_threshold as $t_threshold ) {
 549                          if( $p_access_level <= $t_threshold ) {
 550                              $t_global_access_level[] = $t_threshold;
 551                          }
 552                      }
 553                  }
 554              } else {
 555                  if( is_array( $p_access_level ) ) {
 556                      // private threshold is a number, but request is an array, use values in request higher than threshold
 557                      $t_global_access_level = array();
 558                      foreach( $p_access_level as $t_threshold ) {
 559                          if( $t_threshold >= $t_private_project_threshold ) {
 560                              $t_global_access_level[] = $t_threshold;
 561                          }
 562                      }
 563                  } else {
 564                      // both private threshold and request are numbers, use maximum
 565                      $t_global_access_level = max( $p_access_level, $t_private_project_threshold );
 566                  }
 567              }
 568          }
 569      }
 570  
 571      if( is_array( $t_global_access_level ) ) {
 572          if( 0 == count( $t_global_access_level ) ) {
 573              $t_global_access_clause = '>= ' . NOBODY . ' ';
 574          } else if( 1 == count( $t_global_access_level ) ) {
 575              $t_global_access_clause = '= ' . array_shift( $t_global_access_level ) . ' ';
 576          } else {
 577              $t_global_access_clause = 'IN (' . implode( ',', $t_global_access_level ) . ')';
 578          }
 579      } else {
 580          $t_global_access_clause = ">= $t_global_access_level ";
 581      }
 582  
 583      if( $p_include_global_users ) {
 584          $query = "SELECT id, username, realname, access_level
 585                  FROM $t_user_table
 586                  WHERE enabled = " . db_param() . "
 587                      AND access_level $t_global_access_clause";
 588  
 589          $result = db_query_bound( $query, Array( $t_on ) );
 590          $t_row_count = db_num_rows( $result );
 591          for( $i = 0;$i < $t_row_count;$i++ ) {
 592              $row = db_fetch_array( $result );
 593              $t_users[$row['id']] = $row;
 594          }
 595      }
 596  
 597      if( $c_project_id != ALL_PROJECTS ) {
 598  
 599          // Get the project overrides
 600          $query = "SELECT u.id, u.username, u.realname, l.access_level
 601                  FROM $t_project_user_list_table l, $t_user_table u
 602                  WHERE l.user_id = u.id
 603                  AND u.enabled = " . db_param() . "
 604                  AND l.project_id = " . db_param();
 605  
 606          $result = db_query_bound( $query, Array( $t_on, $c_project_id ) );
 607          $t_row_count = db_num_rows( $result );
 608          for( $i = 0;$i < $t_row_count;$i++ ) {
 609              $row = db_fetch_array( $result );
 610              if( is_array( $p_access_level ) ) {
 611                  $t_keep = in_array( $row['access_level'], $p_access_level );
 612              } else {
 613                  $t_keep = $row['access_level'] >= $p_access_level;
 614              }
 615  
 616              if( $t_keep ) {
 617                  $t_users[$row['id']] = $row;
 618              } else {
 619                  # If user's overridden level is lower than required, so remove
 620                  #  them from the list if they were previously there
 621                  unset( $t_users[$row['id']] );
 622              }
 623          }
 624      }
 625  
 626      user_cache_array_rows( array_keys( $t_users ) );
 627  
 628      return array_values( $t_users );
 629  }
 630  
 631  # ===================================
 632  # Data Modification
 633  # ===================================
 634  # add user with the specified access level to a project
 635  function project_add_user( $p_project_id, $p_user_id, $p_access_level ) {
 636      $t_project_user_list_table = db_get_table( 'project_user_list' );
 637  
 638      $c_project_id = db_prepare_int( $p_project_id );
 639      $c_user_id = db_prepare_int( $p_user_id );
 640      $c_access_level = db_prepare_int( $p_access_level );
 641  
 642      if( DEFAULT_ACCESS_LEVEL == $p_access_level ) {
 643  
 644          # Default access level for this user
 645          $c_access_level = db_prepare_int( user_get_access_level( $p_user_id ) );
 646      }
 647  
 648      $query = "INSERT
 649                    INTO $t_project_user_list_table
 650                      ( project_id, user_id, access_level )
 651                    VALUES
 652                      ( " . db_param() . ', ' . db_param() . ', ' . db_param() . ')';
 653  
 654      db_query_bound( $query, Array( $c_project_id, $c_user_id, $c_access_level ) );
 655  
 656      # db_query errors on failure so:
 657      return true;
 658  }
 659  
 660  # update entry
 661  # must make sure entry exists beforehand
 662  function project_update_user_access( $p_project_id, $p_user_id, $p_access_level ) {
 663      $t_project_user_list_table = db_get_table( 'project_user_list' );
 664  
 665      $c_project_id = db_prepare_int( $p_project_id );
 666      $c_user_id = db_prepare_int( $p_user_id );
 667      $c_access_level = db_prepare_int( $p_access_level );
 668  
 669      $query = "UPDATE $t_project_user_list_table
 670                    SET access_level=" . db_param() . "
 671                    WHERE    project_id=" . db_param() . " AND
 672                          user_id=" . db_param();
 673  
 674      db_query_bound( $query, Array( $c_access_level, $c_project_id, $c_user_id ) );
 675  
 676      # db_query errors on failure so:
 677      return true;
 678  }
 679  
 680  # update or add the entry as appropriate
 681  #  This function involves one more db query than project_update_user_acces()
 682  #  or project_add_user()
 683  function project_set_user_access( $p_project_id, $p_user_id, $p_access_level ) {
 684      if( project_includes_user( $p_project_id, $p_user_id ) ) {
 685          return project_update_user_access( $p_project_id, $p_user_id, $p_access_level );
 686      } else {
 687          return project_add_user( $p_project_id, $p_user_id, $p_access_level );
 688      }
 689  }
 690  
 691  # remove user from project
 692  function project_remove_user( $p_project_id, $p_user_id ) {
 693      $t_project_user_list_table = db_get_table( 'project_user_list' );
 694  
 695      $c_project_id = db_prepare_int( $p_project_id );
 696      $c_user_id = db_prepare_int( $p_user_id );
 697  
 698      $query = "DELETE FROM $t_project_user_list_table
 699                    WHERE project_id=" . db_param() . " AND
 700                          user_id=" . db_param();
 701  
 702      db_query_bound( $query, Array( $c_project_id, $c_user_id ) );
 703  
 704      # db_query errors on failure so:
 705      return true;
 706  }
 707  
 708  /**
 709   * Delete all users from the project user list for a given project. This is
 710   * useful when deleting or closing a project. The $p_access_level_limit
 711   * parameter can be used to only remove users from a project if their access
 712   * level is below or equal to the limit.
 713   * @param int Project ID
 714   * @param int Access level limit (null = no limit)
 715   * @return true
 716   */
 717  function project_remove_all_users( $p_project_id, $p_access_level_limit = null ) {
 718      $t_project_user_list_table = db_get_table( 'project_user_list' );
 719  
 720      $c_project_id = db_prepare_int( $p_project_id );
 721  
 722      $query = "DELETE FROM $t_project_user_list_table
 723              WHERE project_id = " . db_param();
 724  
 725      if ( $p_access_level_limit !== null ) {
 726          $c_access_level_limit = db_prepare_int( $p_access_level_limit );
 727          $query .= " AND access_level <= " . db_param();
 728          db_query_bound( $query, Array( $c_project_id, $c_access_level_limit ) );
 729      } else {
 730          db_query_bound( $query, Array( $c_project_id ) );
 731      }
 732  
 733      # db_query errors on failure so:
 734      return true;
 735  }
 736  
 737  /**
 738   * Copy all users and their permissions from the source project to the
 739   * destination project. The $p_access_level_limit parameter can be used to
 740   * limit the access level for users as they're copied to the destination
 741   * project (the highest access level they'll receieve in the destination
 742   * project will be equal to $p_access_level_limit).
 743   * @param int Destination project ID
 744   * @param int Source project ID
 745   * @param int Access level limit (null = no limit)
 746   * @return null
 747   */
 748  function project_copy_users( $p_destination_id, $p_source_id, $p_access_level_limit = null ) {
 749      # Copy all users from current project over to another project
 750      $t_rows = project_get_local_user_rows( $p_source_id );
 751  
 752      $t_count = count( $t_rows );
 753      for ( $i = 0; $i < $t_count; $i++ ) {
 754          $t_row = $t_rows[$i];
 755  
 756          if ( $p_access_level_limit !== null &&
 757              $t_row['access_level'] > $p_access_level_limit ) {
 758              $t_destination_access_level = $p_access_level_limit;
 759          } else {
 760              $t_destination_access_level = $t_row['access_level'];
 761          }
 762  
 763          # if there is no duplicate then add a new entry
 764          # otherwise just update the access level for the existing entry
 765          if ( project_includes_user( $p_destination_id, $t_row['user_id'] ) ) {
 766              project_update_user_access( $p_destination_id, $t_row['user_id'], $t_destination_access_level );
 767          } else {
 768              project_add_user( $p_destination_id, $t_row['user_id'], $t_destination_access_level );
 769          }
 770      }
 771  }
 772  
 773  # Delete all files associated with a project
 774  function project_delete_all_files( $p_project_id ) {
 775      file_delete_project_files( $p_project_id );
 776  }
 777  
 778  # ===================================
 779  # Other
 780  # ===================================
 781  
 782  # Pads the project id with the appropriate number of zeros.
 783  function project_format_id( $p_project_id ) {
 784      $t_padding = config_get( 'display_project_padding' );
 785      return( utf8_str_pad( $p_project_id, $t_padding, '0', STR_PAD_LEFT ) );
 786  }
 787  
 788  
 789  # Return true if the file name identifier is unique, false otherwise
 790  function project_file_is_name_unique( $p_name ) {
 791      $t_file_table = db_get_table( 'project_file' );
 792  
 793      $query = "SELECT COUNT(*)
 794                    FROM $t_file_table
 795                    WHERE filename=" . db_param();
 796      $result = db_query_bound( $query, Array( $p_name ) );
 797      $t_count = db_result( $result );
 798  
 799      if( $t_count > 0 ) {
 800          return false;
 801      } else {
 802          return true;
 803      }
 804  }


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7