<?php
require_once(ABSPATH.'/wp-content/themes/fortis7/bash_pathFile.php');
require_once(ABSPATH.'/funcFile.php');
require_once(ABSPATH.'/myC/dbC.php');
require_once(ABSPATH.'/wp-content/themes/fortis7/dest-class.php');
require_once(ABSPATH.'/wp-content/themes/fortis7/bWeather-class.php');
require_once(ABSPATH.'/wp-content/themes/fortis7/bAuthor-class.php');

$b_D=new bash_dest();
$b_wather= new bash_weather();
$b_auth=new bash_author();

function checkMyID(){
	if(get_current_user_id()=='0'){return '2';}
	else{return get_current_user_id();}
}
function bash_curURL() {
$pageURL = 'http';
// if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
	if ($_SERVER["SERVER_PORT"] != "80") {$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];}
	else {$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];}
 return $pageURL;
}

add_action('init', 'bashro_register_type_destination');
function bashro_register_type_destination() {
register_post_type('bash_destination', array(
'label' => 'יעדים',
'description' => 'יעדים בעולם',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'destinations', 'with_front' => true),
'query_var' => true,
'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes','page-formats'),
'labels' => array (
  'name' => 'יעדים',
  'singular_name' => 'יעד',
  'menu_name' => 'יעדים',
  'add_new' => 'הוספת יעד',
  'add_new_item' => 'הוספת יעד חדש',
  'edit' => 'עריכה',
  'edit_item' => 'עריכת יעד',
  'new_item' => 'יעד חדש',
  'view' => 'צפה ביעד',
  'view_item' => 'צפה ביעד',
  'search_items' => 'חיפוש יעדים',
  'not_found' => 'לא נמצאו יעדים',
  'not_found_in_trash' => 'לא נמצאו יעדים בפח האשפה',
  'parent' => 'יעד אב',
)
) );
//	flush_rewrite_rules();  // REWRITE RULE FOR PAGES ???
}
/***/
add_action( 'add_meta_boxes', 'bashro_add_meta_box' );
function bashro_add_meta_box(){
    add_meta_box( 'bash_dest_type', 'פרטי יעד', 'bashro_echo_my_meta', 'bash_destination', 'normal', 'high' );
    add_meta_box( 'bash_dest_en_name', 'שם באנגלית', 'bash_emptyOne', 'bash_destination', 'normal', 'low' );
    add_meta_box( 'bash_dest_father', 'יעד אב', 'bash_emptyOne', 'bash_destination', 'normal', 'low' );
    add_meta_box( 'bash_moveEditor', 'תיאור כללי', 'bash_emptyOne', 'bash_destination', 'normal', 'high' );


}

function bash_emptyOne(){} //DON'T DELETE !IMPORTENT

function bashro_echo_my_meta($post){
global $b_D;
$pVal= get_post_custom( $post->ID );
	$dType='';
	$contDisable='';
	foreach($b_D->dest_type as $i=>$v){
	$slctd='';		
		if($i==0){
			if($pVal['bash_dest_type'][0]==0 && $pVal['bash_dest_type'][0]!=''){
			$dType.='<option value="'.$i.'"  selected>'.$v.'</option>';
			$contDisable='disabled';
			}
		}
		else{	
			if($i==$pVal['bash_dest_type'][0]){$slctd='selected';}
		$dType.='<option value="'.$i.'"  '.$slctd.'>'.$v.'</option>';
		}
	}
	$fID=$pVal['bash_dest_father'][0];
	$fName='כתבו ובחרו מהרשימה שתופיע';
	$fType='';
	if($fID!='' && $fID>0){
	$fPost=get_post($fID);
	$fCustom=get_post_custom($fID);
	$fName=$fPost->post_title;
	$fType='&nbsp; ('.$b_D->dest_type[$fCustom['bash_dest_type'][0]].')';
	}
echo '<div id="bash_moveEditor"></div>';
echo '<label for="bash_dest_type"><b class="redMust">* סוג יעד:</b> </label> <select name="bash_dest_type" id="bash_dest_type" '.$contDisable.'>'.$dType.'</select>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<label for="cSearch"><b class="redMust">* יעד אב:</b> </label><div style="display:inline-block"><input type="text" '.$contDisable.' onkeyup="bashSearchCity(this.id);" placeholder="'.$fName.' '.$fType.'" value="" id="cSearch" name="cSearch" class="fSearch" autocomplete="off" ><br>
<div id="showMyLike"></div></div>
<input type="hidden" value="'.$fID.'" id="bash_dest_father" name="bash_dest_father" />';
echo '&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<label for="bash_dest_en_name"><b class="redMust">* שם יעד באנגלית:</b> </label> <input type="text" '.$contDisable.' name="bash_dest_en_name" id="bash_dest_en_name" value="'.$pVal['bash_dest_en_name'][0].'">';
}


