Esempio n. 1
0
/**
 * Return all categories for the specified project id.
 * Obeys project hierarchies and such.
 * @param int $p_project_id Project id
 * @param bool $p_inherit indicates whether to inherit categories from parent projects, or null to use configuration default.
 * @param bool $p_sort_by_project
 * @return array array of categories
 * @access public
 */
 function category_get_all_rows( $p_project_id, $p_inherit = null, $p_sort_by_project = false ) {
	global $g_category_cache, $g_cache_category_project;

	if( isset( $g_cache_category_project[ (int)$p_project_id ] ) ) {
		if( !empty( $g_cache_category_project[ (int)$p_project_id ]) ) {
			foreach( $g_cache_category_project[ (int)$p_project_id ] as $t_id ) {
				$t_categories[] = category_get_row( $t_id );
			}

			if( $p_sort_by_project ) {
				category_sort_rows_by_project( $p_project_id );
				usort( $t_categories, 'category_sort_rows_by_project' );
				category_sort_rows_by_project( null );
			}
			return $t_categories;
		} else {
			return array();
		}
	}

	$c_project_id = db_prepare_int( $p_project_id );

	$t_category_table = db_get_table( 'category' );
	$t_project_table = db_get_table( 'project' );

	if ( $c_project_id == ALL_PROJECTS ) {
		$t_inherit = false;
	} else {
		if ( $p_inherit === null ) {
			$t_inherit = config_get( 'subprojects_inherit_categories' );
		} else {
			$t_inherit = $p_inherit;
		}
	}

	if ( $t_inherit ) {
		$t_project_ids = project_hierarchy_inheritance( $p_project_id );
		$t_project_where = ' project_id IN ( ' . implode( ', ', $t_project_ids ) . ' ) ';
	} else {
		$t_project_where = ' project_id=' . $p_project_id . ' ';
	}

	$query = "SELECT c.*, p.name AS project_name FROM $t_category_table c
				LEFT JOIN $t_project_table p
					ON c.project_id=p.id
				WHERE $t_project_where
				ORDER BY c.name ";
	$result = db_query_bound( $query );
	$count = db_num_rows( $result );
	$rows = array();
	for( $i = 0;$i < $count;$i++ ) {
		$row = db_fetch_array( $result );

		$rows[] = $row;
		$g_category_cache[(int) $row['id']] = $row;
	}

	if( $p_sort_by_project ) {
		category_sort_rows_by_project( $p_project_id );
		usort( $rows, 'category_sort_rows_by_project' );
		category_sort_rows_by_project( null );
	}

	return $rows;
}
Esempio n. 2
0
/**
 * Return all categories for the specified project id.
 * Obeys project hierarchies and such.
 * @param integer $p_project_id      A Project identifier.
 * @param boolean $p_inherit         Indicates whether to inherit categories from parent projects, or null to use configuration default.
 * @param boolean $p_sort_by_project Whether to sort by project.
 * @return array array of categories
 * @access public
 */
function category_get_all_rows($p_project_id, $p_inherit = null, $p_sort_by_project = false)
{
    global $g_category_cache, $g_cache_category_project;
    if (isset($g_cache_category_project[(int) $p_project_id])) {
        if (!empty($g_cache_category_project[(int) $p_project_id])) {
            foreach ($g_cache_category_project[(int) $p_project_id] as $t_id) {
                $t_categories[] = category_get_row($t_id);
            }
            if ($p_sort_by_project) {
                category_sort_rows_by_project($p_project_id);
                usort($t_categories, 'category_sort_rows_by_project');
                category_sort_rows_by_project(null);
            }
            return $t_categories;
        } else {
            return array();
        }
    }
    $c_project_id = (int) $p_project_id;
    if ($c_project_id == ALL_PROJECTS) {
        $t_inherit = false;
    } else {
        if ($p_inherit === null) {
            $t_inherit = config_get('subprojects_inherit_categories');
        } else {
            $t_inherit = $p_inherit;
        }
    }
    if ($t_inherit) {
        $t_project_ids = project_hierarchy_inheritance($p_project_id);
        $t_project_where = ' project_id IN ( ' . implode(', ', $t_project_ids) . ' ) ';
    } else {
        $t_project_where = ' project_id=' . $p_project_id . ' ';
    }
    $t_query = 'SELECT c.*, p.name AS project_name FROM {category} c
				LEFT JOIN {project} p
					ON c.project_id=p.id
				WHERE ' . $t_project_where . ' ORDER BY c.name';
    $t_result = db_query($t_query);
    $t_rows = array();
    while ($t_row = db_fetch_array($t_result)) {
        $t_rows[] = $t_row;
        $g_category_cache[(int) $t_row['id']] = $t_row;
    }
    if ($p_sort_by_project) {
        category_sort_rows_by_project($p_project_id);
        usort($t_rows, 'category_sort_rows_by_project');
        category_sort_rows_by_project(null);
    }
    return $t_rows;
}