# ---------------------------------------------------------------------
# Test CSV Export Page
#
# $RCSfile: test_csv_export.php,v $  $Revision: 1.3 $
# ---------------------------------------------------------------------
include "./api/include_api.php";
auth_authenticate_user();
header('Content-Type: text/plain; name=data.csv');
header('Content-Transfer-Encoding: BASE64;');
header('Content-Disposition: attachment; filename=data.csv');
global $db;
$deleted = TEST_TBL . "." . TEST_DELETED;
$archived = TEST_TBL . "." . TEST_ARCHIVED;
$test_tbl = TEST_TBL;
# get filter and sort options off the session
$s_test_form_filter_options = session_get_filter_options("test");
$s_test_table_display_options = session_get_display_options("test");
$where_clause = test_filter_generate_where_clause($s_test_form_filter_options['manauto'], $s_test_form_filter_options['baowner'], $s_test_form_filter_options['qaowner'], $s_test_form_filter_options['test_type'], $s_test_form_filter_options['test_area']);
$order_clause = ' ORDER BY ' . $s_test_table_display_options['order_by'] . ' ' . $s_test_table_display_options['order_dir'];
# set table headers
print "Test ID, M/A, File Type, Auto Pass, Test Name, BA Owner, QA Owner, Test Type, Area Tested\r" . NEWLINE;
$q = "SELECT * FROM {$test_tbl} WHERE {$deleted} = 'N' AND {$archived} = 'N'";
$q = $q . $where_clause . $order_clause;
$rs =& db_query($db, $q);
while ($row = db_fetch_row($db, $rs)) {
    extract($row, EXTR_PREFIX_ALL, 'v');
    $test_id = ${'v_' . TEST_ID};
    $test_name = ${'v_' . TEST_NAME};
    $ba_owner = ${'v_' . TEST_BA_OWNER};
    $qa_owner = ${'v_' . TEST_QA_OWNER};
    $tester = ${'v_' . TEST_TESTER};
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_for_export($project_id, $page_number = 0, $order_by = REQ_FILENAME, $order_dir = "ASC", $doc_type = "", $status = "", $area_covered = "", $functionality = "", $assign_release = "", $show_versions = "latest", $per_page = null, $search = "", $priority, $csv_name = null, $root_node = false)
{
    global $db;
    $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;
    $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;
    $tbl_req_functionality = REQ_FUNCT_TBL;
    $f_req_functonaility_id = $tbl_req_functionality . "." . REQ_FUNCT_ID;
    $f_req_functionality = $tbl_req_functionality . "." . REQ_FUNCT_NAME;
    //$where_clause = "";
    $where_clause = " WHERE {$f_req_proj_id} = {$project_id}";
    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'";
    }
    if ($root_node) {
        $where_clause .= " AND {$f_req_parent} = 0";
    }
    # SEARCH
    if (!empty($search)) {
        $search = htmlspecialchars($search, ENT_QUOTES);
        $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_filename},\n\t\t\t\t{$f_req_ver_filename},\n\t\t\t\t{$f_req_ver_detail},\n\t\t\t\t{$f_req_doc_type_name},\n\t\t\t\t{$f_req_area_covered_name},\n\t\t\t\t{$f_req_priority},\n\t\t\t\t{$f_req_ver_status},\n\t\t\t\t{$f_req_ver_version},\n\t\t\t\t{$f_req_locked_by},\n\t\t\t\t{$f_req_locked_date},\n\t\t\t\t{$f_release_name}\t\n\t\t\tFROM {$tbl_req}\n\t\t\tINNER JOIN {$tbl_req_ver} ON {$f_req_ver_req_id} = {$f_req_id}\n\t\t\tLEFT JOIN {$tbl_req_doc_type} ON {$f_req_doc_type_id} = {$f_req_type}\n\t\t\tLEFT JOIN {$tbl_req_area_covered} ON {$f_req_area_covered_id} = {$f_req_area_covered}\n\t\t\tLEFT JOIN {$tbl_req_ver_assoc_rel} ON {$f_req_ver_assoc_rel_req_id} = {$f_req_ver_uid}\n\t\t\tLEFT JOIN {$release_tbl} ON {$f_release_id} = {$f_req_ver_assoc_rel_rel_id}";
    //$f_req_functionality
    //LEFT JOIN $tbl_functionality_assoc ON $f_functionality_assoc_req_id = $f_req_id
    //LEFT JOIN $tbl_req_functionality ON $f_functionality_assoc_funct_id = $f_req_functonaility_id";
    $q .= "\t{$where_clause}\n\t\t\tORDER BY {$order_by} {$order_dir}";
    if (is_null($per_page)) {
        $display_options = session_get_filter_options("requirements");
        $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;
}
# ---------------------------------------------------------------------
# Test Workflow CSV Export Page
#
# $RCSfile: test_workflow_csv_export.php,v $  $Revision: 1.2 $
# ---------------------------------------------------------------------
include "./api/include_api.php";
auth_authenticate_user();
header('Content-Type: text/plain; name=data.csv');
header('Content-Transfer-Encoding: BASE64;');
header('Content-Disposition: attachment; filename=data.csv');
global $db;
$deleted = TEST_TBL . "." . TEST_DELETED;
$archived = TEST_TBL . "." . TEST_ARCHIVED;
$test_tbl = TEST_TBL;
# get filter and sort options off the session
$s_test_workflow_form_filter_options = session_get_filter_options("test_workflow");
$s_test_workflow_table_display_options = session_get_display_options("test_workflow");
$where_clause = test_workflow_filter_generate_where_clause($s_test_workflow_form_filter_options['manauto'], $s_test_workflow_form_filter_options['baowner'], $s_test_workflow_form_filter_options['qaowner'], $s_test_workflow_form_filter_options['test_status']);
$order_clause = ' ORDER BY ' . $s_test_workflow_table_display_options['order_by'] . ' ' . $s_test_workflow_table_display_options['order_dir'];
# set table headers
print "Test ID, M/A, Test Name, Test Status, Priority, BA Owner, QA Owner, Date Assigned, Date Expected, Date Complete, BA Sign Off Date, Info\r" . NEWLINE;
$q = "SELECT * FROM {$test_tbl} WHERE {$deleted} = 'N' AND {$archived} = 'N'";
$q = $q . $where_clause . $order_clause;
$rs =& db_query(&$db, $q);
while ($row = db_fetch_row($db, $rs)) {
    extract($row, EXTR_PREFIX_ALL, 'v');
    $test_id = ${'v_' . TEST_ID};
    $test_name = ${'v_' . TEST_NAME};
    $ba_owner = ${'v_' . TEST_BA_OWNER};
    $qa_owner = ${'v_' . TEST_QA_OWNER};
    $tester = ${'v_' . TEST_TESTER};