/**
 * Display threads results table
 *
 * @param array Params
 */
function threads_results_block($params = array())
{
    // Make sure we are not missing any param:
    $params = array_merge(array('edited_User' => NULL, 'results_param_prefix' => 'actv_thrd_', 'results_title' => T_('Threads with private messages sent by the user'), 'results_no_text' => T_('User has not sent any private messages')), $params);
    if (!is_logged_in()) {
        // Only logged in users can access to this function
        return;
    }
    global $current_User;
    if (!$current_User->check_perm('users', 'edit') || !$current_User->check_perm('perm_messaging', 'reply')) {
        // Check minimum permission:
        return;
    }
    $edited_User = $params['edited_User'];
    if (!$edited_User) {
        // No defined User, probably the function is calling from AJAX request
        $user_ID = param('user_ID', 'integer', 0);
        if (empty($user_ID)) {
            // Bad request, Exit here
            return;
        }
        $UserCache =& get_UserCache();
        if (($edited_User =& $UserCache->get_by_ID($user_ID, false)) === false) {
            // Bad request, Exit here
            return;
        }
    }
    global $DB, $current_User;
    param('user_tab', 'string', '', true);
    param('user_ID', 'integer', 0, true);
    // Check permission:
    if ($current_User->check_perm('perm_messaging', 'abuse')) {
        // Create result set:
        $threads_Results = get_threads_results(array('results_param_prefix' => $params['results_param_prefix'], 'user_ID' => $edited_User->ID, 'sent_user_ID' => $edited_User->ID));
        $threads_Results->Cache =& get_ThreadCache();
        $threads_Results->title = $params['results_title'];
        $threads_Results->no_results_text = $params['results_no_text'];
        if ($threads_Results->total_rows > 0) {
            // Display action icon to delete all records if at least one record exists
            $threads_Results->global_icon(sprintf(T_('Delete all private messages sent by %s'), $edited_User->login), 'delete', '?ctrl=user&user_tab=activity&action=delete_all_messages&user_ID=' . $edited_User->ID . '&' . url_crumb('user'), ' ' . T_('Delete all'), 3, 4);
        }
        // Load classes
        load_class('messaging/model/_thread.class.php', 'Thread');
        // Initialize Results object
        threads_results($threads_Results, array('abuse_management' => 1, 'show_only_date' => 1));
        if (is_ajax_content()) {
            // init results param by template name
            if (!isset($params['skin_type']) || !isset($params['skin_name'])) {
                debug_die('Invalid ajax results request!');
            }
            $threads_Results->init_params_by_skin($params['skin_type'], $params['skin_name']);
        }
        $display_params = array('before' => '<div class="results" style="margin-top:25px" id="threads_result">');
        $threads_Results->display($display_params);
        if (!is_ajax_content()) {
            // Create this hidden div to get a function name for AJAX request
            echo '<div id="' . $params['results_param_prefix'] . 'ajax_callback" style="display:none">' . __FUNCTION__ . '</div>';
        }
    } else {
        // No permission for abuse management
        echo '<div style="margin-top:25px;font-weight:bold">' . sprintf(T_('User has sent %s private messages'), $edited_User->get_num_messages('sent')) . '</div>';
    }
}
}
if ($unread_messages_count > 0 && !$perm_abuse_management) {
    $Results->title = $Results->title . ' <span class="badge">' . $unread_messages_count . '</span></b>';
}
/**
 * Callback to add filters on top of the result set
 *
 * @param Form
 */
function filter_recipients(&$Form)
{
    global $perm_abuse_management;
    $Form->text('s', get_param('s'), 20, T_('Search'), '', 255);
    $Form->text('u', get_param('u'), 10, T_('User'), '', 255);
    if (!$perm_abuse_management) {
        $Form->checkbox('show_closed', get_param('show_closed'), T_('Show closed conversations'));
    }
}
$Results->filter_area = array('callback' => 'filter_recipients', 'presets' => array('all' => array(T_('All'), get_dispctrl_url($perm_abuse_management ? 'abuse' : 'threads'))));
// Initialize Results object
threads_results($Results, array_merge(array('abuse_management' => (int) $perm_abuse_management), $display_params));
if (!$perm_abuse_management) {
    // Show link to create a new conversation
    if (is_admin_page()) {
        $newmsg_url = regenerate_url('action', 'action=new');
    } else {
        $newmsg_url = regenerate_url('disp', 'disp=threads&action=new');
    }
    $Results->global_icon(T_('Create a new conversation...'), 'new', $newmsg_url, T_('Compose new') . ' &raquo;', 3, 4);
}
$Results->display($display_params);