function test_field_apply_filter($where_clause, $per_page, $order_by, $order_dir, $page_number, $csv_name)
{
    global $db;
    $field_tbl = FIELD_TBL;
    $f_field_id = FIELD_TBL . "." . FIELD_ID;
    $f_field_name = FIELD_TBL . "." . FIELD_NAME;
    $f_fscreen_id = FIELD_TBL . "." . FIELD_SCREEN_ID;
    $f_description = FIELD_TBL . "." . FIELD_DESC;
    $f_field_order = FIELD_TBL . "." . FIELD_ORDER;
    $f_text_only = FIELD_TBL . "." . FIELD_TEXT_ONLY;
    $screen_tbl = SCREEN_TBL;
    $f_screen_id = SCREEN_TBL . "." . SCREEN_ID;
    $f_screen_name = SCREEN_TBL . "." . SCREEN_NAME;
    $q = "SELECT \n\t\t\t{$f_field_id}, {$f_field_name}, {$f_description}, {$f_field_order}, {$f_text_only}, {$f_screen_id}, {$f_screen_id}, {$f_screen_name}\n\t\t  FROM \n\t\t\t{$field_tbl}, {$screen_tbl}\n\t\t  WHERE\n\t\t\t{$f_fscreen_id} = {$f_screen_id}";
    $order_clause = " ORDER BY {$order_by} {$order_dir}";
    $where_clause = $where_clause . " GROUP BY {$f_field_name}";
    $q .= $where_clause . $order_clause;
    if ($per_page != 0 && $page_number != 0) {
        $row_count = db_num_rows($db, db_query($db, $q));
        $page_number = util_page_number($page_number, $row_count, $per_page);
        # Add the limit clause to the query so that we only show n number of records per page
        $offset = ($page_number - 1) * $per_page;
        html_table_offset($row_count, $per_page, $page_number, $order_by, $order_dir, $csv_name);
        $q .= " LIMIT {$offset}, " . $per_page;
    }
    //print"$q<br>";
    $rs = db_query($db, $q);
    return db_fetch_array($db, $rs);
}
function bug_get($project_id, $page_number = 0, $order_by = BUG_ID, $order_dir = "DESC", $bug_status, $category = "", $component = "", $reported_by = "", $assigned_to = "", $assigned_to_developer = "", $found_in_release = "", $assigned_to_release = null, $per_page = null, $view_closed = 'No', $search = "", $jump = "", $csv_name = null)
{
    global $db;
    $bug_tbl = BUG_TBL;
    $f_bug_id = $bug_tbl . "." . BUG_ID;
    $f_bug_project_id = $bug_tbl . "." . BUG_PROJECT_ID;
    $f_bug_category_id = $bug_tbl . "." . BUG_CATEGORY;
    $f_bug_component_id = $bug_tbl . "." . BUG_COMPONENT;
    $f_priority = $bug_tbl . "." . BUG_PRIORITY;
    $f_severity = $bug_tbl . "." . BUG_SEVERITY;
    $f_closed_reason_code = $bug_tbl . "." . BUG_CLOSED_REASON_CODE;
    $f_status = $bug_tbl . "." . BUG_STATUS;
    $f_reporter = $bug_tbl . "." . BUG_REPORTER;
    $f_reported_date = $bug_tbl . "." . BUG_REPORTED_DATE;
    $f_assigned_to = $bug_tbl . "." . BUG_ASSIGNED_TO;
    $f_assigned_to_developer = $bug_tbl . "." . BUG_ASSIGNED_TO_DEVELOPER;
    $f_closed = $bug_tbl . "." . BUG_CLOSED;
    $f_closed_date = $bug_tbl . "." . BUG_CLOSED_DATE;
    $f_verify_id = $bug_tbl . "." . BUG_TEST_VERIFY_ID;
    $f_req_version_id = $bug_tbl . "." . BUG_REQ_VERSION_ID;
    $f_found_in_release = $bug_tbl . "." . BUG_FOUND_IN_RELEASE;
    $f_assign_to_release = $bug_tbl . "." . BUG_ASSIGN_TO_RELEASE;
    $f_impl_in_release = $bug_tbl . "." . BUG_IMPLEMENTED_IN_RELEASE;
    $f_discovery_period = $bug_tbl . "." . BUG_DISCOVERY_PERIOD;
    $f_summary = $bug_tbl . "." . BUG_SUMMARY;
    $f_description = $bug_tbl . "." . BUG_DESCRIPTION;
    $f_assigned_to_release = $bug_tbl . "." . BUG_ASSIGN_TO_RELEASE;
    $category_tbl = BUG_CATEGORY_TBL;
    $f_category_id = $category_tbl . "." . CATEGORY_ID;
    $f_category_proj_id = $category_tbl . "." . CATEGORY_PROJECT_ID;
    $f_category_name = $category_tbl . "." . CATEGORY_NAME;
    $component_tbl = BUG_COMPONENT_TBL;
    $f_component_id = $component_tbl . "." . COMPONENT_ID;
    $f_component_proj_id = $component_tbl . "." . COMPONENT_PROJECT_ID;
    $f_component_name = $component_tbl . "." . COMPONENT_NAME;
    # Add Release Table
    $release_tbl = RELEASE_TBL;
    $where_clause = " WHERE {$f_bug_project_id} = '{$project_id}'";
    # STATUS
    if (!empty($status) && $status != 'all') {
        $where_clause = $where_clause . " AND {$f_bug_status} = '{$status}'";
    }
    # CATEGORY
    if (!empty($category) && $category != 'all') {
        $where_clause = $where_clause . " AND {$f_category_id} = '{$category}'";
    }
    # COMPONENT
    if (!empty($component) && $component != 'all') {
        $where_clause = $where_clause . " AND {$f_component_id} = '{$component}'";
    }
    # REPORTER
    if (!empty($reporter) && $reporter != 'all') {
        $where_clause = $where_clause . " AND {$f_reporter} = '{$reporter}'";
    }
    # ASSIGNED TO
    if (!empty($assigned_to) && $assigned_to != 'all') {
        $where_clause = $where_clause . " AND {$f_assigned_to} = '{$assigned_to}'";
    }
    # ASSIGNED TO DEVELOPER
    if (!empty($assigned_to_developer) && $assigned_to_developer != 'all') {
        $where_clause = $where_clause . " AND {$f_assigned_to_developer} = '{$assigned_to_developer}'";
    }
    # FOUND IN RELEASE
    if (!empty($found_in_release) && $found_in_release != 'all') {
        $where_clause = $where_clause . " AND {$f_found_in_release} = '{$found_in_release}'";
    }
    # ASSIGN TO RELEASE
    if (!empty($assigned_to_release) && $assigned_to_release != 'all') {
        $where_clause = $where_clause . " AND {$f_assigned_to_release} = '{$assigned_to_release}'";
    }
    # VIEW CLOSED
    if ($view_closed == lang_get('no')) {
        $where_clause = $where_clause . " AND {$f_status} != 'Closed'";
    } else {
        //print"view_closed = $view_closed<br>";
    }
    # SEARCH
    if (!empty($search)) {
        $where_clause = $where_clause . " AND ( ({$f_summary} LIKE '%{$search}%') OR ({$f_description} LIKE '%{$search}%') )";
    }
    //$where_clause = substr( $where_clause, 4, strlen($where_clause) );
    //$where_clause .= $where_clause;
    $q = "\tSELECT\n\t\t\t\t{$f_bug_id},\n\t\t\t\t{$f_priority},\n\t\t\t\t{$f_severity},\n\t\t\t\t{$f_status},\n\t\t\t\t{$f_bug_category_id},\n\t\t\t\t{$f_reporter},\n\t\t\t\t{$f_assigned_to},\n\t\t\t\t{$f_assigned_to_developer},\n\t\t\t\t{$f_found_in_release},\n\t\t\t\t{$f_assign_to_release},\n\t\t\t\t{$f_description},\n\t\t\t\t{$f_summary},\n\t\t\t\t{$f_bug_project_id},\n\t\t\t\t{$f_bug_component_id},\n\t\t\t\t{$f_closed_reason_code},\n\t\t\t\t{$f_reported_date},\n\t\t\t\t{$f_closed},\n\t\t\t\t{$f_closed_date},\n\t\t\t\t{$f_verify_id},\n\t\t\t\t{$f_req_version_id},\n\t\t\t\t{$f_impl_in_release},\n\t\t\t\t{$f_discovery_period},\n\t\t\t\t{$f_category_id},\n\t\t\t\t{$f_category_name},\n\t\t\t\t{$f_component_id},\n\t\t\t\t{$f_component_name}\n\t\t\tFROM {$bug_tbl}\n\t\t\tLEFT JOIN {$category_tbl} ON {$f_bug_category_id} = {$f_category_id}\n\t\t\tLEFT JOIN {$component_tbl} ON {$f_bug_component_id} = {$f_component_id}";
    $q .= "\t{$where_clause}\n\t\t\tORDER BY {$order_by} {$order_dir}";
    #print"$q<br>";
    if (is_null($per_page)) {
        $display_options = session_get_filter_options("bug");
        $per_page = $display_options['per_page'];
    }
    if ($per_page != 0 && $page_number != 0) {
        $row_count = db_num_rows($db, db_query($db, $q));
        $page_number = util_page_number($page_number, $row_count, $per_page);
        # Add the limit clause to the query so that we only show n number of records per page
        $offset = ($page_number - 1) * $per_page;
        html_table_offset($row_count, $per_page, $page_number, $order_by, $order_dir, $csv_name);
        $q .= " LIMIT {$offset}, " . $per_page;
    }
    #print"$q<br>";
    $rows = db_fetch_array($db, db_query($db, $q));
    return $rows;
}
function requirement_get_edit_children($project_id, $req_id, $page_number = 0, $order_by = REQ_FILENAME, $order_dir = "ASC", $doc_type = "", $status = "", $area_covered = "", $functionality = "", $assign_release = "", $show_versions = "latest", $search = "", $priority = "", $per_page = null, $csv_name = null)
{
    $tbl_req = REQ_TBL;
    $f_req_proj_id = $tbl_req . "." . REQ_PROJECT_ID;
    $f_req_id = $tbl_req . "." . REQ_ID;
    $f_req_filename = $tbl_req . "." . REQ_FILENAME;
    $f_req_area_covered = $tbl_req . "." . REQ_AREA_COVERED;
    $f_req_type = $tbl_req . "." . REQ_TYPE;
    $f_req_parent = $tbl_req . "." . REQ_PARENT;
    $f_req_label = $tbl_req . "." . REQ_LABEL;
    $f_req_unique_id = $tbl_req . "." . REQ_UNIQUE_ID;
    $f_req_locked_by = $tbl_req . "." . REQ_LOCKED_BY;
    $f_req_locked_date = $tbl_req . "." . REQ_LOCKED_DATE;
    $f_req_parent = $tbl_req . "." . REQ_PARENT;
    $f_req_priority = $tbl_req . "." . REQ_PRIORITY;
    //$f_req_root		 			= $tbl_req .".". REQ_ROOT;
    $tbl_req_ver = REQ_VERS_TBL;
    $f_req_ver_uid = $tbl_req_ver . "." . REQ_VERS_UNIQUE_ID;
    $f_req_ver_req_id = $tbl_req_ver . "." . REQ_VERS_REQ_ID;
    $f_req_ver_version = $tbl_req_ver . "." . REQ_VERS_VERSION;
    $f_req_ver_timestamp = $tbl_req_ver . "." . REQ_VERS_TIMESTAMP;
    $f_req_ver_uploaded_by = $tbl_req_ver . "." . REQ_VERS_UPLOADED_BY;
    $f_req_ver_filename = $tbl_req_ver . "." . REQ_VERS_FILENAME;
    $f_req_ver_comments = $tbl_req_ver . "." . REQ_VERS_COMMENTS;
    $f_req_ver_status = $tbl_req_ver . "." . REQ_VERS_STATUS;
    $f_req_ver_shed_release = $tbl_req_ver . "." . REQ_VERS_SCHEDULED_RELEASE_IMP;
    $f_req_ver_shed_build = $tbl_req_ver . "." . REQ_VERS_SCHEDULED_BUILD_IMP;
    $f_req_ver_actual_release = $tbl_req_ver . "." . REQ_VERS_ACTUAL_RELEASE_IMP;
    $f_req_ver_actual_build = $tbl_req_ver . "." . REQ_VERS_ACTUAL_BUILD_IMP;
    $f_req_ver_detail = $tbl_req_ver . "." . REQ_VERS_DETAIL;
    $f_req_ver_latest = $tbl_req_ver . "." . REQ_VERS_LATEST;
    $tbl_functionality_assoc = REQ_FUNCT_ASSOC_TBL;
    $f_functionality_assoc_id = $tbl_functionality_assoc . "." . REQ_FUNCT_ASSOC_ID;
    $f_functionality_assoc_req_id = $tbl_functionality_assoc . "." . REQ_FUNCT_ASSOC_REQ_ID;
    $f_functionality_assoc_funct_id = $tbl_functionality_assoc . "." . REQ_FUNCT_ASSOC_FUNCT_ID;
    $release_tbl = RELEASE_TBL;
    $f_release_id = $release_tbl . "." . RELEASE_ID;
    $f_release_name = $release_tbl . "." . RELEASE_NAME;
    $tbl_req_ver_assoc_rel = REQ_VERS_ASSOC_REL;
    $f_req_ver_assoc_rel_id = $tbl_req_ver_assoc_rel . "." . REQ_VERS_ASSOC_REL_ID;
    $f_req_ver_assoc_rel_req_id = $tbl_req_ver_assoc_rel . "." . REQ_VERS_ASSOC_REL_REQ_ID;
    $f_req_ver_assoc_rel_rel_id = $tbl_req_ver_assoc_rel . "." . REQ_VERS_ASSOC_REL_REL_ID;
    $tbl_req_doc_type = REQ_DOC_TYPE_TBL;
    $f_req_doc_type_name = $tbl_req_doc_type . "." . REQ_DOC_TYPE_NAME;
    $f_req_doc_type_id = $tbl_req_doc_type . "." . REQ_DOC_TYPE_ID;
    $f_req_doc_type_project_id = $tbl_req_doc_type . "." . REQ_DOC_TYPE_PROJ_ID;
    $f_req_doc_type_root_doc = $tbl_req_doc_type . "." . REQ_DOC_TYPE_ROOT_DOC;
    $tbl_req_area_covered = REQ_AREA_COVERAGE_TBL;
    $f_req_area_covered_name = $tbl_req_area_covered . "." . REQ_AREA_COVERAGE;
    $f_req_area_covered_id = $tbl_req_area_covered . "." . REQ_AREA_COVERAGE_ID;
    $f_req_area_covered_proj_id = $tbl_req_area_covered . "." . REQ_AREA_PROJ_ID;
    $where_clause = "";
    if (!empty($doc_type)) {
        $where_clause .= "AND {$f_req_type} = '{$doc_type}' ";
    }
    if (!empty($status)) {
        $where_clause .= "AND {$f_req_ver_status} = '{$status}' ";
    }
    if (!empty($area_covered)) {
        $where_clause .= "AND {$f_req_area_covered} = '{$area_covered}' ";
    }
    if (!empty($functionality)) {
        $where_clause .= "AND {$f_functionality_assoc_funct_id} = '{$functionality}' ";
    }
    if (!empty($assign_release)) {
        $where_clause .= "AND {$f_req_ver_assoc_rel_rel_id} = '{$assign_release}'";
    }
    if (!empty($priority)) {
        $where_clause .= "AND {$f_req_priority} = '{$priority}'";
    }
    if ($show_versions == "latest") {
        $where_clause .= "AND {$f_req_ver_latest} = 'Y'";
    }
    # SEARCH
    if (!empty($search)) {
        $where_clause = $where_clause . " AND ( ({$f_req_filename} LIKE '%{$search}%') OR ({$f_req_ver_detail} LIKE '%{$search}%') )";
    }
    $where_clause = substr($where_clause, 3, strlen($where_clause));
    if (!empty($where_clause)) {
        $where_clause = "WHERE {$where_clause}";
    }
    $q = "\tSELECT DISTINCT\n\t\t\t\t{$f_req_id},\n\t\t\t\t{$f_req_area_covered_name},\n\t\t\t\t{$f_req_filename},\n\t\t\t\t{$f_req_doc_type_name},\n\t\t\t\t{$f_req_locked_by},\n\t\t\t\t{$f_req_locked_date},\n\t\t\t\t{$f_req_priority},\n\t\t\t\t{$f_req_ver_uid},\n\t\t\t\t{$f_req_ver_version},\n\t\t\t\t{$f_req_ver_detail},\n\t\t\t\t{$f_req_ver_status},\n\t\t\t\t{$f_req_ver_filename}\n\t\t\tFROM {$tbl_req}\n\t\t\tINNER JOIN {$tbl_req_ver}\n\t\t\t\tON {$f_req_ver_req_id} = {$f_req_id}\n\t\t\tLEFT JOIN {$tbl_req_doc_type}\n\t\t\t\tON {$f_req_doc_type_id} = {$f_req_type}\n\t\t\tLEFT JOIN {$tbl_req_area_covered}\n\t\t\t\tON {$f_req_area_covered_id} = {$f_req_area_covered}";
    if (!empty($assign_release)) {
        $q .= "\tINNER JOIN {$tbl_req_ver_assoc_rel}\n\t\t\t\t\tON {$f_req_ver_assoc_rel_req_id} = {$f_req_ver_uid}\n\t\t\t\tINNER JOIN {$release_tbl}\n\t\t\t\t\tON {$f_release_id} = {$f_req_ver_assoc_rel_rel_id}";
    }
    if (!empty($functionality)) {
        $q .= "\tINNER JOIN {$tbl_functionality_assoc}\n\t\t\t\t\tON {$f_functionality_assoc_req_id} = {$f_req_id}";
    }
    $q .= "\t{$where_clause}\n\t\t\t\tAND {$f_req_proj_id} = {$project_id}\n\t\t\t\tAND ({$f_req_parent} = {$req_id} OR {$f_req_parent} = 0)\n\t\t\t\tAND {$f_req_id} != {$req_id}\n\t\t\tORDER BY {$order_by} {$order_dir}";
    //print nl2br($q);
    global $db;
    //print$q;
    if (is_null($per_page)) {
        $s_properties = session_get_display_options("requirements");
        $per_page = $s_properties['filter']['per_page'];
    }
    if ($per_page != 0 && $page_number != 0) {
        $row_count = db_num_rows($db, db_query($db, $q));
        $page_number = util_page_number($page_number, $row_count, $per_page);
        # Add the limit clause to the query so that we only show n number of records per page
        $offset = ($page_number - 1) * $per_page;
        html_table_offset($row_count, $per_page, $page_number, $order_by, $order_dir, $csv_name);
        $q .= " LIMIT {$offset}, " . $per_page;
    }
    //print$q;exit;
    $rows = db_fetch_array($db, db_query($db, $q));
    return $rows;
}
function testset_filter_apply($where_clause, $project_id, $per_page, $order_by, $order_dir, $page_number)
{
    global $db;
    $tbl_release = RELEASE_TBL;
    $tbl_build = BUILD_TBL;
    $tbl_testset = TS_TBL;
    $tbl_tsa = TEST_TS_ASSOC_TBL;
    $f_testset_id = $tbl_testset . "." . TS_ID;
    $f_testset_name = $tbl_testset . "." . TS_NAME;
    $f_test_ts_id = $tbl_tsa . "." . TEST_TS_ASSOC_TS_ID;
    $f_testset_build_id = $tbl_testset . "." . TS_BUILD_ID;
    $f_build_name = $tbl_build . "." . BUILD_NAME;
    $f_build_id = $tbl_build . "." . BUILD_ID;
    $f_build_release_id = $tbl_build . "." . BUILD_REL_ID;
    $f_release_id = $tbl_release . "." . RELEASE_ID;
    $f_project_id = $tbl_release . "." . RELEASE_PROJECT_ID;
    $f_release_name = $tbl_release . "." . RELEASE_NAME;
    $f_date_created = $tbl_testset . "." . TS_DATE_CREATED;
    $csv_name = null;
    $q = "SELECT  {$f_testset_id}, {$f_date_created}, {$f_testset_name}, {$f_build_name}, {$f_release_name}, {$f_date_created} \t\t\n\t\tFROM {$tbl_testset}, {$tbl_build}, {$tbl_release}\n\t\tWHERE {$f_testset_build_id}  = {$f_build_id} \n\t\tAND {$f_build_release_id} = {$f_release_id}\n\t\tAND {$f_project_id} = {$project_id} " . $where_clause . "\n\t\tGROUP BY {$f_testset_id}";
    $order_clause = " ORDER BY {$order_by} {$order_dir}";
    $q .= $order_clause;
    if ($per_page != 0 && $page_number != 0) {
        $row_count = db_num_rows($db, db_query($db, $q));
        $page_number = util_page_number($page_number, $row_count, $per_page);
        # Add the limit clause to the query so that we only show n number of records per page
        $offset = ($page_number - 1) * $per_page;
        html_table_offset($row_count, $per_page, $page_number, $order_by, $order_dir, $csv_name);
        $q .= " LIMIT {$offset}, " . $per_page;
    }
    $rs = db_query($db, $q);
    return db_fetch_array($db, $rs);
}