[ 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/ -> tag_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: tag_api.php,v 1.4.2.2 2007-10-13 22:35:44 giallu Exp $
  22      # --------------------------------------------------------
  23  
  24      /**
  25       * Tag API
  26       *
  27       * @package TagAPI
  28       * @author John Reese
  29       */
  30  
  31      $t_core_dir = dirname( __FILE__ ).DIRECTORY_SEPARATOR;
  32  
  33      require_once ( $t_core_dir . 'bug_api.php' );
  34      require_once ( $t_core_dir . 'history_api.php' );
  35      
  36      /**
  37       * Determine if a tag exists with the given ID.
  38       * @param integer Tag ID
  39       * @return boolean True if tag exists
  40       */
  41  	function tag_exists( $p_tag_id ) {
  42          $c_tag_id = db_prepare_int( $p_tag_id );
  43          $t_tag_table = config_get( 'mantis_tag_table' );
  44  
  45          $query = "SELECT * FROM $t_tag_table WHERE id='$c_tag_id'";
  46          $result = db_query( $query ) ;
  47  
  48          return db_num_rows( $result ) > 0;
  49      }
  50  
  51      /**
  52       * Ensure a tag exists with the given ID.
  53       * @param integer Tag ID
  54       */
  55  	function tag_ensure_exists( $p_tag_id ) {
  56          if ( !tag_exists( $p_tag_id ) ) {
  57              error_parameters( $p_tag_id );
  58              trigger_error( ERROR_TAG_NOT_FOUND, ERROR );
  59          }
  60      }
  61  
  62      /**
  63       * Determine if a given name is unique (not already used).
  64       * Uses a case-insensitive search of the database for existing tags with the same name.
  65       * @param string Tag name
  66       * @return boolean True if name is unique
  67       */
  68  	function tag_is_unique( $p_name ) {
  69          $c_name = trim( db_prepare_string( $p_name ) );
  70          $t_tag_table = config_get( 'mantis_tag_table' );
  71  
  72          $query = "SELECT id FROM $t_tag_table WHERE ".db_helper_like( 'name', $c_name );
  73          $result = db_query( $query ) ;
  74  
  75          return db_num_rows( $result ) == 0;
  76      }
  77  
  78      /**
  79       * Ensure that a name is unique.
  80       * @param string Tag name
  81       */
  82  	function tag_ensure_unique( $p_name ) {
  83          if ( !tag_is_unique( $p_name ) ) {
  84              trigger_error( ERROR_TAG_DUPLICATE, ERROR );
  85          }
  86      }
  87  
  88      /**
  89       * Determine if a given name is valid.
  90       * Name must start with letter/number and consist of letters, numbers, 
  91       * hyphens, underscores, periods, or spaces.  The matches parameter allows 
  92       * you to also receive an array of regex matches, which by default only 
  93       * includes the valid tag name itself.  The prefix parameter is optional,
  94       * but allows you to prefix the regex check, which is useful for filters, etc.
  95       * @param string Tag name
  96       * @param array Array reference for regex matches
  97       * @param string Prefix regex pattern
  98       * @return boolean True if the name is valid
  99       */
 100  	function tag_name_is_valid( $p_name, &$p_matches, $p_prefix="" ) {
 101          $t_pattern = "/^$p_prefix([a-zA-Z0-9][a-zA-Z0-9-_. ]*)$/";
 102          return preg_match( $t_pattern, $p_name, $p_matches );
 103      }
 104  
 105      /**
 106       * Ensure a tag name is valid.
 107       * @param string Tag name
 108       */
 109  	function tag_ensure_name_is_valid( $p_name ) {
 110          $t_matches = array();
 111          if ( !tag_name_is_valid( $p_name, $t_matches ) ) {
 112              trigger_error( ERROR_TAG_NAME_INVALID, ERROR );
 113          }
 114      }
 115  
 116      /**
 117       * Compare two tag rows based on tag name.
 118       * @param array Tag row 1
 119       * @param array Tag row 2
 120       * @return integer -1 when Tag 1 < Tag 2, 1 when Tag 1 > Tag 2, 0 otherwise
 121       */
 122  	function tag_cmp_name( $p_tag1, $p_tag2 ) {
 123          return strcasecmp( $p_tag1['name'], $p_tag2['name'] );
 124      }
 125  
 126      /**
 127       * Parse a form input string to extract existing and new tags.
 128       * When given a string, parses for tag names separated by configured separator,
 129       * then returns an array of tag rows for each tag.  Existing tags get the full
 130       * row of information returned.  If the tag does not exist, a row is returned with
 131       * id = -1 and the tag name, and if the name is invalid, a row is returned with
 132       * id = -2 and the tag name.  The resulting array is then sorted by tag name.
 133       * @param string Input string to parse
 134       * @return array Rows of tags parsed from input string
 135       */
 136  	function tag_parse_string( $p_string ) {
 137          $t_tags = array();
 138  
 139          $t_strings = explode( config_get( 'tag_separator' ), $p_string );
 140          foreach( $t_strings as $t_name ) {
 141              $t_name = trim( $t_name );
 142              if ( is_blank( $t_name ) ) { continue; }
 143              
 144              $t_matches = array();
 145              $t_tag_row = tag_get_by_name( $t_name );
 146              if ( $t_tag_row !== false ) {
 147                  $t_tags[] = $t_tag_row;
 148              } else {
 149                  if ( tag_name_is_valid( $t_name, $t_matches ) ) {
 150                      $t_id = -1;
 151                  } else {
 152                      $t_id = -2;
 153                  }
 154                  $t_tags[] = array( 'id' => $t_id, 'name' => $t_name );
 155              }
 156          }
 157          usort( $t_tags, "tag_cmp_name" );
 158          return $t_tags;
 159      }
 160  
 161      /**
 162       * Parse a filter string to extract existing and new tags.
 163       * When given a string, parses for tag names separated by configured separator,
 164       * then returns an array of tag rows for each tag.  Existing tags get the full
 165       * row of information returned.  If the tag does not exist, a row is returned with
 166       * id = -1 and the tag name, and if the name is invalid, a row is returned with
 167       * id = -2 and the tag name.  The resulting array is then sorted by tag name.
 168       * @param string Filter string to parse
 169       * @return array Rows of tags parsed from filter string
 170       */
 171  	function tag_parse_filters( $p_string ) {
 172          $t_tags = array();
 173          $t_prefix = "[+-]{0,1}";
 174  
 175          $t_strings = explode( config_get( 'tag_separator' ), $p_string );
 176          foreach( $t_strings as $t_name ) {
 177              $t_name = trim( $t_name );
 178              $t_matches = array();
 179  
 180              if ( !is_blank( $t_name ) && tag_name_is_valid( $t_name, $t_matches, $t_prefix ) ) { 
 181                  $t_tag_row = tag_get_by_name( $t_matches[1] );
 182                  if ( $t_tag_row !== false ) {
 183                      $t_filter = substr( $t_name, 0, 1 );
 184  
 185                      if ( "+" == $t_filter ) {
 186                          $t_tag_row['filter'] = 1;
 187                      } elseif ( "-" == $t_filter ) {
 188                          $t_tag_row['filter'] = -1;
 189                      } else {
 190                          $t_tag_row['filter'] = 0;
 191                      }
 192  
 193                      $t_tags[] = $t_tag_row;
 194                  }
 195              } else {
 196                  continue; 
 197              }
 198          }
 199          usort( $t_tags, "tag_cmp_name" );
 200          return $t_tags;
 201      }
 202  
 203      # CRUD
 204  
 205      /**
 206       * Return a tag row for the given ID.
 207       * @param integer Tag ID
 208       * @return array Tag row
 209       */
 210  	function tag_get( $p_tag_id ) {
 211          tag_ensure_exists( $p_tag_id );
 212  
 213          $c_tag_id        = db_prepare_int( $p_tag_id );
 214          
 215          $t_tag_table    = config_get( 'mantis_tag_table' );
 216  
 217          $query = "SELECT * FROM $t_tag_table
 218                      WHERE id='$c_tag_id'";
 219          $result = db_query( $query );
 220  
 221          if ( 0 == db_num_rows( $result ) ) {
 222              return false;
 223          }
 224          $row = db_fetch_array( $result );
 225  
 226          return $row;
 227      }
 228  
 229      /**
 230       * Return a tag row for the given name.
 231       * @param string Tag name
 232       * @return Tag row
 233       */
 234  	function tag_get_by_name( $p_name ) {
 235          $c_name         = db_prepare_string( $p_name );
 236  
 237          $t_tag_table    = config_get( 'mantis_tag_table' );
 238  
 239          $query = "SELECT * FROM $t_tag_table
 240                      WHERE ".db_helper_like( 'name', $c_name );
 241          $result = db_query( $query );
 242  
 243          if ( 0 == db_num_rows( $result ) ) {
 244              return false;
 245          }
 246          $row = db_fetch_array( $result );
 247  
 248          return $row;
 249      }
 250  
 251      /**
 252       * Return a single field from a tag row for the given ID.
 253       * @param integer Tag ID
 254       * @param string Field name
 255       * @return mixed Field value
 256       */
 257  	function tag_get_field( $p_tag_id, $p_field_name ) {
 258          $row = tag_get( $p_tag_id );
 259  
 260          if ( isset( $row[$p_field_name] ) ) {
 261              return $row[$p_field_name];
 262          } else {
 263              error_parameters( $p_field_name );
 264              trigger_error( ERROR_DB_FIELD_NOT_FOUND, WARNING );
 265              return '';
 266          }
 267      }
 268  
 269      /**
 270       * Create a tag with the given name, creator, and description.
 271       * Defaults to the currently logged in user, and a blank description.
 272       * @param string Tag name
 273       * @param integer User ID
 274       * @param string Description
 275       * @return integer Tag ID
 276       */
 277  	function tag_create( $p_name, $p_user_id=null, $p_description='' ) {
 278          access_ensure_global_level( config_get( 'tag_create_threshold' ) );
 279  
 280          tag_ensure_name_is_valid( $p_name );
 281          tag_ensure_unique( $p_name );
 282  
 283          if ( null == $p_user_id ) {
 284              $p_used_id = auth_get_current_user_id();
 285          } else {
 286              user_ensure_exists( $p_user_id );
 287          }
 288  
 289          $c_name            = trim( db_prepare_string( $p_name ) );
 290          $c_description    = db_prepare_string( $p_description );
 291          $c_user_id        = db_prepare_int( $p_user_id );
 292          $c_date_created    = db_now();
 293          
 294          $t_tag_table    = config_get( 'mantis_tag_table' );
 295  
 296          $query = "INSERT INTO $t_tag_table
 297                  ( user_id,     
 298                    name, 
 299                    description, 
 300                    date_created, 
 301                    date_updated 
 302                  )
 303                  VALUES
 304                  ( '$c_user_id', 
 305                    '$c_name', 
 306                    '$c_description', 
 307                    ".$c_date_created.", 
 308                    ".$c_date_created."
 309                  )";
 310  
 311          db_query( $query );
 312          return db_insert_id( $t_tag_table );
 313      }
 314  
 315      /**
 316       * Update a tag with given name, creator, and description.
 317       * @param integer Tag ID
 318       * @param string Tag name
 319       * @param integer User ID
 320       * @param string Description
 321       */
 322  	function tag_update( $p_tag_id, $p_name, $p_user_id, $p_description ) {
 323          user_ensure_exists( $p_user_id );
 324          
 325          if ( auth_get_current_user_id() == tag_get_field( $p_tag_id, 'user_id' ) ) {
 326              $t_update_level = config_get( 'tag_edit_own_threshold' );
 327          } else {
 328              $t_update_level = config_get( 'tag_edit_threshold' );
 329          }
 330  
 331          access_ensure_global_level( $t_update_level );
 332          
 333          tag_ensure_name_is_valid( $p_name );
 334  
 335          $t_tag_name = tag_get_field( $p_tag_id, 'name' );
 336  
 337          $t_rename = false;
 338          if ( strtolower($p_name) != strtolower($t_tag_name) ) {
 339              tag_ensure_unique( $p_name );
 340              $t_rename = true;
 341          }
 342          
 343          $c_tag_id        = trim( db_prepare_int( $p_tag_id ) );
 344          $c_user_id        = db_prepare_string( $p_user_id );
 345          $c_name            = db_prepare_string( $p_name );
 346          $c_description    = db_prepare_string( $p_description );
 347          $c_date_updated    = db_now();
 348  
 349          $t_tag_table    = config_get( 'mantis_tag_table' );
 350  
 351          $query = "UPDATE $t_tag_table
 352                      SET user_id='$c_user_id',
 353                          name='$c_name',
 354                          description='$c_description',
 355                          date_updated=".$c_date_updated."
 356                      WHERE id='$c_tag_id'";
 357          db_query( $query );
 358  
 359          if ( $t_rename ) {
 360              $t_bugs = tag_get_bugs_attached( $p_tag_id );
 361  
 362              foreach ( $t_bugs as $t_bug_id ) {
 363                  history_log_event_special( $t_bug_id, TAG_RENAMED, $t_tag_name, $c_name );
 364              }
 365          }
 366  
 367          return true;
 368      }
 369  
 370      /**
 371       * Delete a tag with the given ID.
 372       * @param integer Tag ID
 373       */
 374  	function tag_delete( $p_tag_id ) {
 375          tag_ensure_exists( $p_tag_id );
 376          
 377          access_ensure_global_level( config_get( 'tag_edit_threshold' ) );
 378          
 379          $t_bugs = tag_get_bugs_attached( $p_tag_id );
 380          foreach ( $t_bugs as $t_bug_id ) {
 381              tag_bug_detach( $p_tag_id, $t_bug_id );
 382          }
 383          
 384          $c_tag_id            = db_prepare_int( $p_tag_id );
 385  
 386          $t_tag_table        = config_get( 'mantis_tag_table' );
 387          $t_bug_tag_table    = config_get( 'mantis_bug_tag_table' );
 388  
 389          $query = "DELETE FROM $t_tag_table
 390                      WHERE id='$c_tag_id'";
 391          db_query( $query );
 392  
 393          return true;
 394      }
 395      
 396      # Associative
 397  
 398      /**
 399       * Determine if a tag is attached to a bug.
 400       * @param integer Tag ID
 401       * @param integer Bug ID
 402       * @return boolean True if the tag is attached
 403       */
 404  	function tag_bug_is_attached( $p_tag_id, $p_bug_id ) {
 405          $c_tag_id         = db_prepare_int( $p_tag_id );
 406          $c_bug_id         = db_prepare_int( $p_bug_id );
 407  
 408          $t_bug_tag_table= config_get( 'mantis_bug_tag_table' );
 409  
 410          $query = "SELECT * FROM $t_bug_tag_table
 411                      WHERE tag_id='$c_tag_id' AND bug_id='$c_bug_id'";
 412          $result = db_query( $query );
 413          return ( db_num_rows( $result ) > 0 );
 414      }
 415  
 416      /**
 417       * Return the tag attachment row.
 418       * @param integer Tag ID
 419       * @param integer Bug ID
 420       * @return array Tag attachment row
 421       */
 422  	function tag_bug_get_row( $p_tag_id, $p_bug_id ) {
 423          $c_tag_id         = db_prepare_int( $p_tag_id );
 424          $c_bug_id         = db_prepare_int( $p_bug_id );
 425  
 426          $t_bug_tag_table= config_get( 'mantis_bug_tag_table' );
 427  
 428          $query = "SELECT * FROM $t_bug_tag_table
 429                      WHERE tag_id='$c_tag_id' AND bug_id='$c_bug_id'";
 430          $result = db_query( $query );
 431  
 432          if ( db_num_rows( $result ) == 0 ) {
 433              trigger_error( TAG_NOT_ATTACHED, ERROR );
 434          }
 435          return db_fetch_array( $result );
 436      }
 437  
 438      /**
 439       * Return an array of tags attached to a given bug sorted by tag name.
 440       * @param Bug ID
 441       * @return array Array of tag rows with attachement information
 442       */
 443  	function tag_bug_get_attached( $p_bug_id ) {
 444          $c_bug_id         = db_prepare_int( $p_bug_id );
 445  
 446          $t_tag_table    = config_get( 'mantis_tag_table' );
 447          $t_bug_tag_table= config_get( 'mantis_bug_tag_table' );
 448  
 449          $query = "SELECT t.*, b.user_id as user_attached, b.date_attached
 450                      FROM $t_tag_table as t
 451                      LEFT JOIN $t_bug_tag_table as b
 452                          on t.id=b.tag_id
 453                      WHERE b.bug_id='$c_bug_id'";
 454          $result = db_query( $query );
 455  
 456          $rows = array();
 457          while ( $row = db_fetch_array( $result ) ) {
 458              $rows[] = $row;
 459          }
 460          
 461          usort( $rows, "tag_cmp_name" );
 462          return $rows;
 463      }
 464  
 465      /**
 466       * Return an array of bugs that a tag is attached to.
 467       * @param integer Tag ID
 468       * @return array Array of bug ID's.
 469       */
 470  	function tag_get_bugs_attached( $p_tag_id ) {
 471          $c_tag_id         = db_prepare_int( $p_tag_id );
 472  
 473          $t_bug_tag_table= config_get( 'mantis_bug_tag_table' );
 474  
 475          $query = "SELECT bug_id FROM $t_bug_tag_table
 476                      WHERE tag_id='$c_tag_id'";
 477          $result = db_query( $query );
 478  
 479          $bugs = array();
 480          while ( $row = db_fetch_array( $result ) ) {
 481              $bugs[] = $row['bug_id'];
 482          }
 483  
 484          return $bugs;
 485      }
 486  
 487      /**
 488       * Attach a tag to a bug.
 489       * @param integer Tag ID
 490       * @param integer Bug ID
 491       * @param integer User ID
 492       */
 493  	function tag_bug_attach( $p_tag_id, $p_bug_id, $p_user_id=null ) {
 494          access_ensure_bug_level( config_get( 'tag_attach_threshold' ), $p_bug_id, $p_user_id );
 495  
 496          tag_ensure_exists( $p_tag_id );
 497  
 498          if ( tag_bug_is_attached( $p_tag_id, $p_bug_id ) ) {
 499              trigger_error( TAG_ALREADY_ATTACHED, ERROR );
 500          }
 501  
 502          if ( null == $p_user_id ) {
 503              $p_used_id = auth_get_current_user_id();
 504          } else {
 505              user_ensure_exists( $p_user_id );
 506          }
 507  
 508          $c_tag_id         = db_prepare_int( $p_tag_id );
 509          $c_bug_id         = db_prepare_int( $p_bug_id );
 510          $c_user_id         = db_prepare_int( $p_user_id );
 511          $c_date_attached= db_now();
 512  
 513          $t_bug_tag_table= config_get( 'mantis_bug_tag_table' );
 514  
 515          $query = "INSERT INTO $t_bug_tag_table
 516                      ( tag_id,
 517                        bug_id,
 518                        user_id,
 519                        date_attached
 520                      )
 521                      VALUES
 522                      ( '$c_tag_id',
 523                        '$c_bug_id',
 524                        '$c_user_id',
 525                        ".$c_date_attached."
 526                      )";
 527          db_query( $query );
 528  
 529          $t_tag_name = tag_get_field( $p_tag_id, 'name' );
 530          history_log_event_special( $p_bug_id, TAG_ATTACHED, $t_tag_name );
 531  
 532          return true;
 533      }
 534  
 535      /**
 536       * Detach a tag from a bug.
 537       * @param integer Tag ID
 538       * @param integer Bug ID
 539       * @param boolean Add history entries to bug
 540       * @param integer User Id (or null for current logged in user)     
 541       */
 542  	function tag_bug_detach( $p_tag_id, $p_bug_id, $p_add_history=true, $p_user_id = null ) {
 543          if ( $p_user_id === null ) {
 544              $t_user_id = auth_get_current_user_id();
 545          } else {
 546              $t_user_id = $p_user_id;
 547          }
 548  
 549          if ( $t_user_id == tag_get_field( $p_tag_id, 'user_id' ) ) {
 550              $t_detach_level = config_get( 'tag_detach_own_threshold' );
 551          } else {
 552              $t_detach_level = config_get( 'tag_detach_threshold' );
 553          }
 554  
 555          access_ensure_bug_level( config_get( 'tag_detach_threshold' ), $p_bug_id, $t_user_id );
 556  
 557          if ( !tag_bug_is_attached( $p_tag_id, $p_bug_id ) ) {
 558              trigger_error( TAG_NOT_ATTACHED, ERROR );
 559          }
 560          
 561          $c_tag_id         = db_prepare_int( $p_tag_id );
 562          $c_bug_id         = db_prepare_int( $p_bug_id );
 563  
 564          $t_bug_tag_table= config_get( 'mantis_bug_tag_table' );
 565  
 566          $query = "DELETE FROM $t_bug_tag_table 
 567                      WHERE tag_id='$c_tag_id' AND bug_id='$c_bug_id'";
 568          db_query( $query );
 569  
 570          if ( $p_add_history ) {
 571              $t_tag_name = tag_get_field( $p_tag_id, 'name' );
 572              history_log_event_special( $p_bug_id, TAG_DETACHED, $t_tag_name );
 573          }
 574  
 575          return true;
 576      }
 577  
 578      /**
 579       * Detach all tags from a given bug.
 580       * @param integer Bug ID
 581       * @param boolean Add history entries to bug
 582       * @param integer User Id (or null for current logged in user)     
 583       */
 584  	function tag_bug_detach_all( $p_bug_id, $p_add_history=true, $p_user_id = null ) {
 585          $t_tags = tag_bug_get_attached( $p_bug_id );
 586          foreach ( $t_tags as $t_tag_row ) {
 587              tag_bug_detach( $t_tag_row['id'], $p_bug_id, $p_add_history, $p_user_id );
 588          }
 589      }
 590  
 591      # Display
 592  
 593      /**
 594       * Display a tag hyperlink.
 595       * If a bug ID is passed, the tag link will include a detach link if the 
 596       * user has appropriate privileges.
 597       * @param array Tag row
 598       * @param integer Bug ID
 599       */
 600  	function tag_display_link( $p_tag_row, $p_bug_id=0 ) {
 601          if ( auth_get_current_user_id() == $p_tag_row['user_attached'] ) {
 602              $t_detach = config_get( 'tag_detach_own_threshold' );
 603          } else {
 604              $t_detach = config_get( 'tag_detach_threshold' );
 605          }
 606          
 607          $t_name = string_display_line( $p_tag_row['name'] );
 608          $t_description = string_display_line( $p_tag_row['description'] );
 609          
 610          echo "<a href='tag_view_page.php?tag_id=$p_tag_row[id]' title='$t_description'>$t_name</a>";
 611          
 612          if ( access_has_global_level($t_detach) ) {
 613              $t_tooltip = sprintf( lang_get( 'tag_detach' ), $t_name );
 614              echo " <a href='tag_detach.php?bug_id=$p_bug_id&tag_id=$p_tag_row[id]'><img src='images/delete.png' class='delete-icon' title=\"$t_tooltip\"/></a>";
 615          }
 616          
 617          return true;
 618      }
 619  
 620      /**
 621       * Display a list of attached tag hyperlinks separated by the configured hyperlinks.
 622       * @param Bug ID
 623       */
 624  	function tag_display_attached( $p_bug_id ) {
 625          $t_tag_rows = tag_bug_get_attached( $p_bug_id );
 626  
 627          if ( count( $t_tag_rows ) == 0 ) {
 628              echo lang_get( 'tag_none_attached' );
 629          } else {
 630              $i = 0;
 631              foreach ( $t_tag_rows as $t_tag ) {
 632                  echo ( $i > 0 ? config_get('tag_separator')." " : "" );
 633                  tag_display_link( $t_tag, $p_bug_id );
 634                  $i++;
 635              }
 636          }
 637  
 638          return true;
 639      }
 640  
 641      # Statistics
 642  
 643      /**
 644       * Get the number of bugs a given tag is attached to.
 645       * @param integer Tag ID
 646       * @return integer Number of attached bugs
 647       */
 648  	function tag_stats_attached( $p_tag_id ) {
 649          $c_tag_id = db_prepare_int( $p_tag_id );
 650          $t_bug_tag_table = config_get( 'mantis_bug_tag_table' );
 651  
 652          $query = "SELECT COUNT(*) FROM $t_bug_tag_table
 653                      WHERE tag_id='$c_tag_id'";
 654          $result = db_query( $query );
 655  
 656          return db_result( $result );
 657      }
 658  
 659      /**
 660       * Get a list of related tags.
 661       * Returns a list of tags that are the most related to the given tag,
 662       * based on the number of times they have been attached to the same bugs.
 663       * Defaults to a list of five tags.
 664       * @param integer Tag ID
 665       * @param integer List size
 666       * @return array Array of tag rows, with share count added
 667       */
 668  	function tag_stats_related( $p_tag_id, $p_limit=5 ) {
 669          $t_bug_table = config_get( 'mantis_bug_table' );
 670          $t_tag_table = config_get( 'mantis_tag_table' );
 671          $t_bug_tag_table = config_get( 'mantis_bug_tag_table' );
 672          $t_project_user_list_table = config_get( 'mantis_project_user_list_table' );
 673          $t_user_table = config_get( 'mantis_user_table' );
 674  
 675          $c_tag_id = db_prepare_int( $p_tag_id );
 676          $c_user_id = auth_get_current_user_id();
 677  
 678          $subquery = "SELECT b.id FROM $t_bug_table AS b
 679                      LEFT JOIN $t_project_user_list_table AS p
 680                          ON p.project_id=b.project_id AND p.user_id=$c_user_id
 681                      JOIN $t_user_table AS u
 682                          ON u.id=$c_user_id
 683                      JOIN $t_bug_tag_table AS t
 684                          ON t.bug_id=b.id
 685                      WHERE ( p.access_level>b.view_state OR u.access_level>b.view_state )
 686                          AND t.tag_id=$c_tag_id";
 687                      
 688          $query = "SELECT * FROM $t_bug_tag_table
 689                      WHERE tag_id != $c_tag_id
 690                          AND bug_id IN ( $subquery ) ";
 691  
 692          $result = db_query( $query );
 693  
 694          $t_tag_counts = array();
 695          while ( $row = db_fetch_array( $result ) ) {
 696              if ( !isset( $t_tag_counts[$row['tag_id']] ) ) {
 697                  $t_tag_counts[$row['tag_id']] = 1;
 698              } else {
 699                  $t_tag_counts[$row['tag_id']]++;
 700              }
 701          }
 702  
 703          arsort( $t_tag_counts );
 704  
 705          $t_tags = array();
 706          $i = 1;
 707          foreach ( $t_tag_counts as $t_tag_id => $t_count ) {
 708              $t_tag_row = tag_get($t_tag_id);
 709              $t_tag_row['count'] = $t_count;
 710              $t_tags[] = $t_tag_row;
 711              $i++;
 712              if ( $i > $p_limit ) { break; }
 713          }
 714  
 715          return $t_tags;
 716      }


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