function my_calendar_manage_locations()
{
    global $wpdb;
    $mcdb = $wpdb;
    ?>
	<div class="wrap jd-my-calendar">
	<?php 
    my_calendar_check_db();
    // We do some checking to see what we're doing
    mc_update_location_controls();
    mc_mass_delete_locations();
    if (!empty($_POST) && (!isset($_POST['mc_locations']) && !isset($_POST['mass_delete']))) {
        $nonce = $_REQUEST['_wpnonce'];
        if (!wp_verify_nonce($nonce, 'my-calendar-nonce')) {
            die("Security check failed");
        }
    }
    if (isset($_POST['mode']) && $_POST['mode'] == 'add') {
        $add = array('location_label' => $_POST['location_label'], 'location_street' => $_POST['location_street'], 'location_street2' => $_POST['location_street2'], 'location_city' => $_POST['location_city'], 'location_state' => $_POST['location_state'], 'location_postcode' => $_POST['location_postcode'], 'location_region' => $_POST['location_region'], 'location_country' => $_POST['location_country'], 'location_url' => $_POST['location_url'], 'location_longitude' => $_POST['location_longitude'], 'location_latitude' => $_POST['location_latitude'], 'location_zoom' => $_POST['location_zoom'], 'location_phone' => $_POST['location_phone'], 'location_phone2' => $_POST['location_phone2'], 'location_access' => isset($_POST['location_access']) ? serialize($_POST['location_access']) : '');
        $results = mc_insert_location($add);
        do_action('mc_save_location', $results, $add);
        if ($results) {
            echo "<div class=\"updated\"><p><strong>" . __('Location added successfully', 'my-calendar') . "</strong></p></div>";
        } else {
            echo "<div class=\"error\"><p><strong>" . __('Location could not be added to database', 'my-calendar') . "</strong></p></div>";
        }
    } else {
        if (isset($_GET['location_id']) && $_GET['mode'] == 'delete') {
            $sql = "DELETE FROM " . my_calendar_locations_table() . " WHERE location_id=" . (int) $_GET['location_id'];
            $results = $mcdb->query($sql);
            do_action('mc_delete_location', $results, (int) $_GET['location_id']);
            if ($results) {
                echo "<div class=\"updated\"><p><strong>" . __('Location deleted successfully', 'my-calendar') . "</strong></p></div>";
            } else {
                echo "<div class=\"error\"><p><strong>" . __('Location could not be deleted', 'my-calendar') . "</strong></p></div>";
            }
        } else {
            if (isset($_GET['mode']) && isset($_GET['location_id']) && $_GET['mode'] == 'edit' && !isset($_POST['mode'])) {
                $cur_loc = (int) $_GET['location_id'];
                mc_show_location_form('edit', $cur_loc);
            } else {
                if (isset($_POST['location_id']) && isset($_POST['location_label']) && $_POST['mode'] == 'edit') {
                    $update = array('location_label' => $_POST['location_label'], 'location_street' => $_POST['location_street'], 'location_street2' => $_POST['location_street2'], 'location_city' => $_POST['location_city'], 'location_state' => $_POST['location_state'], 'location_postcode' => $_POST['location_postcode'], 'location_region' => $_POST['location_region'], 'location_country' => $_POST['location_country'], 'location_url' => $_POST['location_url'], 'location_longitude' => $_POST['location_longitude'], 'location_latitude' => $_POST['location_latitude'], 'location_zoom' => $_POST['location_zoom'], 'location_phone' => $_POST['location_phone'], 'location_phone2' => $_POST['location_phone2'], 'location_access' => isset($_POST['location_access']) ? serialize($_POST['location_access']) : '');
                    $where = array('location_id' => (int) $_POST['location_id']);
                    $results = mc_modify_location($update, $where);
                    do_action('mc_modify_location', $where, $update);
                    if ($results === false) {
                        echo "<div class=\"error\"><p><strong>" . __('Location could not be edited.', 'my-calendar') . "</strong></p></div>";
                    } else {
                        if ($results == 0) {
                            echo "<div class=\"updated error\"><p><strong>" . __('Location was not changed.', 'my-calendar') . "</strong></p></div>";
                        } else {
                            echo "<div class=\"updated\"><p><strong>" . __('Location edited successfully', 'my-calendar') . "</strong></p></div>";
                        }
                    }
                    $cur_loc = (int) $_POST['location_id'];
                    mc_show_location_form('edit', $cur_loc);
                }
            }
        }
    }
    if (isset($_GET['mode']) && $_GET['mode'] != 'edit' || isset($_POST['mode']) && $_POST['mode'] != 'edit' || !isset($_GET['mode']) && !isset($_POST['mode'])) {
        mc_show_location_form('add');
    }
}
function mc_transition_db()
{
    // copy to post types. Don't do this if referencing remote sites.
    if (get_option('mc_remote') != 'true') {
        global $wpdb;
        $results = $wpdb->get_results('SELECT * FROM ' . my_calendar_locations_table(), ARRAY_A);
        $locations = array();
        foreach ($results as $result) {
            $location_id = $result['location_id'];
            unset($result['location_id']);
            $hash = md5(serialize($result));
            $locations[$location_id] = $result;
        }
        $results = $wpdb->get_results('SELECT * FROM ' . my_calendar_categories_table());
        foreach ($results as $category) {
            $term = wp_insert_term($category->category_name, 'mc-event-category');
            if (!is_wp_error($term)) {
                $term_id = $term['term_id'];
                mc_update_category('category_term', $term_id, $category->category_id);
            } else {
                if (isset($term->error_data['term_exists'])) {
                    $term_id = $term->error_data['term_exists'];
                    mc_update_category('category_term', $term_id, $category->category_id);
                }
            }
        }
        $results = $wpdb->get_results('SELECT * FROM ' . my_calendar_table(), ARRAY_A);
        foreach ($results as $event) {
            $post_id = mc_create_event_post($event, $event['event_id']);
            mc_update_event('event_post', $post_id, $event['event_id']);
            // false if not found, id if found.
            $location = mc_check_location_table($event, $locations);
            if ($location) {
                mc_update_event('event_location', $location, $event['event_id']);
            } else {
                if ($event['event_label'] == '' && $event['event_street'] == '' && $event['event_url'] == '' && $event['event_city'] == '' && $event['event_state'] == '' && $event['event_country'] == '') {
                    // don't insert the row if location does not have basic data.
                } else {
                    $add = array('location_label' => $event['event_label'], 'location_street' => $event['event_street'], 'location_street2' => $event['event_street2'], 'location_city' => $event['event_city'], 'location_state' => $event['event_state'], 'location_postcode' => $event['event_postcode'], 'location_region' => $event['event_region'], 'location_country' => $event['event_country'], 'location_url' => $event['event_url'], 'location_longitude' => $event['event_longitude'], 'location_latitude' => $event['event_latitude'], 'location_zoom' => $event['event_zoom'], 'location_phone' => $event['event_phone'], 'location_access' => '');
                    mc_insert_location($add);
                }
                // could add delete routine to allow user to select what location to use for events using a given location.
            }
        }
    }
}