<?
header('Content-Type: text/html; charset=utf-8');
require_once('funcFile.php');
//require_once('C:/wamp/www/itla-wp/wp-content/themes/fortis7/bashro_func.php');
//require_once('sbPrivate/conFile.php');
require_once('sbPrivate/xX.php');
$xX=new xX();
require_once('myC/dbC.php');
$bashDB=new db();
$b_D=new bash_dest();



class bash_dest{

    // property declaration
	function bash_dest(){
	$this->dest_type=array(0=>'יבשת',1=>'מדינה',2=>'טריטוריה',3=>'עיר');
	$this->dest_cust=array('type'=>'bash_dest_type', 'en'=>'bash_dest_en_name', 'dad'=>'bash_dest_father');
	$this->post_Arr=array(
		'post_title'     =>'',
		'post_author'     =>'',
		'post_status'     =>'',
		'post_type'     =>'',
		'post_content' =>''
		);
		

	$this->bashDB=new db();
	}
	

	function get_the_dada($id){
		
	}
	
	
	
	
}





function bash_dest_to_dest($bashDB,$b_D){
//global $bashDB;
$allowed = array('a' => array( 'href' => array()));
$w=' WHERE  father_id=7'; ///AND post_type=\'bash_destination\'';
$dList=$bashDB->select('*','dest_list',$w,''); //LIMIT 0,1 LIMIT 0,1
	if ($dList!=false){
	$tmpPost=$b_D->post_Arr;
	$bCounter=0;
		foreach($dList as $i=>$v){
			$bCounter++;
		$tmpPost['post_title']=$v['name'];
		$tmpPost['post_author']='1';
		$tmpPost['post_status']='publish';
		$tmpPost['post_type']='bash_destination';
		$tmpPost['post_content']='<b>'.$v['name'].'</b>- מידע למדריכים, תמונות ותוכן על '.$v['name'].' מקצועי למדריכי טיולים. מידע ותכנים עבור קהל הגולשים ועוד אירועים, מזג אוויר...';
		//for custom..
		$t_father='236';
		$t_EN=$v['en_name'];
		$t_bashType='1';
		$vars='post_title,post_author,post_status,post_type,post_content,post_date,post_name';
		$bashDB->insert('wp_posts',$vars,'"'.$v['name'].'","1","publish","bash_destination","'.$tmpPost['post_content'].'","2014-09-04 21:32:13","'.urlencode ($v['name']).'"');
		$tmpID=mysql_insert_id();
		echo $tmpID.'|'.$v['name'].$v['en_name'].'<br><br><br>';
			if($tmpID>0){
			$bashDB->insert('wp_postmeta','post_id,meta_key,meta_value','"'.$tmpID.'","bash_dest_type","1"');
			$bashDB->insert('wp_postmeta','post_id,meta_key,meta_value','"'.$tmpID.'","bash_dest_en_name","'.$t_EN.'"');
			$bashDB->insert('wp_postmeta','post_id,meta_key,meta_value','"'.$tmpID.'","bash_dest_father","'.$t_father.'"');
			}
			else{echo 'no ID<br>';}
		}	
	}
	else{ echo 'NO DEST??!!??'.$dList;}
	echo '<br><br>'.$bCounter;
}







