<?php
/*
Plugin Name: Fruitb Clients
Plugin URI: http://www.fruitbowlmedia.com
Description: Adds ability to manage 'Clients'
Author: Fruitbowl Media
Version: 1.0
Author URI: http://www.fruitbowlmedia.com
*/

class Fruitb_Clients {
	var $meta_fields = array("fruitb-clients-website", "fruitb-clients-logo", "fruitb-clients-screens", "fruitb-clients-thumbnail", "fruitb-clients-quote"); // add more depending on meta fields. This saves time later
	
	function Fruitb_Clients()
	{
		// Register custom post types
		register_post_type('client', array(
			'label' => __('Clients'),
			'singular_label' => __('Client'),
			'public' => true,
			'show_ui' => true, // UI in admin panel
			'_builtin' => false, // It's a custom post type, not built in
			'_edit_link' => 'post.php?post=%d',
			'capability_type' => 'post',
			'hierarchical' => false,
			'rewrite' => array("slug" => "client"), // Permalinks
			'query_var' => "client", // This goes to the WP_Query schema
			'supports' => array('title', 'editor', 'excerpt', 'revisions' /*,'custom-fields'*/) // Let's use custom fields for debugging purposes only
		));
		
		add_filter("manage_edit-client_columns", array(&$this, "edit_columns"));
		add_action("manage_posts_custom_column", array(&$this, "custom_columns"));
		
		// Register custom taxonomy
		//register_taxonomy("type", array("sponsor"), array("hierarchical" => true, "label" => "Sponsor Types", "singular_label" => "Sponsor Type", "rewrite" => true));

		// Admin interface init
		add_action("admin_init", array(&$this, "admin_init"));
		add_action("template_redirect", array(&$this, 'template_redirect'));
		
		// Insert post hook
		add_action("wp_insert_post", array(&$this, "wp_insert_post"), 10, 2);
	}
	
	function edit_columns($columns)
	{
		$columns = array(
			"cb" => "<input type=\"checkbox\" />",
			"title" => "Client Name",
			"fruitb-clients-website" => "Website"
		);
		
		return $columns;
	}
	
	function custom_columns($column)
	{
		global $post;
		switch ($column)
		{
			case "fruitb-clients-website":
				$custom = get_post_custom();
				echo $custom["fruitb-clients-website"][0];
				break;
			/* For taxonomy
			case "fc_speakers_company":
				$types = get_the_terms(0, "type");
				$types_html = array();
				if(is_array($types))
				{
					foreach ($types as $t)
						array_push($types_html, '<a href="' . get_term_link($t->slug, "type") . '">' . $t->name . '</a>');
					
					echo implode($types_html, ", ");
				}
				break;
			*/
		}
	}
	
	// Template selection
	function template_redirect()
	{
		global $wp;
		if ($wp->query_vars["post_type"] == "client")
		{
			include(STYLESHEETPATH . "/client-single.php");
			die();
		}
	}
	
	// When a post is inserted or updated
	function wp_insert_post($post_id, $post = null)
	{
		if ($post->post_type == "client") // you should only need to edit this line
		{
			// Loop through the POST data
			foreach ($this->meta_fields as $key)
			{
				$value = @$_POST[$key];
				if (empty($value))
				{
					delete_post_meta($post_id, $key);
					continue;
				}
				
				

				// If value is a string it should be unique
				if (!is_array($value))
				{
					// Update meta
					if (!update_post_meta($post_id, $key, $value))
					{
						// Or add the meta data
						add_post_meta($post_id, $key, $value);
					}
				}
				else
				{
					// delete old
					delete_post_meta($post_id, $key);
					
					update_post_meta($post_id, $key, $value);
					
					/*
					// If passed along is an array, we should remove all previous data
					delete_post_meta($post_id, $key);
					
					// Loop through the array adding new values to the post meta as different entries with the same name
					foreach ($value as $entry)
						add_post_meta($post_id, $key, $entry);
					*/
				}

			}
		}
	}
	
	function admin_init() 
	{
		// Custom meta boxes for each meta field specified
		add_meta_box("fruitb-clients-meta-website", "The Clients Website", array(&$this, "meta_options_website"), "client", "normal", "high");
		add_meta_box("fruitb-clients-meta-logo", "The Clients Company Logo", array(&$this, "meta_options_logo"), "client", "normal", "high");
		add_meta_box("fruitb-clients-meta-quote", "A Featured Quote From The Case Study To Feature In Large Bold Type", array(&$this, "meta_options_quote"), "client", "normal", "high");
		add_meta_box("fruitb-clients-meta-screens", "Screenshots For The Case Study", array(&$this, "meta_options_screens"), "client", "normal", "high");
		add_meta_box("fruitb-clients-meta-thumbnail", "Featured Homepage Thumbnail", array(&$this, "meta_options_thumbnail"), "client", "normal", "high");
	}
	
