<?php
/*
  Template Name: User profile
 */
wp_enqueue_script('user_profile_js', THEMEMAKERS_THEME_URI . '/js/user_profile.js');

get_header();

?>
<!-- ************ - BEGIN Content Wrapper - ************** -->



<?php
$profile_user_id = 0;
if (isset($_GET['user_id'])) {
    $profile_user_id = (int) $_GET['user_id'];
	
	echo "isset get user id";
	
} else {
    $profile_user_id = get_current_user_id();
}

if ($profile_user_id) {
    $user = ThemeMakersUser::get_user_info($profile_user_id);
}
?>

	<?php  $author = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); ?>
	<?php $profile_user_id=$author->ID; ?>
				
       <div class="title-caption-large">
		   
		   <?php if (is_user_logged_in() AND $profile_user_id != get_current_user_id()){ ?>
                            <?php if (!ThemeMakersUser::is_user_in_favourites($profile_user_id)): ?>
                               
                                <a href="#" class="js_add_to_favorites button small yellow" user-id="<?php echo $profile_user_id ?>"><?php _e('ADD TO FAVORITES', THEMEMAKERS_THEME_FOLDER_NAME); ?></a>
                                
                            <?php else: ?>
                                <span><?php _e('User in Your favorites list!', THEMEMAKERS_THEME_FOLDER_NAME); ?></span>
                            <?php endif; ?>
              <?php }; ?>

            <h3><?php echo $author->user_nicename ?><?php _e(' profile', THEMEMAKERS_THEME_FOLDER_NAME); ?></h3>
			
			 

        </div><!--/ .title-caption-->

        <table class="profile-list">

            <thead>
                <tr>
                    <th><span><?php _e('Role', THEMEMAKERS_THEME_FOLDER_NAME); ?></span></th>
                    <th><span><?php _e('Avatar', THEMEMAKERS_THEME_FOLDER_NAME); ?></span></th>
                    <th><span><?php _e('Name', THEMEMAKERS_THEME_FOLDER_NAME); ?></span></th>
                    <th><span><?php _e('Contact Info', THEMEMAKERS_THEME_FOLDER_NAME); ?></span></th>
                    <th><span><?php _e('About', THEMEMAKERS_THEME_FOLDER_NAME); ?></span></th>
                </tr>
            </thead>

            <tbody>
                <tr>
                    <td>
                        <?php echo $author->roles['0']; ?><br />
                       
                    </td>
                    <td>
                        <?php echo get_avatar($profile_user_id, THEMEMAKERS_THEME_URI . '/images/avatar.png'); ?>
                    </td>
                    <td>
                        <p class="input-block">
                            <b><?php _e('Nickname', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</b>
                            <span><?php echo $author->nickname; ?></span>
                        </p>
						<?php if ($author->first_name){ ?>
                        <p class="input-block">
                            <b><?php _e('First Name', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</b>
                            <span><?php echo $author->first_name; ?></span>
                        </p>
						<?php } ?>
						<?php if ($author->last_name){ ?>
                        <p class="input-block">
                            <b><?php _e('Last Name', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</b>
                            <span><?php echo $author->last_name; ?></span>
                        </p>
						<?php } ?>
						
                    </td>
                    <td>
						
						<?php if (get_the_author_meta('url', $author->ID)){ ?>
                        <p class="input-block">
                            <b><?php _e('Website', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</b>
                            <span><a href="<?php echo $user['url'] ?>" target="_blank"><?php echo get_the_author_meta('url', $author->ID); ?></a></span>
                        </p>
						<?php } ?>
						<?php if (get_the_author_meta('aim', $author->ID)){ ?>
                        <p class="input-block">
                            <b><?php _e('AIM', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</b>
                            <span><?php echo get_the_author_meta('aim', $author->ID); ?></span>
                        </p>
						<?php } ?>
						<?php if (get_the_author_meta('yim', $author->ID)){ ?>
                        <p class="input-block">
                            <b><?php _e('Yahoo IM', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</b>
                            <span><?php echo get_the_author_meta('yim', $author->ID); ?></span>
                        </p>
						<?php } ?>
						<?php if (get_the_author_meta('skype', $author->ID)){ ?>
                        <p class="input-block">
                            <b><?php _e('Skype', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</b>
                            <span><?php echo get_the_author_meta('skype', $author->ID); ?></span>
                        </p>
						<?php } ?>
						<?php if (get_the_author_meta('icq', $author->ID)){ ?>
                        <p class="input-block">
                            <b><?php _e('ICQ', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</b>
                            <span><?php echo get_the_author_meta('icq', $author->ID); ?></span>
                        </p>
						<?php } ?>
						<?php if (get_the_author_meta('jabber', $author->ID)){ ?>						
                        <p class="input-block">
                            <b><?php _e('Jabber', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</b>
                            <span><?php echo get_the_author_meta('jabber', $author->ID);?></span>
                        </p>
						<?php } ?>
						<?php if (get_the_author_meta('facebook', $author->ID)){ ?>						
                        <p class="input-block">
                            <b><?php _e('Facebook', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</b>
                            <span><?php echo get_the_author_meta('facebook', $author->ID); ?></span>
                        </p>
						<?php } ?>
						<?php if (get_the_author_meta('twitter', $author->ID)){ ?>
                        <p class="input-block">
                            <b><?php _e('Twitter', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</b>
                            <span><?php echo get_the_author_meta('twitter', $author->ID); ?></span>
                        </p>
						<?php } ?>
                    </td>
                    <td>
                        <p class="textarea-block">
                            <?php echo get_the_author_meta('description', $author->ID)?>
                        </p>
                    </td>
                </tr>
            </tbody>

        </table><!--/ .profile-list-->



        <div class="my-list">

            <div class="title-caption-large clearfix">

                <h3><?php _e('User interests', THEMEMAKERS_THEME_FOLDER_NAME); ?></h3>

            </div><!--/ .title-caption-->

            <table class="my-list-entry">

                <thead>

                    <tr>
                        <th>
                            <?php _e('Games', THEMEMAKERS_THEME_FOLDER_NAME); ?>
                        </th>
                        <th>
                            <?php _e('Videos', THEMEMAKERS_THEME_FOLDER_NAME); ?>
                        </th>
                        <th><?php _e('Favorite users', THEMEMAKERS_THEME_FOLDER_NAME); ?></th>
                    </tr>

                </thead>

                <tbody>

                    <tr>
                        <td style="width:27%;">

                            <ul class="gameList">

                                <?php if (!empty($posts)): ?>
								

							<?php 	
							
								$my_games = get_the_author_meta('my_games', $author->ID);

								if (!empty($my_games)) {
									
									
									$my_games = implode(',', $my_games);
																	
									$my_games_consoles = get_the_author_meta('my_games_consoles', $author->ID);

									global $wpdb;
									
									$author_my_games = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID IN($my_games) AND post_type= 'games' AND post_status='publish'");
								} else {
									$author_my_games = array();
								}
													
								
								?>	
								
                               <?php foreach ($author_my_games as $post) : ?>
								
								
                                     <?php $permalink = get_permalink($post->ID); ?>
									
									<li class="game_id_<?php echo $post->ID ?>">
											
											<a href="<?php echo $permalink; ?>"><?php echo $post->post_title ?></a>
											
											<?php $games_terms = wp_get_post_terms($post->ID, 'cons'); ?>
											<?php if (!empty($games_terms)){ ?>
												<?php foreach ($games_terms as $games_term){ ?>
												<?php echo "("; ?>
													<a href="<?php echo bloginfo('url');?>/?cons=<?php echo $games_term->slug ?>"><?php echo $games_term->name ?></a>
												<?php echo ")"; ?>
												<?php } ?>
											<?php } ?>
													
											 <div class="clear"></div>

                                            <input type="hidden" name="my_games[<?php echo $post->ID ?>]" value="<?php echo $post->ID ?>" />
                                        </li>

                                    <?php endforeach; ?>

                                <?php endif; ?>


                            </ul><!--/ .gameList-->


                            <?php wp_reset_query() ?>
                        </td>

                        <td>
							<?php $youtube_items = get_the_author_meta('youtube_items', $author->ID); ?>
							
							
							
						<ul id="youtube_list">
								
                                <?php if (!empty($youtube_items)): ?>
                                    <?php foreach ($youtube_items['title'] as $key => $title) : ?>
                                        <li>
                                            <div class="frame">
                                                <span><?php echo $title ?></span>
                                                <?php
												$video_url=$youtube_items['link'][$key];
												
												
												 if (!empty($video_url)) {
												$video_type = 'youtube.com';
												$allows_array = array('youtube.com', 'vimeo.com', '.mp4');

												foreach ($allows_array as $key => $needle) {
													$count = substr_count($video_url, $needle);
													if ($count) {
														$video_type = $allows_array[$key];
													}
												}

												switch ($video_type) {
													case $allows_array[0]:
														$video_url = explode("?v=", $video_url);
														$video_url = explode("&", $video_url[1]);
														if (is_array($video_url)) {
															$video_url = $video_url[0];
														}
														echo do_shortcode('[video type="youtube" html5_poster="" html5_video_url="" src_webm="" src_ogg="" width="140" height="90"]' . $video_url . '[/video]');
														break;
													case $allows_array[1]:
														$video_url = explode("/", $video_url);
														if (is_array($video_url)) {
															$video_url = $video_url[count($video_url) - 1];
														}
														echo do_shortcode('[video type="vimeo" html5_poster="" html5_video_url="" src_webm="" src_ogg="" width="140" height="90"]' . $video_url . '[/video]');
														break;

													default:
														$html5_poster = THEMEMAKERS_THEME_URI . "/images/video_poster.jpg";
														if (has_post_thumbnail($first_preview->ID)) {
															$html5_poster = ThemeMakersHelper::get_post_featured_image($first_preview->ID, 'slider_showcase');
														}
														echo do_shortcode('[video type="html5" html5_poster="' . $html5_poster . '" html5_video_url="' . $video_url . '" src_webm="" src_ogg="" width="' . $video_width . '" height="' . $video_height . '"][/video]');
														break;
												}
											}
												
                                               										
                                                //echo do_shortcode('[video type="youtube" html5_poster="" html5_video_url="" src_webm="" src_ogg="" width="140" height="90"]' . $video_url . '[/video]');
                                                ?>
                                            </div>
																						
											<div class="clear"></div>
                                        </li>
										
                                    <?php endforeach; ?>
                                <?php endif; ?>
										
                           </ul>
							
                        </td>

                        <td style="width:15%;">
                            <?php $favourites_users = ThemeMakersUser::get_favourites_users($profile_user_id); ?>
                            <ul id="favourites_user_list">
                                <?php foreach ($favourites_users as $key => $value) : ?>
                                    <li><a href="<?php echo $_SERVER['SCRIPT_URI'] ?>?author=<?php echo $value->ID ?>"><?php echo(empty($value->display_name) ? $value->user_nicename : $value->display_name) ?></a></li>
                                <?php endforeach; ?>
                            </ul>
                        </td>

                    </tr>

                </tbody>

            </table><!--/ .my-list-entry-->

        </div><!--/ .my-list-->

        <?php if (user_can($profile_user_id, 'edit_posts')): ?>

            <div class="my-list">

                <div class="title-caption-large clearfix">

                    <h3><?php _e('User Reviews', THEMEMAKERS_THEME_FOLDER_NAME); ?></h3>

                </div><!--/ .title-caption-->

                <table class="my-list-entry">

                    <thead>

                        <tr>
                            <th><?php _e('Reviews', THEMEMAKERS_THEME_FOLDER_NAME); ?></th>
                            <th><?php _e('Rating', THEMEMAKERS_THEME_FOLDER_NAME); ?></th>
                        </tr>

                    </thead>

                    <tbody>

                        <?php
                        query_posts(array(
                            'post_type' => 'review',
                            'order' => 'DESC',
                            'orderby' => 'date',
                            'author' => $author->ID,
                            'showposts' => -1
                        ));
                        ?>

                        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>


                                <?php
                             //   $terms = wp_get_post_terms($post->ID, 'cons');
                               
                                $permalink = get_permalink($post->ID);


                              
                                ?>

                                <tr>
                                    <td style="width: 90%">
                                        <ul class="reviewList">
											<li>
											<a href="<?php the_permalink() ?>"><?php echo $post->post_title ?></a>
											
											<?php $games_terms = wp_get_post_terms($post->ID, 'cons'); ?>
											<?php if (!empty($games_terms)){ ?>
												<?php foreach ($games_terms as $games_term){ ?>
												<?php echo "("; ?>
													<a href="<?php echo bloginfo('url');?>/?cons=<?php echo $games_term->slug ?>"><?php echo $games_term->name ?></a>
												<?php echo ")"; ?>
												<?php } ?>
											<?php } ?>
																					
											</li>
										</ul>
                   
                                       
                                    </td>
                                    <td style="width: 10%">
                                        <?php echo Thememakers_Entity_Rating::draw_rating($post->ID) ?>
                                    </td>
                                </tr>

                            <?php endwhile; ?>
                        <?php endif; ?>


                        <?php wp_reset_query() ?>

                    </tbody>

                </table><!--/ .my-list-entry-->

            </div><!--/ .my-list-->
        

<?php endif; ?>


<?php if (is_user_logged_in()): ?>
    <div id="games" class="dialog-inline clearfix">

        <div class="games_search">

            <h5><?php _e('Choose games you\'re playing', THEMEMAKERS_THEME_FOLDER_NAME); ?></h5>

            <input class="medium" type="text" value="" placeholder="Search games"  id="games_search_name"  />&nbsp;<button type="button" class="button yellow medium" id="games_search_button"><?php _e('Search', THEMEMAKERS_THEME_FOLDER_NAME); ?></button><br />

            <div id="games_search_results" class="games_search_results">
                <ul></ul>
            </div>

        </div>
		

        

        <div class="games_list">

            <h5><?php _e('YOUR games list', THEMEMAKERS_THEME_FOLDER_NAME); ?></h5>

            <ul class="dialog-list gameList">

                <?php if (!empty($posts)): ?>

                    <?php foreach ($user['my_games'] as $post) : ?>
                        <?php
                        $permalink = get_permalink($post->ID);
                       // $terms = wp_get_post_terms($post->ID, 'cons');
                        $featured_image = "";
                        if (has_post_thumbnail($post->ID)) {
                            $featured_image = ThemeMakersHelper::get_post_featured_image($post->ID, 'single_thumb2');
                        }
                        ?>

                        <li class="game_id_<?php echo $post->ID ?>">

                            <div class="caption">

                                <span><?php echo $post->post_title ?></span>
                                        

                              

                                    <?php if (!empty($terms)): ?>

                                        <?php foreach ($terms as $term) : ?>
                                            <?php
                                            $cons_color = get_taxonomy_term_type('cons', $term->term_id);
                                            $checked = $user['my_games_consoles'][$post->ID][$term->term_id] == 1 ? true : false;
                                            ?>

                                            <div style="background-color: <?php echo $cons_color ?>" class="game_console"><?php echo $term->name ?>
                                                &nbsp;<input type="checkbox" class="my_games_console_checkbox" value="<?php echo $post->ID ?>_<?php echo $term->term_id ?>" <?php echo($checked ? 'checked="checked"' : '') ?> />
                                                <input type="hidden" name="my_games[<?php echo $post->ID ?>]" value="0" class="hidden_console_value_<?php echo $post->ID ?>_<?php echo $term->term_id ?>" />
                                                <input type="hidden" name="my_games_consoles[<?php echo $post->ID ?>][<?php echo $term->term_id ?>]" value="<?php echo($checked ? 1 : 0) ?>" class="hidden_console_value_<?php echo $post->ID ?>_<?php echo $term->term_id ?>" />
                                            </div>

                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                

                                <button type="button" class="remove_from_my_games button yellow small" game_id="<?php echo $post->ID ?>"><?php _e('Remove This Game', THEMEMAKERS_THEME_FOLDER_NAME); ?></button>

                            </div><!--/ .caption-->

                            <div class="clear"></div>

                            <input type="hidden" name="my_games[<?php echo $post->ID ?>]" value="<?php echo $post->ID ?>" />

                        </li>

                    <?php endforeach; ?>

                <?php endif; ?>

            </ul><!--/ .gameList-->

        </div>
		<div class="clear"></div>
		
        <a href="javascript:jQuery.fancybox.close();void(0);" class="button small yellow search_game_close"><?php _e('Close', THEMEMAKERS_THEME_FOLDER_NAME); ?></a>

    </div><!--/ #inline-->

    <div style="display: none;">

        <div id="youtube_item_template">
            <li>

                <p class="input-block">
					
                    <input type="text" placeholder="title" name="youtube_items[title][]" value="" />
                </p>

                <p class="input-block">
                    <input type="text" placeholder="www.youtube.com" name="youtube_items[link][]" value="" />
                    <a href="#" title="Remove" class="remove remove_youtube_item"><?php _e('Remove', THEMEMAKERS_THEME_FOLDER_NAME); ?></a>
                </p>

            </li>

        </div>

    </div>

<?php endif; ?>

<?php get_footer(); ?>



