function bbconnect_export_process()
{
    // EXTEND EXECUTION TIME
    set_time_limit(3600);
    // SET LOCAL GLOBALS
    global $wpdb;
    $blog_prefix = $wpdb->get_blog_prefix(get_current_blog_id());
    // SET A WPR TEMPORARY ARRAY
    $wpr_trans = array('email', 'url');
    $ret_array = array();
    $exp_array = array();
    // BYPASS PAGINATION AND GET ALL USERS FROM THIS QUERY
    if (isset($_POST['_grexport']['all_users'])) {
        $users = explode('|', $_POST['_grexport']['all_search']);
        unset($_POST['_grexport']['all_search']);
    } else {
        if (isset($_POST['gredit_users'])) {
            $users = array_unique($_POST['gredit_users']);
        } else {
            $users = false;
        }
    }
    // SET UP THE DATA ARRAY
    foreach ($_POST['_grexport'] as $key => $val) {
        $exp_array[$key] = maybe_unserialize(urldecode($val));
    }
    $exp_array['return'] = true;
    if (false != $users) {
        // IF WE WANT ALL FIELDS, DO THAT NOW, ELSE, TAKE WHAT WE'VE SEARCHED FOR
        if (isset($_POST['_grexport']['all_fields'])) {
            $headers_prep = bbconnect_get_user_metadata();
            foreach ($headers_prep as $header) {
                $user_meta = get_option($header);
                if ('taxonomy' == $user_meta['options']['field_type']) {
                    $headers[$user_meta['meta_key']] = array();
                } else {
                    if ('wpr' == $user_meta['source']) {
                        if (in_array($user_meta['meta_key'], $wpr_trans)) {
                            $wpr_key = 'user_' . $user_meta['meta_key'];
                        } else {
                            if ('role' == $user_meta['meta_key']) {
                                $wpr_key = $blog_prefix . 'capabilities';
                            } else {
                                $wpr_key = $user_meta['meta_key'];
                            }
                        }
                        $headers[$wpr_key] = $user_meta['meta_key'];
                    } else {
                        if ('wp' == $user_meta['source'] || 'user' == $user_meta['source']) {
                            $headers[$user_meta['meta_key']] = $user_meta['meta_key'];
                        } else {
                            $headers['bbconnect_' . $user_meta['meta_key']] = $user_meta['meta_key'];
                        }
                    }
                }
            }
            // REPLACE THE FIELD SELECTIONS WITH ALL FIELDS
            $exp_array['table_body'] = $headers;
            $exp_array['post_vars'] = false;
            unset($headers);
        }
        // IF WE HAVE ACTIONS, PROCESS THEM
        if (isset($_POST['gredit_actions'])) {
            $exp_array['action_array'] = apply_filters('bbconnect_export_action_array', $_POST['gredit_actions']);
        }
        foreach ($users as $user_id) {
            // ADD THE CURRENT USER IN
            $exp_array['user_id'] = $user_id;
            $cur_user = bbconnect_rows($exp_array);
            if (isset($cur_user['action'])) {
                if (isset($_POST['_grexport']['all_actions']) || isset($_POST['gredit_actions'])) {
                    $curact_array = apply_filters('bbconnect_export_action_return', $cur_user['action']);
                    unset($cur_user['action']);
                    foreach ($curact_array as $akey => $aval) {
                        $ret_array[] = $cur_user + $aval;
                    }
                } else {
                    unset($cur_user['action']);
                    $ret_array[] = $cur_user;
                }
            } else {
                $ret_array[] = $cur_user;
            }
        }
        // APPLY A HEADER
        array_unshift($ret_array, array_keys($ret_array[0]));
        // FIX THAT DAMN MICROSOFT BUG!
        if ('ID' == $ret_array[0][0]) {
            $ret_array[0][0] = 'user_id';
        }
        if (isset($_POST['export_title'])) {
            $exp_title = $_POST['export_title'];
        } else {
            $exp_title = 'export';
        }
        $inc_file = bbconnect_export_filename(sanitize_title_with_underscores($exp_title));
        $ret_file = bbconnect_export_file($inc_file, $ret_array);
        if (false != $ret_file) {
            $export_results = '<div id="progress"><a href="' . $ret_file . '">' . __('Your file', 'bbconnect') . '</a></div>';
        } else {
            $export_results = __('I was not able to complete the export.', 'bbconnect');
        }
    } else {
        $export_results = __('Please make a selection', 'bbconnect');
    }
    $ret_arr = array('all_search' => false, 'member_search' => false, 'action_search' => false, 'user_display' => false, 'export_array' => $ret_array, 'max_num_pages' => false, 'post_vars' => false, 'users_per_page' => false, 'users_count' => false, 'export_results' => $export_results);
    return $ret_arr;
}
function bbconnect_report_display($ret_res = array())
{
    // SETUP DISPLAY GLOBALS
    global $ret_res, $table_head, $table_body, $pagenow, $action_array;
    global $totalValues, $fieldInfos;
    // EXTRACT THE VARIABLES
    extract($ret_res, EXTR_SKIP);
    // COUNT THE ADDRESSES FOR DISPLAY
    global $bbconnect_address_count;
    $bbconnect_user_metas = bbconnect_get_user_metadata(array('group_break' => true));
    $bbconnect_address_count = 0;
    foreach ($bbconnect_user_metas as $pmkey => $pmval) {
        if (false != strstr($pmval, 'address')) {
            $bbconnect_address_count++;
        }
    }
    if (is_admin()) {
        // IF THERE ARE ANY ERRORS, LET THE USER KNOW!
        if (isset($bbconnect_errors) && !empty($bbconnect_errors)) {
            echo '<div class="error">';
            if (is_array($bbconnect_errors)) {
                foreach ($bbconnect_errors as $error) {
                    if (is_wp_error($error)) {
                        $error->get_error_message();
                    } else {
                        echo $error;
                    }
                }
            } else {
                echo $bbconnect_errors;
            }
            echo '</div>';
        }
        // FIRE OFF ANY NOTIFICATIONS FOR PLUGINS
        do_action('bbconnect_report_notifications', $ret_res);
    }
    $report_nav = '';
    $report_nav .= '<div id="report-navigation"><div class="inside">';
    $report_nav .= '<span class="report-tab">';
    if (is_admin()) {
        $report_nav .= bbconnect_pagination($ret_res);
    } else {
        $report_nav .= bbconnect_pagination($ret_res, true);
    }
    // ALLOW OTHER PLUGINS TO ADD VIEWS AND DO STUFF
    if (is_admin()) {
        $report_nav .= bbctheme_toggle_view(apply_filters('bbconnect_dir_tabs', array('list')));
    }
    $report_nav .= '</span>';
    echo $report_nav;
    do_action('bbconnect_report_navigation');
    echo '</div></div>';
    if (!isset($all_search) || empty($all_search)) {
        $all_search = $member_search;
    }
    // HANDLE ACTIONS HERE
    do_action('bbconnect_process_action_search', $action_search, $ret_res);
    $action_array = apply_filters('bbconnect_process_action_array', false, $ret_res);
    ?>

        <div id="report-data-array" style="display: none;">
            <input name="_grexport[post_vars]" type="hidden" value="<?php 
    echo rawurlencode(serialize($ret_res['post_vars']));
    ?>
" />
            <input name="_grexport[table_body]" type="hidden" value="<?php 
    echo rawurlencode(serialize($table_body));
    ?>
" />
            <input name="_grexport[bbconnect_address_count]" type="hidden" value="<?php 
    echo rawurlencode(serialize($bbconnect_address_count));
    ?>
" />
            <input name="_grexport[all_search]" type="hidden" value="<?php 
    echo implode('|', $ret_res['all_search']);
    ?>
" />
            <input name="_grexport[action_search]" type="hidden" value="<?php 
    echo rawurlencode(serialize($action_search));
    ?>
" />
        </div>

        <form id="report-data-form" action="" enctype="multipart/form-data" method="POST">

            <div id="list" class="content-display" style="<?php 
    if (!isset($_COOKIE['view']) || 'list' == $_COOKIE['view']) {
        echo ' display: block;';
    }
    ?>
">

                <?php 
    // DISPLAY USERS OR...
    if (false != $all_search && false != $member_search) {
        // SETUP TABLE HEADER DISPLAY
        if ('' != $table_head) {
            if (is_array($table_head)) {
                $t_head = '';
                foreach ($table_head as $head) {
                    // DISPLAY THE HEADER
                    $t_head .= '<th>' . $head[1] . '</th>';
                }
            } else {
                $t_head = $table_head;
            }
        } else {
            $t_head = '';
        }
        if (false != $action_search) {
            if (is_admin()) {
                $t_head .= '<th><table><tr><td class="gredit-column" width="5%"><input type="checkbox" id="action-gredit" class="gredit-action"';
                if (!isset($ret_res['edit'])) {
                    $t_head .= ' disabled="disabled"';
                } else {
                    $t_head .= ' style="display: block;"';
                }
                $t_head .= ' /><span style="padding: 0 0 0 4%;">' . __('Actions', 'bbconnect') . '</span></td></tr></table></th>';
            } else {
                $t_head .= '<th>' . __('Actions', 'bbconnect') . '</th>';
            }
        }
        // COUNT THE NUMBER OF COLUMNS
        $col_count = 1;
        $col_span = 1;
        if (!empty($table_body)) {
            $col_count = $col_count + count($table_body);
            $col_span = count($table_body);
        }
        if (false != $action_search) {
            $col_count = $col_count + 2;
            $act_span = true;
        }
        // CALCULATE THE COLUMN WIDTHS
        $tdw = 95 / $col_count;
        if (!isset($act_span)) {
            $stw = '95%';
        } else {
            $stw = $tdw * $col_span;
            $atw = $tdw * 2;
        }
        ?>

                <table class="wp-list-table widefat" cellspacing="5">
                    <thead>
                        <tr>
                            <?php 
        if (is_admin()) {
            ?>
                            <?php 
        }
        ?>
                            <th style="min-width: 185px;text-align:left;"><?php 
        echo apply_filters('bbconnect_reports_user_header', __('Users', 'bbconnect'));
        ?>
</th>
                            <?php 
        echo $t_head;
        ?>
                        </tr>
                    </thead>
                    <tbody>
                <?php 
        // DO AN ACTION FOR OTHER TABS
        // YOU'LL HAVE TO CALL THE GLOBALS MANUALLY!
        do_action('bbconnect_prep_results_display', $ret_res);
        $user_rows = '';
        $note_ids = array();
        // FOR THE REST... PAGINATE!
        foreach ($member_search as $user_id) {
            foreach ($ret_res['post_vars'] as $post_var) {
                foreach ($post_var as $var_details) {
                    if ($var_details['field'] == 'bb_work_queue' && $var_details['operator'] == 'is' && function_exists('bbconnect_workqueues_get_action_items')) {
                        $work_queues = $var_details['query'];
                        $args = array('author' => $user_id, 'tax_query' => array(array('taxonomy' => 'bb_note_type', 'field' => 'term_id', 'terms' => $work_queues)));
                        $notes = bbconnect_workqueues_get_action_items($args);
                        foreach ($notes as $note) {
                            $note_ids[$note->ID] = $note->ID;
                        }
                    }
                }
            }
            echo bbconnect_rows(array('table_body' => $table_body, 'user_id' => $user_id, 'action_search' => $action_search, 'action_array' => $action_array, 'bbconnect_address_count' => $bbconnect_address_count, 'post_vars' => $ret_res['post_vars'], 'tdw' => $tdw));
        }
        ?>
                    </tbody>
                    <tfoot>
                        <?php 
        if (count($totalValues) > 0) {
            ?>
                        <tr>
                            <?php 
            if (is_admin()) {
                ?>
                            <?php 
            }
            ?>
                            <th><?php 
            echo apply_filters('bbconnect_reports_user_header', __('Total for displayed results)', 'bbconnect'));
            ?>
</th>
                            <?php 
            for ($i = 1; $i <= count($table_body); $i++) {
                $value_display = $totalValues[$i] ? $totalValues[$i] : '';
                if ($fieldInfos[$i]['options']['field_type'] == 'number' && $fieldInfos[$i]['options']['is_currency'] && $value_display != '') {
                    $value_display = '$' . number_format($value_display, 2);
                }
                echo "<th style='text-align: right;'>" . apply_filters('bbconnect_reports_user_header', $value_display) . "</th>" . "\n";
            }
            ?>

                        </tr>
                        <tr>
                            <?php 
            if (is_admin()) {
                ?>
                            <?php 
            }
            ?>
                            <th><?php 
            echo apply_filters('bbconnect_reports_user_header', __('Average for displayed results)', 'bbconnect'));
            ?>
</th>
                            <?php 
            for ($i = 1; $i <= count($table_body); $i++) {
                //echo "<th>".apply_filters( 'bbconnect_reports_user_header', ($valueforavg)?__($valueforavg/$ret_res["users_count"],'bbconnect'):'' )."</th>"."\n";
                $value_display = $totalValues[$i] ? number_format($totalValues[$i] / count($member_search), 2) : '';
                if ($fieldInfos[$i]['options']['field_type'] == 'number' && $fieldInfos[$i]['options']['is_currency'] && $value_display != '') {
                    $value_display = '$' . $value_display;
                }
                echo "<th style='text-align: right;'>" . apply_filters('bbconnect_reports_user_header', $value_display) . "</th>" . "\n";
            }
            ?>

                        </tr>
                        <?php 
        }
        ?>
                    </tfoot>
                </table>

                <?php 
    } else {
        if (isset($export_results)) {
            echo '<p>' . $export_results . '</p>';
        } else {
            if (isset($errors)) {
                if ($errors) {
                    echo '<p>' . $errors . '</p>';
                } else {
                    echo '<p>No records found.</p>';
                }
            } else {
                echo '<p>' . __('terribly sorry. i did not find anything.', 'bbconnect') . '</p>';
            }
        }
    }
    ?>

            </div>

            <?php 
    if (is_admin()) {
        do_action('bbconnect_results_display', $ret_res);
    }
    ?>

        </form>

<?php 
    if (function_exists('bbconnect_report_quicklinks')) {
        $args = array();
        if (!empty($note_ids)) {
            $args['note_ids'] = $note_ids;
        }
        bbconnect_report_quicklinks($member_search, $args);
    } else {
        echo '<div id="quicklinks-wrapper">You should get the quicklinks addon!</div>';
    }
    unset($ret_res);
    unset($table_head);
    unset($table_body);
    unset($pagenow);
    unset($all_search);
    unset($member_search);
    unset($action_search);
    unset($action_array);
    unset($bbconnect_errors);
    unset($bbconnect_query);
    unset($bbconnect_user_metas);
    unset($bbconnect_address_count);
    unset($current_member);
    unset($currency);
    unset($currency_array);
    unset($tdw);
    unset($_POST);
}