<?php
// Custom fields for WP write panel
// This code is protected under Creative Commons License: http://creativecommons.org/licenses/by-nc-nd/3.0/

if($_REQUEST['post'])
{
	$postid = $_REQUEST['post'];
}else
{
	$postid = $_REQUEST['post_ID'];
}

$blog_cat = get_option('ptthemes_blogcategory');
$blog_cat = get_blog_sub_cats_str($type='array');
$cagInfo = get_the_category($postid);
foreach($cagInfo as $cagInfo_obj)
{
	$cat_array[] = $cagInfo_obj->term_id;
}
if(@array_intersect($cat_array,$blog_cat)) //blog detail
{
$pt_metaboxes = array();
}else
{
	
$pt_metaboxes = array(
		"video" => array (
			"name"		=> "video",
			"default" 	=> "",
			"label" 	=> __("Custom Video code"),
			"type" 		=> "textarea",
			"desc"      => __("Enter embedded code for video. eg. : code from youtube, vimeo, etc. ")
		),
		"address" => array (
			"name"		=> "address",
			"default" 	=> "",
			"label" 	=> __("Event Address"),
			"type" 		=> "text",
			"desc"      => __("Enter Event place address. eg. : 230 Vine Street And locations throughout Old City,Philadelphia, PA 19106")
		),
		"geo_latitude" => array (
			"name"		=> "geo_latitude",
			"default" 	=> "",
			"label" 	=> __("Map Latitude"),
			"type" 		=> "text",
			"desc"      => __("Enter Google Map Latitude. eg. : 39.955823048131286")
		),
		"geo_longitude" => array (
			"name"		=> "geo_longitude",
			"default" 	=> "",
			"label" 	=> __("Map Longitude"),
			"type" 		=> "text",
			"desc"      => __("Enter Google Map Longitude. eg. : -75.14408111572266")
		),
		"map_view" => array (
			"name"		=> "map_view",
			"default" 	=> "",
			"label" 	=> __("Map View Type"),
			"type" 		=> "select",
			"options" 	=>	 array('ROADMAP','TERRAIN','SATELLITE'),
			"desc"      => __("Enter Google Map View Type. eg. : ROADMAP,TERRAIN,SATELLITE")
		),
		"st_date" => array (
			"name"		=> "st_date",
			"default" 	=> "",
			"label" 	=> __("Start Date"),
			"type" 		=> "text",
			"desc"      => sprintf(__("Enter Event Start Date. eg. : %s</b>"),date('Y-m-d  h:i')),
		),
		"st_time" => array (
			"name"		=> "st_time",
			"default" 	=> "",
			"label" 	=> __("Start Time"),
			"type" 		=> "text",
			"desc"      => sprintf(__("Enter Event Start Time. 24 hours time format. eg. : %s"),date('H:i')),
		),
		"end_date" => array (
			"name"		=> "end_date",
			"default" 	=> "",
			"label" 	=> __("End Date"),
			"type" 		=> "text",
			"desc"      => sprintf(__("Enter Event End Date. eg. : %s"),date('Y-m-d  h:i')),
		),
		"end_time" => array (
			"name"		=> "end_time",
			"default" 	=> "",
			"label" 	=> __("End Time"),
			"type" 		=> "text",
			"desc"      => sprintf(__("Enter Event End Time. 24 hours time format. eg. : %s"),date('H:i')),
		),
		"is_featured" => array (
			"name"		=> "is_featured",
			"default" 	=> "",
			"label" 	=> __("Want to Set as Featured"),
			"type" 		=> "checkbox",
			"desc"      => __("Featured events will be displayed at top in every listing (search and category listing).")
		),
		"phone" => array (
			"name"		=> "phone",
			"default" 	=> "",
			"label" 	=> __("Phone Number"),
			"type" 		=> "text",
			"desc"      => __("Enter Phone Number")
		),
		"email" => array (
			"name"		=> "email",
			"default" 	=> "",
			"label" 	=> __("Email Address"),
			"type" 		=> "text",
			"desc"      => __("Enter Address. eg. : info@myplace.com")
		),
		"website" => array (
			"name"		=> "website",
			"default" 	=> "",
			"label" 	=> __("Website"),
			"type" 		=> "text",
			"desc"      => __("Enter Website Address. eg. : http://myplace.com")
		),
		"twitter" => array (
			"name"		=> "twitter",
			"default" 	=> "",
			"label" 	=> __("Twitter"),
			"type" 		=> "text",
			"desc"      => __("Enter Twitter Address. eg. : http://twitter.com/myplace")
		),
		"facebook" => array (
			"name"		=> "facebook",
			"default" 	=> "",
			"label" 	=> __("Facebook"),
			"type" 		=> "text",
			"desc"      => __("Enter Facebook Address. eg. : http://facebook.com/myplace")
		),
		"reg_desc" => array (
			"name"		=> "reg_desc",
			"default" 	=> "",
			"label" 	=> __("Register Now Description"),
			"type" 		=> "textarea",
			"desc"      => __('Enter Register now Description. eg. : &lt;a class="b_register" href="#"&gt; Register Now &lt;/a&gt;&lt;p&gt; &lt;strong&gt;Admission :&lt;/strong&gt;&lt;br&gt;Event member receive free entry, a free drink and are eligible to win prizes.Entry for non-member is $ 10 &lt;/p&gt;')
		),
		
		"organizer_name" => array (
			"name"		=> "organizer_name",
			"default" 	=> "",
			"label" 	=> __("Organizer Name"),
			"type" 		=> "text",
			"desc"      => __("Enter Organizer Name. eg. : Castor Event Organisers")
		),
		"organizer_email" => array (
			"name"		=> "organizer_email",
			"default" 	=> "",
			"label" 	=> __("Organizer Email Address"),
			"type" 		=> "text",
			"desc"      => __("Enter Organizer  Email Address. eg. : info@myevent.com")
		),
		"organizer_logo" => array (
			"name"		=> "organizer_logo",
			"default" 	=> "",
			"label" 	=> __("Organizer Logo"),
			"type" 		=> "text",
			"desc"      => __("Enter Organizer Logo image URL. eg. : http://mysite.com/images/logo.jpg")
		),
		"organizer_address" => array (
			"name"		=> "organizer_address",
			"default" 	=> "",
			"label" 	=> __("Organizer Address"),
			"type" 		=> "text",
			"desc"      => __("Enter Organizer Address. eg. : 230 Vine Street And locations throughout Old City, Philadelphia, PA 19106")
		),
		"organizer_contact" => array (
			"name"		=> "organizer_contact",
			"default" 	=> "",
			"label" 	=> __("Organizer Contact Information"),
			"type" 		=> "text",
			"desc"      => __("Enter Organizer Information. eg. : phone number, mobile, Instant Message ID")
		),
		"organizer_website" => array (
			"name"		=> "organizer_website",
			"default" 	=> "",
			"label" 	=> __("Organizer Website"),
			"type" 		=> "text",
			"desc"      => __("Enter Organizer Website. eg. : http://myevent.org")
		),
		"organizer_mobile" => array (
			"name"		=> "organizer_mobile",
			"default" 	=> "",
			"label" 	=> __("Organizer Mobile"),
			"type" 		=> "text",
			"desc"      => __("Enter Organizer Mobile. eg. : +01 9845623178")
		),
		"organizer_desc" => array (
			"name"		=> "organizer_desc",
			"default" 	=> "",
			"label" 	=> __("Organizer Description"),
			"type" 		=> "textarea",
			"desc"      => __("Enter Organizer Description. eg. : &lt;p&gt;Event member receive free entry, a free drink and are eligible to win prizes.&lt;/p&gt;")
		),
		"paid_amount" => array (
			"name"		=> "paid_amount",
			"default" 	=> "",
			"label" 	=> __("Paid Amount"),
			"type" 		=> "text",
			"desc"      => __("Paid Amount to publish the listing. eg. : $20")
		),
		"paid_amount" => array (
			"name"		=> "paid_amount",
			"default" 	=> "",
			"label" 	=> __("Paid Amount"),
			"type" 		=> "text",
			"desc"      => __("Paid Amount to publish the listing. eg. : $20")
		),
		"paymentmethod" => array (
			"name"		=> "paymentmethod",
			"default" 	=> "",
			"label" 	=> __("Payment Method"),
			"type" 		=> "text",
			"desc"      => __("Payment Method used to publish the listing. eg. : paypal/googlecheckout/authorizenet/cashondelivery ... etc")
		),
		"alive_days" => array (
			"name"		=> "alive_days",
			"default" 	=> "",
			"label" 	=> __("Number of days to be Published "),
			"type" 		=> "text",
			"desc"      => __("Enter Number of publish days for event. eg. : 30")
		)
	);
	
}


