<?php

$b_gPath='/home1/bashro/public_html/itla.org.il/';
require_once($b_gPath.'funcFile.php');
require_once($b_gPath.'myC/dbC.php');
require_once($b_gPath.'wp-content/themes/fortis7/dest-class.php');
require_once($b_gPath.'wp-content/themes/fortis7/bWeather-class.php');

$bashDB=new db();
$b_D= new bash_dest();

if( isset($_POST['doW']) ){
$back=array();
$tName=array();
	if ($_POST['doW']=='destComp'){
	$w=' WHERE post_title LIKE \''.$_POST['dVal'].'%\'  AND post_status=\'publish\' AND post_type=\'bash_destination\'';
	$resMe=$bashDB->select('*','wp_posts',$w,'');
		if($resMe!=false && $resMe!=''){		
			foreach($resMe as $i=>$a){
			$tName=$resMe[$i]['post_title'];
			$ii=$resMe[$i]['ID'];
			$back[$ii]=''.$tName.'';
			}
				echo json_encode($back);
		}
		else{
		echo json_encode(array(''=>'יעד לא נמצא'));
		}
	}
	else if ($_POST['doW']=='destComp_w'){
		$xtra_w_type=' AND meta_value>1';
		if($_POST['d_sType']=='goToDest' || $_POST['d_sType']=='goToDest2'){
		$xtra_w_type='';
		}
	$w=' WHERE post_title LIKE \''.$_POST['dVal'].'%\'  AND post_status=\'publish\' AND post_type=\'bash_destination\'';
	$jw=' wp_postmeta.post_id=wp_posts.ID AND meta_key=\'bash_dest_type\' '.$xtra_w_type.'';
	$jTBL2='';	$jOnWhere2='';
	$limit=' ORDER BY wp_posts.post_title';	
	$resMe=$bashDB->selectJ('*','wp_posts','wp_postmeta',$jw,$jTBL2,$jOnWhere2,$w,$limit);
		if($resMe!=false && $resMe!=''){		
			foreach($resMe as $i=>$a){
			$tName=$resMe[$i]['post_title'];
			$ii=$resMe[$i]['ID'];

			$d_w=' WHERE post_id='.$resMe[$i]['ID'].' AND (meta_key=\'bash_dest_father\' OR meta_key=\'bash_dest_en_name\' ) ';
			$d_resMe=$bashDB->select('*','wp_postmeta',$d_w,'');
			$tmpFatherID='';
			$tmpEN='';
			$tmpEN_father='';
			$dType=$resMe[$i]['meta_value'];
			$wUS='';
//build_w_city_side($dType,$dEN,$dFather_cust['bash_dest_en_name'][0],$wUS,$b_D->cleanName(get_the_title()));			
					foreach($d_resMe as $iii=>$vvv){
						if($d_resMe[$iii]['meta_key']=='bash_dest_en_name'){$tmpEN=$d_resMe[$iii]['meta_value'];}
						else if($d_resMe[$iii]['meta_key']=='bash_dest_father'){
						$tmpFatherID=$d_resMe[$iii]['meta_value'];
						if($tmpFatherID!=0 && $tmpFatherID!=''){
						$d_w2=' WHERE post_id='.$tmpFatherID.' AND (meta_key=\'bash_dest_father\' OR meta_key=\'bash_dest_en_name\' )';
						$d_resMe2=$bashDB->select('*','wp_postmeta',$d_w2,'');
							if($d_resMe2!=false){
								foreach($d_resMe2 as $it=>$vVal){
									if($d_resMe2[$it]['meta_key']=='bash_dest_en_name'){$tmpEN_father=$d_resMe2[$it]['meta_value'];}
									else if($d_resMe2[$it]['meta_key']=='bash_dest_father'){
										if($d_resMe2[$it]['meta_value']=='220'){
										$wUS='usa';
										}
									}
								}							
							}
						}
					}
				}
					
			$back[$ii]=$dType.','.$b_D->cleanName($tmpEN).','.$b_D->cleanName($tmpEN_father).','.$wUS.','.$b_D->cleanName($tName);
			//'destEN'=>,'.$b_D->cleanName($tName).' ('.$b_D->cleanName($tmpEN).', '.$b_D->cleanName($tmpEN_father).')';
			}
				echo json_encode($back);
		}
		else{
		echo json_encode(array(''=>'יעד לא נמצא'));
		}		
		
	}
	
	else if($_POST['doW']=='destComp_w_call'){
	$b_wather= new bash_weather();
	echo $b_wather->build_w_city_side($_POST['d_type'],$_POST['d_EN'],$_POST['d_fEN'],$_POST['d_us'],$_POST['d_he']);	
	}
	
}
?>