	// Admin post meta contents
	function meta_options_website()
	{
		global $post;
		$custom = get_post_custom($post->ID);
		$val = $custom["fruitb-clients-website"][0];
		
		?>
		<input name="fruitb-clients-website" value="<?php echo $val; ?>" type="text" style="width:100%" />
		<p>Enter the clients website, i.e. <em>http://www.acmewidgets.com</em></p>
		
		<?php
	}
	
	// Admin post meta contents
	function meta_options_quote()
	{
		global $post;
		$custom = get_post_custom($post->ID);
		$val = $custom["fruitb-clients-quote"][0];
		
		?>
		<input name="fruitb-clients-quote" value="<?php echo $val; ?>" type="text" style="width:100%" />
		<p>Enter a short (i.e. 1 sentence) quote from the case study text that will feature in large bold type on the case study page</p>
		<p><strong>This is different to client testimonials, and you should still enter a separate <em>Testimonial</em> post from the client if possible</p>
		
		<?php
	}
	
	// Admin post meta contents
	function meta_options_logo()
	{
		global $post;
		$custom = get_post_custom($post->ID);
		$val = $custom["fruitb-clients-logo"][0];
		
		?>
		<input name="fruitb-clients-logo" value="<?php echo $val; ?>" type="text" style="width:100%" />
		<p>Enter the URL to the clients logo 150px x 120px, i.e. <em>http://www.fruitbowlmedia.com/wp-content/uploads/logo.jpg</em></p>
		
		<?php
	}
	
	// Admin post meta contents
	function meta_options_thumbnail()
	{
		global $post;
		$custom = get_post_custom($post->ID);
		$val = $custom["fruitb-clients-thumbnail"][0];
		
		?>
		<input name="fruitb-clients-thumbnail" value="<?php echo $val; ?>" type="text" style="width:100%" />
		<p>Enter the URL to a thumbnail for this case study (perhaps a funky collection of screenshots?) 300px x 200px, i.e. <em>http://www.fruitbowlmedia.com/wp-content/uploads/logo.jpg</em></p>
		
		<?php
	}
	
	// Admin post meta contents
	function meta_options_screens()
	{
		global $post;
		$custom = get_post_custom($post->ID);
		$val = @unserialize($custom["fruitb-clients-screens"][0]);
		?>
		
		<p>Enter 3 sets of screenshots (with matching thumbnails) to showcase this case study. Thumbnails should be 210px x 158px and full size images should be 800px x 600px exactly</p>		
		<h4>Screenshot 1</h4>
		<div style="width:50%; float:left;">
			<input name="fruitb-clients-screens[0][thumb]" value="<?php echo $val[0]['thumb']; ?>" type="text" style="width:90%" />
			<p>Thumb (210px x 158px)</p>
		</div>
		
		<div style="width:50%; float:left;">
			<input name="fruitb-clients-screens[0][full]" value="<?php echo $val[0]['full']; ?>" type="text" style="width:90%" />
			<p>Full Size (800px x 600px)</p>
		</div>
		<br clear="both" />
		
		<h4>Screenshot 2</h4>
		<div style="width:50%; float:left;">
			<input name="fruitb-clients-screens[1][thumb]" value="<?php echo $val[1]['thumb']; ?>" type="text" style="width:90%" />
			<p>Thumb (210px x 158px)</p>
		</div>
		
		<div style="width:50%; float:left;">
			<input name="fruitb-clients-screens[1][full]" value="<?php echo $val[1]['full']; ?>" type="text" style="width:90%" />
			<p>Full Size (800px x 600px)</p>
		</div>
		<br clear="both" />
		
		<h4>Screenshot 3</h4>
		<div style="width:50%; float:left;">
			<input name="fruitb-clients-screens[2][thumb]" value="<?php echo $val[2]['thumb']; ?>" type="text" style="width:90%" />
			<p>Thumb (210px x 158px)</p>
		</div>
		
		<div style="width:50%; float:left;">
			<input name="fruitb-clients-screens[2][full]" value="<?php echo $val[2]['full']; ?>" type="text" style="width:90%" />
			<p>Full Size (800px x 600px)</p>
		</div>
		<br clear="both" />
				
		<?php
	}
	
	

}

// Initiate the plugin
add_action("init", "FruitbClientsInit");
function FruitbClientsInit() { global $fruitb_clients; $fruitb_clients = new Fruitb_Clients(); }