function mc_edit_category_form($view = 'edit', $catID = '')
{
    global $wpdb;
    $dir = plugin_dir_path(__FILE__);
    $url = plugin_dir_url(__FILE__);
    $mcdb = $wpdb;
    $cur_cat = false;
    if ($catID != '') {
        $catID = (int) $catID;
        $sql = "SELECT * FROM " . my_calendar_categories_table() . " WHERE category_id={$catID}";
        $cur_cat = $mcdb->get_row($sql);
    }
    if (is_custom_icon()) {
        $directory = str_replace('/my-calendar', '', $dir) . '/my-calendar-custom/';
        $path = '/my-calendar-custom';
        $iconlist = my_dirlist($directory);
    } else {
        $directory = dirname(__FILE__) . '/images/icons/';
        $path = '/' . dirname(plugin_basename(__FILE__)) . '/images/icons';
        $iconlist = my_dirlist($directory);
    }
    if ($view == 'add') {
        ?>
		<h2><?php 
        _e('Add Category', 'my-calendar');
        ?>
</h2>
	<?php 
    } else {
        ?>
		<h2><?php 
        _e('Edit Category', 'my-calendar');
        ?>
</h2>
	<?php 
    }
    ?>

	<div class="postbox-container jcd-wide">
		<div class="metabox-holder">

			<div class="ui-sortable meta-box-sortables">
				<div class="postbox">
					<h3><?php 
    _e('Category Editor', 'my-calendar');
    ?>
</h3>

					<div class="inside">
						<form id="my-calendar" method="post"
						      action="<?php 
    echo admin_url('admin.php?page=my-calendar-categories');
    ?>
">
							<div><input type="hidden" name="_wpnonce"
							            value="<?php 
    echo wp_create_nonce('my-calendar-nonce');
    ?>
"/></div>
							<?php 
    if ($view == 'add') {
        ?>
								<div>
									<input type="hidden" name="mode" value="add"/>
									<input type="hidden" name="category_id" value=""/>
								</div>
							<?php 
    } else {
        ?>
								<div>
									<input type="hidden" name="mode" value="edit"/>
									<input type="hidden" name="category_id"
									       value="<?php 
        if (is_object($cur_cat)) {
            echo $cur_cat->category_id;
        }
        ?>
"/>
								</div>
							<?php 
    }
    ?>
							<fieldset>
								<legend><?php 
    if ($view == 'add') {
        _e('Add Category', 'my-calendar');
    } else {
        _e('Edit Category', 'my-calendar');
    }
    ?>
</legend><?php 
    if (!empty($cur_cat) && is_object($cur_cat)) {
        $color = strpos($cur_cat->category_color, '#') !== 0 ? '#' : '';
        $color .= $cur_cat->category_color;
    } else {
        $color = '';
    }
    ?>
								<ul>
								<li>
								<label for="cat_name"><?php 
    _e('Category Name', 'my-calendar');
    ?>
</label> <input
									type="text" id="cat_name" name="category_name" class="input" size="30"
									value="<?php 
    if (!empty($cur_cat) && is_object($cur_cat)) {
        echo stripslashes(esc_attr($cur_cat->category_name));
    }
    ?>
"/>
								<label for="cat_color"><?php 
    _e('Color', 'my-calendar');
    ?>
</label> <input
									type="text
									id="cat_color"
									name="category_color"
									class="mc-color-input"
									size="10"
									maxlength="7"
									value="<?php 
    esc_attr_e($color);
    ?>
"/>
								</li>
								<li>
								<label for="cat_icon"><?php 
    _e('Category Icon', 'my-calendar');
    ?>
</label> <select
									name="category_icon" id="cat_icon">
									<?php 
    foreach ($iconlist as $value) {
        $selected = !empty($cur_cat) && is_object($cur_cat) && $cur_cat->category_icon == $value ? " selected='selected'" : '';
        echo "<option value='" . esc_attr($value) . "'{$selected} style='background: url(" . str_replace('my-calendar/', '', esc_url($url . "{$path}/{$value}")) . ") left 50% no-repeat;'>{$value}</option>";
    }
    ?>
								</select>
								</li>
								<li>
									<?php 
    if ($view == 'add') {
        $private_checked = '';
    } else {
        if (!empty($cur_cat) && is_object($cur_cat) && $cur_cat->category_private == 1) {
            $private_checked = ' checked="checked"';
        } else {
            $private_checked = '';
        }
    }
    ?>
									<?php 
    $checked = $view == 'add' ? '' : mc_is_checked('mc_default_category', $cur_cat->category_id, '', true);
    ?>
									<?php 
    $holiday_checked = $view == 'add' ? '' : mc_is_checked('mc_skip_holidays_category', $cur_cat->category_id, '', true);
    ?>
									<input type="checkbox" value="on" name="category_private"
									       id="cat_private"<?php 
    echo $private_checked;
    ?>
 /> <label
										for="cat_private"><?php 
    _e('Private category (logged-in users only)', 'my-calendar');
    ?>
</label>
									<input type="checkbox" value="on" name="mc_default_category"
									       id="mc_default_category"<?php 
    echo $checked;
    ?>
 /> <label
										for="mc_default_category"><?php 
    _e('Default category', 'my-calendar');
    ?>
</label>
									<input type="checkbox" value="on" name="mc_skip_holidays_category"
									       id="mc_shc"<?php 
    echo $holiday_checked;
    ?>
 /> <label
										for="mc_shc"><?php 
    _e('Holiday Category', 'my-calendar');
    ?>
</label>
								</li>
								<?php 
    echo apply_filters('mc_category_fields', '', $cur_cat);
    ?>
							</ul>
							</fieldset>
							<p>
								<input type="submit" name="save" class="button-primary"
								       value="<?php 
    if ($view == 'add') {
        _e('Add Category', 'my-calendar');
    } else {
        _e('Save Changes', 'my-calendar');
    }
    ?>
 &raquo;"/>
							</p>
							<?php 
    do_action('mc_post_category_form', $cur_cat, $view);
    ?>
						</form>
					</div>
				</div>
			</div>
			<?php 
    if ($view == 'edit') {
        ?>
				<p>
					<a href="<?php 
        echo admin_url('admin.php?page=my-calendar-categories');
        ?>
"><?php 
        _e('Add a New Category', 'my-calendar');
        ?>
 &raquo;</a>
				</p>
			<?php 
    }
    ?>
			<div class="ui-sortable meta-box-sortables">
				<div class="postbox">
					<h3><?php 
    _e('Category List', 'my-calendar');
    ?>
</h3>

					<div class="inside">
						<?php 
    mc_manage_categories();
    ?>
					</div>
				</div>
			</div>
		</div>
	</div>
	<?php 
    mc_show_sidebar();
}
function my_category_key($category)
{
    global $wpdb;
    $url = plugin_dir_url(__FILE__);
    $dir = plugin_dir_path(__FILE__);
    $mcdb = $wpdb;
    $category_key = '';
    if (get_option('mc_remote') == 'true' && function_exists('mc_remote_db')) {
        $mcdb = mc_remote_db();
    }
    $cat_details = '';
    $cat_limit = mc_select_category($category, 'all', 'category');
    $sql = "SELECT * FROM " . MY_CALENDAR_CATEGORIES_TABLE . " {$cat_limit} ORDER BY category_name ASC";
    $cat_details = $mcdb->get_results($sql);
    $category_key .= '<div class="category-key">
			<h3>' . __('Categories', 'my-calendar') . "</h3>\n<ul>\n";
    $subpath = is_custom_icon() ? 'my-calendar-custom/' : 'my-calendar/images/icons/';
    $path = str_replace(basename($dir) . '/', '', $url) . $subpath;
    foreach ($cat_details as $cat_detail) {
        $hex = strpos($cat_detail->category_color, '#') !== 0 ? '#' : '';
        $title_class = sanitize_title($cat_detail->category_name);
        if ($cat_detail->category_private == 1) {
            $title_class .= " private";
        }
        $url = add_query_arg('mcat', $cat_detail->category_id, mc_get_current_url());
        if ($cat_detail->category_icon != "" && get_option('mc_hide_icons') != 'true') {
            $category_key .= '<li class="cat_' . $title_class . '"><a href="' . $url . '"><span class="category-color-sample"><img src="' . $path . $cat_detail->category_icon . '" alt="" style="background:' . $hex . $cat_detail->category_color . ';" /></span>' . stripcslashes($cat_detail->category_name) . "</a></li>\n";
        } else {
            $category_key .= '<li class="cat_' . $title_class . '"><a href="' . $url . '"><span class="category-color-sample no-icon" style="background:' . $hex . $cat_detail->category_color . ';"> &nbsp; </span>' . stripcslashes($cat_detail->category_name) . "</a></li>\n";
        }
    }
    if (isset($_GET['mcat'])) {
        $category_key .= "<li><a href='" . mc_get_current_url() . "'>" . __('All Categories', 'my-calendar') . "</a></li>";
    }
    $category_key .= "</ul>\n</div>";
    $category_key = apply_filters('mc_category_key', $category_key, $cat_details);
    return $category_key;
}
function my_category_key($category)
{
    global $wpdb;
    $url = plugin_dir_url(__FILE__);
    $mcdb = $wpdb;
    if (get_option('mc_remote') == 'true' && function_exists('mc_remote_db')) {
        $mcdb = mc_remote_db();
    }
    $key = '';
    $cat_limit = mc_select_category($category, 'all', 'category');
    $sql = "SELECT * FROM " . my_calendar_categories_table() . " {$cat_limit} ORDER BY category_name ASC";
    $categories = $mcdb->get_results($sql);
    $key .= '<div class="category-key">
	<h3>' . __('Categories', 'my-calendar') . "</h3>\n<ul>\n";
    $path = is_custom_icon() ? str_replace('my-calendar', 'my-calendar-custom', $url) : plugins_url('images/icons', __FILE__) . '/';
    foreach ($categories as $cat) {
        $hex = strpos($cat->category_color, '#') !== 0 ? '#' : '';
        $class = sanitize_title($cat->category_name);
        if (isset($_GET['mcat']) && $_GET['mcat'] == $cat->category_id) {
            $class .= " current";
        }
        if ($cat->category_private == 1) {
            $class .= " private";
        }
        $url = mc_build_url(array('mcat' => $cat->category_id), array('mcat'));
        if ($cat->category_icon != "" && get_option('mc_hide_icons') != 'true') {
            $key .= '<li class="cat_' . $class . '"><a href="' . $url . '"><span class="category-color-sample"><img src="' . $path . $cat->category_icon . '" alt="" style="background:' . $hex . $cat->category_color . ';" /></span>' . mc_kses_post(stripcslashes($cat->category_name)) . "</a></li>\n";
        } else {
            $key .= '<li class="cat_' . $class . '"><a href="' . $url . '"><span class="category-color-sample no-icon" style="background:' . $hex . $cat->category_color . ';"> &nbsp; </span>' . mc_kses_post(stripcslashes($cat->category_name)) . "</a></li>\n";
        }
    }
    if (isset($_GET['mcat'])) {
        $key .= "<li class='all-categories'><a href='" . esc_url(remove_query_arg('mcat', mc_get_current_url())) . "'>" . apply_filters('mc_text_all_categories', __('All Categories', 'my-calendar')) . "</a></li>";
    }
    $key .= "</ul>\n</div>";
    $key = apply_filters('mc_category_key', $key, $categories);
    return $key;
}