<?

//	function selectJ('*','wp_posts','',$jOnWhere,$jTBL2,$jOnWhere2,$where,$limit){}

//	echo json_encode(array('12'=>'חולון','14'=>'בת ים','16'=>'חוף הונים'));		
//	bashro_dest_search($_POST['dVal'],$_POST['doW']); //
//	echo json_encode(array('12'=>'חולון','14'=>'בת ים','16'=>'חוף הונים'));		

//}
//else{//echo json_encode(array('12'=>'חולון','14'=>'בת ים','16'=>'חוף הונים'));		
//}

//function resMe(){}
//add_action('wp', 'bashro_dest_search', 10, 2);

//function bashro_dest_search($dVal,$dW){//$dVal,$dW
//echo json_encode(array('12'=>'חולון','14'=>'בת ים','16'=>'חוף הונים'));		

//$back=array();
// global $wpdb;
	//if($dW=='destComp'){
//	$w=' WHERE city_name LIKE \''.$dName.'%\'  ';
//	$res=$db->select('*' ,'city',$w,' ORDER BY city_name');
//	$res = $wpdb->get_post( 'SELECT * FROM $wpdb->posts WHERE post_title LIKE \'' . esc_sql( like_escape( $dVal ) ) . '%\'' );		
//$args = array('post_title_like' => $dVal);
//$res = WP_Query($arg);
//$finalArgs =  array (       
  //      'posts_per_page'=>1000,
    //    'order' => 'BY post_title',
      //  'post_type' => 'bash_destination'                         
//    );

    // Create a new instance
//    $searchSchools = new WP_Query( $finalArgs );

//    $mypostids = $wpdb->get_post("select ID from $wpdb->posts where post_title LIKE '".$dVal."%' ");
/*		if(!empty($res)){		
			foreach($res as $i=>$a){
			$tName=$res[$i]->post_title;
			$ii=$res[$i]->ID;
			$back[$ii]=''.$tName.'';
			}
		}
		else{$back.='לא מצאתי יעד בשם הזה';}
*/
//	echo json_encode($back);

//echo $back;
//	}
//echo json_encode(array('12'=>'חולון','14'=>'בת ים','16'=>'חוף הונים'));		
//}





/*
add_filter( 'posts_where', 'wpse18703_posts_where', 10, 2 );
function wpse18703_posts_where( $where, &$wp_query )
{
    global $wpdb;
    if ( $wpse18703_title = $wp_query->get( 'wpse18703_title' ) ) {
        $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' . esc_sql( like_escape( $wpse18703_title ) ) . '%\'';
    }
    return $where;
}


*/



/*$finalArgs =  array (       
        'posts_per_page'=>1000,
        'order' => 'BY post_title',
        'post_type' => 'bash_destination'                         
    );

    // Create a new instance
    $searchSchools = new WP_Query( $finalArgs );

    $mypostids = $wpdb->get_post("select ID from $wpdb->posts where post_title LIKE '".$str."%' ");

    $args = array(
        'post__in'=> $mypostids,
        'post_type'=>'school',
        'orderby'=>'title',
        'order'=>'asc'
    );

    $res = new WP_Query($args);

    while( $res->have_posts() ) : $res->the_post();

        global $post;

        $EstablishmentNumber = get_post_meta($post->ID,'EstablishmentNumber', true);

        $schl = array('id'=>$EstablishmentNumber, 'label'=>$post->post_title , 'value'=>$EstablishmentNumber );     
        $matchedSchools[] = $schl;


    endwhile;
	*/
?>

