Skip to content

Commit

Permalink
Fix Bug #10156: Categories "[any] / General". Move data retrieval int…
Browse files Browse the repository at this point in the history
…o new category

api function category_get_filter_list.  Use existing api functions to properly
populate the list based on configured project/category inheritance. The print api
function now handles only display functionality.  It sorts and prints the select list.
The print function will eventually be removed in favor of a view. This separation
prepares for that.
  • Loading branch information
Daryn Warriner committed Dec 23, 2010
1 parent 9b87be3 commit 011050b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 34 deletions.
43 changes: 43 additions & 0 deletions core/category_api.php
Expand Up @@ -399,6 +399,49 @@ function category_cache_array_rows_by_project( $p_project_id_array ) {
return;
}

/**
* Get a distinct array of categories accessible to the current user for
* the specified projects. If no project is specified, use the current project.
* If the current project is ALL_PROJECTS get all categories for all accessible projects.
* For all cases, get global categories and subproject categories according to configured inheritance settings.
* @param mixed $p_project_id A specific project or null
* @return array A unique array of category names
*/
function category_get_filter_list( $p_project_id = null ) {
if( null === $p_project_id ) {
$t_project_id = helper_get_current_project();
} else {
$t_project_id = $p_project_id;
}

if( $t_project_id == ALL_PROJECTS ) {
$t_project_ids = current_user_get_accessible_projects();
} else {
$t_project_ids = array( $t_project_id );
}

$t_subproject_ids = array();
foreach( $t_project_ids as $t_project_id ) {
$t_subproject_ids = array_merge( $t_subproject_ids, current_user_get_all_accessible_subprojects( $t_project_id ) );
}

$t_project_ids = array_merge( $t_project_ids, $t_subproject_ids );

$t_categories = array();
foreach( $t_project_ids AS $t_id ) {
$t_categories = array_merge( $t_categories, category_get_all_rows( $t_id ) );
}

$t_unique = array();
foreach( $t_categories AS $t_category ) {
if( !in_array( $t_category['name'], $t_unique ) ) {
$t_unique[] = $t_category['name'];
}
}

return $t_unique;
}

/**
* Return all categories for the specified project id.
* Obeys project hierarchies and such.
Expand Down
44 changes: 10 additions & 34 deletions core/print_api.php
Expand Up @@ -615,42 +615,18 @@ function print_category_option_list( $p_category_id = 0, $p_project_id = null )
}
}

# Now that categories are identified by numerical ID, we need an old-style name
# based option list to keep existing filter functionality.
/**
* Now that categories are identified by numerical ID, we need an old-style name
* based option list to keep existing filter functionality.
* @param string $p_category_name The selected category
* @param mixed $p_project_id A specific project or null
*/
function print_category_filter_option_list( $p_category_name = '', $p_project_id = null ) {
$t_category_table = db_get_table( 'category' );
$t_project_table = db_get_table( 'project' );

if( null === $p_project_id ) {
$c_project_id = helper_get_current_project();
} else {
$c_project_id = db_prepare_int( $p_project_id );
}

$t_project_ids = project_hierarchy_inheritance( $c_project_id );

$t_subproject_ids = array();
foreach( $t_project_ids as $t_project_id ) {
$t_subproject_ids = array_merge( $t_subproject_ids, current_user_get_all_accessible_subprojects( $t_project_id ) );
}

$t_project_ids = array_merge( $t_project_ids, $t_subproject_ids );
$t_project_where = ' project_id IN ( ' . implode( ', ', $t_project_ids ) . ' ) ';

# grab all categories in the project category table
$cat_arr = array();
$query = "SELECT DISTINCT name FROM $t_category_table
WHERE $t_project_where
ORDER BY name";
$result = db_query( $query );

while( $row = db_fetch_array( $result ) ) {
$cat_arr[] = $row['name'];
}
sort( $cat_arr );
$t_cat_arr = category_get_filter_list( $p_project_id );

foreach( $cat_arr as $t_name ) {
$t_name = string_attribute( $t_name );
natcasesort( $t_cat_arr );
foreach( $t_cat_arr as $t_cat ) {
$t_name = string_attribute( $t_cat );
echo '<option value="' . $t_name . '"';
check_selected( string_attribute( $p_category_name ), $t_name );
echo '>' . $t_name . '</option>';
Expand Down

0 comments on commit 011050b

Please sign in to comment.