[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/core/ -> project_hierarchy_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  /**
  19   * Project Hierarchy API
  20   *
  21   * @package CoreAPI
  22   * @subpackage ProjectHierarchyAPI
  23   * @copyright Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
  24   * @copyright Copyright (C) 2002 - 2011  MantisBT Team - mantisbt-dev@lists.sourceforge.net
  25   * @link http://www.mantisbt.org
  26   *
  27   * @uses constant_inc.php
  28   * @uses database_api.php
  29   */
  30  
  31  require_api( 'constant_inc.php' );
  32  require_api( 'database_api.php' );
  33  
  34  $g_cache_project_hierarchy = null;
  35  $g_cache_project_inheritance = null;
  36  $g_cache_show_disabled = null;
  37  
  38  /**
  39   * Add project to project hierarchy
  40   * @param int $p_child_id Child project ID
  41   * @param int $p_parent_id Parent project ID
  42   * @param bool $p_inherit_parent Whether or not the child project inherits from the parent project
  43   * @return null
  44   */
  45  function project_hierarchy_add( $p_child_id, $p_parent_id, $p_inherit_parent = true ) {
  46      if( in_array( $p_parent_id, project_hierarchy_get_all_subprojects( $p_child_id ) ) ) {
  47          trigger_error( ERROR_PROJECT_RECURSIVE_HIERARCHY, ERROR );
  48      }
  49  
  50      $t_project_hierarchy_table = db_get_table( 'project_hierarchy' );
  51  
  52      $c_child_id = db_prepare_int( $p_child_id );
  53      $c_parent_id = db_prepare_int( $p_parent_id );
  54      $c_inherit_parent = db_prepare_bool( $p_inherit_parent );
  55  
  56      $query = "INSERT INTO $t_project_hierarchy_table
  57                          ( child_id, parent_id, inherit_parent )
  58                          VALUES
  59                          ( " . db_param() . ', ' . db_param() . ', ' . db_param() . ' )';
  60  
  61      db_query_bound( $query, Array( $c_child_id, $c_parent_id, $c_inherit_parent ) );
  62  }
  63  
  64  /**
  65   * Update project hierarchy
  66   * @param int $p_child_id Child project ID
  67   * @param int $p_parent_id Parent project ID
  68   * @param bool $p_inherit_parent Whether or not the child project inherits from the parent project
  69   * @return null
  70   */
  71  function project_hierarchy_update( $p_child_id, $p_parent_id, $p_inherit_parent = true ) {
  72      $t_project_hierarchy_table = db_get_table( 'project_hierarchy' );
  73  
  74      $c_child_id = db_prepare_int( $p_child_id );
  75      $c_parent_id = db_prepare_int( $p_parent_id );
  76      $c_inherit_parent = db_prepare_bool( $p_inherit_parent );
  77  
  78      $query = "UPDATE $t_project_hierarchy_table
  79                      SET inherit_parent=" . db_param() . '
  80                      WHERE child_id=' . db_param() . '
  81                          AND parent_id=' . db_param();
  82      db_query_bound( $query, Array( $c_inherit_parent, $c_child_id, $c_parent_id ) );
  83  }
  84  
  85  /**
  86   * Remove project from project hierarchy
  87   * @param int $p_child_id Child project ID
  88   * @param int $p_parent_id Parent project ID
  89   * @return null
  90   */
  91  function project_hierarchy_remove( $p_child_id, $p_parent_id ) {
  92      $t_project_hierarchy_table = db_get_table( 'project_hierarchy' );
  93  
  94      $c_child_id = db_prepare_int( $p_child_id );
  95      $c_parent_id = db_prepare_int( $p_parent_id );
  96  
  97      $query = "DELETE FROM $t_project_hierarchy_table
  98                          WHERE child_id = " . db_param() . "
  99                          AND parent_id = " . db_param();
 100  
 101      db_query_bound( $query, Array( $c_child_id, $c_parent_id ) );
 102  }
 103  
 104  /**
 105   * Remove any project hierarchy entries relating to project_id
 106   * @param int $p_project_id Project ID
 107   * @return null
 108   */
 109  function project_hierarchy_remove_all( $p_project_id ) {
 110      $t_project_hierarchy_table = db_get_table( 'project_hierarchy' );
 111  
 112      $c_project_id = db_prepare_int( $p_project_id );
 113  
 114      $query = "DELETE FROM $t_project_hierarchy_table
 115                          WHERE child_id = " . db_param() . "
 116                            OR parent_id = " . db_param();
 117  
 118      db_query_bound( $query, Array( $c_project_id, $c_project_id ) );
 119  }
 120  
 121  /**
 122   * Returns true if project is at top of hierarchy
 123   * @param bool $p_project_id Project ID
 124   * @param bool $p_show_disabled Whether or not to consider projects which are disabled
 125   * @return bool
 126   */
 127  function project_hierarchy_is_toplevel( $p_project_id, $p_show_disabled = false ) {
 128      global $g_cache_project_hierarchy;
 129  
 130      project_hierarchy_cache( $p_show_disabled );
 131  
 132      if( isset( $g_cache_project_hierarchy[ALL_PROJECTS] ) ) {
 133          return in_array( $p_project_id, $g_cache_project_hierarchy[ALL_PROJECTS] );
 134      } else {
 135          return false;
 136      }
 137  }
 138  
 139  /**
 140   * Cache project hierarchy
 141   * @param bool $p_show_disabled Whether or not to cache projects which are disabled
 142   * @return bool
 143   */
 144  function project_hierarchy_cache( $p_show_disabled = false ) {
 145      global $g_cache_project_hierarchy, $g_cache_project_inheritance;
 146      global $g_cache_show_disabled;
 147  
 148      if( !is_null( $g_cache_project_hierarchy ) && ( $g_cache_show_disabled == $p_show_disabled ) ) {
 149          return;
 150      }
 151      $g_cache_show_disabled = $p_show_disabled;
 152  
 153      $t_project_table = db_get_table( 'project' );
 154      $t_project_hierarchy_table = db_get_table( 'project_hierarchy' );
 155      $t_enabled_clause = $p_show_disabled ? '1=1' : 'p.enabled = ' . db_param();
 156  
 157      $query = "SELECT DISTINCT p.id, ph.parent_id, p.name, p.inherit_global, ph.inherit_parent
 158                    FROM $t_project_table p
 159                    LEFT JOIN $t_project_hierarchy_table ph
 160                      ON ph.child_id = p.id
 161                    WHERE $t_enabled_clause
 162                    ORDER BY p.name";
 163  
 164      $result = db_query_bound( $query, ( $p_show_disabled ? null : Array( true ) ) );
 165      $row_count = db_num_rows( $result );
 166  
 167      $g_cache_project_hierarchy = array();
 168      $g_cache_project_inheritance = array();
 169  
 170      for( $i = 0;$i < $row_count;$i++ ) {
 171          $row = db_fetch_array( $result );
 172  
 173          if( null === $row['parent_id'] ) {
 174              $row['parent_id'] = ALL_PROJECTS;
 175          }
 176  
 177          if( isset( $g_cache_project_hierarchy[(int)$row['parent_id']] ) ) {
 178              $g_cache_project_hierarchy[(int)$row['parent_id']][] = (int)$row['id'];
 179          } else {
 180              $g_cache_project_hierarchy[(int)$row['parent_id']] = array(
 181                  (int)$row['id'],
 182              );
 183          }
 184  
 185          if( !isset( $g_cache_project_inheritance[(int)$row['id']] ) ) {
 186              $g_cache_project_inheritance[(int)$row['id']] = array();
 187          }
 188  
 189          if( $row['inherit_global'] && !isset( $g_cache_project_inheritance[(int)$row['id']][ALL_PROJECTS] ) ) {
 190              $g_cache_project_inheritance[(int)$row['id']][] = ALL_PROJECTS;
 191          }
 192          if( $row['inherit_parent'] && !isset( $g_cache_project_inheritance[(int)$row['id']][(int)$row['parent_id']] ) ) {
 193              $g_cache_project_inheritance[(int)$row['id']][] = (int) $row['parent_id'];
 194          }
 195      }
 196  }
 197  
 198  /**
 199   * Returns true if the child project inherits categories from the parent.
 200   * @param int $p_child_id Child project ID
 201   * @param int $p_parent_id Parent project ID
 202   * @param bool $p_show_disabled Whether or not to consider projects which are disabled
 203   * @return bool
 204   */
 205  function project_hierarchy_inherit_parent( $p_child_id, $p_parent_id, $p_show_disabled = false ) {
 206      global $g_cache_project_inheritance;
 207  
 208      project_hierarchy_cache( $p_show_disabled );
 209  
 210      return in_array( $p_parent_id, $g_cache_project_inheritance[$p_child_id] );
 211  }
 212  
 213  /**
 214   * Generate an array of project's the given project inherits from,
 215   * including the original project in the result.
 216   * @param int $p_project_id Project ID
 217   * @param bool $p_show_disabled Whether or not to consider projects which are disabled
 218   * @return array
 219   */
 220  function project_hierarchy_inheritance( $p_project_id, $p_show_disabled = false ) {
 221      global $g_cache_project_inheritance;
 222  
 223      project_hierarchy_cache( $p_show_disabled );
 224  
 225      $t_project_ids = array(
 226          (int) $p_project_id,
 227      );
 228      $t_lookup_ids = array(
 229          (int) $p_project_id,
 230      );
 231  
 232      while( count( $t_lookup_ids ) > 0 ) {
 233          $t_project_id = array_shift( $t_lookup_ids );
 234  
 235          if( !isset( $g_cache_project_inheritance[$t_project_id] ) ) {
 236              continue;
 237          }
 238  
 239          foreach( $g_cache_project_inheritance[$t_project_id] as $t_parent_id ) {
 240              if( !in_array( $t_parent_id, $t_project_ids ) ) {
 241                  $t_project_ids[] = $t_parent_id;
 242  
 243                  if( !in_array( $t_lookup_ids, $t_project_ids ) ) {
 244                      $t_lookup_ids[] = $t_parent_id;
 245                  }
 246              }
 247          }
 248      }
 249  
 250      return $t_project_ids;
 251  }
 252  
 253  /**
 254   * Get subprojects for a project
 255   * @param int $p_project_id Project ID
 256   * @param bool $p_show_disabled Whether or not to consider projects which are disabled
 257   * @return array
 258   */
 259  function project_hierarchy_get_subprojects( $p_project_id, $p_show_disabled = false ) {
 260      global $g_cache_project_hierarchy;
 261  
 262      project_hierarchy_cache( $p_show_disabled );
 263  
 264      if( isset( $g_cache_project_hierarchy[$p_project_id] ) ) {
 265          return $g_cache_project_hierarchy[$p_project_id];
 266      } else {
 267          return array();
 268      }
 269  }
 270  
 271  /**
 272   * Get complete subproject hierarchy for a project
 273   * @param int $p_project_id Project ID
 274   * @param bool $p_show_disabled Whether or not to consider projects which are disabled
 275   * @return array
 276   */
 277  function project_hierarchy_get_all_subprojects( $p_project_id, $p_show_disabled = false ) {
 278      $t_todo = project_hierarchy_get_subprojects( $p_project_id, $p_show_disabled );
 279      $t_subprojects = Array();
 280  
 281      while( $t_todo ) {
 282          $t_elem = array_shift( $t_todo );
 283          if( !in_array( $t_elem, $t_subprojects ) ) {
 284              array_push( $t_subprojects, $t_elem );
 285              $t_todo = array_merge( $t_todo, project_hierarchy_get_subprojects( $t_elem, $p_show_disabled ) );
 286          }
 287      }
 288  
 289      return $t_subprojects;
 290  }


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