<?php
/*
Plugin Name: Main categories
Plugin URI: http://rudd-o.com/projects/wp-list-main-cats/
Description: Provides a template function to display main categories and a management interface for them
Version: 1.0.0
Author: Rudd-O
Author URI: http://rudd-o.com/

derivative work of Scott Merrill
copyright (c) 2005 Scott Merrill (skippy@skippy.net) and Manuel Amador (rudd-o@rudd-o.com)
Released under the terms of the GNU GPL
*/

function wp_list_main_cats($args = '') {

	global $wpdb;
	$exclusions = " ";
	$maincats = main_categories();
	$allcats = $wpdb->get_results("SELECT cat_ID FROM $wpdb->categories;");
	foreach ($allcats as $cat) {
		if (!$maincats[$cat->cat_ID]) $exclusions .= "{$cat->cat_ID} ";
	}

	if ($args) $args .= "&";
	$args .= "exclude=$exclusions";

	return wp_list_cats($args);

}

function wp_list_secondary_cats($args = '') {

	global $wpdb;
	$exclusions = " ";
	$maincats = main_categories();
	$allcats = $wpdb->get_results("SELECT cat_ID FROM $wpdb->categories;");
	foreach ($allcats as $cat) {
		if ($maincats[$cat->cat_ID]) $exclusions .= "{$cat->cat_ID} ";
	}

	if ($args) $args .= "&";
	$args .= "exclude=$exclusions";

	return wp_list_cats($args);

}


function main_categories() {
	$option = get_settings('maincats');
	if (!$option) $option = array();
	return $option;
}

function post_has_main_category() { /* returns the cat name (or none) */
	global $post,$wpdb;
	$cats = $wpdb->get_col("SELECT category_id FROM $wpdb->post2cat WHERE post_id = $post->ID");
	if (!$cats) {
		return false;
	}
	foreach ($cats as $cat) {
		if (category_is_main($cat)) {
			return true;
		}
	}
	return false;
}

function category_is_main($id) {
	if (!is_numeric($id)) return;
	$id = intval($id);
	$option = main_categories();
	return array_key_exists($id,$option) && $option[$id];
}

function main_cats_add_category($id) {
	if (!is_numeric($id)) return;
	$id = intval($id);
	$option = main_categories();
	$option[$id] = true;
	update_option("maincats",$option);
}

function main_cats_remove_category($id) {
	if (!is_numeric($id)) return;
	$id = intval($id);
	$option = main_categories();
	$option[$id] = false;
	update_option("maincats",$option);
}

function main_cats_admin_js() {
if (strstr($_SERVER["SCRIPT_NAME"],"categories.php") === false) return;
if ( !current_user_can('manage_categories') ) return;
?>
<script type="text/javascript" language="javascript">

function isMainCat(t) {
	var maincats_array = new Array (<?php
		$cats = main_categories();
		foreach ($cats as $key=>$in) { if ($in) print "'" . $key . "'" . ","; }
		print "'-1'";
	?>);
	var i; for (i = 0; i < maincats_array.length; i++) {
		if (t == maincats_array[i]) return true;
	} return false;
}

function mainCatsToggle(id) {
// 	alert(id);
	var checkbox = document.getElementById('maincats-'+id.toString());
	if (!checkbox) return
	if (!checkbox.checked) {
		window.location = "edit.php?page=wp-list-main-cats/wp-list-main-cats-ajax.php&removecat="+id.toString();
	} else {
		window.location = "edit.php?page=wp-list-main-cats/wp-list-main-cats-ajax.php&addcat="+id.toString();
	}
}

function addMainCatsColumn(table) {
	var rows = table.getElementsByTagName('tr');
	if (rows.length < 1) return;
	var heading = document.createElement('th');
	heading.appendChild(document.createTextNode('Main'));
	rows.item(0).appendChild(heading);
	var i;
	for (i = 1; i < rows.length; i++) {
		var item = rows.item(i);
		var tdid = item.firstChild.firstChild.nodeValue;
		var node = document.createElement('td');
		var tinput = document.createElement('input');
		tinput.setAttribute('id','maincats-'+tdid);
		if (isMainCat(tdid)) tinput.checked = true;
		tinput.setAttribute('onclick','mainCatsToggle(' + tdid + ');');
		node.setAttribute("align","center");
		tinput.setAttribute("type","checkbox");
		node.appendChild(tinput);
		item.appendChild(node);
	}
}

var t = document.getElementById('the-list-x'); if (t) addMainCatsColumn(t);
</script>
<?php
}

add_action("admin_footer","main_cats_admin_js");

