[ 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/ -> custom_field_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: custom_field_api.php,v 1.66.2.1 2007-10-13 22:35:21 giallu Exp $
  22      # --------------------------------------------------------
  23  
  24      $t_core_dir = dirname( __FILE__ ).DIRECTORY_SEPARATOR;
  25  
  26      require_once ( $t_core_dir . 'bug_api.php' );
  27      require_once ( $t_core_dir . 'helper_api.php' );
  28      require_once ( $t_core_dir . 'date_api.php' );
  29  
  30      ### Custom Fields API ###
  31  
  32  #*******************************************
  33  #    TODO
  34  #    - add an object to store field data like BugData and UserPrefs ?
  35  #    - add caching functions like user, bug, etc
  36  #    - make existing api functions use caching functions
  37  #    - add functions to return individual db columns for a field definition
  38  #*******************************************
  39  
  40      #===================================
  41      # Caching
  42      #===================================
  43  
  44      #########################################
  45      # SECURITY NOTE: cache globals are initialized here to prevent them
  46      #   being spoofed if register_globals is turned on
  47  
  48      $g_cache_custom_field = array();
  49      $g_cache_cf_list = NULL;
  50      $g_cache_cf_linked = array();
  51  
  52      # Cache a custom field row if necessary and return the cached copy
  53      #  If the second parameter is true (default), trigger an error
  54      #  if the field can't be found.  If the second parameter is
  55      #  false, return false if the field can't be found.
  56  	function custom_field_cache_row( $p_field_id, $p_trigger_errors=true ) {
  57          global $g_cache_custom_field;
  58  
  59          $c_field_id = db_prepare_int( $p_field_id );
  60  
  61          $t_custom_field_table = config_get( 'mantis_custom_field_table' );
  62  
  63          if ( isset ( $g_cache_custom_field[$c_field_id] ) ) {
  64              return $g_cache_custom_field[$c_field_id];
  65          }
  66  
  67          $query = "SELECT *
  68                    FROM $t_custom_field_table
  69                    WHERE id='$c_field_id'";
  70          $result = db_query( $query );
  71  
  72          if ( 0 == db_num_rows( $result ) ) {
  73              if ( $p_trigger_errors ) {
  74                  error_parameters( 'Custom ' . $p_field_id );
  75                  trigger_error( ERROR_CUSTOM_FIELD_NOT_FOUND, ERROR );
  76              } else {
  77                  return false;
  78              }
  79          }
  80  
  81          $row = db_fetch_array( $result );
  82  
  83          $g_cache_custom_field[$c_field_id] = $row;
  84  
  85          return $row;
  86      }
  87  
  88      # --------------------
  89      # Clear the custom field cache (or just the given id if specified)
  90  	function custom_field_clear_cache( $p_field_id = null ) {
  91          global $g_cache_custom_field, $g_cached_custom_field_lists;
  92  
  93          $g_cached_custom_field_lists = null;
  94  
  95          if ( null === $p_field_id ) {
  96              $g_cache_custom_field = array();
  97          } else {
  98              $c_field_id = db_prepare_int( $p_field_id );
  99              unset( $g_cache_custom_field[$c_field_id] );
 100          }
 101  
 102          return true;
 103      }
 104  
 105  
 106      #===================================
 107      # Boolean queries and ensures
 108      #===================================
 109  
 110      # --------------------
 111      # Check to see whether the field is included in the given project
 112      #  return true if the field is included, false otherwise
 113      #
 114  	function custom_field_is_linked( $p_field_id, $p_project_id ) {
 115          $c_project_id    = db_prepare_int( $p_project_id );
 116          $c_field_id        = db_prepare_int( $p_field_id );
 117  
 118          # figure out if this bug_id/field_id combination exists
 119          $t_custom_field_project_table = config_get( 'mantis_custom_field_project_table' );
 120          $query = "SELECT COUNT(*)
 121                  FROM $t_custom_field_project_table
 122                  WHERE field_id='$c_field_id' AND
 123                        project_id='$c_project_id'";
 124          $result = db_query( $query );
 125          $count = db_result( $result );
 126  
 127          if ( $count > 0 ) {
 128              return true;
 129          } else {
 130              return false;
 131          }
 132      }
 133  
 134      # --------------------
 135      # Check to see whether the field id is defined
 136      #  return true if the field is defined, false otherwise
 137  	function custom_field_exists( $p_field_id ) {
 138          if ( false == custom_field_cache_row( $p_field_id, false ) ) {
 139              return false;
 140          } else {
 141              return true;
 142          }
 143      }
 144  
 145      # --------------------
 146      # Return the type of a custom field if it exists.
 147  	function custom_field_type( $p_field_id ) {
 148          $t_field = custom_field_cache_row( $p_field_id, false ) ;
 149          if ( $t_field == false ) {
 150              return -1 ;
 151          } else {
 152              return $t_field[ 'type' ] ;
 153          }
 154      }
 155  
 156      # --------------------
 157      # Check to see whether the field id is defined
 158      #  return true if the field is defined, error otherwise
 159  	function custom_field_ensure_exists( $p_field_id ) {
 160          if ( custom_field_exists( $p_field_id ) ) {
 161              return true;
 162          } else {
 163              error_parameters( 'Custom ' . $p_field_id );
 164              trigger_error( ERROR_CUSTOM_FIELD_NOT_FOUND, ERROR );
 165          }
 166      }
 167  
 168      # --------------------
 169      # Check to see whether the name is unique
 170      #  return false if a field with the name already exists, true otherwise
 171      #  if an id is specified, then the corresponding record is excluded from the
 172      #  uniqueness test.
 173  	function custom_field_is_name_unique( $p_name, $p_custom_field_id = null ) {
 174          $c_name    = db_prepare_string( $p_name );
 175  
 176          $t_custom_field_table = config_get( 'mantis_custom_field_table' );
 177          $query = "SELECT COUNT(*)
 178                    FROM $t_custom_field_table
 179                    WHERE name='$c_name'";
 180          if ( $p_custom_field_id !== null ) {
 181              $c_id = db_prepare_int( $p_custom_field_id );
 182              $query .= " AND (id <> $c_id)";
 183          }
 184          $result = db_query( $query );
 185          $count = db_result( $result );
 186  
 187          if ( $count > 0 ) {
 188              return false;
 189          } else {
 190              return true;
 191          }
 192      }
 193  
 194      # --------------------
 195      # Check to see whether the name is unique
 196      #  return true if the name has not been used, error otherwise
 197  	function custom_field_ensure_name_unique( $p_name ) {
 198          if ( custom_field_is_name_unique( $p_name ) ) {
 199              return true;
 200          } else {
 201              trigger_error( ERROR_CUSTOM_FIELD_NAME_NOT_UNIQUE, ERROR );
 202          }
 203      }
 204  
 205      # --------------------
 206      # Return true if the user can read the value of the field for the given bug,
 207      #  false otherwise.
 208  	function custom_field_has_read_access( $p_field_id, $p_bug_id, $p_user_id = null ) {
 209          custom_field_ensure_exists( $p_field_id );
 210  
 211          if ( null === $p_user_id ) {
 212              $p_user_id = auth_get_current_user_id();
 213          }
 214  
 215          $t_access_level_r = custom_field_get_field( $p_field_id, 'access_level_r' );
 216  
 217          $t_project_id = bug_get_field( $p_bug_id, 'project_id' );
 218  
 219          return access_has_project_level( $t_access_level_r, $t_project_id, $p_user_id );
 220      }
 221  
 222      # --------------------
 223      # Return true if the user can modify the value of the field for the given project,
 224      #  false otherwise.
 225  	function custom_field_has_write_access_to_project( $p_field_id, $p_project_id, $p_user_id = null ) {
 226          custom_field_ensure_exists( $p_field_id );
 227  
 228          if ( null === $p_user_id ) {
 229              $p_user_id = auth_get_current_user_id();
 230          }
 231  
 232          $t_access_level_rw = custom_field_get_field( $p_field_id, 'access_level_rw' );
 233  
 234          return access_has_project_level( $t_access_level_rw, $p_project_id, $p_user_id );
 235      }
 236  
 237      # --------------------
 238      # Return true if the user can modify the value of the field for the given bug,
 239      #  false otherwise.
 240  	function custom_field_has_write_access( $p_field_id, $p_bug_id, $p_user_id = null ) {
 241          $t_project_id = bug_get_field( $p_bug_id, 'project_id' );
 242          return ( custom_field_has_write_access_to_project( $p_field_id, $t_project_id, $p_user_id ) );
 243      }
 244  
 245      #===================================
 246      # Creation / Deletion / Updating
 247      #===================================
 248  
 249      # --------------------
 250      # create a new custom field with the name $p_name
 251      # the definition are the default values and can be changes later
 252      # return the ID of the new definition
 253  	function custom_field_create( $p_name ) {
 254          if ( string_contains_scripting_chars( $p_name ) ) {
 255              trigger_error( ERROR_CUSTOM_FIELD_INVALID_DEFINITION, ERROR );
 256          }
 257  
 258          $c_name = db_prepare_string( trim( $p_name ) );
 259  
 260          if ( is_blank( $c_name ) ) {
 261              error_parameters( 'name' );
 262              trigger_error( ERROR_EMPTY_FIELD, ERROR );
 263          }
 264  
 265          custom_field_ensure_name_unique( $c_name );
 266  
 267          $t_custom_field_table = config_get( 'mantis_custom_field_table' );
 268          $query = "INSERT INTO $t_custom_field_table
 269                      ( name )
 270                    VALUES
 271                      ( '$c_name' )";
 272  
 273          db_query( $query );
 274  
 275          return db_insert_id( $t_custom_field_table );
 276      }
 277  
 278      # --------------------
 279      # Update the field definition
 280      #  return true on success, false on failure
 281  	function custom_field_update( $p_field_id, $p_def_array ) {
 282          if ( string_contains_scripting_chars( $p_def_array['name'] ) ) {
 283              trigger_error( ERROR_CUSTOM_FIELD_INVALID_DEFINITION, ERROR );
 284          }
 285  
 286          $c_field_id            = db_prepare_int( $p_field_id );
 287          $c_name                = db_prepare_string( trim( $p_def_array['name'] ) );
 288          $c_type                = db_prepare_int(    $p_def_array['type']            );
 289          $c_possible_values    = db_prepare_string( $p_def_array['possible_values'] );
 290          $c_default_value    = db_prepare_string( $p_def_array['default_value']   );
 291          $c_valid_regexp        = db_prepare_string( $p_def_array['valid_regexp']    );
 292          $c_access_level_r    = db_prepare_int(    $p_def_array['access_level_r']  );
 293          $c_access_level_rw    = db_prepare_int(    $p_def_array['access_level_rw'] );
 294          $c_length_min        = db_prepare_int(    $p_def_array['length_min']      );
 295          $c_length_max        = db_prepare_int(    $p_def_array['length_max']      );
 296          $c_advanced            = db_prepare_bool(   $p_def_array['advanced']        );
 297          $c_display_report    = db_prepare_bool(      $p_def_array['display_report'] );
 298          $c_display_update    = db_prepare_bool(      $p_def_array['display_update'] );
 299          $c_display_resolved    = db_prepare_bool(      $p_def_array['display_resolved'] );
 300          $c_display_closed    = db_prepare_bool(      $p_def_array['display_closed']   );
 301          $c_require_report    = db_prepare_bool(      $p_def_array['require_report']  );
 302          $c_require_update    = db_prepare_bool(      $p_def_array['require_update']  );
 303          $c_require_resolved = db_prepare_bool(      $p_def_array['require_resolved'] );
 304          $c_require_closed    = db_prepare_bool(      $p_def_array['require_closed']   );
 305  
 306          if ( is_blank( $c_name ) ) {
 307              error_parameters( 'name' );
 308              trigger_error( ERROR_EMPTY_FIELD, ERROR );
 309          }
 310  
 311          if (( $c_access_level_rw < $c_access_level_r ) ||
 312              ( $c_length_min < 0 ) ||
 313              ( ( $c_length_max != 0 ) && ( $c_length_min > $c_length_max ) ) ) {
 314              trigger_error( ERROR_CUSTOM_FIELD_INVALID_DEFINITION, ERROR );
 315          }
 316  
 317          if ( $c_advanced == true && ( $c_require_report == true || $c_require_update ) ) {
 318              trigger_error( ERROR_CUSTOM_FIELD_INVALID_DEFINITION, ERROR );
 319          }
 320  
 321          if ( !custom_field_is_name_unique( $c_name, $c_field_id ) ) {
 322              trigger_error( ERROR_CUSTOM_FIELD_NAME_NOT_UNIQUE, ERROR );
 323          }
 324  
 325          $t_update_something = false;
 326          $t_mantis_custom_field_table = config_get( 'mantis_custom_field_table' );
 327          $query = "UPDATE $t_mantis_custom_field_table
 328                    SET ";
 329          if( array_key_exists( 'name', $p_def_array ) ) {
 330              if ( !$t_update_something ) {
 331                  $t_update_something = true;
 332              } else {
 333                  $query .= ', ';
 334              }
 335              $query .= "name='$c_name'";
 336          }
 337          if( array_key_exists( 'type', $p_def_array ) ) {
 338              if ( !$t_update_something ) {
 339                  $t_update_something = true;
 340              } else {
 341                  $query .= ', ';
 342              }
 343              $query .= "type='$c_type'";
 344          }
 345          if( array_key_exists( 'possible_values', $p_def_array ) ) {
 346              if ( !$t_update_something ) {
 347                  $t_update_something = true;
 348              } else {
 349                  $query .= ', ';
 350              }
 351              $query .= "possible_values='$c_possible_values'";
 352          }
 353          if( array_key_exists( 'default_value', $p_def_array ) ) {
 354              if ( !$t_update_something ) {
 355                  $t_update_something = true;
 356              } else {
 357                  $query .= ', ';
 358              }
 359              $query .= "default_value='$c_default_value'";
 360          }
 361          if( array_key_exists( 'valid_regexp', $p_def_array ) ) {
 362              if ( !$t_update_something ) {
 363                  $t_update_something = true;
 364              } else {
 365                  $query .= ', ';
 366              }
 367              $query .= "valid_regexp='$c_valid_regexp'";
 368          }
 369          if( array_key_exists( 'access_level_r', $p_def_array ) ) {
 370              if ( !$t_update_something ) {
 371                  $t_update_something = true;
 372              } else {
 373                  $query .= ', ';
 374              }
 375              $query .= "access_level_r='$c_access_level_r'";
 376          }
 377          if( array_key_exists( 'access_level_rw', $p_def_array ) ) {
 378              if ( !$t_update_something ) {
 379                  $t_update_something = true;
 380              } else {
 381                  $query .= ', ';
 382              }
 383              $query .= "access_level_rw='$c_access_level_rw'";
 384          }
 385          if( array_key_exists( 'length_min', $p_def_array ) ) {
 386              if ( !$t_update_something ) {
 387                  $t_update_something = true;
 388              } else {
 389                  $query .= ', ';
 390              }
 391              $query .= "length_min='$c_length_min'";
 392          }
 393          if( array_key_exists( 'length_max', $p_def_array ) ) {
 394              if ( !$t_update_something ) {
 395                  $t_update_something = true;
 396              } else {
 397                  $query .= ', ';
 398              }
 399              $query .= "length_max='$c_length_max'";
 400          }
 401          if( array_key_exists( 'advanced', $p_def_array ) ) {
 402              if ( !$t_update_something ) {
 403                  $t_update_something = true;
 404              } else {
 405                  $query .= ', ';
 406              }
 407              $query .= "advanced='$c_advanced'";
 408          }
 409          if( array_key_exists( 'display_report', $p_def_array ) ) {
 410              if ( !$t_update_something ) {
 411                  $t_update_something = true;
 412              } else {
 413                  $query .= ', ';
 414              }
 415              $query .= "display_report='$c_display_report'";
 416          }
 417          if( array_key_exists( 'display_update', $p_def_array ) ) {
 418              if ( !$t_update_something ) {
 419                  $t_update_something = true;
 420              } else {
 421                  $query .= ', ';
 422              }
 423              $query .= "display_update='$c_display_update'";
 424          }
 425          if( array_key_exists( 'display_resolved', $p_def_array ) ) {
 426              if ( !$t_update_something ) {
 427                  $t_update_something = true;
 428              } else {
 429                  $query .= ', ';
 430              }
 431              $query .= "display_resolved='$c_display_resolved'";
 432          }
 433          if( array_key_exists( 'display_closed', $p_def_array ) ) {
 434              if ( !$t_update_something ) {
 435                  $t_update_something = true;
 436              } else {
 437                  $query .= ', ';
 438              }
 439              $query .= "display_closed='$c_display_closed'";
 440          }
 441          if( array_key_exists( 'require_report', $p_def_array ) ) {
 442              if ( !$t_update_something ) {
 443                  $t_update_something = true;
 444              } else {
 445                  $query .= ', ';
 446              }
 447              $query .= "require_report='$c_require_report'";
 448          }
 449          if( array_key_exists( 'require_update', $p_def_array ) ) {
 450              if ( !$t_update_something ) {
 451                  $t_update_something = true;
 452              } else {
 453                  $query .= ', ';
 454              }
 455              $query .= "require_update='$c_require_update'";
 456          }
 457          if( array_key_exists( 'require_resolved', $p_def_array ) ) {
 458              if ( !$t_update_something ) {
 459                  $t_update_something = true;
 460              } else {
 461                  $query .= ', ';
 462              }
 463              $query .= "require_resolved='$c_require_resolved'";
 464          }
 465          if( array_key_exists( 'require_closed', $p_def_array ) ) {
 466              if ( !$t_update_something ) {
 467                  $t_update_something = true;
 468              } else {
 469                  $query .= ', ';
 470              }
 471              $query .= "require_closed='$c_require_closed'";
 472          }
 473  
 474  
 475          $query .= " WHERE id='$c_field_id'";
 476  
 477          if( $t_update_something ) {
 478              db_query( $query );
 479              custom_field_clear_cache( $p_field_id );
 480          } else {
 481              return false;   # there is nothing to update...
 482          }
 483  
 484          # db_query() errors on failure so:
 485          return true;
 486      }
 487  
 488      # --------------------
 489      # Add a custom field to a project
 490      #  return true on success, false on failure or if already added
 491  	function custom_field_link( $p_field_id, $p_project_id ) {
 492          $c_field_id        = db_prepare_int( $p_field_id );
 493          $c_project_id    = db_prepare_int( $p_project_id );
 494  
 495          custom_field_ensure_exists( $p_field_id );
 496          project_ensure_exists( $p_project_id );
 497  
 498          if ( custom_field_is_linked( $p_field_id, $p_project_id ) ) {
 499              return false;
 500          }
 501  
 502          $t_custom_field_project_table = config_get( 'mantis_custom_field_project_table' );
 503          $query = "INSERT INTO $t_custom_field_project_table
 504                      ( field_id, project_id )
 505                    VALUES
 506                      ( '$c_field_id', '$c_project_id' )";
 507          db_query( $query );
 508  
 509          # db_query() errors on failure so:
 510          return true;
 511      }
 512  
 513      # --------------------
 514      # Remove a custom field from a project
 515      #  return true on success, false on failure
 516      #
 517      # The values for the custom fields are not deleted.  This is to allow for the
 518      # case where a bug is moved to another project that has the field, or the
 519      # field is linked again to the project.
 520  	function custom_field_unlink( $p_field_id, $p_project_id ) {
 521          $c_field_id        = db_prepare_int( $p_field_id );
 522          $c_project_id    = db_prepare_int( $p_project_id );
 523  
 524          $t_custom_field_project_table = config_get( 'mantis_custom_field_project_table' );
 525          $query = "DELETE FROM $t_custom_field_project_table
 526                    WHERE field_id = '$c_field_id' AND
 527                            project_id = '$c_project_id'";
 528          db_query( $query );
 529  
 530          # db_query() errors on failure so:
 531          return true;
 532      }
 533  
 534      # --------------------
 535      # Delete the field definition and all associated values and project
 536      #  associations
 537      #  return true on success, false on failure
 538  	function custom_field_destroy( $p_field_id ) {
 539          $c_field_id = db_prepare_int( $p_field_id );
 540  
 541          # delete all values
 542          $t_custom_field_string_table = config_get( 'mantis_custom_field_string_table' );
 543          $query = "DELETE FROM $t_custom_field_string_table
 544                    WHERE field_id='$c_field_id'";
 545          db_query( $query );
 546  
 547          # delete all project associations
 548          $t_custom_field_project_table = config_get( 'mantis_custom_field_project_table' );
 549          $query = "DELETE FROM $t_custom_field_project_table
 550                    WHERE field_id='$c_field_id'";
 551          db_query( $query );
 552  
 553          $t_custom_field_table = config_get( 'mantis_custom_field_table' );
 554          # delete the definition
 555          $query = "DELETE FROM $t_custom_field_table
 556                    WHERE id='$c_field_id'";
 557          db_query( $query );
 558  
 559          custom_field_clear_cache( $p_field_id );
 560  
 561          # db_query() errors on failure so:
 562          return true;
 563      }
 564  
 565      # --------------------
 566      # Delete all associations of custom fields to the specified project
 567      #  return true on success, false on failure
 568      #
 569      # To be called from within project_delete().
 570  	function custom_field_unlink_all( $p_project_id ) {
 571          $c_project_id = db_prepare_int( $p_project_id );
 572  
 573          # delete all project associations
 574          $t_custom_field_project_table = config_get( 'mantis_custom_field_project_table' );
 575          $query = "DELETE FROM $t_custom_field_project_table
 576                    WHERE project_id='$c_project_id'";
 577          db_query( $query );
 578  
 579          # db_query() errors on failure so:
 580          return true;
 581      }
 582  
 583      # --------------------
 584      # Delete all custom values associated with the specified bug.
 585      #  return true on success, false on failure
 586      #
 587      # To be called from bug_delete().
 588  	function custom_field_delete_all_values( $p_bug_id ) {
 589          $c_bug_id = db_prepare_int( $p_bug_id );
 590  
 591          $t_custom_field_string_table = config_get( 'mantis_custom_field_string_table' );
 592          $query = "DELETE FROM $t_custom_field_string_table
 593                    WHERE bug_id='$c_bug_id'";
 594          db_query( $query );
 595  
 596          #db_query() errors on failure so:
 597          return true;
 598      }
 599  
 600      #===================================
 601      # Data Access
 602      #===================================
 603  
 604      # --------------------
 605      # Get the id of the custom field with the specified name.
 606      # false is returned if no custom field found with the specified name.
 607  	function custom_field_get_id_from_name( $p_field_name, $p_truncated_length = null ) {
 608          if ( $p_field_name == '' ) {
 609            return false;
 610          }
 611  
 612          $t_custom_field_table = config_get( 'mantis_custom_field_table' );
 613  
 614          $c_field_name = db_prepare_string( $p_field_name );
 615  
 616          if ( ( null === $p_truncated_length ) || ( strlen( $c_field_name ) != $p_truncated_length ) ) {
 617              $query = "SELECT id FROM $t_custom_field_table WHERE name = '$c_field_name'";
 618          } else {
 619              # @@@ This is to handle the case where we only have a truncated part of the name.  This happens in the case where
 620              # we are getting the custom field name from the history logs, since history is 32 and custom field name is 64.
 621              # This fix will handle entries already in the database, future entries should be handled by making the field name max lengths match.
 622              $query = "SELECT id FROM $t_custom_field_table WHERE name LIKE '$c_field_name%'";
 623          }
 624  
 625          $t_result = db_query( $query, 1 );
 626  
 627          if ( db_num_rows( $t_result ) == 0 ) {
 628              return false;
 629          }
 630  
 631          $row = db_fetch_array( $t_result );
 632  
 633          return $row['id'];
 634      }
 635  
 636      # --------------------
 637      # Return an array of ids of custom fields bound to the specified project
 638      #
 639      # The ids will be sorted based on the sequence number associated with the binding
 640  	function custom_field_get_linked_ids( $p_project_id = ALL_PROJECTS ) {
 641      global $g_cache_cf_linked;
 642          
 643          if ( ! isset( $g_cache_cf_linked[$p_project_id] ) ) {
 644          
 645               $t_custom_field_table            = config_get( 'mantis_custom_field_table' );
 646               $t_custom_field_project_table    = config_get( 'mantis_custom_field_project_table' );
 647  
 648              if ( ALL_PROJECTS == $p_project_id ) {
 649                  $t_project_user_list_table = config_get( 'mantis_project_user_list_table' );
 650                  $t_project_table = config_get( 'mantis_project_table' );
 651                  $t_user_table = config_get( 'mantis_user_table' );
 652                  $t_user_id = auth_get_current_user_id();
 653                  $t_pub = VS_PUBLIC;
 654                  $t_priv = VS_PRIVATE;
 655              
 656                  $t_private_access = config_get( 'private_project_threshold' );
 657                  if ( is_array( $t_private_access ) ) {
 658                      if ( 1 == count( $t_private_access ) ) {
 659                          $t_access_clause = "= " . array_shift( $t_private_access ) . " ";
 660                      } else {
 661                          $t_access_clause = "IN (" . implode( ',', $t_private_access ) . ")";
 662                      }
 663                  } else {
 664                      $t_access_clause = ">= $t_private_access ";
 665                  }            
 666  
 667              
 668                  # select only the ids that the user has some access to 
 669                  #  e.g., all fields in public projects, or private projects where the user is listed
 670                  #    or private projects where the user is implicitly listed
 671                  $query = "SELECT cft.id as id, cft.name as name
 672                      FROM $t_custom_field_table as cft, $t_user_table ut, $t_project_table pt, $t_custom_field_project_table cfpt
 673                          LEFT JOIN $t_project_user_list_table pult 
 674                              on cfpt.project_id = pult.project_id and pult.user_id = $t_user_id
 675                      WHERE cft.id = cfpt.field_id AND cfpt.project_id = pt.id AND ut.id = $t_user_id AND 
 676                          ( pt.view_state = $t_pub OR 
 677                          ( pt.view_state = $t_priv and pult.user_id = $t_user_id ) OR 
 678                          ( pult.user_id is null and ut.access_level $t_access_clause ) )
 679                      GROUP BY cft.id, cft.name
 680                      ORDER BY cft.name ASC";
 681              } else {
 682                  if ( is_array( $p_project_id ) ) {
 683                      if ( 1 == count( $p_project_id ) ) {
 684                          $t_project_clause = "= " . array_shift( $p_project_id ) . " ";
 685                      } else {
 686                          $t_project_clause = "IN (" . implode( ',', $p_project_id ) . ")";
 687                      }
 688                  } else {
 689                      $t_project_clause = "= $p_project_id ";
 690                  }            
 691                  $query = "SELECT cft.id, cft.name, cfpt.sequence
 692                        FROM $t_custom_field_table cft, $t_custom_field_project_table cfpt
 693                        WHERE cfpt.project_id $t_project_clause AND
 694                              cft.id = cfpt.field_id
 695                        ORDER BY sequence ASC, name ASC";
 696              }
 697              $result = db_query( $query );
 698              $t_row_count = db_num_rows( $result );
 699              $t_ids = array();
 700  
 701              for ( $i=0 ; $i < $t_row_count ; $i++ ) {
 702                  $row = db_fetch_array( $result );
 703  
 704                  array_push( $t_ids, $row['id'] );
 705              }
 706              $g_cache_cf_linked[$p_project_id] = $t_ids;
 707          } else {
 708              $t_ids = $g_cache_cf_linked[$p_project_id];
 709          }
 710          
 711          return $t_ids;
 712      }
 713  
 714      # --------------------
 715      # Return an array all custom field ids sorted by name
 716  	function custom_field_get_ids( ) {
 717          global $g_cache_cf_list;
 718         
 719          if ( $g_cache_cf_list === NULL ) {
 720              $t_custom_field_table            = config_get( 'mantis_custom_field_table' );
 721              $query = "SELECT id, name
 722                    FROM $t_custom_field_table
 723                    ORDER BY name ASC";
 724              $result = db_query( $query );
 725              $t_row_count = db_num_rows( $result );
 726              $t_ids = array();
 727  
 728              for ( $i=0 ; $i < $t_row_count ; $i++ ) {
 729                  $row = db_fetch_array( $result );
 730  
 731                  array_push( $t_ids, $row['id'] );
 732              }
 733              $g_cache_cf_list = $t_ids;
 734  
 735          } else {
 736              $t_ids = $g_cache_cf_list;
 737          }
 738          return $t_ids;
 739      }
 740  
 741      # --------------------
 742      # Return an array of ids of projects related to the specified custom field
 743      # (the array may be empty)
 744  	function custom_field_get_project_ids( $p_field_id ) {
 745          $c_field_id = db_prepare_int( $p_field_id );
 746  
 747          $t_custom_field_project_table = config_get( 'mantis_custom_field_project_table' );
 748          $query = "SELECT project_id
 749                    FROM $t_custom_field_project_table
 750                    WHERE field_id = '$c_field_id'";
 751          $result = db_query( $query );
 752  
 753          $t_row_count = db_num_rows( $result );
 754          $t_ids = array();
 755  
 756          for ( $i=0 ; $i < $t_row_count ; $i++ ) {
 757              $row = db_fetch_array( $result );
 758  
 759              array_push( $t_ids, $row['project_id'] );
 760          }
 761  
 762          return $t_ids;
 763      }
 764  
 765      # --------------------
 766      # Return a field definition row for the field or error if the field does
 767      #  not exist
 768  	function custom_field_get_definition( $p_field_id ) {
 769          return custom_field_cache_row( $p_field_id );
 770      }
 771  
 772      # --------------------
 773      # Return a single database field from a custom field definition row
 774      #  for the field
 775      # if the database field does not exist, display a warning and return ''
 776  	function custom_field_get_field( $p_field_id, $p_field_name ) {
 777          $row = custom_field_get_definition( $p_field_id );
 778  
 779          if ( isset( $row[$p_field_name] ) ) {
 780              return $row[$p_field_name];
 781          } else {
 782              error_parameters( $p_field_name );
 783              trigger_error( ERROR_DB_FIELD_NOT_FOUND, WARNING );
 784              return '';
 785          }
 786      }
 787  
 788      # --------------------
 789      # Get the value of a custom field for the given bug
 790      #
 791      # @@@ return values are unclear... should we error when access is denied
 792      #   and provide an api to check whether it will be?
 793  	function custom_field_get_value( $p_field_id, $p_bug_id ) {
 794          $c_field_id = db_prepare_int( $p_field_id );
 795          $c_bug_id   = db_prepare_int( $p_bug_id );
 796  
 797          custom_field_ensure_exists( $p_field_id );
 798  
 799          $t_custom_field_table = config_get( 'mantis_custom_field_table' );
 800          $query = "SELECT access_level_r, default_value, type
 801                    FROM $t_custom_field_table
 802                    WHERE id='$c_field_id'";
 803          $result = db_query( $query );
 804          $row = db_fetch_array( $result );
 805  
 806          $t_access_level_r    = $row['access_level_r'];
 807          $t_default_value    = $row['default_value'];
 808  
 809          if( !custom_field_has_read_access( $p_field_id, $p_bug_id, auth_get_current_user_id() ) ) {
 810              return false;
 811          }
 812  
 813          $t_custom_field_string_table = config_get( 'mantis_custom_field_string_table' );
 814          $query = "SELECT value
 815                    FROM $t_custom_field_string_table
 816                    WHERE bug_id='$c_bug_id' AND
 817                            field_id='$c_field_id'";
 818          $result = db_query( $query );
 819  
 820          if( db_num_rows( $result ) > 0 ) {
 821              return custom_field_database_to_value( db_result( $result ) , $row['type'] );
 822          } else {
 823              return $t_default_value;
 824          }
 825      }
 826  
 827      # --------------------
 828      # Gets the custom fields array for the given bug readable by specified level.
 829      # Array keys are custom field names. Array is sorted by custom field sequence number;
 830      # Array items are arrays with the next keys:
 831      # 'type', 'value', 'access_level_r'
 832  	function custom_field_get_linked_fields( $p_bug_id, $p_user_access_level ) {
 833          $t_custom_fields = custom_field_get_all_linked_fields( $p_bug_id );
 834  
 835          # removing restricted fields
 836          foreach ( $t_custom_fields as $t_custom_field_name => $t_custom_field_data ) {
 837              if ( $p_user_access_level < $t_custom_field_data['access_level_r'] ) {
 838                  unset( $t_custom_fields[$t_custom_field_name] );
 839              }
 840          }
 841          return $t_custom_fields;
 842      }
 843  
 844      # --------------------
 845      # Gets the custom fields array for the given bug. Array keys are custom field names.
 846      # Array is sorted by custom field sequence number; Array items are arrays with the next keys:
 847      # 'type', 'value', 'access_level_r'
 848  	function custom_field_get_all_linked_fields( $p_bug_id ) {
 849          global $g_cached_custom_field_lists;
 850  
 851          if ( !is_array( $g_cached_custom_field_lists ) ) {
 852              $g_cached_custom_field_lists = array();
 853          }
 854  
 855          # is the list in cache ?
 856          if( !array_key_exists( $p_bug_id, $g_cached_custom_field_lists ) ) {
 857              $c_bug_id     = db_prepare_int( $p_bug_id );
 858              $c_project_id = db_prepare_int( bug_get_field( $p_bug_id, 'project_id' ) );
 859  
 860              $t_custom_field_project_table = config_get( 'mantis_custom_field_project_table' );
 861              $t_custom_field_table         = config_get( 'mantis_custom_field_table' );
 862              $t_custom_field_string_table  = config_get( 'mantis_custom_field_string_table' );
 863  
 864              $query = "SELECT f.name, f.type, f.access_level_r, f.default_value, f.type, s.value
 865                      FROM $t_custom_field_project_table p INNER JOIN $t_custom_field_table f
 866                          ON p.field_id = f.id
 867                      LEFT JOIN $t_custom_field_string_table s
 868                          ON  p.field_id=s.field_id AND s.bug_id='$c_bug_id'
 869                      WHERE   p.project_id = '$c_project_id'
 870                      ORDER BY p.sequence ASC, f.name ASC";
 871  
 872              $result = db_query( $query );
 873  
 874              $t_row_count = db_num_rows( $result );
 875  
 876              $t_custom_fields = array();
 877  
 878              for ( $i=0 ; $i < $t_row_count ; ++$i ) {
 879                  $row = db_fetch_array( $result );
 880  
 881                  if( is_null( $row['value'] ) ) {
 882                      $t_value = $row['default_value'];
 883                  } else {
 884                      $t_value = custom_field_database_to_value( $row['value'], $row['type'] );
 885                  }
 886  
 887                  $t_custom_fields[$row['name']] = array( 'type'  => $row['type'],
 888                                                            'value' => $t_value,
 889                                                            'access_level_r' => $row['access_level_r'] );
 890              }
 891  
 892              $g_cached_custom_field_lists[$p_bug_id] = $t_custom_fields;
 893          }
 894  
 895          return $g_cached_custom_field_lists[$p_bug_id];
 896      }
 897  
 898  
 899      # --------------------
 900      # Gets the sequence number for the specified custom field for the specified
 901      # project.  Returns false in case of error.
 902  	function custom_field_get_sequence( $p_field_id, $p_project_id ) {
 903          $c_field_id = db_prepare_int( $p_field_id );
 904          $c_project_id = db_prepare_int( $p_project_id );
 905  
 906          $t_custom_field_project_table = config_get( 'mantis_custom_field_project_table' );
 907          $query = "SELECT sequence
 908                    FROM $t_custom_field_project_table
 909                    WHERE field_id='$c_field_id' AND
 910                          project_id='$c_project_id'";
 911          $result = db_query( $query, 1 );
 912  
 913          if ( 0 == db_num_rows( $result ) ) {
 914              return false;
 915          }
 916  
 917          $t_row = db_fetch_array( $result );
 918  
 919          return $t_row['sequence'];
 920      }
 921  
 922      # --------------------
 923      # Allows the validation of a custom field value without setting it
 924      # or needing a bug to exist.
 925  	function custom_field_validate( $p_field_id, $p_value ) {
 926          $c_field_id    = db_prepare_int( $p_field_id );
 927          $c_value    = db_prepare_string( $p_value );
 928  
 929          custom_field_ensure_exists( $p_field_id );
 930  
 931          $t_custom_field_table = config_get( 'mantis_custom_field_table' );
 932          $query = "SELECT name, type, possible_values, valid_regexp,
 933                             access_level_rw, length_min, length_max, default_value
 934                    FROM $t_custom_field_table
 935                    WHERE id='$c_field_id'";
 936          $result = db_query( $query );
 937          $row = db_fetch_array( $result );
 938  
 939          $t_name                = $row['name'];
 940          $t_type                = $row['type'];
 941          $t_possible_values    = $row['possible_values'];
 942          $t_valid_regexp        = $row['valid_regexp'];
 943          $t_length_min        = $row['length_min'];
 944          $t_length_max        = $row['length_max'];
 945          $t_default_value    = $row['default_value'];
 946  
 947          # check for valid value
 948          if ( !is_blank( $t_valid_regexp ) ) {
 949              if ( !ereg( $t_valid_regexp, $p_value ) ) {
 950                  return false;
 951              }
 952          }
 953  
 954          if ( strlen( $p_value ) < $t_length_min ) {
 955              return false;
 956          }
 957  
 958          if ( ( 0 != $t_length_max ) && ( strlen( $p_value ) > $t_length_max ) ) {
 959              return false;
 960          }
 961  
 962          return true;
 963      }
 964  
 965      # --------------------
 966      # $p_possible_values: possible values to be pre-processed.  If it has enum values,
 967      #                     it will be left as is.  If it has a method, it will be replaced
 968      #                     by the list.
 969  	function custom_field_prepare_possible_values( $p_possible_values ) {
 970          $t_possible_values = $p_possible_values;
 971  
 972          if ( !is_blank( $t_possible_values ) && ( $t_possible_values[0] == '=' ) ) {
 973              $t_possible_values = helper_call_custom_function( 'enum_' . substr( $t_possible_values, 1 ), array() );
 974          }
 975  
 976          return $t_possible_values;
 977      }
 978  
 979      # --------------------
 980      # Get All Possible Values for a Field.
 981  	function custom_field_distinct_values( $p_field_id, $p_project_id = ALL_PROJECTS ) {
 982          $c_field_id                        = db_prepare_int( $p_field_id );
 983          $c_project_id                    = db_prepare_int( $p_project_id );
 984          $t_custom_field_string_table    = config_get( 'mantis_custom_field_string_table' );
 985          $t_custom_field_table            = config_get( 'mantis_custom_field_table' );
 986          $t_mantis_bug_table                = config_get( 'mantis_bug_table' );
 987          $t_return_arr                    = array();
 988  
 989          $query = "SELECT type, possible_values
 990                    FROM $t_custom_field_table
 991                    WHERE id='$c_field_id'";
 992          $result = db_query( $query );
 993  
 994          $t_row_count = db_num_rows( $result );
 995          if ( 0 == $t_row_count ) {
 996              return false;
 997          }
 998          $row = db_fetch_array( $result );
 999  
1000          # If an enumeration type, we get all possible values, not just used values
1001          if ( CUSTOM_FIELD_TYPE_ENUM == $row['type'] ||
1002               CUSTOM_FIELD_TYPE_CHECKBOX == $row['type'] ||
1003               CUSTOM_FIELD_TYPE_LIST == $row['type'] ||
1004               CUSTOM_FIELD_TYPE_MULTILIST == $row['type']
1005              ) {
1006              $t_possible_values = custom_field_prepare_possible_values( $row['possible_values'] );
1007  
1008              $t_values_arr = explode( '|', $t_possible_values );
1009  
1010              foreach( $t_values_arr as $t_option ) {
1011                  array_push( $t_return_arr, $t_option );
1012              }
1013          } else {
1014              $t_where = '';
1015              $t_from = $t_custom_field_string_table;
1016              if ( ALL_PROJECTS != $p_project_id ) {
1017                  $t_where = " AND $t_mantis_bug_table.id = $t_custom_field_string_table.bug_id AND
1018                              $t_mantis_bug_table.project_id = '$p_project_id'";
1019                  $t_from = $t_from . ", $t_mantis_bug_table";
1020              }
1021              $query2 = "SELECT $t_custom_field_string_table.value FROM $t_from
1022                          WHERE $t_custom_field_string_table.field_id='$c_field_id' $t_where
1023                          GROUP BY $t_custom_field_string_table.value";
1024              $result2 = db_query( $query2 );
1025              $t_row_count = db_num_rows( $result2 );
1026              if ( 0 == $t_row_count ) {
1027                  return false;
1028              }
1029  
1030              for ( $i = 0; $i < $t_row_count; $i++ ) {
1031                  $row = db_fetch_array( $result2 );
1032                  if( !is_blank( trim( $row['value'] ) ) ) {
1033                      array_push( $t_return_arr, $row['value'] );
1034                  }
1035              }
1036          }
1037          return $t_return_arr;
1038      }
1039  
1040      #===================================
1041      # Data Modification
1042      #===================================
1043  
1044       # --------------------
1045      # Convert the value to save it into the database, depending of the type
1046      # return value for database
1047  	function custom_field_value_to_database( $p_value, $p_type ) {
1048          switch ($p_type) {
1049          case CUSTOM_FIELD_TYPE_MULTILIST:
1050          case CUSTOM_FIELD_TYPE_CHECKBOX:
1051              if ( '' == $p_value ) {
1052                  $result = '';
1053              } else {
1054                  $result = '|' . $p_value . '|';
1055              }
1056              break;
1057          default:
1058              $result = $p_value;
1059          }
1060          return $result;
1061      }
1062  
1063      # --------------------
1064      # Convert the database-value to value, depending of the type
1065      # return value for further operation
1066  	function custom_field_database_to_value( $p_value, $p_type ) {
1067          switch ($p_type) {
1068          case CUSTOM_FIELD_TYPE_MULTILIST:
1069          case CUSTOM_FIELD_TYPE_CHECKBOX:
1070              $result = str_replace( '||', '', '|' . $p_value . '|' );
1071              break;
1072          default:
1073              $result = $p_value;
1074          }
1075          return $result;
1076      }
1077  
1078      # --------------------
1079      # Set the value of a custom field for a given bug
1080      #  return true on success, false on failure
1081  	function custom_field_set_value( $p_field_id, $p_bug_id, $p_value ) {
1082          $c_field_id    = db_prepare_int( $p_field_id );
1083          $c_bug_id    = db_prepare_int( $p_bug_id );
1084  
1085          custom_field_ensure_exists( $p_field_id );
1086  
1087          $t_custom_field_table = config_get( 'mantis_custom_field_table' );
1088          $query = "SELECT name, type, possible_values, valid_regexp,
1089                    access_level_rw, length_min, length_max, default_value
1090                    FROM $t_custom_field_table
1091                    WHERE id='$c_field_id'";
1092          $result = db_query( $query );
1093          $row = db_fetch_array( $result );
1094  
1095          $t_name                = $row['name'];
1096          $t_type                = $row['type'];
1097          $t_possible_values    = $row['possible_values'];
1098          $t_valid_regexp        = $row['valid_regexp'];
1099          $t_access_level_rw    = $row['access_level_rw'];
1100          $t_length_min        = $row['length_min'];
1101          $t_length_max        = $row['length_max'];
1102          $t_default_value    = $row['default_value'];
1103  
1104          $c_value    = db_prepare_string( custom_field_value_to_database( $p_value, $t_type ) );
1105  
1106          # check for valid value
1107          if ( !is_blank( $t_valid_regexp ) ) {
1108              if ( !ereg( $t_valid_regexp, $p_value ) ) {
1109                  return false;
1110              }
1111          }
1112  
1113          if ( strlen( $p_value ) < $t_length_min ) {
1114              return false;
1115          }
1116  
1117          if ( ( 0 != $t_length_max ) && ( strlen( $p_value ) > $t_length_max ) ) {
1118              return false;
1119          }
1120  
1121          if( !custom_field_has_write_access( $p_field_id, $p_bug_id, auth_get_current_user_id() ) ) {
1122              return false;
1123          }
1124  
1125          $t_custom_field_string_table = config_get( 'mantis_custom_field_string_table' );
1126  
1127          # do I need to update or insert this value?
1128          $query = "SELECT value
1129                    FROM $t_custom_field_string_table
1130                    WHERE field_id='$c_field_id' AND
1131                            bug_id='$c_bug_id'";
1132          $result = db_query( $query );
1133  
1134          if ( db_num_rows( $result ) > 0 ) {
1135              $query = "UPDATE $t_custom_field_string_table
1136                        SET value='$c_value'
1137                        WHERE field_id='$c_field_id' AND
1138                                bug_id='$c_bug_id'";
1139              db_query( $query );
1140  
1141              $row = db_fetch_array( $result );
1142              history_log_event_direct( $c_bug_id, $t_name, custom_field_database_to_value( $row['value'], $t_type ), $p_value );
1143          } else {
1144              # Always store the value, even if it's the dafault value
1145              # This is important, as the definitions might change but the
1146              #  values stored with a bug must not change
1147              $query = "INSERT INTO $t_custom_field_string_table
1148                          ( field_id, bug_id, value )
1149                        VALUES
1150                          ( '$c_field_id', '$c_bug_id', '$c_value' )";
1151              db_query( $query );
1152              history_log_event_direct( $c_bug_id, $t_name, '', $p_value );
1153          }
1154  
1155          custom_field_clear_cache( $p_field_id );
1156  
1157          #db_query() errors on failure so:
1158          return true;
1159      }
1160  
1161      # --------------------
1162      # Sets the sequence number for the specified custom field for the specified
1163      # project.
1164  	function custom_field_set_sequence( $p_field_id, $p_project_id, $p_sequence ) {
1165          $c_field_id = db_prepare_int( $p_field_id );
1166          $c_project_id = db_prepare_int( $p_project_id );
1167          $c_sequence = db_prepare_int( $p_sequence );
1168  
1169          $t_custom_field_project_table = config_get( 'mantis_custom_field_project_table' );
1170  
1171          $query = "UPDATE $t_custom_field_project_table
1172                    SET sequence='$c_sequence'
1173                    WHERE field_id='$c_field_id' AND
1174                            project_id='$c_project_id'";
1175          $result = db_query( $query );
1176  
1177          custom_field_clear_cache( $p_field_id );
1178  
1179          return true;
1180      }
1181  
1182      #===================================
1183      # Output
1184      #===================================
1185  
1186      # --------------------
1187      # Print an input field
1188      # $p_field_def contains the definition of the custom field (including it's
1189      #              field id
1190      # $p_bug_id    contains the bug where this field belongs to. If it's left
1191      #              away, it'll default to 0 and thus belongs to a new (i.e.
1192      #              non-existant) bug
1193      # NOTE: This probably belongs in the print_api.php
1194  	function print_custom_field_input( $p_field_def, $p_bug_id = null ) {
1195          $t_id = $p_field_def['id'];
1196  
1197          if( null === $p_bug_id ) {
1198              $t_custom_field_value = $p_field_def['default_value'];
1199          } else {
1200              $t_custom_field_value = custom_field_get_value( $t_id, $p_bug_id );
1201          }
1202  
1203          $t_custom_field_value = string_attribute( $t_custom_field_value );
1204  
1205          switch ($p_field_def['type']) {
1206          case CUSTOM_FIELD_TYPE_ENUM:
1207          case CUSTOM_FIELD_TYPE_LIST:
1208          case CUSTOM_FIELD_TYPE_MULTILIST:
1209               $t_values = explode( '|', custom_field_prepare_possible_values( $p_field_def['possible_values'] ) );
1210              $t_list_size = $t_possible_values_count = count( $t_values );
1211  
1212              if ( $t_possible_values_count > 5 ) {
1213                  $t_list_size = 5;
1214              }
1215  
1216              if ( $p_field_def['type'] == CUSTOM_FIELD_TYPE_ENUM ) {
1217                  $t_list_size = 0;    # for enums the size is 0
1218              }
1219  
1220              if ( $p_field_def['type'] == CUSTOM_FIELD_TYPE_MULTILIST ) {
1221                  echo '<select ', helper_get_tab_index(), ' name="custom_field_' . $t_id . '[]" size="' . $t_list_size . '" multiple="multiple">';
1222              } else {
1223                  echo '<select ', helper_get_tab_index(), ' name="custom_field_' . $t_id . '" size="' . $t_list_size . '">';
1224              }
1225  
1226              $t_selected_values = explode( '|', $t_custom_field_value );
1227               foreach( $t_values as $t_option ) {
1228                  if( in_array( $t_option, $t_selected_values, true ) ) {
1229                       echo '<option value="' . $t_option . '" selected="selected"> ' . $t_option . '</option>';
1230                   } else {
1231                       echo '<option value="' . $t_option . '">' . $t_option . '</option>';
1232                   }
1233               }
1234               echo '</select>';
1235              break;
1236          case CUSTOM_FIELD_TYPE_CHECKBOX:
1237              $t_values = explode( '|', custom_field_prepare_possible_values( $p_field_def['possible_values'] ) );
1238              $t_checked_values = explode( '|', $t_custom_field_value );
1239              foreach( $t_values as $t_option ) {
1240                  echo '<input ', helper_get_tab_index(), ' type="checkbox" name="custom_field_' . $t_id . '[]"';
1241                  if( in_array( $t_option, $t_checked_values, true ) ) {
1242                      echo ' value="' . $t_option . '" checked="checked">&nbsp;' . $t_option . '&nbsp;&nbsp;';
1243                  } else {
1244                      echo ' value="' . $t_option . '">&nbsp;' . $t_option . '&nbsp;&nbsp;';
1245                  }
1246              }
1247               break;
1248          case CUSTOM_FIELD_TYPE_NUMERIC:
1249          case CUSTOM_FIELD_TYPE_FLOAT:
1250          case CUSTOM_FIELD_TYPE_EMAIL:
1251          case CUSTOM_FIELD_TYPE_STRING:
1252              echo '<input ', helper_get_tab_index(), ' type="text" name="custom_field_' . $t_id . '" size="80"';
1253              if( 0 < $p_field_def['length_max'] ) {
1254                  echo ' maxlength="' . $p_field_def['length_max'] . '"';
1255              } else {
1256                  echo ' maxlength="255"';
1257              }
1258              echo ' value="' . $t_custom_field_value .'"></input>';
1259              break ;
1260  
1261          case CUSTOM_FIELD_TYPE_DATE:
1262              print_date_selection_set("custom_field_" . $t_id, config_get('short_date_format'), $t_custom_field_value, false, true) ;
1263              break ;
1264          }
1265      }
1266  
1267      # --------------------
1268      # Prepare a string containing a custom field value for display
1269      # $p_def         contains the definition of the custom field
1270      # $p_field_id     contains the id of the field
1271      # $p_bug_id        contains the bug id to display the custom field value for
1272      # NOTE: This probably belongs in the string_api.php
1273  	function string_custom_field_value( $p_def, $p_field_id, $p_bug_id ) {
1274          $t_custom_field_value = custom_field_get_value( $p_field_id, $p_bug_id );
1275          switch( $p_def['type'] ) {
1276              case CUSTOM_FIELD_TYPE_EMAIL:
1277                  return "<a href=\"mailto:$t_custom_field_value\">$t_custom_field_value</a>";
1278                  break;
1279              case CUSTOM_FIELD_TYPE_ENUM:
1280              case CUSTOM_FIELD_TYPE_LIST:
1281              case CUSTOM_FIELD_TYPE_MULTILIST:
1282              case CUSTOM_FIELD_TYPE_CHECKBOX:
1283                  return str_replace( '|', ', ', $t_custom_field_value );
1284                  break;
1285              case CUSTOM_FIELD_TYPE_DATE:
1286                  if ($t_custom_field_value != null) {
1287                      return date( config_get( 'short_date_format'), $t_custom_field_value) ;
1288                  }
1289                  break ;
1290              default:
1291                  return string_display_links( $t_custom_field_value );
1292          }
1293      }
1294  
1295      # --------------------
1296      # Print a custom field value for display
1297      # $p_def         contains the definition of the custom field
1298      # $p_field_id     contains the id of the field
1299      # $p_bug_id        contains the bug id to display the custom field value for
1300      # NOTE: This probably belongs in the print_api.php
1301  	function print_custom_field_value( $p_def, $p_field_id, $p_bug_id ) {
1302          echo string_custom_field_value( $p_def, $p_field_id, $p_bug_id );
1303      }
1304  
1305      # --------------------
1306      # Prepare a string containing a custom field value for email
1307      # $p_value        value of custom field
1308      # $p_type        type of custom field
1309      # NOTE: This probably belongs in the string_api.php
1310  	function string_custom_field_value_for_email( $p_value, $p_type ) {
1311          switch( $p_type ) {
1312              case CUSTOM_FIELD_TYPE_EMAIL:
1313                  return 'mailto:'.$p_value;
1314                  break;
1315              case CUSTOM_FIELD_TYPE_ENUM:
1316              case CUSTOM_FIELD_TYPE_LIST:
1317              case CUSTOM_FIELD_TYPE_MULTILIST:
1318              case CUSTOM_FIELD_TYPE_CHECKBOX:
1319                  return str_replace( '|', ', ', $p_value );
1320                  break;
1321              case CUSTOM_FIELD_TYPE_DATE:
1322                  if ($p_value != null) {
1323                      return date( config_get( 'short_date_format' ), $p_value) ;
1324                  }
1325                  break ;
1326              default:
1327                  return $p_value;
1328          }
1329          return $p_value;
1330      }
1331  
1332  ?>


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