<?php 
/**
 * The extra post info function.
 * 
 * @Dependancy functions.php
 * @package WordPress
 * @subpackage evol
 * @since eVol 1.0.0
 */
?>
<?php
add_action('admin_menu', 'evol_add_custom_box');

/* Use the save_post action to do something with the data entered */
add_action('save_post', 'evol_save_postdata');

/* Adds a custom section to the "advanced" Post and Page edit screens */
function evol_add_custom_box() {

	if( function_exists( 'add_meta_box' )) {
		add_meta_box( 'evol_sectionid', __( 'eVol post option', 'evol' ),
                'evol_inner_custom_box', 'post', 'normal', 'high' );
	} else {
		add_action('dbx_post_advanced', 'evol_add_custom_box' );
	}
}

/* Prints the inner fields for the custom post/page section */
function evol_inner_custom_box() {

	echo '<form method="post">';
	// Use nonce for verification
	
	echo '<input type="hidden" name="_evol_nonce" id="_evol_nonce" value="' .
	wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

	// The actual fields for data entry

	global $post;
	$featured_post_checked = NULL;
	$manual_content_checked = NULL;
	
	if(get_post_meta($post->ID, 'evol_featured_post',false)) $featured_post_checked = 'checked=checked';	
	if(get_post_meta($post->ID, 'evol_manual_content',false)) $manual_content_checked = 'checked=checked';	

	echo '<input type="checkbox" name="evol_featured_post" value=yes ' . $featured_post_checked . ' /> ' . __('Display on front page slide?','evol');
	echo '<p>'.__('**Use theme option to control how many image to display.','evol').'</p>';
	echo '<input type="checkbox" name="evol_manual_content" value=yes ' . $manual_content_checked . ' /> ' . __('Disable auto format','evol');
	echo '<p>' . __('Disable auto content formating, useful if you stay with wordpress post format but full content control.','evol') . '</p>';
	echo '<strong>' . __('Extra information for posts.','evol').'</strong><br>';
	echo '<small>' . __('*Fields allow HTML','evol').'</small>';
	echo '<table>';
	$td = 0;
	global $evol_post_metas; //Defined in functions.php
	foreach( $evol_post_metas as $key ) {
	$label = $key['label'];
	$name = $key['id'];
	$value = htmlentities(stripcslashes(get_post_meta($post->ID, $key['id'],true)));
	if(!$td % 2) echo '<tr>';
		echo '<td>';
		echo '<label style="width: 40px" for id="'.$name.'">'.$label.'</label>';
		echo '</td>';
		echo '<td>';
		echo '<input style="float:left;" type="text" size="20" name="'.$name.'" value="' . $value .'" >';
		echo '</td>';
	if($td % 2) echo '</tr>';

	$td++;
	}
	echo '</table>';
	echo '</form>';


}


/* When the post is saved, saves our custom data */
function evol_save_postdata( $post_id ) {
	if ( !wp_verify_nonce( $_POST['_evol_nonce'], plugin_basename(__FILE__) )) {
                return $post_id;
    }

	// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
	// to do anything
	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
	return $post_id;


	// Check permissions
	if ( 'page' == $_POST['post_type'] ) {
		if ( !current_user_can( 'edit_page', $post_id ) )
		return $post_id;
	} else {
		if ( !current_user_can( 'edit_post', $post_id ) )
		return $post_id;
	}

	// OK, we're authenticated: we need to find and save the data
	// Sorry, i've no time to make it shorter, maybe later.
	$featured_post = $_POST['evol_featured_post'];
	if(!empty($featured_post)) {
		update_post_meta($post_id, 'evol_featured_post', 'yes');
	} else { 
	delete_post_meta($post_id, 'evol_featured_post');
		}
	
	$manual_content = $_POST['evol_manual_content'];
	if(!empty($manual_content)) {
	update_post_meta($post_id, 'evol_manual_content', 'yes');
		} else { 
	delete_post_meta($post_id, 'evol_manual_content');
	}
	
	global $evol_post_metas, $post;
	foreach( $evol_post_metas as $key ) {
	$name = $key['id'];
	$data = $_POST[$name];
	if(!empty($data)) {
			$data = addslashes(trim(($data)));
			update_post_meta($post_id, $name, $data);
	} else {
			delete_post_meta($post_id, $name);
	}
	}
	return $data;
	return $featured_post;
	return $manual_content;
	
	
}
?>
