/**
 * Loads the comment template specified in $file.
 *
 * Will not display the comments template if not on single post or page, or if
 * the post does not have comments.
 *
 * Uses the WordPress database object to query for the comments. The comments
 * are passed through the 'comments_array' filter hook with the list of comments
 * and the post ID respectively.
 *
 * The $file path is passed through a filter hook called, 'comments_template'
 * which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
 * first and if it fails it will require the default comment template from the
 * default theme. If either does not exist, then the WordPress process will be
 * halted. It is advised for that reason, that the default theme is not deleted.
 *
 * @since 1.5.0
 * @global array $comment List of comment objects for the current post
 * @uses $wpdb
 * @uses $post
 * @uses $withcomments Will not try to get the comments if the post has none.
 *
 * @param string $file Optional, default '/comments.php'. The file to load
 * @param bool $separate_comments Optional, whether to separate the comments by comment type. Default is false.
 * @return null Returns null if no comments appear
 */
function comments_template($file = '/comments.php', $separate_comments = false)
{
    global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity, $overridden_cpage;
    if (!(is_single() || is_page() || $withcomments) || empty($post)) {
        return;
    }
    if (empty($file)) {
        $file = '/comments.php';
    }
    $req = get_option('require_name_email');
    /**
     * Comment author information fetched from the comment cookies.
     *
     * @uses wp_get_current_commenter()
     */
    $commenter = wp_get_current_commenter();
    /**
     * The name of the current comment author escaped for use in attributes.
     */
    $comment_author = $commenter['comment_author'];
    // Escaped by sanitize_comment_cookies()
    /**
     * The email address of the current comment author escaped for use in attributes.
     */
    $comment_author_email = $commenter['comment_author_email'];
    // Escaped by sanitize_comment_cookies()
    /**
     * The url of the current comment author escaped for use in attributes.
     */
    $comment_author_url = esc_url($commenter['comment_author_url']);
    /** @todo Use API instead of SELECTs. */
    #echo "HERE!! - UID: $user_ID ";
    if ($user_ID or $user_ID == 0) {
        // show only one language if logged in (ToDo: allow selection of languages instead)
        // or if not logged in (important for search engine localization
        #echo "DONE!";
        // comment_id FROM $wpdb->commentmeta WHERE meta_key = 'rating-id' AND meta_value = $ratingid
        //$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) )  ORDER BY comment_date_gmt", $post->ID, $user_ID));
        $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) )  ORDER BY comment_date_gmt", $post->ID, $user_ID));
        //print_r($comments);
        global $lang;
        global $region;
        $a_size = sizeof($comments);
        #echo "<br>size: $a_size <br>";
        #echo "LANG: $lang<br>";
        #echo "Reg: $region<br>";
        $j = 0;
        for ($i = 0; $i < $a_size; $i++) {
            $cid = $comments[$i]->comment_ID;
            //echo "CID:".$cid;
            $clang = get_comment_meta($cid, 'language', true);
            //echo "Lang: $clang"."<br>";
            $check = lhg_show_language($clang, $region);
            //echo "CHK: $check<br>";
            if ($check == 1) {
                $comments_temp[$j] = $comments[$i];
                $j++;
            }
        }
        //overwrited with filtered version
        $comments = $comments_temp;
    } else {
        if (empty($comment_author)) {
            #echo "empty";
            $comments = get_comments(array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC'));
        } else {
            #echo "not logged in";
            $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_post_ID = %d AND ( comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) ORDER BY comment_date_gmt", $post->ID, wp_specialchars_decode($comment_author, ENT_QUOTES), $comment_author_email));
        }
    }
    // keep $comments for legacy's sake
    $wp_query->comments = apply_filters('comments_array', $comments, $post->ID);
    $comments =& $wp_query->comments;
    $wp_query->comment_count = count($wp_query->comments);
    update_comment_cache($wp_query->comments);
    if ($separate_comments) {
        $wp_query->comments_by_type =& separate_comments($comments);
        $comments_by_type =& $wp_query->comments_by_type;
    }
    $overridden_cpage = FALSE;
    if ('' == get_query_var('cpage') && get_option('page_comments')) {
        set_query_var('cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1);
        $overridden_cpage = TRUE;
    }
    if (!defined('COMMENTS_TEMPLATE') || !COMMENTS_TEMPLATE) {
        define('COMMENTS_TEMPLATE', true);
    }
    $include = apply_filters('comments_template', STYLESHEETPATH . $file);
    if (file_exists($include)) {
        require $include;
    } elseif (file_exists(TEMPLATEPATH . $file)) {
        require TEMPLATEPATH . $file;
    } else {
        // Backward compat code will be removed in a future release
        require ABSPATH . WPINC . '/theme-compat/comments.php';
    }
}
function lhg_comment_language_filter($post_ID, $user_ID, $lang, $region)
{
    global $wpdb;
    #echo "UID: $user_ID";
    #echo "PID: $post_ID";
    $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) )  ORDER BY comment_date_gmt", $post_ID, $user_ID));
    //print_r($comments);
    $a_size = sizeof($comments);
    //echo "<br>size: $a_size <br>";
    #echo "LANG: $lang<br>";
    #echo "Reg: $region<br>";
    $j = 0;
    for ($i = 0; $i < $a_size; $i++) {
        $cid = $comments[$i]->comment_ID;
        #echo "CID:".$cid;
        $clang = get_comment_meta($cid, 'language', true);
        #echo "Lang: $clang"."<br>";
        $check = lhg_show_language($clang, $region);
        if ($region == "de") {
            $check = 1;
        }
        //all comments in german database used
        #echo "CHK: $check<br>";
        if ($check == 1) {
            //$comments_temp[$j]=$comments[$i];
            $j++;
        }
    }
    return $j;
}
    function widget($args, $instance)
    {
        global $comments, $comment;
        $cache = wp_cache_get('widget_recent_comments', 'widget');
        if (!is_array($cache)) {
            $cache = array();
        }
        if (!isset($args['widget_id'])) {
            $args['widget_id'] = $this->id;
        }
        if (isset($cache[$args['widget_id']])) {
            echo $cache[$args['widget_id']];
            return;
        }
        extract($args, EXTR_SKIP);
        $output = '';
        $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Comments') : $instance['title'], $instance, $this->id_base);
        if (empty($instance['number']) || !($number = absint($instance['number']))) {
            $number = 5;
        }
        $comments = get_comments(array('number' => $number, 'status' => 'approve', 'post_status' => 'publish'));
        $output .= $before_widget;
        if ($title) {
            $output .= $before_title . '<i class="icon-comments"></i> ' . $title . $after_title;
        }
        $output .= "\n\n" . '<ul id="recentcomments1">' . "\n\n";
        if ($comments) {
            foreach ((array) $comments as $comment) {
                //$output .= "Test";
                global $region;
                $lang_url = lhg_get_lang_url_from_region($region);
                if ($lang_url != "") {
                    $lang_url = "/" . $lang_url;
                }
                $cid = $comment->comment_ID;
                //echo "CID:".$cid;
                $clang = get_comment_meta($cid, 'language', true);
                //echo "Lang: $clang"."<br>";
                $check = lhg_show_language($clang, $region);
                //echo "CHK: $check";
                //author ID
                $uid = 0;
                //reset
                $output_tmp_1 = "";
                $output_tmp_2 = "";
                $uid = get_comment($cid)->user_id;
                //echo "UID: $uid";
                //$CAID    = get_comment_author ($comment->comment_ID);
                //echo "CID: $CAID";
                $avatar = get_avatar($comment, 24);
                preg_match('#src=["|\'](.+)["|\']#Uuis', $avatar, $avatarurla);
                //echo "URL: $avatarurl[1]";
                $avatarurl = $avatarurla[1];
                if ($lang_url == "/de") {
                    $lang_url = "";
                }
                $user_link = '<a href="' . $lang_url . '/hardware-profile/user' . $uid . '">';
                $user_link_end = '</a>';
                if ($uid != 0) {
                    $output_tmp_1 = $user_link;
                }
                if ($uid != 0) {
                    $output_tmp_2 = $user_link_end;
                }
                $output_tmp = '
        <li class="recentcomments">
          <div class="widgetimg_all">
            <div class="widgetimg_avatar">';
                $output_tmp .= $output_tmp_1 . '
              <img src="' . $avatarurl . '" height="24" width="24" alt="Avatar" />' . $output_tmp_2;
                $output_tmp .= '
             </div>


            <div class="widgetimg_text">
              ' . sprintf(_x('%1$s on %2$s', 'widgets'), $output_tmp_1 . str_replace("admin", "LHG Team", get_comment_author()) . $output_tmp_2, '<a href="' . esc_url(get_comment_link($comment->comment_ID)) . '">' . translate_title(get_the_title($comment->comment_post_ID)) . '</a> ' . get_comment_date("", $coment->comment_ID) . " ") . '
            </div>
          </div>
        </li>
        ';
                if ($comment_found < 5) {
                    if ($check != 0) {
                        //echo "A";
                        $output_lang .= $output_tmp;
                        $comment_found++;
                    } else {
                        //echo "B";
                        if ($comment_default_found < 5) {
                            $output_default .= $output_tmp;
                        }
                        $comment_default_found++;
                    }
                }
            }
            if ($output_lang == "") {
                $output .= $output_default;
            }
            if ($output_lang != "") {
                $output .= $output_lang;
            }
        }
        $output .= '</ul>';
        $output .= $after_widget;
        echo $output;
        $cache[$args['widget_id']] = $output;
        wp_cache_set('widget_recent_comments', $cache, 'widget');
    }