function bash_city_to_state($bashDB,$b_D){
//global $bashDB;
	$w=' WHERE meta_key=\'bash_dest_type\'  AND meta_value=1 ';
//	$jw='wp_postmeta.meta_key=\'bash_dest_en_name\' AND  wp_postmeta.post_id=p_id ';
	$limit='';
	//$jw=' wp_posts.ID=wp_postmeta.post_id AND wp_posts.post_status=\'publish\' ';
	$jTBL2='';$jOnWhere2='';$limit=' ';
//	$dList=$bashDB->selectJ('*,post_id AS p_id','wp_postmeta','wp_postmeta',$jw,$jTBL2,$jOnWhere2,$w,$limit);
	$dList=$bashDB->select('post_id','wp_postmeta',$w,$limit);	
	
	if ($dList!=false){
	$idArr=array();
		foreach($dList as $i=>$v){
		$idArr[]=$dList[$i][0];
//					echo $dList[$i]['p_id'].'-'.$dList[$i]['meta_value'].'<br>';
//						$name_list=$bashDB->select('*,post_id AS p_id','wp_postmeta',$w,$limit);	
		}
	$ids = join(',',$idArr);  
	$w2=' WHERE post_id IN ('.$ids.') AND meta_key=\'bash_dest_en_name\' ';
	$enName = $bashDB->select('*','wp_postmeta',$w2,$limit);	
//	$tmpEN_arr=array();
		foreach($enName as $i=>$v){
//		$tmpEN_arr[$i]=$enName[$i]['meta_value'];
//		echo $dList[$i]['post_id'].'- '.$enName[$i]['post_id'].' | '.$enName[$i]['meta_value'].'<br>';
		$w3=' WHERE en_name=\''.$enName[$i]['meta_value'].'\' ';
		$dest_list_f= $bashDB->select('*','dest_list',$w3,$limit );
		echo '<b>'.$enName[$i]['meta_value'].'</b>:<br>';
			foreach($dest_list_f as $ii => $vv){
			echo 'new ID:'.$enName[$i]['post_id'].' <br>old ID:'.$dest_list_f[$ii]['id'].'<br>'.$dest_list_f[$ii]['en_name'].'<br>';			
			$w4=' WHERE father_id='.$dest_list_f[$ii]['id'].' ';
			$sonsOfDest= $bashDB->select('*','dest_list',$w4,$limit );			
				if($sonsOfDest!=false){
					foreach ($sonsOfDest as $iii=>$vvv){
					echo ($iii+1).') '.$vvv['en_name'].'<br>';
					$tmpCont='<b>'.$vvv['name'].'</b>- אתרי תיירות ואטרקציות ב'.$vvv['name'].', תמונות ותוכן מקצועי למדריכי טיולים. אירועים, מזג אוויר...';
						bash_dest_make_sons($bashDB,$vvv['name'], $vvv['en_name'], $enName[$i]['post_id'],$tmpCont);					
					}
				}
				else {echo 'no childrens';}
					echo '<br><br><br>';				
			}
		}		
	}
	else{ echo 'NO DEST??!!??'.$dList;}
//	echo '<br><br>'.$bCounter;
}



function bash_dest_make_sons($bashDB,$pName,$pEN,$pNewDad,$cont){
$t_bashType='2';
$vars='post_title,post_author,post_status,post_type,post_content,post_date,post_name';
$bashDB->insert('wp_posts',$vars,'"'.$pName.'","1","publish","bash_destination","'.$cont.'","2014-09-06 21:32:13","'.urlencode ($pName).'"');
$tmpID=mysql_insert_id();
	if($tmpID>0){
	$bashDB->insert('wp_postmeta','post_id,meta_key,meta_value','"'.$tmpID.'","bash_dest_type","'.$t_bashType.'"');
	$bashDB->insert('wp_postmeta','post_id,meta_key,meta_value','"'.$tmpID.'","bash_dest_en_name","'.$pEN.'"');
	$bashDB->insert('wp_postmeta','post_id,meta_key,meta_value','"'.$tmpID.'","bash_dest_father","'.$pNewDad.'"');	
	echo '- SAVED<br>';
	}
	else{echo ' - FAIL<br>';}
}


?>
<html>
<head>
<style>
body{ direction:rtl; text-align:right}
</style>
</head>
<body>
<form name="doit"  method="post"  action="testfile.php">
<input name="fubu" value="1">
<input type="submit" value="שלח" />
</form>

<form name="fixPlus"  method="post"  action="testfile.php">
<input name="fixB" value="1">
<input type="submit" value="נקה אותי" />
</form>
<?
if(isset($_POST['fixB'])){
	$q=$bashDB->select('*','wp_posts',' WHERE post_status=\'publish\'','');
		foreach	($q as $i=>$v){
				if (strpos($v['post_name'],'+') !== false) {
//			echo $v['post_name'];				   echo 'צריך לנקות אותי';
					 if($bashDB->update('wp_posts','post_name=\''.str_replace('+','-',$v['post_name']).'\'',' WHERE ID=\''.$v['ID'].'\'')){
						echo 'cleaned	'.$v['post_title'].'';
					 }
					 else{ echo '<b>'.$v['post_title']. ' - failed</b>';}
					
							echo '<br>';
				}
			}
	}
?>




<?
if (isset($_POST['fubu']) && $_POST['fubu']==1){
//	bash_dest_to_dest($bashDB,$b_D);
	bash_city_to_state($bashDB,$b_D);
	}
?>
</body>
</html>

<?





?>