/**
 * This functions inserts new event data for a post of event type.
 * Optionally it can delete existing occurrences / data before inserting new ones.
 * It creates (or validates) the event before inserting. If there is an error, nothing
 * is deleted. Post_id must be supplied. Returns the created and inserted event as an object.
 
 * @since 1.1.0
 *
 * @param array $event_data - array of data to be used by EO_Event::create() or EO_Event::createFromObject()
 * @param int $post_id  the ID of the post for which the event is being inserted.
 * @param bool $delete - if true, deletes existing event-data before inserting new ones.
 * @return EO_Event $event - the event object
 */
 function insertEvent($event_data = array(), $post_id = null, $delete = false)
 {
     if (!current_user_can('edit_events')) {
         wp_die(__('You do not have sufficient permissions to create events'));
     }
     if (empty($event_data) || empty($post_id)) {
         wp_die(__('Inserting event error: Event data and associated post id must be supplied.'));
     }
     $post = get_post($post_id);
     if (empty($post) || $post->post_type != 'event') {
         wp_die(__('Event Organiser error: Invalid post'));
     }
     //First of all 'create' the event - this performs necessary validation checks and populates the event object
     $event = new EO_Event($post_id);
     if (!empty($event_data['dateObjects'])) {
         $result = $event->createFromObjects($event_data);
     } else {
         $result = $event->create($event_data);
     }
     if ($result) {
         if ($delete) {
             eventorganiser_event_delete($post_id);
         }
         $event->insert($post_id);
     } else {
         return false;
     }
     return $event;
 }
/**
 * Saves the event data posted from the event metabox.
 * Hooked to the 'save_post' action
 * 
 * @since 1.0.0
 *
 * @param int $post_id the event post ID
 * @return int $post_id the event post ID
 */
function eventorganiser_details_save($post_id)
{
    global $wpdb, $eventorganiser_events_table;
    //make sure data came from our meta box
    if (!isset($_POST['_eononce']) || !wp_verify_nonce($_POST['_eononce'], 'eventorganiser_event_update_' . $post_id)) {
        return;
    }
    // verify this is not an auto save routine.
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }
    //authentication checks
    if (!current_user_can('edit_event', $post_id)) {
        return $post_id;
    }
    $raw_data = isset($_POST['eo_input']) ? $_POST['eo_input'] : array();
    //Update venue
    $raw_data['Venue'] = !empty($raw_data['event-venue']) ? intval($raw_data['event-venue']) : null;
    $r = wp_set_post_terms($post_id, array($raw_data['Venue']), 'event-venue', false);
    //TODO Reoccurrence details wil be sotred as post meta - leaving just dates/occurrence/IDs
    //Check if there is existing event data.
    $event = new EO_Event($post_id);
    /*
     * If event data exists, we may have to delete the occurrances and replace them with new ones.
     * First we check if this is necessary. If not, we just update the data and exit.
     */
    $delete_existing = false;
    if ($event->exists) {
        if (isset($raw_data['schedule']) && $raw_data['schedule'] == 'once' && $event->is_schedule('once')) {
            //We are updating a single event (and it is still a one time event), can just update all data easily
            $event->create($raw_data);
            $event_input = array('post_id' => $post_id, 'StartDate' => $event->start->format('Y-m-d'), 'StartTime' => $event->start->format('H:i:s'), 'EndDate' => $event->end->format('Y-m-d'), 'FinishTime' => $event->end->format('H:i:s'), 'Venue' => $event->venue, 'event_schedule' => $event->schedule, 'event_schedule_meta' => $event->meta, 'event_frequency' => $event->frequency, 'event_occurrence' => 0, 'event_allday' => $event->allday, 'reoccurrence_start' => $event->schedule_start->format('Y-m-d'), 'reoccurrence_end' => $event->schedule_end->format('Y-m-d'));
            $upd = $wpdb->update($eventorganiser_events_table, $event_input, array('post_id' => $post_id));
            return;
        } elseif (!$event->is_schedule('once')) {
            //Event was reoccurring
            //If 'edit reocurrences' is checked we need to replace reoccurrences
            if (isset($raw_data['AlterRe']) && $raw_data['AlterRe'] == 'yes') {
                $delete_existing = true;
            }
        } else {
            //Was a one-time event, is now a reoccurring event - delete event.
            $delete_existing = true;
        }
    }
    //Populate event data from raw input and inserts (after deleting existing occurrences, if necessary)
    $event->insertEvent($raw_data, $post_id, $delete_existing);
    do_action('eventorganiser_save_event', $post_id);
    return $post_id;
}