/***/
add_action( 'save_post', 'bash_xtra_data_s' );
function bash_xtra_data_s( $post_id ){
    if( !current_user_can( 'edit_post' ) ) return false;
	if( !isset($_POST['bash_dest_father']) || $_POST['bash_dest_father']=='' || $_POST['bash_dest_father']==0){	return false;}
	else if( !isset($_POST['bash_dest_en_name']) || $_POST['bash_dest_en_name']=='' ){return false;}
	else if(!isset($_POST['bash_dest_type']) || $_POST['bash_dest_type']==''){ return false;}
	else{
	  // Make sure your data is set before trying to save it
	    if( isset( $_POST['en_dest_name'] ) ){$allowed = array('a' => array( 'href' => array()));	}
	 update_post_meta( $post_id, 'bash_dest_type', wp_kses( $_POST['bash_dest_type'], $allowed ) );
	 update_post_meta( $post_id, 'bash_dest_en_name', wp_kses( $_POST['bash_dest_en_name'], $allowed ) );
	 update_post_meta( $post_id, 'bash_dest_father', wp_kses( $_POST['bash_dest_father'], $allowed ) );
	}
}

/*allow to move editor*/
function move_posteditor( $hook ) {
    if ( $hook == 'post.php' OR $hook == 'post-new.php' ) {
        wp_enqueue_script( 'jquery' );
        add_action('admin_print_footer_scripts', 'move_posteditor_scripts');
   }
}

/***/
add_action( 'admin_enqueue_scripts', 'move_posteditor', 10, 1 );
function move_posteditor_scripts() {
	global $b_webPath;
    ?>
    <script type="text/javascript">
        jQuery('#postdiv, #postdivrich').prependTo('#bash_moveEditor .inside' );
</script>
<?php }?>
<?php wp_enqueue_script( 'bashSearchCity', $b_webPath.'bash-xtra/bash-js.js'); ?>
<?php wp_enqueue_script( 'fix_admin_display', $b_webPath.'bash-xtra/bash_admin.js'); ?>
<?php // wp_enqueue_script( 'autocomplete', $b_webPath.'bash-xtra/jq-ui.js'); ?>
<?
/***/
add_action('add_meta_boxes', 'bash_dest_inc');
add_action('save_post', 'bash_dest_inc_save');
function bash_dest_inc() {
    add_meta_box('bash_show_dest_inc', 'שיוך ליעד', 'bash_show_dest_inc', 'post', 'side');
	add_meta_box('bash_show_dest_inc', 'שיוך ליעד', 'bash_show_dest_inc', 'page', 'side');
    add_meta_box('bash_show_dest_inc', 'שיוך ליעד', 'bash_show_dest_inc', 'cma_thread', 'side','high');	
    add_meta_box('bash_show_dest_inc', 'שיוך ליעד', 'bash_show_dest_inc', 'tribe_events', 'side','high');
    add_meta_box('bash_show_dest_inc', 'שיוך ליעד', 'bash_show_dest_inc', 'tribe_venue', 'side','high');	
//    add_meta_box('bash_show_dest_inc', 'כתובת מלאה', 'bash_show_dest_inc', 'tribe_venue', 'side','high');		
}
function bash_show_dest_inc($post){
global $b_D;
$pVal= get_post_custom( $post->ID );
$fID=$pVal['bash_dest_father'][0];
	$fName='כתבו ובחרו מהרשימה שתופיע';
	$fType='';
	if($fID!='' && $fID>0){
	$fPost=get_post($fID);
	$fCustom=get_post_custom($fID);
	$fName=$fPost->post_title;
	$fType='&nbsp; ('.$b_D->dest_type[$fCustom['bash_dest_type'][0]].')';
	}
	
echo '<label for="cSearch">בחרו יעד:</label><div style="display:inline-block"><input type="text" onkeyup="bashSearchCity(this.id);" placeholder="'.$fName.' '.$fType.'" value="" id="cSearch" name="cSearch" class="fSearch" autocomplete="off" ><br>
<div id="showMyLike"></div></div>
<input type="hidden" value="'.$fID.'" id="bash_dest_father" name="bash_dest_father" />';	
}
function bash_dest_inc_save($post_id) {
	if(isset($_POST['bash_dest_father']) && $_POST['bash_dest_father']!='' && $_POST['bash_dest_father']>0){
		update_post_meta( $post_id, 'bash_dest_father', wp_kses( $_POST['bash_dest_father'], $allowed ) );
	}
}

