<?
include_once('bash_pathFile.php');
class bash_dest{
    // property declaration
	function bash_dest(){
	$this->dest_type=array(0=>'יבשת',1=>'מדינה',2=>'עיר'); //,2=>'טריטוריה'
	$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 sonsOfDest($id){ //Jax and Gemma 
	$w=' WHERE meta_key=\'bash_dest_father\'  AND meta_value='.$id.'';
	$jw=' wp_posts.ID=wp_postmeta.post_id AND wp_posts.post_status=\'publish\' AND wp_posts.post_type=\'bash_destination\'';
	$jTBL2='';$jOnWhere2='';$limit=' ORDER BY wp_posts.post_title';
	return $this->bashDB->selectJ('*','wp_postmeta','wp_posts',$jw,$jTBL2,$jOnWhere2,$w,$limit);
	}
	

	function qForDestDisplay($qSons){ //Jax and Gemma 
		if($qSons!='' && $qSons!=false){
		$back='<div id="qForDest_main">';
		foreach($qSons as $i=>$v){
		$back.='<div class="qForDest_li"><div class="bash_qMark">?</div><a href="../../answers/'.str_replace('+','-',$qSons[$i]['post_name']).'" >'.$this->cleanName($qSons[$i]['post_title']).'</a></div>';
		}
		$back.='</div>';
		return $back;
		}
		else{return '<div style="text-align:center">לא נמצאו שאלות הקשורות ליעד זה</div>';}
	}
	
	function qForDest($id){ //Jax and Gemma 
	$w='  WHERE  post_status=\'publish\'  AND post_type=\'cma_thread\'';
	$jw=' wp_postmeta.post_id=wp_posts.ID AND meta_key=\'bash_dest_father\'  AND meta_value='.$id.'';
	$jTBL2='';$jOnWhere2='';$limit=' ORDER BY wp_posts.post_title';
	return $this->bashDB->selectJ('*','wp_posts','wp_postmeta',$jw,$jTBL2,$jOnWhere2,$w,$limit);
	}
	
	
	
	function cleanName($name){
		$sogar_en= strpos($name, '(');
		if ($sogar_en==true){
		$b_c2= explode('(', $name); return $b_c2[0];}
		else {return $name;}
	}



	function sonsOfDisplay($sons){
		if($sons!='' && $sons!=false){
		$back='<div class="bash_sons_main">';
		foreach($sons as $i=>$v){
		$back.='<div class="bash_sons_dest"><a href="../'.str_replace('+','-',$sons[$i]['post_name']).'" >'.$this->cleanName($sons[$i]['post_title']).'</a></div>';
		}
		$back.='</div>';
		return $back;
		}
		else{return '<div style="text-align:center">אין יעדי משנה לאזור זה</div>';}
	}


	function destToCM($dID,$pID){
	$back=false;
	$makeNew=$this->bashDB->insert('wp_postmeta','post_id,meta_key,meta_value','"'.$pID.'","bash_dest_father","'.$dID.'"');
		if($makeNew!=false){$back=array($pID,$dID);}
	return $back;
	}



	function runDest($myP,$cust){
	$dType	=$cust[0]['bash_dest_type'];
	$dDad	=$cust[0]['bash_dest_type'];
	$dEN	=$cuts[0]['bash_dest_en_name'];
//		if($dType<4){
		return $this->sonsOfDisplay($this->sonsOfDest($myP->ID));
//		}
	}
	
	function getFileIcon($file){
	$back='icon_file.png';
	$type=array('.pdf'=>'icon_pdf.gif','.csv'=>'icon_csv.gif','.xlsx'=>'icon_csv.gif','.docx'=>'icon_doc.gif','.doc'=>'icon_doc.gif','.ppt'=>'icon_ppt.gif','.pptx'=>'icon_ppt.gif','.txt'=>'icon_text.gif');
		foreach($type	as $i=>$v){
			$isArr=explode('.',$file);
			if('.'.end($isArr)==$i){$back=$v;}
			else{}
//			if(is_array($isArr)===false){}
//			else{$back=$v;}
		}
	return $back;
	}
		
	function displayFileInDest($dID,$arr,$type,$dPagen){
		global $b_gPath;
global $b_webPath;
$back='<div id="dest_'.$type.'_list">';
		if($arr==false){
			if($type=='img'){		$back.='<div class="centMe"> לא נמצאו תמונות ליעד זה</div>';}
			else if($type=='file'){		$back.='<div class="centMe">לא נמצאו קבצים הקשורים ליעד זה</div>';}			
		}
		else{	
			if($type=='img'){
				//data-pagination="'.$dPagen.'" 
				$back.='<div id="thumbGrid"  style="" data-thumbgrid="true" data-effect="zoomIn" data-delay="60" data-timing="800" data-galleryeffectnext="zoomIn" data-galleryeffectprev="zoomOut" data-pagination="'.$dPagen.'" >';
				}
			foreach($arr as $i=>$v){
				if($type=='img'){
			//		http://www.itla.org.il/wp-content/plugins/wp-filebase/wp-filebase_thumb.php?fid=$arr[$i]['file_id'].png
				$back.='<img class="dest_img_img" src="'.$b_webPath.'wp-content/plugins/wp-filebase/wp-filebase_thumb.php?fid='.$arr[$i]['file_id'].'.png" alt="'.$arr[$i]['file_display_name'].'" data-highres="'.$b_webPath.'download/'.$arr[$i]['file_path'].'" 
				data-caption="'.$arr[$i]['file_display_name'].'"/>';

				}
				else{
				$back.='<div class="dest_file_li" id="" title="'.$arr[$i]['file_display_name'].'"><img src="'.$b_webPath.'bash-xtra/img/'.$this->getFileIcon($arr[$i]['file_path']).'" class="bash_fileIcon" />'.$arr[$i]['file_display_name'].'</div> <div style="text-align:left"><a class="dest_file_li_a" href="'.$b_webPath.'download/'.$arr[$i]['file_path'].'" target="_blank">הורדה\צפייה</a></div>';	
				}
			}
			if($type=='img'){$back.='</div><div class="moreResLink f12 FL"><a href="http://www.itla.org.il/חיפוש-קבצים/?bash_id='.$dID.'&bash_type=img">לכל התמונות</a></div>';}
			else if($type=='file'){$back.='<div class="moreResLink f12 FL"><a href="http://www.itla.org.il/חיפוש-קבצים/?bash_id='.$dID.'&bash_type=file">לכל הקבצים</a></div>';}
		}
		return $back.'</div>';		
	}
	
	
	function getTheFiles($dID,$isUse,$fType,$resNum){
	$roleW='';
	if($dID=='h'){$xtraID='';}
	else{$xtraID=" file_version=".$dID."  AND ";}
		if($fType=='img'){ $likeW=' LIKE '; $oaAnd=' OR ';}
		else{ $likeW=' NOT LIKE';  $oaAnd=' AND ';}
		if($isUse==false){ $roleW.=" AND file_user_roles=''";}		
	$w=" WHERE (".$xtraID." file_offline=1 ".$roleW.") AND ( file_path ".$likeW." '%png%' ".$oaAnd."  file_path  ".$likeW." '%gif%' ".$oaAnd."  file_path  ".$likeW." '%jpg%') ";	
	// OR file_path ".$likeW." '%.gif%' OR file_path ".$likeW." '%.png%'
	$back.='';
	
	$limit=' ORDER BY file_date DESC ';
	if($resNum>0){$limit.=' LIMIT 0,'.$resNum;}
		
		return $this->bashDB->select('*','wp_wpfb_files',$w,$limit);
	}
	
	function getTheEvent($pID,$type){
		global $b_webPath;
	$back='';
//	$w='  WHERE  (post_status=\'future\' OR post_status=\'publish\')  AND post_type=\'tribe_events\'';
	$w='  WHERE  meta_key=\'_EventStartDate\' ';
	$limit=' ORDER BY wp_postmeta.meta_value ';
	$jw=' wp_posts.ID=wp_postmeta.post_id AND wp_posts.post_type=\'tribe_events\' AND wp_posts.post_status=\'publish\' ';
//	$jw=' wp_postmeta.post_id=wp_posts.ID AND meta_key=\'bash_dest_father\'  AND meta_value='.$pID.'';
	$jTBL2='';$jOnWhere2=''; 
//	$limit=' ORDER BY wp_posts.ID '; //LIMIT 0,10
		if($type=='side'){}	
	$res=$this->bashDB->selectJ('*','wp_postmeta','wp_posts',$jw,$jTBL2,$jOnWhere2,$w,$limit);
	
		if($res!=false){
			foreach($res as $i=>$v){
			$ww2=' WHERE meta_key=\'bash_dest_father\' AND meta_value='.$pID.' AND post_id='.$res[$i]['ID'].' ';
		    $isID=$this->bashDB->select('*','wp_postmeta',$ww2,'');
				if($isID!=false){
					if($type=='side'){				
					$back.='<div id="" class="bash_side_event"><a href="'.$b_webPath.'event/'.$res[$i]['post_name'].'">'.$res[$i]['post_title'].'</a></div>';
					}
					else if($type=='page'){}
				}
			}
		
		}
		else{ $back='לא נמצאו אירועים ליעד זה';}
		if($back!=''){	echo $back;	}else {echo 'לא נמצאו אירועים ליעד זה'; }

	}
	function bs_rollOptions($r,$val){
		$back='';
		foreach ($r as $i=>$v){
		$back.='<option value="'.$i.'" ';
		if($i==$val){$back.=' selected="selected" ' ;}
		if($i=='tLeader' || $i=='artiPost'){$back.=' disabled="disabled" ';}
		$back.='>'.$v.'</option>';		
		}
		return $back;
	}
	
	
	
	
	
	
	
	




	function bs_displayFileInDest($arr,$type,$dPagen){
		global $b_gPath;
global $b_webPath;
//$back='<div id="bashro_main_resDiv">';
$back='';
		if($arr==false){
			if($type=='img'){		$back.='<div class="centMe"> לא נמצאו תמונות ליעד זה</div>';}
			else if($type=='file'){		$back.='<div class="centMe">לא נמצאו קבצים הקשורים ליעד זה</div>';}			
		}
		else{	
			if($type=='img'){
				$back.='<div id="thumbGrid"  style="" data-thumbgrid="true" data-effect="zoomIn" data-delay="60" data-timing="800" data-galleryeffectnext="zoomIn" data-galleryeffectprev="zoomOut" data-pagination="'.$dPagen.'" >';
				}
			foreach($arr as $i=>$v){
				if($type=='img'){
				$back.='<img class="dest_img_img" src="'.$b_webPath.'wp-content/plugins/wp-filebase/wp-filebase_thumb.php?fid='.$arr[$i]['file_id'].'.png" alt="'.$arr[$i]['file_display_name'].'" data-highres="'.$b_webPath.'download/'.$arr[$i]['file_path'].'" 
				data-caption="'.$arr[$i]['file_display_name'].'"/>';

				}
				else{
				$sizeR= $this->bs_display_size($arr[$i]['file_size']);
				$size_type=$sizeR[1];	$tmp_size=$sizeR[0];
				$back.='<div class="dest_file_li" id="bash_res_'.$arr[$i]['file_id'].'" title="'.$arr[$i]['file_display_name'].'"><img src="'.$b_webPath.'bash-xtra/img/'.$this->getFileIcon($arr[$i]['file_path']).'" class="bash_fileIcon" />'.$arr[$i]['file_display_name'].' <div class="bs_info_div" >('.$tmp_size.' '.$size_type.')</div></div> <div style="text-align:left"><a class="dest_file_li_a" href="'.$b_webPath.'download/'.$arr[$i]['file_path'].'" target="_blank">הורדה\צפייה</a></div>';	
				}
			}
			if($type=='img'){$back.='</div> ';}
		}
//		return $back.'</div>';		
		return $back;
	}
	
	function bs_display_size($fSize){
	$size_type='bytes';		$tmp_size='';
		if($fSize<1000){$tmp_size=$fSize;}
		else if($fSize<1000000){$tmp_size=round($fSize/1000,2); $size_type='KB';}
		else{$tmp_size=round($fSize/1000000,2); $size_type='MB';}
		return array($tmp_size,$size_type);
	}
	
	function bs_getTheFiles($dID,$isUse,$fType,$resNum,$fDate,$sDate){
	$roleW='';
	if($dID=='h'){$xtraID='';}
	else{$xtraID=" file_version=".$dID."  AND ";}
		if($fType=='img'){ $likeW=' LIKE '; $oaAnd=' OR ';}
		else{ $likeW=' NOT LIKE';  $oaAnd=' AND ';}
		if($isUse==false){ $roleW.=" AND file_user_roles=''";}		
	$w=" WHERE (".$xtraID." file_offline=1 ".$roleW.") AND ( file_path ".$likeW." '%png%' ".$oaAnd."  file_path  ".$likeW." '%gif%' ".$oaAnd."  file_path  ".$likeW." '%jpg%') ";	
	$back.='';
	
	$limit=' ORDER BY file_date DESC ';
	if($resNum>0){$limit.=' LIMIT 0,'.$resNum;}
		
		return $this->bashDB->select('*','wp_wpfb_files',$w,$limit);
	}




	function bs_getTheArrObj($dID,$isUse,$fType,$resNum,$fDate,$sDate){
	$roleW='';
	if($dID=='h' || $dID=='' || $dID=='0'){$xtraID='';}
	else{$xtraID=" file_version=".$dID."  AND ";}
		if($fType=='img'){ $likeW=' LIKE '; $oaAnd=' OR ';}
		else{ $likeW=' NOT LIKE';  $oaAnd=' AND ';}
		if($isUse==false){ $roleW.=" AND file_user_roles=''";}		
	$w=" WHERE (".$xtraID." file_offline=1 ".$roleW.") AND ( file_path ".$likeW." '%png%' ".$oaAnd."  file_path  ".$likeW." '%gif%' ".$oaAnd."  file_path  ".$likeW." '%jpg%') ";	
	$back.='';
	
	$limit=' ORDER BY file_date DESC ';
	if($resNum>0){$limit.=' LIMIT 0,'.$resNum;}
		
		return $this->bashDB->select('*','wp_wpfb_files',$w,$limit);
	}




	function bs_rollFileCats(){
	$back='';
	$faCR=array();
	$sonCR=array();		
	$cR=$this->bashDB->select('*','wp_wpfb_cats',' ',' ORDER BY cat_name');
		if($cR!=false){
			$back.='<option value="ALL" >כל הקטגוריות</option>';
			foreach($cR as $i=>$v){
				if(	$cR[$i]['cat_parent']==0){$faCR[$i]=$cR[$i];}
				else{$sonCR[$i]=$cR[$i];}
			}	
			foreach($faCR as $ii=>$vv){
			$f_ID=$faCR[$ii]['cat_id'];
			$back.='<option value="f-'.$f_ID.'">'.$faCR[$ii]['cat_name'].'</option>';
				foreach($sonCR as $iii=>$vvv){
					if($sonCR[$iii]['cat_parent']==$f_ID){
					$back.='<option value="s-'.$sonCR[$iii]['cat_id'].'">&nbsp;&nbsp;-&nbsp;'.$sonCR[$iii]['cat_name'].'</option>';
					}
				}
			}
			return $back;
		}
		else{ return '<option value="">לא נמצאו קטגוריות</option>';}
	}

	function bs_getTheArrNewObj($arr,$isUse,$resNum){
	$roleW='';
	
		if($arr['type']=='img' || $arr['type']=='file'){
			if($arr['id']=='h' || $arr['id']=='' || $arr['id']=='0'){$xtraID='';}
			else{$xtraID=" file_version=".$arr['id']."  AND ";}
//		if($arr['cID']!='' && $arr['cID']=='img'
			if($arr['type']=='img'){ $likeW=' LIKE '; $oaAnd=' OR ';}	
			else{ $likeW=' NOT LIKE';  $oaAnd=' AND ';}
	
			
			if($arr['cID']!='ALL'){
			 $cID_arr=explode('-',$arr['cID']);
			 $roleW.=" AND file_category=".$cID_arr[1]." ";
			 }
			if(str_replace(' ','',$arr['sTitle'])!='' ){
			 $roleW.=" AND file_display_name LIKE '%".$arr['sTitle']."%' ";
			 }
			 
				
//			if($arr['sD']!='' && $arr['sD']!='0'){
			 if(str_replace('-', "", $arr['sD'])>0 && $arr['sD']!=''){
			 $roleW.=" AND file_date >= '".$arr['sD']." 00:00:00' ";
			 }
			 if(str_replace('-', "", $arr['fD'])>0 && $arr['fD']!=''){
			 $roleW.=" AND file_date <= '".$arr['fD']." 23:59:59' ";
			 }			 

			 if($isUse==false){ $roleW.=" AND file_user_roles='' ";}		
		$w=" WHERE (".$xtraID." file_offline=1 ".$roleW.") AND ( file_path ".$likeW." '%png%' ".$oaAnd."  file_path  ".$likeW." '%gif%' ".$oaAnd."  file_path  ".$likeW." '%jpg%') ";	
	
		$limit=' ORDER BY file_date DESC ';
			if($resNum>0){$limit.=' LIMIT 0,'.$resNum;}

		}
		elseif($arr['type']=='tLeader'){}	
		elseif($arr['type']=='artiPost'){}		
		else{}

	return $this->bashDB->select('*','wp_wpfb_files',$w,$limit);
	}
	
	
	
	
}




?>