<?php
session_start();
ob_start();
$payable_amount = 0;
$property_price_info = get_property_price_info($_SESSION['property_info']['price_select']);		
$property_price_info = $property_price_info[0];
if($property_price_info['price']>0){
$payable_amount = $property_price_info['price'];
}
$price_capable_cat = $property_price_info['cat'];
if($property_price_info['price']>0){
$payable_amount = get_payable_amount_with_coupon($payable_amount,$_SESSION['property_info']['proprty_add_coupon']);
}

if($_REQUEST['pid']=='' && $payable_amount>0 && $_REQUEST['paymentmethod']=='')
{
	wp_redirect(get_option( 'siteurl' ).'/?ptype=preview_event&msg=nopaymethod');
	exit;
}
global $current_user;
if($current_user->data->ID=='' && $_SESSION['property_info'])
{
	include_once(TEMPLATEPATH . '/library/includes/single_page_checkout_insertuser.php');
}
global $wpdb;
if($_POST)
{	
	if($_POST['paynow'])
	{
		$property_info = $_SESSION['property_info'];
		if($property_info){
			if($property_info['website'] && !strstr($property_info['website'],'http://'))
			{
				$property_info['website'] = 'http://'.$property_info['website'];
			}
			if($property_info['twitter'] && !strstr($property_info['twitter'],'http://'))
			{
				$property_info['twitter'] = 'http://'.$property_info['twitter'];
			}
			if($property_info['facebook'] && !strstr($property_info['facebook'],'http://'))
			{
				$property_info['facebook'] = 'http://'.$property_info['facebook'];
			}
		}
		$stdate = $property_info['stdate'];
		$enddate = $property_info['enddate'];
		if($property_info['sttime'])
		{
			$stdate = $stdate . ' ' .$property_info['sttime'];	
		}
		if($property_info['enddate'])
		{
			$enddate = $enddate . ' ' .$property_info['endtime'];	
		}
		$custom = array("address" 		=> $property_info['address'],
						"geo_latitude"	=> $property_info['geo_latitude'],
						"geo_longitude"	=> $property_info['geo_longitude'],
						"st_date"		=> $stdate,
						"st_time"		=> $property_info['sttime'],
						"end_date"		=> $enddate,
						"end_time"		=> $property_info['endtime'],
						"video"			=> $property_info['video'],
						"phone"			=> $property_info['phone'],
						"email"			=> $property_info['email'],
						"website"		=> $property_info['website'],
						"twitter"		=> $property_info['twitter'],
						"facebook"		=> $property_info['facebook'],
						"reg_desc"		=> $property_info['reg_desc'],
						"map_view"		=> $property_info['map_view'],
						"post_city_id"	=> $property_info['post_city_id'],
						"organizer_name"		=> $property_info['organizer_name'],
						"organizer_email"		=> $property_info['organizer_email'],
						"organizer_logo"		=> $property_info['organizer_logo'],
						"organizer_address"		=> $property_info['organizer_address'],
						"organizer_contact"		=> $property_info['organizer_contact'],
						"organizer_website"		=> $property_info['organizer_website'],
						"organizer_mobile"		=> $property_info['organizer_mobile'],
						"organizer_desc"		=> $property_info['organizer_desc'],
					);
		if($property_price_info['is_featured'])
		{
			$custom['is_featured'] = $property_price_info['is_featured'];
		}
		$post_title = $property_info['proprty_name'];
		$description = $property_info['proprty_desc'];
		
		$catids_arr = array();
		if($property_info['category'])
		{
			$catids_arr = $property_info['category'];
		}else
		{
			$catids_arr[] = 1;	
		}
		
		$my_post = array();
		if($_REQUEST['pid'] && $property_info['renew']=='')
		{
			$my_post['ID'] = $_POST['pid'];
			$my_post['post_status'] = get_post_status($_POST['pid']);
		}else
		{
			$custom['paid_amount'] = $payable_amount;
			$custom['alive_days'] = $property_price_info['alive_days'];
			$custom['paymentmethod'] = $_REQUEST['paymentmethod'];
			
			if($payable_amount>0)
			{
				$post_default_status = 'draft';
			}else
			{
				$post_default_status = get_property_default_status();
			}			
			$my_post['post_status'] = $post_default_status;
		}
		if($current_user_id)
		{
			$my_post['post_author'] = $current_user_id;
		}
		$my_post['post_title'] = $post_title;
		$my_post['post_content'] = $description;
		$my_post['post_type'] = CUSTOM_POST_TYPE1;
		if($property_info['category'])
		{
			$cat_display=get_option('ptthemes_category_dislay');
			if($cat_display=='select'){
				$post_category = $property_info['category'];
				for($c=0;$c<count($post_category);$c++)
				{
					$catid = $post_category[$c];
					$catname = $wpdb->get_var("select name from $wpdb->terms where term_id=\"$catid\"");
					$post_category[$c] = $catname;	
				}
			}else
			{
				$post_category = $property_info['category'];
			}
		}else
		{
			$post_category = array(1);	
		}
		if($price_capable_cat)
		{
			$post_category[] = $price_capable_cat;
		}
		$my_post['tax_input'] = $post_category;
		
		if($_REQUEST['pid'])
		{
			if($property_info['renew'])
			{
				$my_post['post_date'] = date('Y-m-d H:i:s');
				$custom['paid_amount'] = $payable_amount;
				$custom['alive_days'] = $property_price_info['alive_days'];
				$custom['paymentmethod'] = $_REQUEST['paymentmethod'];
				$my_post['ID'] = $_REQUEST['pid'];
				$last_postid = wp_update_post($my_post);
				wp_set_object_terms($last_postid, $post_category, $taxonomy=CUSTOM_CATEGORY_TYPE1);
			}else
			{
				$last_postid = wp_update_post($my_post);
				wp_set_object_terms($last_postid, $post_category, $taxonomy=CUSTOM_CATEGORY_TYPE1);
			}
		}else
		{
			$last_postid = wp_insert_post( $my_post ); //Insert the post into the database
			//wp_set_post_terms( $last_postid, $post_category, $taxonomy = eventcategory');
			wp_set_object_terms($last_postid, $post_category, $taxonomy=CUSTOM_CATEGORY_TYPE1);
		}
		$custom["paid_amount"] = $payable_amount;
		foreach($custom as $key=>$val)
		{				
			update_post_meta($last_postid, $key, $val);
		}
		if($_SESSION["file_info"])
		{
			
			$menu_order = 0;
			foreach($_SESSION["file_info"] as $image_id=>$val)
			{
				$src = get_image_tmp_phy_path().$image_id.'.jpg';
				if(file_exists($src))
				{
					$menu_order++;
					$dest_path = get_image_phy_destination_path().$image_id.'.jpg';
					$original_size = get_image_size($src);
					$thumb_info = image_resize_custom($src,$dest_path,get_option('thumbnail_size_w'),get_option('thumbnail_size_h'));
					$medium_info = image_resize_custom($src,$dest_path,get_option('medium_size_w'),get_option('medium_size_h'));
					$post_img = move_original_image_file($src,$dest_path);
					$post_img['post_status'] = 'attachment';
					$post_img['post_parent'] = $last_postid;
					$post_img['post_type'] = 'attachment';
					$post_img['post_mime_type'] = 'image/jpeg';
					$post_img['menu_order'] = $menu_order;
					
					$last_postimage_id = wp_insert_post( $post_img ); // Insert the post into the database
		
					$thumb_info_arr = array();
					if($thumb_info)
					{
						$sizes_info_array = array();
						if($thumb_info)
						{
						$sizes_info_array['thumbnail'] =  array(
																"file" =>	$thumb_info['file'],
																"height" =>	$thumb_info['height'],
																"width" =>	$thumb_info['width'],
																);
						}
						if($medium_info)
						{
						$sizes_info_array['medium'] =  array(
																"file" =>	$medium_info['file'],
																"height" =>	$medium_info['height'],
																"width" =>	$medium_info['width'],
																);
						}
						$hwstring_small = "height='".$thumb_info['height']."' width='".$thumb_info['width']."'";
					}else
					{
						$hwstring_small = "height='".$original_size['height']."' width='".$original_size['width']."'";
					}
					update_post_meta($last_postimage_id, '_wp_attached_file', get_attached_file_meta_path($post_img['guid']));
					$post_attach_arr = array(
										"width"	=>	$original_size['width'],
										"height" =>	$original_size['height'],
										"hwstring_small"=> $hwstring_small,
										"file"	=> get_attached_file_meta_path($post_img['guid']),
										"sizes"=> $sizes_info_array,
										);
					wp_update_attachment_metadata( $last_postimage_id, $post_attach_arr );
				}				
			}
		}
		$_SESSION['property_info'] = array();
		$_SESSION["file_info"] = array();
		if($_REQUEST['pid'] && $property_info['renew']=='')
		{
			wp_redirect(get_author_link($echo = false, $current_user->data->ID));
			exit;
		}else
		{
			if($payable_amount <= 0)
			{
				///////ADMIN EMAIL START//////
				$fromEmail = get_site_emailId();
				$fromEmailName = get_site_emailName();
				$store_name = get_option('blogname');
				$client_message = __('<p>Dear '.$fromEmail.',</p><p>A new event listing of ID:#'.$last_postid.' </p><p>Thanks</p>');
				$subject = __('New event listing of ID:#'.$last_postid);
				$headers  = 'MIME-Version: 1.0' . "\r\n";
				$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
				$headers .= 'To: '.$fromEmailName.' <'.$fromEmail.'>' . "\r\n";
				@mail($fromEmail,$subject,$client_message,$headers);///To clidne email
				//////ADMIN EMAIL END////////		
				$suburl .= "&pid=$last_postid";
				if($property_info['renew'])
				{
					$suburl .= "&renew=1";
				}		
				wp_redirect(get_option('siteurl')."/?ptype=success$suburl");
				exit;
			}else
			{
				$paymentmethod = $_REQUEST['paymentmethod'];
				$paymentSuccessFlag = 0;
				if($paymentmethod == 'prebanktransfer' || $paymentmethod == 'payondelevary')
				{
					
					if($property_info['renew'])
					{
						$suburl = "&renew=1";
					}
					$suburl .= "&pid=$last_postid";
					wp_redirect(get_option('siteurl').'/?ptype=success&paydeltype='.$paymentmethod.$suburl);
				}
				else
				{
					if(file_exists( TEMPLATEPATH.'/library/payment/'.$paymentmethod.'/'.$paymentmethod.'_response.php'))
					{
						include_once(TEMPLATEPATH.'/library/payment/'.$paymentmethod.'/'.$paymentmethod.'_response.php');
					}
				}
				exit;	
			}
		}
	}
}
?>