function qem_settings_init()
{
    qem_generate_csv();
    qem_add_role_caps();
    return;
}
function qem_ics()
{
    global $post;
    $display = event_get_stored_display();
    $summary = $post->post_title;
    $eventstart = get_post_meta($post->ID, 'event_date', true);
    if (!$eventstart) {
        $eventstart = time();
    }
    $start = get_post_meta($post->ID, 'event_start', true);
    $date = date('Ymd\\T', $eventstart);
    $time = qem_time($start);
    $time = date('His', $time);
    $datestart = $date . $time;
    $dateend = get_post_meta($post->ID, 'event_end_date', true);
    $address = get_post_meta($post->ID, 'event_address', true);
    $url = get_permalink();
    $description = get_post_meta($post->ID, 'event_desc', true);
    $filename = $post->post_title . '.ics';
    if (!$dateend) {
        $dateend = $eventstart;
        $finish = get_post_meta($post->ID, 'event_finish', true);
        $date = date('Ymd\\T', $eventstart);
        $time = qem_time($finish);
        $time = date('His', $time);
        $dateend = $date . $time;
    } else {
        $finish = get_post_meta($post->ID, 'event_finish', true);
        $date = date('Ymd\\T', $dateend);
        $time = qem_time($finish);
        $time = date('His', $time);
        $dateend = $date . $time;
    }
    $ics = 'BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
CALSCALE:GREGORIAN
BEGIN:VEVENT
UID:' . uniqid() . '
DTSTAMP:' . dateToCal(time()) . '
DTSTART:' . $datestart . '
DTEND:' . $dateend . '
LOCATION:' . $address . '
DESCRIPTION:' . $description . '
URL;VALUE=URI:' . $url . '
SUMMARY:' . $summary . '
END:VEVENT
END:VCALENDAR';
    qem_generate_csv();
    $content = '<form method="post" action="">
    <input type="hidden" name="qem_ics" value="' . $ics . '">
    <input type="hidden" name="qem_filename" value="' . $filename . '">
    <input type="submit" name="qem_create_ics" class="qem-register" id="submit" value="' . $display['useicsbutton'] . '" /></form>';
    return $content;
}
function qem_messages()
{
    $event = $title = '';
    global $_GET;
    $event = isset($_GET["event"]) ? $_GET["event"] : null;
    $title = isset($_GET["title"]) ? $_GET["title"] : null;
    $unixtime = get_post_meta($event, 'event_date', true);
    $date = date_i18n("d M Y", $unixtime);
    $noregistration = '<p>No event selected</p>';
    $register = qem_get_stored_register();
    $category = 'All Categories';
    if (isset($_POST['qem_reset_message'])) {
        $event = $_POST['qem_download_form'];
        $title = get_the_title($event);
        delete_option('qem_messages_' . $event);
        delete_option($event);
        qem_admin_notice('Registrants for ' . $title . ' have been deleted.');
        $eventnumber = get_post_meta($event, 'event_number', true);
        update_option($event . 'places', $eventnumber);
    }
    if (isset($_POST['category'])) {
        $category = $_POST["category"];
    }
    if (isset($_POST['select_event']) || isset($_POST['eventid'])) {
        $event = $_POST["eventid"];
        if ($event) {
            $unixtime = get_post_meta($event, 'event_date', true);
            $date = date_i18n("d M Y", $unixtime);
            $title = get_the_title($event);
            $noregistration = '<h2>' . $title . ' | ' . $date . '</h2><p>Nobody has registered for ' . $title . ' yet</p>';
        } else {
            $noregistration = '<p>No event selected</p>';
        }
    }
    if (isset($_POST['changeoptions'])) {
        $options = array('showevents', 'category');
        foreach ($options as $item) {
            $messageoptions[$item] = stripslashes($_POST[$item]);
        }
        $category = $messageoptions['category'];
        update_option('qem_messageoptions', $messageoptions);
    }
    if (isset($_POST['qem_delete_selected'])) {
        $event = $_POST["qem_download_form"];
        $message = get_option('qem_messages_' . $event);
        $eventnumber = get_option($event . 'places');
        $check = get_post_meta($event, 'event_counter', true);
        for ($i = 0; $i <= 100; $i++) {
            if ($_POST[$i] == 'checked') {
                $num = $message[$i]['yourplaces'] ? $message[$i]['yourplaces'] : 1;
                if ($check) {
                    $eventnumber = $eventnumber + $num;
                }
                unset($message[$i]);
            }
        }
        $message = array_values($message);
        update_option('qem_messages_' . $event, $message);
        if ($check) {
            update_option($event . 'places', $eventnumber);
        }
        qem_admin_notice('Selected registrations have been deleted.');
    }
    if (isset($_POST['qem_emaillist'])) {
        $event = $_POST["qem_download_form"];
        $title = $_POST["qem_download_title"];
        $message = get_option('qem_messages_' . $event);
        $register = qem_get_stored_register();
        $content = '';
        $content = qem_build_registration_table($register, $message, '', '', '', '');
        global $current_user;
        get_currentuserinfo();
        $qem_email = $current_user->user_email;
        $headers = "From: {<{$qem_email}>\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: text/html; charset=\"utf-8\"\r\n";
        wp_mail($qem_email, $title, $content, $headers);
        qem_admin_notice('Registration list has been sent to ' . $qem_email . '.');
    }
    qem_generate_csv();
    $content = $current = $all = '';
    $messageoptions = qem_get_stored_msg();
    ${$messageoptions}['showevents'] = "checked";
    $message = get_option('qem_messages_' . $event);
    $places = get_option($event . 'places');
    $check = get_post_meta($event, 'event_counter', true);
    if (!is_array($message)) {
        $message = array();
    }
    $dashboard = '<div class="wrap">
<h1>Event Registation Report</h1>
<p><form method="post" action="">' . qem_message_categories($category) . '
&nbsp;&nbsp;' . qem_get_eventlist($event, $register, $messageoptions, $category) . '
&nbsp;&nbsp;<b>Show:</b> <input style="margin:0; padding:0; border:none;" type="radio" name="showevents" value="all" ' . $all . ' /> All Events <input style="margin:0; padding:0; border:none;" type="radio" name="showevents" value="current" ' . $current . ' /> Current Events&nbsp;&nbsp;<input type="submit" name="changeoptions" class="button-secondary" value="Update options" />
</form>
</p>
<div id="qem-widget">
<form method="post" id="qem_download_form" action="">';
    $content = qem_build_registration_table($register, $message, $check, '', $event);
    if ($content) {
        $dashboard .= '<h2>' . $title . ' | ' . $date . '</h2>';
        $dashboard .= '<p>Event ID: ' . $event . '</p>';
        $dashboard .= $content;
        $dashboard .= '<input type="hidden" name="qem_download_form" value = "' . $event . '" />
    <input type="hidden" name="qem_download_title" value = "' . $title . '" />
    <input type="submit" name="qem_download_csv" class="button-primary" value="Export to CSV" />
    <input type="submit" name="qem_emaillist" class="button-primary" value="Email List" />
    <input type="submit" name="qem_reset_message" class="button-secondary" value="Delete All Registrants" onclick="return window.confirm( \'Are you sure you want to delete all the registrants for ' . $title . '?\' );"/>
    <input type="submit" name="qem_delete_selected" class="button-secondary" value="Delete Selected" onclick="return window.confirm( \'Are you sure you want to delete the selected registrants?\' );"/>
    </form>';
    } else {
        $dashboard .= $noregistration;
    }
    $dashboard .= '</div></div>';
    echo $dashboard;
}