function dbem_migrate_old_events()
{
    global $wpdb;
    $events_table = $wpdb->prefix . EVENTS_TBNAME;
    $sql = "SELECT event_id, event_time, event_venue, event_address, event_town FROM {$events_table}";
    //echo $sql;
    $events = $wpdb->get_results($sql, ARRAY_A);
    foreach ($events as $event) {
        // Migrating location data to the location table
        $location = array('location_name' => $event['event_venue'], 'location_address' => $event['event_address'], 'location_town' => $event['event_town']);
        $related_location = dbem_get_identical_location($location);
        if ($related_location) {
            $event['location_id'] = $related_location['location_id'];
        } else {
            $new_location = dbem_insert_location($location);
            $event['location_id'] = $new_location['location_id'];
        }
        // migrating event_time to event_start_date and event_start_time
        $event['event_start_date'] = substr($event['event_time'], 0, 10);
        $event['event_start_time'] = substr($event['event_time'], 11, 8);
        $event['event_end_time'] = substr($event['event_time'], 11, 8);
        $where = array('event_id' => $event['event_id']);
        $wpdb->update($events_table, $event, $where);
    }
}
function dbem_events_subpanel()
{
    global $wpdb;
    $action = $_GET['action'];
    $action2 = $_GET['action2'];
    $event_ID = $_GET['event_id'];
    $recurrence_ID = $_GET['recurrence_id'];
    $scope = $_GET['scope'];
    $offset = $_GET['offset'];
    $order = $_GET['order'];
    $selectedEvents = $_GET['events'];
    // Disable Hello to new user if requested
    if (isset($_GET['disable_hello_to_user']) && $_GET['disable_hello_to_user'] == 'true') {
        update_option('dbem_hello_to_user', 0);
    }
    if ($order == "") {
        $order = "ASC";
    }
    if ($offset == "") {
        $offset = "0";
    }
    $event_table_name = $wpdb->prefix . EVENTS_TBNAME;
    // Debug code, to make sure I get the correct page
    // DELETE action
    if ($action == 'deleteEvents') {
        //  $sql="DELETE FROM ".$event_table_name." WHERE event_id='"."$event_ID"."'";
        // TODO eventual error if ID in non-existant
        //$wpdb->query($sql);
        foreach ($selectedEvents as $event_ID) {
            dbem_delete_event($event_ID);
        }
        $events = dbem_get_events("", "future");
        dbem_events_table($events, 10, "Future events");
    }
    // UPDATE or CREATE action
    if ($action == 'update_event' || $action == 'update_recurrence') {
        $event = array();
        $location = array();
        $event['event_name'] = stripslashes($_POST[event_name]);
        // Set event end time to event time if not valid
        // if (!_dbem_is_date_valid($event['event_end_date']))
        // 	$event['event_end_date'] = $event['event-date'];
        $event['event_start_date'] = $_POST[event_date];
        $event['event_end_date'] = $_POST[event_end_date];
        //$event['event_start_time'] = $_POST[event_hh].":".$_POST[event_mm].":00";
        //$event['event_end_time'] = $_POST[event_end_hh].":".$_POST[event_end_mm].":00";
        $event['event_start_time'] = date("G:i:00", strtotime($_POST['event_start_time']));
        $event['event_end_time'] = date("G:i:00", strtotime($_POST['event_end_time']));
        $recurrence['recurrence_name'] = $event['event_name'];
        $recurrence['recurrence_start_date'] = $event['event_start_date'];
        $recurrence['recurrence_end_date'] = $event['event_end_date'];
        $recurrence['recurrence_start_time'] = $event['event_start_time'];
        $recurrence['recurrence_end_time'] = $event['event_end_time'];
        $recurrence['recurrence_id'] = $_POST[recurrence_id];
        $recurrence['recurrence_freq'] = $_POST[recurrence_freq];
        $recurrence['recurrence_freq'] == 'weekly' ? $recurrence[recurrence_byday] = implode(",", $_POST['recurrence_bydays']) : ($recurrence['recurrence_byday'] = $_POST[recurrence_byday]);
        $_POST['recurrence_interval'] == "" ? $recurrence['recurrence_interval'] = 1 : ($recurrence['recurrence_interval'] = $_POST['recurrence_interval']);
        $recurrence['recurrence_byweekno'] = $_POST[recurrence_byweekno];
        $event['event_rsvp'] = $_POST['event_rsvp'];
        $event['event_seats'] = $_POST['event_seats'];
        if (isset($_POST['event_contactperson_id']) && $_POST['event_contactperson_id'] != '' && $_POST['event_contactperson_id'] != '-1') {
            $event['event_contactperson_id'] = $_POST['event_contactperson_id'];
            $recurrence['event_contactperson_id'] = $_POST['event_contactperson_id'];
        }
        if (!_dbem_is_time_valid($event_end_time)) {
            $event_end_time = $event_time;
        }
        $location['location_name'] = $_POST[location_name];
        $location['location_address'] = $_POST[location_address];
        $location['location_town'] = $_POST[location_town];
        $location['location_latitude'] = $_POST[location_latitude];
        $location['location_longitude'] = $_POST[location_longitude];
        $location['location_description'] = "";
        /* Marcus Begin Edit */
        //switched to WP TinyMCE field
        //$event ['event_notes'] = stripslashes ( $_POST [event_notes] );
        $event['event_notes'] = stripslashes($_POST['content']);
        /* Marcus End Edit */
        $recurrence['recurrence_notes'] = $event['event_notes'];
        $validation_result = dbem_validate_event($event);
        if ($validation_result == "OK") {
            // validation successful
            $related_location = dbem_get_identical_location($location);
            // print_r($related_location);
            if ($related_location) {
                $event['location_id'] = $related_location['location_id'];
                $recurrence['location_id'] = $related_location['location_id'];
            } else {
                $new_location = dbem_insert_location($location);
                $event['location_id'] = $new_location['location_id'];
                $recurrence['location_id'] = $new_location['location_id'];
                //print_r($new_location);
            }
            if (!$event_ID && !$recurrence_ID) {
                // there isn't anything
                if ($_POST['repeated_event']) {
                    //insert new recurrence
                    dbem_insert_recurrent_event($event, $recurrence);
                    $feedback_message = __('New recurrent event inserted!', 'dbem');
                } else {
                    // INSERT new event
                    $wpdb->insert($event_table_name, $event);
                    $feedback_message = __('New event successfully inserted!', 'dbem');
                }
            } else {
                // something exists
                if ($recurrence_ID) {
                    // UPDATE old recurrence
                    $recurrence['recurrence_id'] = $recurrence_ID;
                    //print_r($recurrence);
                    if (dbem_update_recurrence($recurrence)) {
                        $feedback_message = __('Recurrence updated!', 'dbem');
                    } else {
                        $feedback_message = __('Something went wrong with the recurrence update...', 'dbem');
                    }
                } else {
                    // UPDATE old event
                    // unlink from recurrence in case it was generated by one
                    $event['recurrence_id'] = null;
                    $where['event_id'] = $event_ID;
                    $wpdb->update($event_table_name, $event, $where);
                    $feedback_message = "'" . $event['event_name'] . "' " . __('updated', 'dbem') . "!";
                }
            }
            /* Marcus Begin Edit */
            //Save the category if selected
            if (is_numeric($_POST['event_category_id'])) {
                $insert_id = !$event_ID && !$recurrence_ID ? $wpdb->insert_id : $event_ID;
                $wpdb->update($event_table_name, array("event_category_id" => $_POST['event_category_id']), array('event_id' => $insert_id));
            }
            /* Marcus End Edit */
            //$wpdb->query($sql);
            echo "<div id='message' class='updated fade'>\n\t\t\t\t\t\t<p>{$feedback_message}</p>\n\t\t\t\t\t  </div>";
            $events = dbem_get_events("", "future");
            dbem_events_table($events, 10, "Future events");
        } else {
            // validation unsuccessful
            echo "<div id='message' class='error '>\n\t\t\t\t\t\t<p>" . __("Ach, there's a problem here:", "dbem") . " {$validation_result}</p>\n\t\t\t\t\t  </div>";
            dbem_event_form($event, "Edit event {$event_ID}", $event_ID);
        }
    }
    if ($action == 'edit_event') {
        if (!$event_ID) {
            $title = __("Insert New Event", 'dbem');
        } else {
            $event = dbem_get_event($event_ID);
            $title = __("Edit Event", 'dbem') . " '" . $event['event_name'] . "'";
        }
        //$event=$wpdb->get_row($sql, ARRAY_A);
        // Enter new events and updates old ones
        // DEBUG: echo"Nome: $event->event_name";
        dbem_event_form($event, $title, $event_ID);
    }
    /* Marcus Begin Edit */
    //Add duplicate event if requested
    if ($action == 'duplicate_event') {
        dbem_duplicate_event($event_ID);
    }
    /* Marcus End Edit */
    if ($action == 'edit_recurrence') {
        $event_ID = $_GET['recurrence_id'];
        $recurrence = dbem_get_recurrence($event_ID);
        $title = __("Reschedule", 'dbem') . " '" . $recurrence['recurrence_name'] . "'";
        dbem_event_form($recurrence, $title, $event_ID);
    }
    if ($action == 'update_recurrence') {
        //print_r($recurrence);
        //die('update recurrence!');
    }
    if ($action == "-1" || $action == "") {
        // No action, only showing the events list
        switch ($scope) {
            case "past":
                $title = __('Past Events', 'dbem');
                break;
            case "all":
                $title = __('All Events', 'dbem');
                break;
            default:
                $title = __('Future Events', 'dbem');
                $scope = "future";
        }
        $limit = 20;
        $events = dbem_get_events($limit, $scope, $order, $offset);
        dbem_events_table($events, $limit, $title);
    }
}