function my_event_email($details, $event, $type, $time)
{
    if (my_calendar_date_xcomp($event->occur_end, date('Y-m-d', current_time('timestamp')))) {
        $data = mc_create_tags($event);
        // this is just a sample template; you'd want to customize it.
        $details = jd_draw_template($data, '<h2>{title}</h2>{image}');
    }
    return $details;
}
function mc_event_expired($event)
{
    if (my_calendar_date_xcomp($event->occur_end, date('Y-m-d', current_time('timestamp')))) {
        return true;
    }
    return false;
}
/**
 * Test whether an event has an invalid overlap.
 * 
 * @object $data Event object
 *
 * @return string Warning text about problem with event.
 */
function mc_test_occurrence_overlap($data, $return = false)
{
    $warning = '';
    // if this event is single, skip query
    $single_recur = $data->event_recur == 'S' || $data->event_recur == 'S1' ? true : false;
    // if event starts and ends on same day, skip query
    $start_end = $data->event_begin == $data->event_end ? true : false;
    // only run test when an event is set up to recur & starts/ends on different days.
    if (!$single_recur && !$start_end) {
        $check = mc_increment_event($data->event_id, array(), 'test');
        if (my_calendar_date_xcomp($check['occur_begin'], $data->event_end . '' . $data->event_endtime)) {
            $warning = "<div class='error'><span class='problem-icon dashicons dashicons-performance'></span> <p><strong>" . __('Event hidden from public view.', 'my-calendar') . "</strong> " . __('This event ends after the next occurrence begins. Events must end <strong>before</strong> the next occurrence begins.', 'my-calendar') . "</p><p>" . sprintf(__('Event end date: <strong>%s %s</strong>. Next occurrence starts: <strong>%s</strong>', 'my-calendar'), $data->event_end, $data->event_endtime, $check['occur_begin']) . "</p></div>";
            update_post_meta($data->event_post, '_occurrence_overlap', 'false');
        } else {
            delete_post_meta($data->event_post, '_occurrence_overlap');
        }
    }
    if ($return) {
        return $warning;
    } else {
        echo $warning;
    }
}
function mc_event_link($event)
{
    if ($event->event_link_expires == 0) {
        $link = esc_url($event->event_link);
    } else {
        if (my_calendar_date_xcomp($event->occur_end, date('Y-m-d', current_time('timestamp')))) {
            $link = '';
            do_action('mc_event_expired', $event);
        } else {
            $link = esc_url($event->event_link);
        }
    }
    return $link;
}
function mcs_on_sale($discounts)
{
    if (!isset($discounts['begins']) || !isset($discounts['ends']) || !isset($discounts['rate'])) {
        $return = false;
    }
    if ($discounts['begins'] == '') {
        $return = false;
    }
    $end = $discounts['ends'] == '' ? date('Y-m-d', time() + 60 * 60 * 24 * 7) : $discounts['ends'];
    if (function_exists('my_calendar_date_xcomp')) {
        if (my_calendar_date_xcomp($discounts['begins'], date('Y-m-d', time())) && my_calendar_date_xcomp(date('Y-m-d', time()), $end) && (int) $discounts['rate'] > 0) {
            $return = true;
        }
    } else {
        $return = false;
    }
    return $return;
}
function mc_form_fields($data, $mode, $event_id)
{
    global $wpdb, $user_ID;
    $mcdb = $wpdb;
    $has_data = empty($data) ? false : true;
    $warning = '';
    if ($data) {
        if (!($data->event_recur == 'S' || $data->event_recur == 'S1')) {
            $check = mc_increment_event($data->event_id, array(), 'test');
            if (my_calendar_date_xcomp($check['occur_begin'], $data->event_end . '' . $data->event_endtime)) {
                $warning = "<div class='updated'><p>" . __('This event ends after the next occurrence begins. Events must end <strong>before</strong> the next occurrence begins.', 'my-calendar') . "</p><p>" . sprintf(__('Event end date: <strong>%s %s</strong>. Next occurrence starts: <strong>%s</strong>', 'my-calendar'), $data->event_end, $data->event_endtime, $check['occur_begin']) . "</p></div>";
                echo $warning;
            }
        }
    }
    $instance = isset($_GET['date']) ? (int) $_GET['date'] : false;
    if ($instance) {
        $ins = mc_get_instance_data($instance);
        $event_id = $ins->occur_event_id;
        $data = mc_get_event_core($event_id);
    }
    ?>

<div class="postbox-container jcd-wide">
<div class="metabox-holder">
<?php 
    if ($mode == 'add' || $mode == 'copy') {
        $edit_args = '';
    } else {
        $edit_args = "&amp;mode={$mode}&amp;event_id={$event_id}";
        if ($instance) {
            $edit_args .= "&amp;date={$instance}";
        }
    }
    ?>
<form id="my-calendar" method="post" action="<?php 
    echo admin_url('admin.php?page=my-calendar') . $edit_args;
    ?>
">
<div>
	<input type="hidden" name="_wpnonce" value="<?php 
    echo wp_create_nonce('my-calendar-nonce');
    ?>
" />
	<?php 
    if (isset($_GET['ref'])) {
        ?>
		<input type="hidden" name="ref" value="<?php 
        echo esc_url($_GET['ref']);
        ?>
" />
	<?php 
    }
    ?>
	<input type="hidden" name="event_group_id" value="<?php 
    if (!empty($data->event_group_id) && $mode != 'copy') {
        echo $data->event_group_id;
    } else {
        echo mc_group_id();
    }
    ?>
" />
	<input type="hidden" name="event_action" value="<?php 
    echo $mode;
    ?>
" />
	<?php 
    if (!empty($_GET['date'])) {
        ?>
 
		<input type="hidden" name="event_instance" value="<?php 
        echo (int) $_GET['date'];
        ?>
" />
	<?php 
    }
    ?>
	
	<input type="hidden" name="event_id" value="<?php 
    echo (int) $event_id;
    ?>
" />
	<?php 
    if ($mode == 'edit') {
        ?>
		<input type='hidden' name='event_post' value="<?php 
        echo $data->event_post;
        ?>
" />
	<?php 
    }
    ?>
	<input type="hidden" name="event_author" value="<?php 
    if ($mode != 'edit') {
        echo $user_ID;
    } else {
        echo $data->event_author;
    }
    ?>
" />
	<input type="hidden" name="event_nonce_name" value="<?php 
    echo wp_create_nonce('event_nonce');
    ?>
" />
</div>

<div class="ui-sortable meta-box-sortables">
<div class="postbox">
	<?php 
    if (isset($_GET['mode']) && $_GET['mode'] == 'edit') {
        $text = __('Edit Event', 'my-calendar');
        $delete = " &middot; <a href='" . admin_url("admin.php?page=my-calendar-manage&amp;mode=delete&amp;event_id={$data->event_id}") . "' class='delete'>" . __('Delete', 'my-calendar') . "</a>";
    } else {
        $text = __('Add Event', 'my-calendar');
        $delete = '';
    }
    $post_id = $has_data ? $data->event_post : false;
    if ($has_data && !$data->event_post) {
        $array_data = (array) $data;
        $post_id = mc_event_post('add', $array_data, $data->event_id);
    }
    if (apply_filters('mc_use_permalinks', get_option('mc_use_permalinks')) == 'true') {
        $post_link = $post_id ? get_edit_post_link($post_id) : false;
        $text_link = $post_link ? sprintf(" &middot; <a href='%s'>" . __('Edit Event Post', 'my-calendar') . "</a>", $post_link) : '';
    } else {
        $text_link = '';
    }
    ?>
	<h3><?php 
    echo $text;
    ?>
 <span class="alignright"><a href="<?php 
    echo admin_url('admin.php?page=my-calendar-manage');
    ?>
"><?php 
    _e('Manage events', 'my-calendar');
    ?>
</a><?php 
    echo $delete;
    echo $text_link;
    ?>
</span>
</h3>
	<div class="inside">
	<?php 
    if (!empty($_GET['date']) && $data->event_recur != 'S') {
        $event = mc_get_event($instance);
        $date = date_i18n(get_option('mc_date_format'), strtotime($event->occur_begin));
        $message = __("You are editing the <strong>{$date}</strong> instance of this event. Other instances of this event will not be changed.", 'my-calendar');
        //echo "<div><input type='hidden' name='event_instance' value='$instance' /></div>";
        echo "<div class='message updated'><p>{$message}</p></div>";
    } else {
        if (isset($_GET['date']) && empty($_GET['date'])) {
            echo "<div class='message updated'><p>" . __('There was an error acquiring information about this event instance. The ID for this event instance was not provided. <strong>You are editing this entire recurrence set.</strong>', 'my-calendar') . "</p></div>";
        }
    }
    ?>
        <fieldset>
		<legend><?php 
    _e('Event Details', 'my-calendar');
    ?>
</legend>
		<p>
		<label for="e_title"><?php 
    _e('Event Title', 'my-calendar');
    ?>
 <span class='required'><?php 
    _e('(required)', 'my-calendar');
    ?>
</span></label><br /><input type="text" id="e_title" name="event_title" size="50" maxlength="255" value="<?php 
    if ($has_data) {
        echo apply_filters('mc_manage_event_title', stripslashes(esc_attr($data->event_title)), $data);
    }
    ?>
" /><?php 
    if ($mode == 'edit') {
        ?>
			<input type='hidden' name='prev_event_status' value='<?php 
        echo $data->event_approved;
        ?>
' /><?php 
        if (get_option('mc_event_approve') == 'true') {
            if (current_user_can('mc_approve_events')) {
                // Added by Roland P.
                if ($has_data && $data->event_approved == '1') {
                    $checked = " checked=\"checked\"";
                } else {
                    if ($has_data && $data->event_approved == '0') {
                        $checked = "";
                    } else {
                        if (get_option('mc_event_approve') == 'true') {
                            $checked = "checked=\"checked\"";
                        }
                    }
                }
                ?>
					<input type="checkbox" value="1" id="e_approved" name="event_approved" <?php 
                echo $checked;
                ?>
 /> <label for="e_approved"><?php 
                _e('Approve', 'my-calendar');
                ?>
</label><?php 
            } else {
                // case: editing, approval enabled, user cannot approve
                ?>
					<input type="hidden" value="0" name="event_approved" /><?php 
                _e('An administrator must approve your new event.', 'my-calendar');
            }
        } else {
            // Case: editing, approval system is disabled - auto approve
            ?>
	
				<input type="hidden" value="1" name="event_approved" /><?php 
        }
    } else {
        // case: adding new event (if use can, then 1, else 0)
        if (get_option('mc_event_approve') != 'true') {
            $dvalue = 1;
        } else {
            if (current_user_can('mc_approve_events')) {
                $dvalue = 1;
            } else {
                $dvalue = 0;
            }
        }
        ?>
				<input type="hidden" value="<?php 
        echo $dvalue;
        ?>
" name="event_approved" /><?php 
    }
    ?>
		</p>
		<?php 
    if (is_object($data) && $data->event_flagged == 1) {
        ?>
		<div class="error">
		<p>
		<input type="checkbox" value="0" id="e_flagged" name="event_flagged"<?php 
        if ($has_data && $data->event_flagged == '0') {
            echo " checked=\"checked\"";
        } else {
            if ($has_data && $data->event_flagged == '1') {
                echo "";
            }
        }
        ?>
 /> <label for="e_flagged"><?php 
        _e('This event is not spam', 'my-calendar');
        ?>
</label>
		</p>
		</div>
		<?php 
    }
    if (function_exists('jd_doTwitterAPIPost') && current_user_can('wpt_can_tweet')) {
        if (!($mode == 'edit' && $data->event_approved == 1)) {
            ?>
			<p>
			<label for='mc_twitter'><?php 
            _e('Post to Twitter (via WP to Twitter)', 'my-calendar');
            ?>
</label><br />
			<textarea cols='70' rows='2' id='mc_twitter' name='mc_twitter'><?php 
            do_action('mc_twitter_text', $data);
            ?>
</textarea>
			</p><?php 
        }
    }
    mc_show_block('event_desc', $has_data, $data);
    mc_show_block('event_short', $has_data, $data);
    mc_show_block('event_image', $has_data, $data);
    ?>
		<p>
		<label for="e_host"><?php 
    _e('Host', 'my-calendar');
    ?>
</label>
		<select id="e_host" name="event_host">
			<?php 
    // Grab all the categories and list them
    $users = my_calendar_getUsers();
    foreach ($users as $u) {
        $display_name = $u->display_name == '' ? $u->user_nicename : $u->display_name;
        if (is_object($data) && $data->event_host == $u->ID) {
            $selected = ' selected="selected"';
        } else {
            if (is_object($u) && $u->ID == $user_ID && empty($data->event_host)) {
                $selected = ' selected="selected"';
            } else {
                $selected = '';
            }
        }
        echo "<option value='{$u->ID}'{$selected}>{$display_name}</option>\n";
    }
    ?>
		</select>
		</p>			
		<?php 
    mc_show_block('event_category', $has_data, $data);
    mc_show_block('event_link', $has_data, $data);
    echo apply_filters('mc_event_details', '', $has_data, $data, 'admin');
    ?>
							
	</fieldset>
</div>
</div>
</div>

<div class="ui-sortable meta-box-sortables">
<div class="postbox">
<h3><?php 
    _e('Date and Time', 'my-calendar');
    ?>
</h3>
<div class="inside">
			<?php 
    if (is_object($data)) {
        // information for rewriting recurring data
        ?>
			<input type="hidden" name="prev_event_begin" value="<?php 
        echo $data->event_begin;
        ?>
" />
			<input type="hidden" name="prev_event_time" value="<?php 
        echo $data->event_time;
        ?>
" />
			<input type="hidden" name="prev_event_end" value="<?php 
        echo $data->event_end;
        ?>
" />
			<input type="hidden" name="prev_event_endtime" value="<?php 
        echo $data->event_endtime;
        ?>
" />			
			<?php 
    }
    ?>
			<fieldset><legend><?php 
    _e('Event Date and Time', 'my-calendar');
    ?>
</legend>
			<div id="e_schedule">
			<div id="event1" class="clonedInput" aria-live="polite">
			<?php 
    echo apply_filters('mc_datetime_inputs', '', $has_data, $data, 'admin');
    ?>
			</div>
			<?php 
    if ($mode != 'edit') {
        ?>
			
			<p id="event_span">
				<input type="checkbox" value="1" id="e_span" name="event_span"<?php 
        if ($has_data && $data->event_span == '1') {
            echo " checked=\"checked\"";
        } else {
            if ($has_data && $data->event_span == '0') {
                echo "";
            } else {
                if (get_option('mc_event_span') == 'true') {
                    echo " checked=\"checked\"";
                }
            }
        }
        ?>
 /> <label for="e_span"><?php 
        _e('This is a multi-day event.', 'my-calendar');
        ?>
</label>
			</p>
			<p class="note"><em><?php 
        _e('Enter start and end dates/times for each occurrence of the event.', 'my-calendar');
        ?>
</em></p>
			<div>
				<input type="button" id="add_field" value="<?php 
        _e('Add another occurrence', 'my-calendar');
        ?>
" class="button" />
				<input type="button" id="del_field" value="<?php 
        _e('Remove last occurrence', 'my-calendar');
        ?>
" class="button" />
			</div>
			<?php 
    } else {
        ?>
			<div id='mc-accordion'>
			<?php 
        if ($data->event_recur != 'S') {
            ?>
			<h4><?php 
            _e('Scheduled dates for this event', 'my-calendar');
            ?>
</h4>
			<div>
				<?php 
            _e('Editing a single date of an event changes only that date. Editing the root event changes all events in the series.', 'my-calendar');
            ?>
			
				<ul class="columns">
					<?php 
            if (isset($_GET['date'])) {
                $date = (int) $_GET['date'];
            } else {
                $date = false;
            }
            ?>
					<?php 
            echo mc_instance_list($data->event_id, $date);
            ?>
				</ul>
			</div>
			<?php 
        }
        ?>
			<?php 
        if ($data->event_group_id != 0) {
            ?>
				<?php 
            $edit_group_url = admin_url('admin.php?page=my-calendar-groups&mode=edit&event_id=' . $data->event_id . '&group_id=' . $data->event_group_id);
            ?>
			
				<h4><?php 
            _e('Related Events:', 'my-calendar');
            ?>
 (<a href='<?php 
            echo $edit_group_url;
            ?>
'><?php 
            _e('Edit group', 'my-calendar');
            ?>
</a>)</h4>
				<div>
					<ul class="columns">
						<?php 
            mc_related_events($data->event_group_id);
            ?>
					</ul>
				</div>
				<?php 
        }
        ?>
			</div>				
			<?php 
    }
    ?>
			</div>
			</fieldset>			
		</div>
		</div>
		</div>
		<?php 
    mc_show_block('event_recur', $has_data, $data);
    mc_show_block('event_access', $has_data, $data);
    mc_show_block('event_open', $has_data, $data);
    if (mc_show_edit_block('event_location') || mc_show_edit_block('event_location_dropdown')) {
        ?>

			<div class="ui-sortable meta-box-sortables">
			<div class="postbox">
			<h3><?php 
        _e('Event Location', 'my-calendar');
        ?>
</h3>
			<div class="inside location_form">
				<fieldset>
				<legend><?php 
        _e('Event Location', 'my-calendar');
        ?>
</legend><?php 
    }
    if (mc_show_edit_block('event_location_dropdown')) {
        $locs = $mcdb->get_results("SELECT location_id,location_label FROM " . my_calendar_locations_table() . " ORDER BY location_label ASC");
        if (!empty($locs)) {
            ?>
				
				<p>
					<label for="l_preset"><?php 
            _e('Choose a preset location:', 'my-calendar');
            ?>
</label> <select name="location_preset" id="l_preset">
					<option value="none"> -- </option><?php 
            foreach ($locs as $loc) {
                echo "<option value=\"" . $loc->location_id . "\">" . stripslashes($loc->location_label) . "</option>";
            }
            ?>
				</select>
				</p><?php 
        } else {
            ?>
				<input type="hidden" name="location_preset" value="none" />
				<p><a href="<?php 
            echo admin_url('admin.php?page=my-calendar-locations');
            ?>
"><?php 
            _e('Add recurring locations for later use.', 'my-calendar');
            ?>
</a></p><?php 
        }
    } else {
        ?>
			<input type="hidden" name="location_preset" value="none" /><?php 
    }
    mc_show_block('event_location', $has_data, $data);
    if (mc_show_edit_block('event_location') || mc_show_edit_block('event_location_dropdown')) {
        ?>
					</fieldset>
				</div>
			</div>
			</div><?php 
    }
    if (mc_show_edit_block('event_specials')) {
        ?>
			
			<div class="ui-sortable meta-box-sortables">
				<div class="postbox">
				<h3><?php 
        _e('Special scheduling options', 'my-calendar');
        ?>
</h3>
					<div class="inside">		
						<fieldset>
						<legend><?php 
        _e('Special Options', 'my-calendar');
        ?>
</legend>
						<p>
						<label for="e_holiday"><?php 
        _e('Cancel this event if it occurs on a date with an event in the Holidays category', 'my-calendar');
        ?>
</label> <input type="checkbox" value="true" id="e_holiday" name="event_holiday"<?php 
        if ($has_data && $data->event_holiday == '1') {
            echo " checked=\"checked\"";
        } else {
            if ($has_data && $data->event_holiday == '0') {
                echo "";
            } else {
                if (get_option('mc_skip_holidays') == 'true') {
                    echo " checked=\"checked\"";
                }
            }
        }
        ?>
 />
						</p>
						<p>
						<label for="e_fifth_week"><?php 
        _e('If this event recurs, and falls on the 5th week of the month in a month with only four weeks, move it back one week.', 'my-calendar');
        ?>
</label> <input type="checkbox" value="true" id="e_fifth_week" name="event_fifth_week"<?php 
        if ($has_data && $data->event_fifth_week == '1') {
            echo " checked=\"checked\"";
        } else {
            if ($has_data && $data->event_fifth_week == '0') {
                echo "";
            } else {
                if (get_option('mc_no_fifth_week') == 'true') {
                    echo " checked=\"checked\"";
                }
            }
        }
        ?>
 />
						</p>
						</fieldset>
					</div>
				</div>
			</div><?php 
    } else {
        ?>
				<div>
				<input type="hidden" name="event_holiday" value="true"<?php 
        if (get_option('mc_skip_holidays') == 'true') {
            echo " checked=\"checked\"";
        }
        ?>
 />
				<input type="hidden" name="event_fifth_week" value="true"<?php 
        if (get_option('mc_no_fifth_week') == 'true') {
            echo " checked=\"checked\"";
        }
        ?>
/>
				</div><?php 
    }
    ?>
		<p>
			<input type="submit" name="save" class="button-primary" value="<?php 
    _e('Save Event', 'my-calendar');
    ?>
" />
		</p>
	</form>	
	</div>
	</div><?php 
}