PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/core/project_hierarchy_api.php

https://github.com/fusenigk/mantisbt-1
PHP | 290 lines | 151 code | 52 blank | 87 comment | 21 complexity | 940e5646811aa806af1d87acd60b47fb MD5 | raw file
  1. <?php
  2. # MantisBT - A PHP based bugtracking system
  3. # MantisBT is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # MantisBT is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Project Hierarchy API
  17. *
  18. * @package CoreAPI
  19. * @subpackage ProjectHierarchyAPI
  20. * @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
  21. * @copyright Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  22. * @link http://www.mantisbt.org
  23. *
  24. * @uses constant_inc.php
  25. * @uses database_api.php
  26. */
  27. require_api( 'constant_inc.php' );
  28. require_api( 'database_api.php' );
  29. $g_cache_project_hierarchy = null;
  30. $g_cache_project_inheritance = null;
  31. $g_cache_show_disabled = null;
  32. /**
  33. * Add project to project hierarchy
  34. * @param int $p_child_id Child project ID
  35. * @param int $p_parent_id Parent project ID
  36. * @param bool $p_inherit_parent Whether or not the child project inherits from the parent project
  37. * @return null
  38. */
  39. function project_hierarchy_add( $p_child_id, $p_parent_id, $p_inherit_parent = true ) {
  40. if( in_array( $p_parent_id, project_hierarchy_get_all_subprojects( $p_child_id ) ) ) {
  41. trigger_error( ERROR_PROJECT_RECURSIVE_HIERARCHY, ERROR );
  42. }
  43. $t_project_hierarchy_table = db_get_table( 'project_hierarchy' );
  44. $c_child_id = db_prepare_int( $p_child_id );
  45. $c_parent_id = db_prepare_int( $p_parent_id );
  46. $c_inherit_parent = db_prepare_bool( $p_inherit_parent );
  47. $query = "INSERT INTO $t_project_hierarchy_table
  48. ( child_id, parent_id, inherit_parent )
  49. VALUES
  50. ( " . db_param() . ', ' . db_param() . ', ' . db_param() . ' )';
  51. db_query_bound( $query, Array( $c_child_id, $c_parent_id, $c_inherit_parent ) );
  52. }
  53. /**
  54. * Update project hierarchy
  55. * @param int $p_child_id Child project ID
  56. * @param int $p_parent_id Parent project ID
  57. * @param bool $p_inherit_parent Whether or not the child project inherits from the parent project
  58. * @return null
  59. */
  60. function project_hierarchy_update( $p_child_id, $p_parent_id, $p_inherit_parent = true ) {
  61. $t_project_hierarchy_table = db_get_table( 'project_hierarchy' );
  62. $c_child_id = db_prepare_int( $p_child_id );
  63. $c_parent_id = db_prepare_int( $p_parent_id );
  64. $c_inherit_parent = db_prepare_bool( $p_inherit_parent );
  65. $query = "UPDATE $t_project_hierarchy_table
  66. SET inherit_parent=" . db_param() . '
  67. WHERE child_id=' . db_param() . '
  68. AND parent_id=' . db_param();
  69. db_query_bound( $query, Array( $c_inherit_parent, $c_child_id, $c_parent_id ) );
  70. }
  71. /**
  72. * Remove project from project hierarchy
  73. * @param int $p_child_id Child project ID
  74. * @param int $p_parent_id Parent project ID
  75. * @return null
  76. */
  77. function project_hierarchy_remove( $p_child_id, $p_parent_id ) {
  78. $t_project_hierarchy_table = db_get_table( 'project_hierarchy' );
  79. $c_child_id = db_prepare_int( $p_child_id );
  80. $c_parent_id = db_prepare_int( $p_parent_id );
  81. $query = "DELETE FROM $t_project_hierarchy_table
  82. WHERE child_id = " . db_param() . "
  83. AND parent_id = " . db_param();
  84. db_query_bound( $query, Array( $c_child_id, $c_parent_id ) );
  85. }
  86. /**
  87. * Remove any project hierarchy entries relating to project_id
  88. * @param int $p_project_id Project ID
  89. * @return null
  90. */
  91. function project_hierarchy_remove_all( $p_project_id ) {
  92. $t_project_hierarchy_table = db_get_table( 'project_hierarchy' );
  93. $c_project_id = db_prepare_int( $p_project_id );
  94. $query = "DELETE FROM $t_project_hierarchy_table
  95. WHERE child_id = " . db_param() . "
  96. OR parent_id = " . db_param();
  97. db_query_bound( $query, Array( $c_project_id, $c_project_id ) );
  98. }
  99. /**
  100. * Returns true if project is at top of hierarchy
  101. * @param bool $p_project_id Project ID
  102. * @param bool $p_show_disabled Whether or not to consider projects which are disabled
  103. * @return bool
  104. */
  105. function project_hierarchy_is_toplevel( $p_project_id, $p_show_disabled = false ) {
  106. global $g_cache_project_hierarchy;
  107. project_hierarchy_cache( $p_show_disabled );
  108. if( isset( $g_cache_project_hierarchy[ALL_PROJECTS] ) ) {
  109. return in_array( $p_project_id, $g_cache_project_hierarchy[ALL_PROJECTS] );
  110. } else {
  111. return false;
  112. }
  113. }
  114. /**
  115. * Cache project hierarchy
  116. * @param bool $p_show_disabled Whether or not to cache projects which are disabled
  117. * @return bool
  118. */
  119. function project_hierarchy_cache( $p_show_disabled = false ) {
  120. global $g_cache_project_hierarchy, $g_cache_project_inheritance;
  121. global $g_cache_show_disabled;
  122. if( !is_null( $g_cache_project_hierarchy ) && ( $g_cache_show_disabled == $p_show_disabled ) ) {
  123. return;
  124. }
  125. $g_cache_show_disabled = $p_show_disabled;
  126. $t_project_table = db_get_table( 'project' );
  127. $t_project_hierarchy_table = db_get_table( 'project_hierarchy' );
  128. $t_enabled_clause = $p_show_disabled ? '1=1' : 'p.enabled = ' . db_param();
  129. $query = "SELECT DISTINCT p.id, ph.parent_id, p.name, p.inherit_global, ph.inherit_parent
  130. FROM $t_project_table p
  131. LEFT JOIN $t_project_hierarchy_table ph
  132. ON ph.child_id = p.id
  133. WHERE $t_enabled_clause
  134. ORDER BY p.name";
  135. $result = db_query_bound( $query, ( $p_show_disabled ? null : Array( true ) ) );
  136. $row_count = db_num_rows( $result );
  137. $g_cache_project_hierarchy = array();
  138. $g_cache_project_inheritance = array();
  139. for( $i = 0;$i < $row_count;$i++ ) {
  140. $row = db_fetch_array( $result );
  141. if( null === $row['parent_id'] ) {
  142. $row['parent_id'] = ALL_PROJECTS;
  143. }
  144. if( isset( $g_cache_project_hierarchy[(int)$row['parent_id']] ) ) {
  145. $g_cache_project_hierarchy[(int)$row['parent_id']][] = (int)$row['id'];
  146. } else {
  147. $g_cache_project_hierarchy[(int)$row['parent_id']] = array(
  148. (int)$row['id'],
  149. );
  150. }
  151. if( !isset( $g_cache_project_inheritance[(int)$row['id']] ) ) {
  152. $g_cache_project_inheritance[(int)$row['id']] = array();
  153. }
  154. if( $row['inherit_global'] && !isset( $g_cache_project_inheritance[(int)$row['id']][ALL_PROJECTS] ) ) {
  155. $g_cache_project_inheritance[(int)$row['id']][] = ALL_PROJECTS;
  156. }
  157. if( $row['inherit_parent'] && !isset( $g_cache_project_inheritance[(int)$row['id']][(int)$row['parent_id']] ) ) {
  158. $g_cache_project_inheritance[(int)$row['id']][] = (int) $row['parent_id'];
  159. }
  160. }
  161. }
  162. /**
  163. * Returns true if the child project inherits categories from the parent.
  164. * @param int $p_child_id Child project ID
  165. * @param int $p_parent_id Parent project ID
  166. * @param bool $p_show_disabled Whether or not to consider projects which are disabled
  167. * @return bool
  168. */
  169. function project_hierarchy_inherit_parent( $p_child_id, $p_parent_id, $p_show_disabled = false ) {
  170. global $g_cache_project_inheritance;
  171. project_hierarchy_cache( $p_show_disabled );
  172. return in_array( $p_parent_id, $g_cache_project_inheritance[$p_child_id] );
  173. }
  174. /**
  175. * Generate an array of project's the given project inherits from,
  176. * including the original project in the result.
  177. * @param int $p_project_id Project ID
  178. * @param bool $p_show_disabled Whether or not to consider projects which are disabled
  179. * @return array
  180. */
  181. function project_hierarchy_inheritance( $p_project_id, $p_show_disabled = false ) {
  182. global $g_cache_project_inheritance;
  183. project_hierarchy_cache( $p_show_disabled );
  184. $t_project_ids = array(
  185. (int) $p_project_id,
  186. );
  187. $t_lookup_ids = array(
  188. (int) $p_project_id,
  189. );
  190. while( count( $t_lookup_ids ) > 0 ) {
  191. $t_project_id = array_shift( $t_lookup_ids );
  192. if( !isset( $g_cache_project_inheritance[$t_project_id] ) ) {
  193. continue;
  194. }
  195. foreach( $g_cache_project_inheritance[$t_project_id] as $t_parent_id ) {
  196. if( !in_array( $t_parent_id, $t_project_ids ) ) {
  197. $t_project_ids[] = $t_parent_id;
  198. if( !in_array( $t_lookup_ids, $t_project_ids ) ) {
  199. $t_lookup_ids[] = $t_parent_id;
  200. }
  201. }
  202. }
  203. }
  204. return $t_project_ids;
  205. }
  206. /**
  207. * Get subprojects for a project
  208. * @param int $p_project_id Project ID
  209. * @param bool $p_show_disabled Whether or not to consider projects which are disabled
  210. * @return array
  211. */
  212. function project_hierarchy_get_subprojects( $p_project_id, $p_show_disabled = false ) {
  213. global $g_cache_project_hierarchy;
  214. project_hierarchy_cache( $p_show_disabled );
  215. if( isset( $g_cache_project_hierarchy[$p_project_id] ) ) {
  216. return $g_cache_project_hierarchy[$p_project_id];
  217. } else {
  218. return array();
  219. }
  220. }
  221. /**
  222. * Get complete subproject hierarchy for a project
  223. * @param int $p_project_id Project ID
  224. * @param bool $p_show_disabled Whether or not to consider projects which are disabled
  225. * @return array
  226. */
  227. function project_hierarchy_get_all_subprojects( $p_project_id, $p_show_disabled = false ) {
  228. $t_todo = project_hierarchy_get_subprojects( $p_project_id, $p_show_disabled );
  229. $t_subprojects = Array();
  230. while( $t_todo ) {
  231. $t_elem = array_shift( $t_todo );
  232. if( !in_array( $t_elem, $t_subprojects ) ) {
  233. array_push( $t_subprojects, $t_elem );
  234. $t_todo = array_merge( $t_todo, project_hierarchy_get_subprojects( $t_elem, $p_show_disabled ) );
  235. }
  236. }
  237. return $t_subprojects;
  238. }