* @copyright (c)2003-2015 by Francois Planque - {@link http://fplanque.com/}.
 * Parts of this file are copyright (c)2005 by Daniel HAHLER - {@link http://thequod.de/contact}.
 *
 * @package admin
 */
if (!defined('EVO_MAIN_INIT')) {
    die('Please, do not access this page directly.');
}
/**
 * @var Slug
 */
global $edited_IPRange;
// Determine if we are creating or updating...
global $action;
$creating = $action == 'iprange_new';
$Form = new Form(NULL, 'iprange_checkchanges', 'post', 'compact');
$Form->global_icon(T_('Cancel editing!'), 'close', regenerate_url('action,iprange_ID'));
$Form->begin_form('fform', ($creating ? T_('New IP Range') : T_('IP Range')) . get_manual_link('ip-range-editing'));
$Form->add_crumb('iprange');
$Form->hidden('action', $creating ? 'iprange_create' : 'iprange_update');
$Form->hidden_ctrl();
$Form->hidden('tab', get_param('tab'));
$Form->hidden('tab3', get_param('tab3'));
$Form->hidden('iprange_ID', param('iprange_ID', 'integer', 0));
$Form->select_input_array('aipr_status', $edited_IPRange->get('status'), aipr_status_titles(), T_('Status'), '', array('force_keys_as_values' => true, 'background_color' => aipr_status_colors(), 'required' => true));
$Form->text_input('aipr_IPv4start', int2ip($edited_IPRange->get('IPv4start')), 50, T_('IP Range Start'), '', array('maxlength' => 15, 'required' => true));
$Form->text_input('aipr_IPv4end', int2ip($edited_IPRange->get('IPv4end')), 50, T_('IP Range End'), '', array('maxlength' => 15, 'required' => true));
$Form->date_input('aipr_IP_timestamp', date2mysql($edited_IPRange->IP_timestamp), T_('Date'), array('required' => true));
$Form->info(T_('User count'), (int) $edited_IPRange->get('user_count'));
$Form->info(T_('Block count'), (int) $edited_IPRange->get('block_count'));
$Form->end_form(array(array('submit', 'submit', $creating ? T_('Record') : T_('Save Changes!'), 'SaveButton')));
/**
 * Get status title of ip range by status value
 *
 * @param string Status value
 * @return string Status title
 */
function aipr_status_title($status)
{
    $aipr_statuses = aipr_status_titles();
    return isset($aipr_statuses[$status]) ? $aipr_statuses[$status] : $status;
}
$Results->cols[] = array('th' => T_('Anon. contact form submits'), 'td' => '$aipr_contact_email_count$', 'order' => 'aipr_contact_email_count', 'default_dir' => 'D');
$Results->cols[] = array('th' => T_('Block count'), 'td' => '$aipr_block_count$', 'order' => 'aipr_block_count', 'default_dir' => 'D');
// Get additional columns from the Plugins
$Plugins->trigger_event('GetAdditionalColumnsTable', array('table' => 'ipranges', 'column' => 'aipr_IPv4start', 'Results' => $Results));
if ($current_User->check_perm('spamblacklist', 'edit')) {
    // Check permission to edit IP ranges:
    /**
     * Get actions links for IP range
     *
     * @param integer IP range ID
     * @param string Current tab value
     * @return string HTML links to edit and delete IP range
     */
    function antispam_ipranges_actions($aipr_ID, $tab_param)
    {
        global $admin_url;
        // A link to edit IP range
        $r = action_icon(T_('Edit this IP range...'), 'properties', $admin_url . '?ctrl=antispam' . $tab_param . '&tab3=ipranges&iprange_ID=' . $aipr_ID . '&action=iprange_edit');
        // A link to delete IP range
        $r .= action_icon(T_('Delete this IP range!'), 'delete', regenerate_url('iprange_ID,action', 'iprange_ID=' . $aipr_ID . '&action=iprange_delete&' . url_crumb('iprange')));
        return $r;
    }
    $Results->cols[] = array('th' => T_('Actions'), 'th_class' => 'shrinkwrap', 'td_class' => 'shrinkwrap', 'td' => '%antispam_ipranges_actions( #aipr_ID#, "' . $tab_param . '" )%');
}
$Results->global_icon(T_('Add a new IP range...'), 'new', regenerate_url('action', 'action=iprange_new'), T_('New IP range') . ' »', 3, 4, array('class' => 'action_icon btn-primary'));
$Results->display();
if ($current_User->check_perm('spamblacklist', 'edit')) {
    // Check permission to edit IP ranges:
    // Print JS to edit status of IP range
    echo_editable_column_js(array('column_selector' => '.iprange_status_edit', 'ajax_url' => get_secure_htsrv_url() . 'async.php?action=iprange_status_edit&' . url_crumb('iprange'), 'options' => aipr_status_titles(), 'new_field_name' => 'new_status', 'ID_value' => 'jQuery( ":first", jQuery( this ).parent() ).text()', 'ID_name' => 'iprange_ID', 'colored_cells' => true));
}
$Form->info_field(T_('Account registered on'), $edited_User->dget('datecreated'), array('note' => '(' . date_ago(strtotime($edited_User->get('datecreated'))) . ')'));
$Form->info_field(T_('From IP'), format_to_output(int2ip($UserSettings->get('created_fromIPv4', $edited_User->ID))));
// Get status and name of IP range
$IPRangeCache =& get_IPRangeCache();
if ($IPRange =& $IPRangeCache->get_by_ip(int2ip($UserSettings->get('created_fromIPv4', $edited_User->ID)))) {
    // IP range exists in DB
    $iprange_status = $IPRange->get('status');
    $iprange_name = $IPRange->get_name();
} else {
    // There is no IP range in DB
    $iprange_status = '';
    $iprange_name = '';
}
$Form->info_field(T_('IP range'), $iprange_name);
$email_status_icon = '<div id="iprange_status_icon">' . aipr_status_icon($iprange_status) . '</div>';
$Form->select_input_array('edited_iprange_status', $iprange_status, aipr_status_titles(true), T_('IP range status'), '', array('force_keys_as_values' => true, 'background_color' => aipr_status_colors(), 'field_suffix' => $email_status_icon));
$Form->info_field(T_('From Country'), $from_country, array('field_suffix' => $user_from_country_suffix));
$Form->info_field(T_('From Domain'), format_to_output($UserSettings->get('user_domain', $edited_User->ID)));
$Form->info_field(T_('With Browser'), format_to_output($UserSettings->get('user_browser', $edited_User->ID)));
$Form->text_input('edited_user_source', $edited_User->source, 30, T_('Source link/code'), '', array('maxlength' => 30));
$Form->info_field(T_('Registration trigger Page'), $UserSettings->get('registration_trigger_url', $edited_User->ID));
$Form->info_field(T_('Initial Blog ID'), $UserSettings->get('initial_blog_ID', $edited_User->ID));
$Form->info_field(T_('Initial URI'), $UserSettings->get('initial_URI', $edited_User->ID));
$Form->info_field(T_('Initial referer'), $UserSettings->get('initial_referer', $edited_User->ID));
//$registration_ts = strtotime( $edited_User->get( 'datecreated' ) );
if ($edited_User->check_status('is_closed')) {
    $account_close_ts = $UserSettings->get('account_close_ts', $edited_User->ID);
    $account_close_date = empty($account_close_ts) ? T_('Unknown date') : format_to_output(date2mysql($account_close_ts));
    //$days_on_site = empty( $account_close_ts ) ? T_( 'Unknown' ) : ( round( ( $account_close_ts - $registration_ts ) / 86400/* 60*60*24 */) );
} else {
    $account_close_date = 'n/a';