/issues/core/project_hierarchy_api.php

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