function my_calendar_save($action, $output, $event_id = false)
{
    global $wpdb, $event_author;
    $mcdb = $wpdb;
    $proceed = $output[0];
    $message = '';
    $formats = array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%f', '%f');
    if (($action == 'add' || $action == 'copy') && $proceed == true) {
        $add = $output[2];
        // add format here
        $add = apply_filters('mc_before_save_insert', $add);
        // this db write takes most of the processing time for this process.
        $result = $mcdb->insert(my_calendar_table(), $add, $formats);
        $event_id = $mcdb->insert_id;
        mc_increment_event($event_id);
        if (!$result) {
            $message = "<div class='error notice'><p><strong>" . __('Error', 'my-calendar') . ":</strong> " . __('I\'m sorry! I couldn\'t add that event to the database.', 'my-calendar') . "</p></div>";
        } else {
            // do an action using the $action and processed event data
            $data = $add;
            $event_error = '';
            do_action('mc_save_event', $action, $data, $event_id, $result);
            // Call mail function
            if (get_option('mc_event_mail') == 'true') {
                $event = mc_get_first_event($event_id);
                // insert_id is last occurrence inserted in the db
                my_calendar_send_email($event);
            }
            if ($add['event_approved'] == 0) {
                $message = "<div class='updated notice'><p>" . __('Event saved. An administrator will review and approve your event.', 'my-calendar') . "</p></div>";
            } else {
                if (function_exists('jd_doTwitterAPIPost') && isset($_POST['mc_twitter']) && trim($_POST['mc_twitter']) != '') {
                    jd_doTwitterAPIPost(stripslashes($_POST['mc_twitter']));
                }
                if (get_option('mc_uri') != '') {
                    $event_ids = mc_get_occurrences($event_id);
                    //$event_link = mc_build_url( array( 'mc_id' => $event_ids[0]->occur_id ), array( 'page' ), get_option( 'mc_uri' ) );
                    //$event_link = add_query_arg( 'mc_id', $event_ids[0]->occur_id, get_option( 'mc_uri' ) );
                    $event_link = mc_get_details_link($event_ids[0]->occur_id);
                    $event_error = mc_error_check($event_ids[0]->occur_event_id);
                } else {
                    $event_link = false;
                }
                if ($event_error != '') {
                    $message = $event_error;
                } else {
                    $message = "<div class='updated notice'><p>" . __('Event added. It will now show on the calendar.', 'my-calendar');
                    if ($event_link !== false) {
                        $message .= sprintf(__(' <a href="%s">View Event</a>', 'my-calendar'), $event_link);
                    }
                    $message .= "</p></div>";
                }
            }
            mc_delete_cache();
        }
    }
    if ($action == 'edit' && $proceed == true) {
        $current_user = wp_get_current_user();
        $result = true;
        $url = get_option('mc_uri') != '' && !is_numeric(get_option('mc_uri')) ? '' . sprintf(__('View <a href="%s">your calendar</a>.', 'my-calendar'), get_option('mc_uri')) : '';
        if (mc_can_edit_event($event_id)) {
            $update = $output[2];
            $update = apply_filters('mc_before_save_update', $update, $event_id);
            $endtime = date("H:i:00", strtotime($update['event_endtime']));
            $date_changed = $update['event_begin'] != $_POST['prev_event_begin'] || date("H:i:00", strtotime($update['event_time'])) != $_POST['prev_event_time'] || $update['event_end'] != $_POST['prev_event_end'] || $endtime != $_POST['prev_event_endtime'] && ($_POST['prev_event_endtime'] != '' && $endtime != '23:59:59') ? true : false;
            if (isset($_POST['event_instance'])) {
                $is_changed = mc_compare($update, $event_id);
                // compares the information sent to the information saved for a given event.
                $event_instance = (int) $_POST['event_instance'];
                if ($is_changed) {
                    // if changed, create new event, match group id, update instance to reflect event connection, same group id.
                    // if group ID == 0, need to add group ID to both records.
                    // if a single instance is edited, it should *not* inherit the recurring settings from parent.
                    $update['event_recur'] = 'S1';
                    if ($update['event_group_id'] == 0) {
                        $update['event_group_id'] = $event_id;
                        mc_update_data($event_id, 'event_group_id', $event_id);
                    }
                    $mcdb->insert(my_calendar_table(), $update, $formats);
                    $new_event = $mcdb->insert_id;
                    // need to get this variable into URL for form submit
                    $result = mc_update_instance($event_instance, $new_event, $update);
                    mc_delete_cache();
                } else {
                    if ($update['event_begin'][0] == $_POST['prev_event_begin'] && $update['event_end'][0] == $_POST['prev_event_end']) {
                        // There were no changes at all.
                    } else {
                        $result = mc_update_instance($event_instance, $event_id, $update);
                        // Only dates were changed
                        $message = "<div class='updated notice'><p>" . __('Date/time information for this event has been updated.', 'my-calendar') . " {$url}</p></div>";
                        mc_delete_cache();
                    }
                }
            } else {
                $result = $mcdb->update(my_calendar_table(), $update, array('event_id' => $event_id), $formats, '%d');
                $recur_changed = $update['event_repeats'] != $_POST['prev_event_repeats'] || $update['event_recur'] != $_POST['prev_event_recur'] ? true : false;
                if ($date_changed || $recur_changed) {
                    // TODO: if date or recur changed, do generation of new instances, then iterate over existing occurrences
                    // to update & delete remaining
                    mc_delete_instances($event_id);
                    mc_increment_event($event_id);
                    mc_delete_cache();
                }
            }
            $data = $update;
            do_action('mc_save_event', $action, $data, $event_id, $result);
            if ($result === false) {
                $message = "<div class='error'><p><strong>" . __('Error', 'my-calendar') . ":</strong>" . __('Your event was not updated.', 'my-calendar') . " {$url}</p></div>";
            } else {
                // do an action using the $action and processed event data
                $event_approved = isset($_POST['event_approved']) ? intval($_POST['event_approved']) : 0;
                do_action('mc_transition_event', (int) $_POST['prev_event_status'], $event_approved);
                $message = "<div class='updated'><p>" . __('Event updated successfully', 'my-calendar') . ". {$url}</p></div>";
                mc_delete_cache();
            }
        } else {
            $message = "<div class='error'><p><strong>" . __('You do not have sufficient permissions to edit that event.', 'my-calendar') . "</strong></p></div>";
        }
    }
    $message = $message . "\n" . $output[3];
    return array('event_id' => $event_id, 'message' => $message);
}
function my_calendar_save($action, $output, $event_id = false)
{
    global $wpdb, $event_author;
    $mcdb = $wpdb;
    $proceed = $output[0];
    $message = '';
    $formats = array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%f', '%f');
    if (($action == 'add' || $action == 'copy') && $proceed == true) {
        $add = $output[2];
        // add format here
        $add = apply_filters('mc_before_save_insert', $add);
        $result = $mcdb->insert(my_calendar_table(), $add, $formats);
        $event_id = $mcdb->insert_id;
        mc_increment_event($event_id);
        if (!$result) {
            $message = "<div class='error notice'><p><strong>" . __('Error', 'my-calendar') . ":</strong> " . __('I\'m sorry! I couldn\'t add that event to the database.', 'my-calendar') . "</p></div>";
        } else {
            // do an action using the $action and processed event data
            $data = $add;
            do_action('mc_save_event', $action, $data, $event_id, $result);
            // Call mail function
            if (get_option('mc_event_mail') == 'true') {
                $event = mc_get_first_event($event_id);
                // insert_id is last occurrence inserted in the db
                my_calendar_send_email($event);
            }
            if ($add['event_approved'] == 0) {
                $message = "<div class='updated notice'><p>" . __('Event saved. An administrator will review and approve your event.', 'my-calendar') . "</p></div>";
            } else {
                if (function_exists('jd_doTwitterAPIPost') && isset($_POST['mc_twitter']) && trim($_POST['mc_twitter']) != '') {
                    jd_doTwitterAPIPost(stripslashes($_POST['mc_twitter']));
                }
                if (get_option('mc_uri') != '') {
                    $event_ids = mc_get_occurrences($event_id);
                    $event_link = mc_build_url(array('mc_id' => $event_ids[0]->occur_id), array('page'), get_option('mc_uri'));
                } else {
                    $event_link = false;
                }
                $message = "<div class='updated notice'><p>" . __('Event added. It will now show on the calendar.', 'my-calendar');
                if ($event_link !== false) {
                    $message .= sprintf(__(' <a href="%s">View Event</a>', 'my-calendar'), $event_link);
                }
                $message .= "</p></div>";
            }
            mc_delete_cache();
        }
    }
    if ($action == 'edit' && $proceed == true) {
        $result = true;
        $url = get_option('mc_uri') != '' && !is_numeric(get_option('mc_uri')) ? '' . sprintf(__('View <a href="%s">your calendar</a>.', 'my-calendar'), get_option('mc_uri')) : '';
        $event_author = (int) $_POST['event_author'];
        if (mc_can_edit_event($event_author)) {
            $update = $output[2];
            $update = apply_filters('mc_before_save_update', $update, $event_id);
            $date_changed = $update['event_begin'] != $_POST['prev_event_begin'] || date("H:i:00", strtotime($update['event_time'])) != $_POST['prev_event_time'] || $update['event_end'] != $_POST['prev_event_end'] || date("H:i:00", strtotime($update['event_endtime'])) != $_POST['prev_event_endtime'] && ($_POST['prev_event_endtime'] != '' && date("H:i:00", strtotime($update['event_endtime'])) != '00:00:00') ? true : false;
            if (isset($_POST['event_instance'])) {
                $is_changed = mc_compare($update, $event_id);
                // compares the information sent to the information saved for a given event.
                $event_instance = (int) $_POST['event_instance'];
                if ($is_changed) {
                    // if changed, create new event, match group id, update instance to reflect event connection, same group id.
                    // if group ID == 0, need to add group ID to both records.
                    if ($update['event_group_id'] == 0) {
                        $update['event_group_id'] = $event_id;
                        mc_update_data($event_id, 'event_group_id', $event_id);
                    }
                    $mcdb->insert(my_calendar_table(), $update, $formats);
                    $new_event = $mcdb->insert_id;
                    // need to get this variable into URL for form submit
                    $result = mc_update_instance($event_instance, $new_event, $update);
                    mc_delete_cache();
                } else {
                    if ($update['event_begin'][0] == $_POST['prev_event_begin'] && $update['event_end'][0] == $_POST['prev_event_end']) {
                        // There were no changes at all.
                    } else {
                        $result = mc_update_instance($event_instance, $event_id, $update);
                        // Only dates were changed
                        $message = "<div class='updated notice'><p>" . __('Date/time information for this event has been updated.', 'my-calendar') . "{$url}</p></div>";
                        mc_delete_cache();
                    }
                }
            } else {
                $result = $mcdb->update(my_calendar_table(), $update, array('event_id' => $event_id), $formats, '%d');
                $recur_changed = $update['event_repeats'] != $_POST['prev_event_repeats'] || $update['event_recur'] != $_POST['prev_event_recur'] ? true : false;
                if ($date_changed || $recur_changed) {
                    mc_delete_instances($event_id);
                    mc_increment_event($event_id);
                    mc_delete_cache();
                }
            }
            $data = $update;
            do_action('mc_save_event', $action, $data, $event_id, $result);
            if ($result === false) {
                $message = "<div class='error'><p><strong>" . __('Error', 'my-calendar') . ":</strong>" . __('Your event was not updated.', 'my-calendar') . "{$url}</p></div>";
            } else {
                // do an action using the $action and processed event data
                do_action('mc_transition_event', (int) $_POST['prev_event_status'], (int) $_POST['event_approved']);
                $message = "<div class='updated'><p>" . __('Event updated successfully', 'my-calendar') . ".{$url}</p></div>";
                mc_delete_cache();
            }
        } else {
            $message = "<div class='error'><p><strong>" . __('You do not have sufficient permissions to edit that event.', 'my-calendar') . "</strong></p></div>";
        }
    }
    if ($action == 'delete') {
        // Deal with deleting an event from the database
        if (empty($event_id)) {
            $message = "<div class='error'><p><strong>" . __('Error', 'my-calendar') . ":</strong>" . __("You can't delete an event if you haven't submitted an event id", 'my-calendar') . "</p></div>";
        } else {
            $post_id = mc_get_data('event_post', $event_id);
            if (empty($_POST['event_instance'])) {
                $sql = "DELETE FROM " . my_calendar_table() . " WHERE event_id='" . (int) $event_id . "'";
                $delete_occurrences = "DELETE FROM " . my_calendar_event_table() . " WHERE occur_event_id = " . (int) $event_id;
                $mcdb->query($delete_occurrences);
                $mcdb->query($sql);
                $sql = "SELECT event_id FROM " . my_calendar_table() . " WHERE event_id='" . (int) $event_id . "'";
                $result = $mcdb->get_results($sql);
            } else {
                $delete = "DELETE FROM " . my_calendar_event_table() . " WHERE occur_id = " . (int) $_POST['event_instance'];
                $result = $mcdb->get_results($delete);
            }
            if (empty($result) || empty($result[0]->event_id)) {
                mc_delete_cache();
                // do an action using the event_id
                do_action('mc_delete_event', $event_id, $post_id);
                $message = "<div class='updated'><p>" . __('Event deleted successfully', 'my-calendar') . "</p></div>";
            } else {
                $message = "<div class='error'><p><strong>" . __('Error', 'my-calendar') . ":</strong>" . __('Despite issuing a request to delete, the event still remains in the database. Please investigate.', 'my-calendar') . "</p></div>";
            }
        }
    }
    $message = $message . "\n" . $output[3];
    return array('event_id' => $event_id, 'message' => $message);
}