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