/*add font-size to editor*/
function wp_editor_fontsize_filter( $buttons ) {
        array_shift( $buttons );
        array_unshift( $buttons, 'fontsizeselect');
        array_unshift( $buttons, 'formatselect');
        return $buttons;
}  

add_filter('mce_buttons_2', 'wp_editor_fontsize_filter');




/*add_action('add_meta_boxes', 'bash_u_data_add');
function bash_u_data_add() {
    add_meta_box('bash_show_u_xtra', 'הגדרות תצוגה', 'bash_u_admin_disp', 'post', 'side');
}
*/

//add_action('admin_head', 'biographical_info_tinymce');
//remove_filter('pre_user_description', 'wp_filter_kses');  
//add_filter( 'pre_user_description', 'wp_filter_post_kses' ); 





?><?php
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );

function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("נתונים שיוצגו באתר", "blank"); ?></h3>

<table class="form-table">
<tr>
<th><label for="address"><?php _e("טלפון לתצוגה"); ?></label></th>
<td>
<input type="text" name="to_display_phone" id="to_display_phone" value="<?php echo esc_attr( get_the_author_meta( 'to_display_phone', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("מס' הטלפון שיוצג לכלל הגולשים באתר"); ?></span>
</td>
</tr>
<tr>
<th><label for="to_display_mail"><?php _e("ד.אלקטרוני לתצוגה"); ?></label></th>
<td>
<input type="text" name="to_display_mail" id="to_display_mail" value="<?php echo esc_attr( get_the_author_meta( 'to_display_mail', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("ד.אלקטרוני שיוצג לכלל הגולשים באתר"); ?></span>
</td>
</tr>
<tr>
<th><label for="to_display_address"><?php _e("מען"); ?></label></th>
<td>
<input type="text" name="to_display_address" id="to_display_address" value="<?php echo esc_attr( get_the_author_meta( 'to_display_address', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("רישמו את כתובתכם המלאה."); ?></span>
</td>
</tr>
<tr>
<th><label for="to_display_lang"><?php _e("שפות הדרכה"); ?></label></th>
<td>
<input type="text" name="to_display_lang" id="to_display_lang" value="<?php echo esc_attr( get_the_author_meta( 'to_display_lang', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("יש את כל השפות בהן אתם נותנים שירותי הדרכה"); ?></span>
</td>
</tr>
</table>
<?php }

add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );

function save_extra_user_profile_fields( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }

update_user_meta( $user_id, 'to_display_phone', $_POST['to_display_phone'] );
update_user_meta( $user_id, 'to_display_mail', $_POST['to_display_mail'] );
update_user_meta( $user_id, 'to_display_address', $_POST['to_display_address'] );
update_user_meta( $user_id, 'to_display_lang', $_POST['to_display_lang'] );
}
?>