/* widgets */

function widget_main_categories($args) {
	extract($args);
	$options = get_option('widget_main_categories');
	$c = $options['count'] ? '1' : '0';
	$h = $options['hierarchical'] ? '1' : '0';
	$title = empty($options['title']) ? __('Main categories') : $options['title'];
?>
		<?php echo $before_widget; ?>
			<?php echo $before_title . $title . $after_title; ?>
			<ul>
			<?php wp_list_main_cats("sort_column=name&optioncount=$c&hierarchical=$h"); ?>
			</ul>
		<?php echo $after_widget; ?>
<?php
}

function widget_main_categories_control() {
	$options = $newoptions = get_option('widget_main_categories');
	if ( $_POST['main-categories-submit'] ) {
		$newoptions['count'] = isset($_POST['main-categories-count']);
		$newoptions['hierarchical'] = isset($_POST['main-categories-hierarchical']);
		$newoptions['title'] = strip_tags(stripslashes($_POST['main-categories-title']));
	}
	if ( $options != $newoptions ) {
		$options = $newoptions;
		update_option('widget_main_categories', $options);
	}
	$count = $options['count'] ? 'checked="checked"' : '';
	$hierarchical = $options['hierarchical'] ? 'checked="checked"' : '';
	$title = wp_specialchars($options['title']);
?>
			<p><label for="main-categories-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="main-categories-title" name="main-categories-title" type="text" value="<?php echo $title; ?>" /></label></p>
			<p style="text-align:right;margin-right:40px;"><label for="main-categories-count">Show post counts <input class="checkbox" type="checkbox" <?php echo $count; ?> id="main-categories-count" name="main-categories-count" /></label></p>
			<p style="text-align:right;margin-right:40px;"><label for="main-categories-hierarchical" style="text-align:right;">Show hierarchy <input class="checkbox" type="checkbox" <?php echo $hierarchical; ?> id="main-categories-hierarchical" name="main-categories-hierarchical" /></label></p>
			<input type="hidden" id="main-categories-submit" name="main-categories-submit" value="1" />
<?php
}


function widget_secondary_categories($args) {
	extract($args);
	$options = get_option('widget_secondary_categories');
	$c = $options['count'] ? '1' : '0';
	$h = $options['hierarchical'] ? '1' : '0';
	$title = empty($options['title']) ? __('Secondary categories') : $options['title'];
?>
		<?php echo $before_widget; ?>
			<?php echo $before_title . $title . $after_title; ?>
			<ul>
			<?php wp_list_secondary_cats("sort_column=name&optioncount=$c&hierarchical=$h"); ?>
			</ul>
		<?php echo $after_widget; ?>
<?php
}

function widget_secondary_categories_control() {
	$options = $newoptions = get_option('widget_secondary_categories');
	if ( $_POST['secondary-categories-submit'] ) {
		$newoptions['count'] = isset($_POST['secondary-categories-count']);
		$newoptions['hierarchical'] = isset($_POST['secondary-categories-hierarchical']);
		$newoptions['title'] = strip_tags(stripslashes($_POST['secondary-categories-title']));
	}
	if ( $options != $newoptions ) {
		$options = $newoptions;
		update_option('widget_secondary_categories', $options);
	}
	$count = $options['count'] ? 'checked="checked"' : '';
	$hierarchical = $options['hierarchical'] ? 'checked="checked"' : '';
	$title = wp_specialchars($options['title']);
?>
			<p><label for="secondary-categories-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="secondary-categories-title" name="secondary-categories-title" type="text" value="<?php echo $title; ?>" /></label></p>
			<p style="text-align:right;margin-right:40px;"><label for="secondary-categories-count">Show post counts <input class="checkbox" type="checkbox" <?php echo $count; ?> id="secondary-categories-count" name="secondary-categories-count" /></label></p>
			<p style="text-align:right;margin-right:40px;"><label for="secondary-categories-hierarchical" style="text-align:right;">Show hierarchy <input class="checkbox" type="checkbox" <?php echo $hierarchical; ?> id="secondary-categories-hierarchical" name="secondary-categories-hierarchical" /></label></p>
			<input type="hidden" id="secondary-categories-submit" name="secondary-categories-submit" value="1" />
<?php
}

register_sidebar_widget('Main categories', 'widget_main_categories');
register_widget_control('Main categories', 'widget_main_categories_control', 300, 150);
register_sidebar_widget('Secondary categories', 'widget_secondary_categories');
register_widget_control('Secondary categories', 'widget_secondary_categories_control', 300, 150);



?>