if(!function_exists('ptthemes_meta_box_content')){
	function ptthemes_meta_box_content() {
		global $post, $pt_metaboxes;
		$output = '';
		$output .= '<div class="pt_metaboxes_table">'."\n";
		$output .='<input type="hidden" name="templatic_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
		foreach ($pt_metaboxes as $pt_id => $pt_metabox) {
		if($pt_metabox['type'] == 'text' OR $pt_metabox['type'] == 'select' OR $pt_metabox['type'] == 'checkbox' OR $pt_metabox['type'] == 'textarea' OR $pt_metabox['type'] == 'radio'  OR $pt_metabox['type'] == 'multiselect')
				$pt_metaboxvalue = get_post_meta($post->ID,$pt_metabox["name"],true);
				if ($pt_metaboxvalue == "" || !isset($pt_metaboxvalue)) {
					$pt_metaboxvalue = $pt_metabox['default'];
				}
				if($pt_metabox['type'] == 'text'){
					$output .= "\t".'<div>';
					$output .= "\t\t".'<br/><p class=lable_title><label for="'.$pt_id.'">'.$pt_metabox['label'].'</label></p>'."\n";
					$output .= "\t\t".'<p><input size="100" class="pt_input_text" type="'.$pt_metabox['type'].'" value="'.$pt_metaboxvalue.'" name="ptthemes_'.$pt_metabox["name"].'" id="'.$pt_id.'"/></p>'."\n";
					$output .= "\t\t".'<p class=note>'.$pt_metabox['desc'].'</p>'."\n";
					$output .= "\t".'</div>'."\n";
				}
				elseif ($pt_metabox['type'] == 'textarea'){
					$output .= "\t".'<div>';
					$output .= "\t\t".'<br/><p class=lable_title><label for="'.$pt_id.'">'.$pt_metabox['label'].'</label></p>'."\n";
					$output .= "\t\t".'<p><textarea rows="5" cols="98" class="pt_input_textarea" name="ptthemes_'.$pt_metabox["name"].'" id="'.$pt_id.'">' . $pt_metaboxvalue . '</textarea></p>'."\n";
					$output .= "\t\t".'<p class=note>'.$pt_metabox['desc'].'</p>'."\n";
					$output .= "\t".'</div>'."\n";
				}
			   elseif ($pt_metabox['type'] == 'select'){
					$output .= "\t".'<div>';
					$output .= "\t\t".'<br/><p class=lable_title><label for="'.$pt_id.'">'.$pt_metabox['label'].'</label></p>'."\n";
					$output .= "\t\t".'<p><select class="pt_input_select" id="'.$pt_id.'" name="ptthemes_'. $pt_metabox["name"] .'"></p>'."\n";
					$array = $pt_metabox['options'];
					if($array){
						foreach ( $array as $id => $option ) {
							$selected = '';
							if($pt_metabox['default'] == $option){$selected = 'selected="selected"';} 
							if($pt_metaboxvalue == $option){$selected = 'selected="selected"';}
							$output .= '<option value="'. $option .'" '. $selected .'>' . $option .'</option>';
						}
					}
					$output .= '</select><p class=note>'.$pt_metabox['desc'].'</p>'."\n";
					$output .= "\t".'</div>'."\n";
				}elseif ($pt_metabox['type'] == 'multiselect'){
					$output .= "\t".'<div>';
					$output .= "\t\t".'<br/><p class=lable_title><label for="'.$pt_id.'">'.$pt_metabox['label'].'</label></p>'."\n";
					$output .= "\t\t".'<p><select class="pt_input_select" id="'.$pt_id.'" name="ptthemes_'. $pt_metabox["name"] .'" class="pt_input_multiple"  style="height: 80px;" multiple="multiple">'."\n";
					$array = $pt_metabox['options'];
					if($array){
						foreach ( $array as $id => $option ) {
							$selected = '';
							if(in_array($pt_metabox['default'],$pt_metabox['options'])){$selected = 'selected="selected"';}
							if(in_array($option,$pt_metaboxvalue)){$selected = 'selected="selected"';}
							$output .= '<option value="'. $option .'" '. $selected .'>' . $option .'</option>';
						}
					}
					$output .= '</select></p><p class=note>'.$pt_metabox['desc'].'</p>'."\n";
					$output .= "\t".'</div>'."\n";
				}
				elseif ($pt_metabox['type'] == 'radio'){
					$output .= "\t".'<div>';
					$output .= "\t\t".'<br/><p class=lable_title><label for="'.$pt_id.'">'.$pt_metabox['label'].'</label></p>'."\n";
					$array = $pt_metabox['options'];
					if($array){
						foreach ( $array as $id => $option ) {
						   
						   $checked='';
						   if($pt_metabox['default'] == $option){$checked = 'checked="checked"';} 
							if($pt_metaboxvalue == $option){$checked = 'checked="checked"';}
							$output .= "\t\t".'<div class=\"input_radio\"><input type="radio" '.$checked.' class="pt_input_radio"  name="ptthemes_'. $pt_metabox["name"] .'" />  ' . $option .'</div>'."\n";
						}
					}
					$output .= ''.$pt_metabox['desc'].'</p>'."\n";
					$output .= "\t".'</div>'."\n";
				}
				elseif ($pt_metabox['type'] == 'checkbox'){
					if($pt_metaboxvalue == 'on') { $checked = 'checked="checked"';} else {$checked='';}
					$output .= "\t".'<div>';
					$output .= "\t\t".'<br/><p class=lable_title><label for="'.$pt_id.'">'.$pt_metabox['label'].'</label></p>'."\n";
					$output .= "\t\t".'<p><input type="checkbox" '.$checked.' class="pt_input_checkbox"  id="'.$pt_id.'" name="ptthemes_'. $pt_metabox["name"] .'" /></p>'."\n";
					$output .= "\t\t".'<p class=note>'.$pt_metabox['desc'].'</p>'."\n";
					$output .= "\t".'</div>'."\n";
				}        
			}
		
		$output .= '</div>'."\n\n";
		echo $output;
	}
}
if(!function_exists('ptthemes_metabox_insert')){
	function ptthemes_metabox_insert($post_id) {
		global $pt_metaboxes;
		global $globals;
		// verify nonce
		if (!wp_verify_nonce($_POST['templatic_meta_box_nonce'], basename(__FILE__))) {
			return $post_id;
		}
		
		// check autosave
		if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
			return $post_id;
		}
		
		$pID = $_POST['post_ID'];
		$counter = 0;
		foreach ($pt_metaboxes as $pt_metabox) { // On Save.. this gets looped in the header response and saves the values submitted
		if($pt_metabox['type'] == 'text' OR $pt_metabox['type'] == 'select' OR $pt_metabox['type'] == 'checkbox' OR $pt_metabox['type'] == 'textarea' OR $pt_metabox['type'] == 'radio'  OR $pt_metabox['type'] == 'multiselect') // Normal Type Things...
			{
				$var = "ptthemes_".$pt_metabox["name"];
				 if (1) {            
					if( get_post_meta( $pID, $pt_metabox["name"] ) == "" )
						add_post_meta($pID, $pt_metabox["name"], $_POST[$var], true );
					elseif($_POST[$var] != get_post_meta($pID, $pt_metabox["name"], true))
						update_post_meta($pID, $pt_metabox["name"], $_POST[$var]);
					elseif($_POST[$var] == "")
						delete_post_meta($pID, $pt_metabox["name"], get_post_meta($pID, $pt_metabox["name"], true));
				}  
			} 
		}
	}
}
if(!function_exists('ptthemes_header_inserts')){
	function ptthemes_header_inserts(){
		echo '<link rel="stylesheet" type="text/css" href="'.get_bloginfo('template_directory').'/library/functions/admin_style.css" media="screen" />';
	}
}
if(!function_exists('ptthemes_meta_box')){
	function ptthemes_meta_box() {
		if ( function_exists('add_meta_box') ) {
			//add_meta_box('ptthemes-settings',$GLOBALS['themename'].' Custom Settings','ptthemes_meta_box_content','post','normal','high');
			add_meta_box('ptthemes-settings',$GLOBALS['themename'].' Custom Settings','ptthemes_meta_box_content',CUSTOM_POST_TYPE1,'normal','high');
		}
	}
}
add_action('admin_menu', 'ptthemes_meta_box');
add_action('admin_head', 'ptthemes_header_inserts');
add_action('save_post', 'ptthemes_metabox_insert');
?>