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

get_header();

if (!is_user_logged_in()){
	
	?>
<p>
	Please <a id="login" href="#login-popup">login</a> or <a id="create" href="#create-account">create an account</a> to access this page!
</p>
<?php
	
}

global $post;
?>
<!-- ************ - BEGIN Content Wrapper - ************** -->

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <?php the_content(); ?>
        <div class="clear"></div>
        <?php
    endwhile;
endif;
?>

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

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

<?php if (is_user_logged_in() AND !isset($_GET['user_id'])): ?>

		
		
    <form id="profile_form" name="profile_form" method="post">

        <div class="title-caption-large">
            <h3><?php the_title(); ?></h3>
        </div>

        <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><a class="edit" href="http://en.gravatar.com/" target="_blank">(<?php _e('edit', THEMEMAKERS_THEME_FOLDER_NAME); ?>)</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>
								<a href="#" class="button small yellow button_save_user_profile"><?php _e('Save All Settings', THEMEMAKERS_THEME_FOLDER_NAME); ?></a>
							</th>
						</tr>
					</thead>

					<tbody>
						<tr>
							<td>
								<?php echo $user['user_role'] ?>
							</td>
							<td>
								<?php echo get_avatar($user['user_id'],  THEMEMAKERS_THEME_URI . '/images/avatar.png'); ?>
							</td>
							<td>
								<p class="input-block">
									<label><?php _e('Nickname', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</label>
									<input type="text" placeholder="Games" name="nickname" value="<?php echo $user['nickname'] ?>" />
								</p>
								<p class="input-block">
									<label><?php _e('First Name', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</label>
									<input type="text" placeholder="John" name="first_name" value="<?php echo $user['first_name'] ?>" />
								</p>
								<p class="input-block">
									<label><?php _e('Last Name', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</label>
									<input type="text" placeholder="Smith" name="last_name" value="<?php echo $user['last_name'] ?>" />
								</p>
							</td>
							<td>
								
								 <p class="input-block">
									<label><?php _e('Website', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</label>
									<input type="text" placeholder="www.20script.ir" name="url" value="<?php echo $user['url'] ?>" />
								</p>
								<p class="input-block">
									<label><?php _e('AIM', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</label>
									<input type="text" placeholder="" name="aim" value="<?php echo $user['aim'] ?>" />
								</p>
								<p class="input-block">
									<label><?php _e('Yahoo IM', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</label>
									<input type="text" placeholder="" name="yim" value="<?php echo $user['yim'] ?>" />
								</p>
								<p class="input-block">
									<label><?php _e('Skype', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</label>
									<input type="text" placeholder="" name="skype" value="<?php echo $user['skype'] ?>" />
								</p>
								<p class="input-block">
									<label><?php _e('ICQ', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</label>
									<input type="text" placeholder="" name="icq" value="<?php echo $user['icq'] ?>" />
								</p>
								<p class="input-block">
									<label><?php _e('Jabber', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</label>
									<input type="text" placeholder="" name="jabber" value="<?php echo $user['jabber'] ?>" />
								</p>
								<p class="input-block">
									<label><?php _e('Facebook', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</label>
									<input type="text" placeholder="" name="facebook" value="<?php echo $user['facebook'] ?>" />
								</p>
								<p class="input-block">
									<label><?php _e('Twitter', THEMEMAKERS_THEME_FOLDER_NAME); ?>:</label>
									<input type="text" placeholder="" name="twitter" value="<?php echo $user['twitter'] ?>" />
								</p>
								
							</td>
							<td>
								<p class="textarea-block">
									<textarea name="description" id="textfield" cols="20" rows="10"><?php echo $user['description'] ?></textarea>
								</p>
							</td>
						</tr>
					</tbody>

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



        <div class="my-list">

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

				<a href="#" class="button small yellow button_save_user_profile"><?php _e('Save All Settings', THEMEMAKERS_THEME_FOLDER_NAME); ?></a>
				
                <h3><?php _e('My interests', THEMEMAKERS_THEME_FOLDER_NAME); ?></h3>
			
			</div><!--/ .title-caption-->

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

                <thead>

                    <tr>
                        <th>
                            <?php _e('Games / Consoles', THEMEMAKERS_THEME_FOLDER_NAME); ?>&nbsp;<a href="#games" title="Edit" id="games_popup" class="edit"><?php _e('Edit', THEMEMAKERS_THEME_FOLDER_NAME); ?></a>
                        </th>
                        <th>
                            <?php _e('My Videos', THEMEMAKERS_THEME_FOLDER_NAME); ?>&nbsp;<a id="add_youtube_item" href="#" title="Add" class="add"><?php _e('Add', THEMEMAKERS_THEME_FOLDER_NAME); ?></a>
                        </th>
                        <th><?php _e('Favorite users', THEMEMAKERS_THEME_FOLDER_NAME); ?></th>
                        <th>
                            <?php _e('Subscriptions', THEMEMAKERS_THEME_FOLDER_NAME); ?>
                        </th>
                    </tr>

                </thead>

                <tbody>

                    <tr>
                        <td>

                            <ul class="gameList">

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

                                    <?php foreach ($user['my_games'] as $post) { ?>
                                        <?php
                                        $permalink = get_permalink($post->ID);
										
                                        $games_terms = wp_get_post_terms($post->ID, 'cons');
										
																						
										
                                        ?>

                                        <li class="game_id_<?php echo $post->ID ?>">
											
											<a href="<?php echo $permalink ?>"><?php echo $post->post_title ?></a>
											
												<div class="caption table-cell">

												

														<?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 }; ?>

													

                                                <input type="button" value="<?php _e('Remove this game', THEMEMAKERS_THEME_FOLDER_NAME); ?>" class="remove_from_my_games button yellow small" game_id="<?php echo $post->ID ?>" /><br />

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

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

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

                                    <?php }; ?>

                                <?php }; ?>


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


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

                        <td>
                            <?php $youtube_items = $user['youtube_items']; ?>
                            <ul id="youtube_list_bk">
                                <?php if (!empty($youtube_items)){ ?>
                                    <?php foreach ($youtube_items['title'] as $key => $title) { ?>
                                        <li>
                                            <p class="input-block">
                                                <input type="text" placeholder="title" name="youtube_items[title][]" value="<?php echo $title ?>" />
												
												<input type="hidden" name="youtube_items[date][]" value="<?php echo $youtube_items['date'][$key] ?>"  >
												<a href="#" title="Remove" class="remove remove_youtube_item"><?php _e('Remove', THEMEMAKERS_THEME_FOLDER_NAME); ?></a>
                                            </p>
                                            <p class="input-block">
                                                <input type="text" placeholder="www.youtube.com/blablla" name="youtube_items[link][]" value="<?php echo $youtube_items['link'][$key] ?>" />
                                                
                                            </p>
                                        </li>
                                    <?php }; ?>
                                <?php }; ?>
                            </ul>
                        </td>

                        <td>

                            <?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 ThemeMakersUser::get_profile_url($value->ID) ?>"><?php echo(empty($value->display_name) ? $value->user_nicename : $value->display_name) ?></a>&nbsp;<a href="#" class="js_delete_favourite_user remove" user-id="<?php echo $value->ID ?>"><?php _e('Remove', THEMEMAKERS_THEME_FOLDER_NAME); ?></a></li>
                                <?php }; ?>
                            </ul>

                        </td>

                        <td>

                            <?php Application_Mail_Subscriber::print_registered_user_mail_subscription_page($profile_user_id); ?>

                        </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">

					<a href="<?php echo home_url() ?>/wp-admin/post-new.php?post_type=review" class="button small yellow"><?php _e('Write a review', THEMEMAKERS_THEME_FOLDER_NAME); ?></a>
					
                    <h3><?php _e('My 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' => $user['user_id'],
                            'showposts' => -1
                        ));
                        ?>

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


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

                                
                                ?>
						
						
								<tr>
                                    <td style="width: 90%">

                                        <div class="table-cell">

                                            <a href="<?php the_permalink() ?>"><?php echo $post->post_title ?></a>
										
                                                <?php if (!empty($review_terms)): ?>
                                                    <?php foreach ($review_terms as $review_term) : ?>
                                                        														
															<?php echo "("; ?>
																<a href="<?php echo bloginfo('url');?>/?cons=<?php echo $review_term->slug ?>"><?php echo $review_term->name ?></a>
															<?php echo ")"; ?>
                                                        
	                                                    <?php endforeach; ?>
		                                            <?php endif; ?>
                                                                       

                                            <?php if (!$_REQUEST['disable_blog_comments']): ?>
                                                <div class="comments-icon"><a href="<?php the_permalink(); ?>#comments"><?php comments_number('0', '1', '%'); ?> <?php _e('comments', THEMEMAKERS_THEME_FOLDER_NAME); ?></a></div><!--/ comments-icon-->
                                                <div class="clear"></div>
                                            <?php endif; ?>

                                        </div>

                                    </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; ?>

    </form>

<?php else: ?>

		
   
<?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="" />
					<?php $today=date('y-m-d, H:i:s'); ?>
					
					<input type="hidden" name="youtube_items[date][]" value="<?php echo $today ?>"  >
                </p>

                <p class="input-block">
                    <input type="text" placeholder="www.youtube.com/id" 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>
	
	<div class="info_popup">
		
	</div>
	
	
	
	

<?php endif; ?>

<?php get_footer(); ?>


