<?php
class frontend_rhc_rating_box {

	var $return_data_content = '';

	function __construct(){
		$this->create_col();
		$this->create_box();
	}
	
	function create_col(){
		global $post,$wpdb,$rhc_rating_plugin;
		
		$get_rating = 0;
		$get_rating_count = 0;
		
		$settings = get_option('rhc_rating_options');
		$get_social_connection = get_option('rhsco_options', '');

		$icon_count = 5;
		$icon_type = 'star';
		
		$faces_array = array('icon-angry','icon-sad','icon-wondering','icon-neutral','icon-cool','icon-smiley','icon-happy');
		$faces_string_array = array('Angry','Sad','Wondering','Neutral','Cool','Smilie','Happy');
		
		if(!empty($settings['rating_icon_numbers'])){
			$icon_count = (int)$settings['rating_icon_numbers'];
		}
		if(!empty($settings['rating_icon_type'])){
			$icon_type = $settings['rating_icon_type'];
		}
		
		if($icon_type == 'faces'){
			$icon_count = 7;
		}
		
		
		if(empty($settings['rating_accept_inputs'])){
			$settings['rating_accept_inputs'] = false;
		}
		
		
		
		if(!empty($post->ID)){
				$get_custom_list_date = $wpdb->get_results("SELECT event_date FROM {$wpdb->prefix}rhc_rating WHERE ".($settings['rating_accept_inputs'] == true ? ' `show`=1 and' : '')." postID=".$post->ID." group by event_date order by event_date ");
		
		
		
				$get_data_sql = "(SELECT sum(T0.rating) FROM  {$wpdb->prefix}rhc_rating T0 WHERE ".($settings['rating_accept_inputs'] == true ? ' T0.show=1 and' : '')." T0.postID=".$post->ID." and T0.rating <= ".$icon_count.") as rating_sum,";
				$get_data_sql .= "(SELECT count(T1.rating) FROM  {$wpdb->prefix}rhc_rating T1 WHERE ".($settings['rating_accept_inputs'] == true ? ' T1.show=1 and' : '')." T1.postID=".$post->ID." and T1.rating <= ".$icon_count.") as rating_count";
								
				for($i=0;$i<$icon_count;$i++){
					if(!empty($get_data_sql)){
						$get_data_sql .= ",";
					}
				
					$get_data_sql .= "(SELECT count(M".$i.".id) FROM {$wpdb->prefix}rhc_rating M".$i." WHERE ".($settings['rating_accept_inputs'] == true ? 'M'.$i.'.show=1 and' : '')." M".$i.".postID = ".$post->ID." and M".$i.".rating >= ".($i+0.4)." and M".$i.".rating < ".($i+1.4)."  LIMIT 1) as v".$i." ";
				}
				
				$get_data_sql = "SELECT ". $get_data_sql . " FROM {$wpdb->prefix}rhc_rating WHERE 1 limit 1";
				
				$get_result_data = $wpdb->get_row($get_data_sql, ARRAY_A);

				if(!empty($get_result_data)){
					$get_rating = $get_result_data['rating_sum'];
					$get_rating_count = $get_result_data['rating_count'];	
				}
		}


		if(empty($get_rating)){$get_rating = 0;}
		if(empty($get_rating_count)){$get_rating_count = 0;}
	
		if(!empty($get_rating) && !empty($get_rating_count) ){
			$avage_rating = $get_rating/$get_rating_count;
		} else {
			$avage_rating = 0;
		}

		// Pos box 1/3
		$this->return_data_content .= '<div id="rhc_rating_pos1" style="position: relative;" class="columbox span12">';	
			$this->return_data_content .= '<div class="span12" style="margin-bottom: 15px;margin-left: 0px;">';
		
				$this->return_data_content .= '<input id="rhc_total_rating" type="hidden" value="'.$get_rating.'">';
				$this->return_data_content .= '<input id="rhc_total_count" type="hidden" value="'.$get_rating_count.'">';
				
				$this->return_data_content .= '<div class="columbox span12 first" style="text-align: center">';
					if($get_rating_count == 1){
						$this->return_data_content .= '<div class="totalselect_text">'.__('Thank you for <span id="display_big_count">0</span> rating','rhcrating').'!</div>';
					} else if($get_rating_count > 1){
						$this->return_data_content .= '<div class="totalselect_text">'.__('Thank you for <span id="display_big_count">0</span> ratings','rhcrating').'!</div>';
					} else {
						$this->return_data_content .= '<div class="totalselect_text">'.__('Be the first to rate and review','rhcrating').'!</div>';
					}
				$this->return_data_content .= '</div>';
		
				$this->return_data_content .= '<div class="columbox span12 first" id="main_show_rating" data-rating-type="'.$icon_type.'" data-rating="'.$avage_rating.'" style="text-align: center">';
					for($i=0;$i<$icon_count;$i++){
						if($icon_type == 'faces'){
							$this->return_data_content .= '<div data-standart-type="'.$faces_array[$i].'" class="'.$faces_array[$i].'-3 totalselect"></div>';	
						} else {
							$this->return_data_content .= '<div data-standart-type="icon-'.$icon_type.'" class="icon-'.$icon_type.'-3 totalselect"></div>';	
						}
					}
				$this->return_data_content .= '</div>';
				
				if($icon_type != 'faces'){
					$this->return_data_content .= '<div class="columbox span12 first" style="text-align: center">';
						$this->return_data_content .= '<div class="totalsub_text"><span id="display_avage">0.00</span> '.__('average','rhcrating').'</div>';
					$this->return_data_content .= '</div>';
				}
				
				$this->return_data_content .= '<div class="columbox span12 first" style="padding-left: 0px; padding-right: 0px;">';
					$this->return_data_content .= '<div class="columbox span7 first" style="padding-left: 0px; padding-right: 0px;">';
		
		
						for($i=$icon_count-1;$i>=0;$i--){
							$tmp_d = '';
							
							if(!empty($get_result_data)){
								$tmp_d = $get_result_data['v'.$i];
							}
							
							if(empty($tmp_d)){
								$tmp_d = 0;
							}

							
								$this->return_data_content .= '<table style="width:100%" class="progress-bar-bars" data-number="'.$tmp_d.'"><tr><td class="progress-bar_width_a">';
								
								
								if($icon_type == 'faces'){
									$this->return_data_content .= ucfirst(__($faces_string_array[$i],'rhcrating'));
								} else if($icon_type == 'splash'){	
									$this->return_data_content .= ($i+1) . ' ' . ucfirst(__($icon_type,'rhcrating'));
								} else {
								
									if($i == 0){
										$this->return_data_content .= ($i+1) . ' ' . ucfirst(__($icon_type,'rhcrating'));
									} else {
										$this->return_data_content .= ($i+1) . ' ' . ucfirst(__($icon_type.'s','rhcrating'));
									}
									
								}
								
								$this->return_data_content .= '</td><td><div class="progress">';
								$this->return_data_content .= 	'<div class="progress-bar" style="width: 0%;"></div>';
								$this->return_data_content .= '</div></td><td class="progress-bar_width_b"><div class="rhc_text">0</div></td></tr></table>';
						}
		
					$this->return_data_content .= '</div>';
					
					$this->return_data_content .= '<div class="columbox span5" style="padding-left: 0px; padding-right: 0px;">';
						if(!empty($get_custom_list_date)){
					
					         $this->return_data_content .= '<select multiple="multiple" name="small" id="get_loading_data" style="display:none" class="select-block mbl">';
					         	$this->return_data_content .= '<option disabled="disabled" value="">'.__('Filter Events','rhcrating').'</option>';
								foreach($get_custom_list_date as $tmp){
									$devide = explode(',', $tmp->event_date);
									$this->return_data_content .= '<option value="'.$tmp->event_date.'">'.date('F d, Y',strtotime($devide[0])).'</option>';	
								}
							$this->return_data_content .= '</select>';
							}

						if(!empty($settings['rating_attend_show'])){
							$this->return_data_content .= '<a data-close-name="'.__('Close Ratings and Reviews','rhcrating').'" data-open-name="'.__('View Ratings and Reviews','rhcrating').'" class="btn btn-huge btn-embossed btn-primary btn-danger btn_view_rating" onclick="rhc_rating_openwindow(\'#rhc_rating_pos3\',\'\',true,this);return false;" href="#fakelink">'.__('View Ratings and Reviews','rhcrating').'</a>';
						}
						$this->return_data_content .= '<a class="btn btn-huge btn-embossed btn-primary btn_submit_rating" onclick="rhc_rating_openwindow(\'#rhc_rating_pos2\',\'#rhc_rating_pos1\',false);return false;" href="#fakelink">'.__('Submit Rating and Review','rhcrating').'</a>';
					$this->return_data_content .= '</div>';	
						
				$this->return_data_content .= '</div>';
				
			$this->return_data_content .= '</div>';
		$this->return_data_content .= '</div>';		
		
		

		
		
		// 2/3	
		$this->return_data_content .= '<div id="rhc_rating_pos2" class="columbox span12">';
			$this->return_data_content .= '<div class="rhc-info-cell fe-cell-label rch_h1"><label class="fe-extrainfo-label">'.__('Submit Rating and Review','rhcrating').'</label></div>';
			$this->return_data_content .= '<div class="rhc-info-cell fe-cell-taxonomymeta rch_h2"><label class="fe-extrainfo-label">'.__('Please enter your information','rhcrating').'</label></div>';	
			if(empty($settings['rating_remove_manaul_input'])){
				$this->return_data_content .= '<div class="span5 first">';
					$this->return_data_content .= '<div>';
						$this->return_data_content .= '<input id="rhc_rating_firstname" type="text" style="margin-top: 0px;" class="form-control large" placeholder="'.__('First Name','rhcrating').'">';
					$this->return_data_content .= '</div>';
			
					$this->return_data_content .= '<div>';
						$this->return_data_content .= '<input id="rhc_rating_lastname" type="text" class="form-control large" placeholder="'.__('Last Name','rhcrating').'">';
					$this->return_data_content .= '</div>';
			
					$this->return_data_content .= '<div>';
						$this->return_data_content .= '<input id="rhc_rating_email" type="text" class="form-control large" placeholder="'.__('Email','rhcrating').'">';
						$this->return_data_content .= '<input id="rhc_rating_extern" type="hidden" value=""><input id="rhc_rating_externUrl" type="hidden" value="">';
					$this->return_data_content .= '</div>';
					
				$this->return_data_content .= '</div>';				
				$this->return_data_content .= '<div class="span7">';
			} else {
				$this->return_data_content .= '<div class="span12 first">';
			}
				$this->return_data_content .= '<div>';
					$this->return_data_content .= '<textarea class="form-control large" rows="4" id="rhc_rating_comment" placeholder="'.__('Add comment...','rhcrating').'"></textarea>';
				$this->return_data_content .= '</div>';

				$this->return_data_content .= '<div class="span12 first main_selectstars" id="manual_select_show_rating" data-rating-type="'.$icon_type.'" >';
					for($i=0;$i<$icon_count;$i++){
						if($icon_type == 'faces'){
								$this->return_data_content .= '<div data-standart-type="'.$faces_array[$i].'" class="ratting_select '.$faces_array[$i].'-3 selectstars"></div>';	
						} else {
								$this->return_data_content .= '<div data-standart-type="icon-'.$icon_type.'" class="ratting_select icon-'.$icon_type.'-3 selectstars"></div>';	
						}
					}			
				$this->return_data_content .= '</div>';
			$this->return_data_content .= '</div>';
			
			$this->return_data_content .= '<div>';	
			
				$this->return_data_content .= '<div class="span12 first" style="margin-top:20px;">';
				
				
				if(empty($settings['rating_remove_manaul_input'])){
					$this->return_data_content .= '<a href="#JoinandRSVP" class="btn btn-embossed btn-primary btn-social-twitter next" id="rhc_rating_next" onclick="return rhc_rating_next('.$post->ID.');">'.__('Submit Rating','rhcrating').'</a>';
				}
				
				
					if(!empty($settings['rating_wp_show']) &&  !empty($settings['rating_wp_show'])){
						$this->return_data_content .= '<a class="btn btn-sm btn-social-wp wp" href="#WP" id="rhc_rating_wp" onclick="return rhc_rating_social_connect('.$post->ID.',\'wp\');"><i class="fui-wp"></i> '.__('Connect with Wordpress','rhcrating').'</a>';
					}
								
					if(!empty($settings['rating_twitter_show']) &&  !empty($get_social_connection['twitter_key']) && !empty($get_social_connection['twitter_secret'])){
						$this->return_data_content .= '<a href="#Twitter" class="btn btn-sm btn-social-twitter twitter " id="rhc_rating_tweeter" onclick="return rhc_rating_social_connect('.$post->ID.',\'twitter\');"><i class="fui-twitter"></i> '.__('Connect with Twitter','rhcrating').'</a>';
					}
					
					if(!empty($settings['rating_linkedin_show']) &&  !empty($get_social_connection['linkedin_app_key'])){
						$this->return_data_content .= '<a href="#Linkedin" class="btn btn-sm btn-social-linkedin linkedin " id="rhc_rating_linkedin" onclick="return rhc_rating_social_connect('.$post->ID.',\'linkedin\');"><i class="fui-linkedin"></i> '.__('Connect with LinkedIn','rhcrating').'</a>';
					}

					if(!empty($settings['rating_facebook_show']) &&  !empty($get_social_connection['facebook_app_id'])){
						$this->return_data_content .= '<a class="btn btn-sm btn-social-facebook facebook" href="#Facebook" id="rhc_rating_facebook" onclick="return rhc_rating_social_connect('.$post->ID.',\'facebook\');"><i class="fui-facebook"></i> '.__('Connect with Facebook','rhcrating').'</a>';
					}
					
					if(!empty($settings['rating_googleplus_show']) &&  !empty($get_social_connection['googleplus_apikey']) && !empty($get_social_connection['googleplus_clientid'])){
						$this->return_data_content .= '<a href="#googleplus" class="btn btn-sm btn-social-googleplus googleplus " id="rhc_rating_google" onclick="return rhc_rating_social_connect('.$post->ID.',\'googleplus\');"><i class="fui-googleplus"></i> '.__('Connect with Google+','rhcrating').'</a>';
					}
					
										
					
					
					
					$this->return_data_content .= '<a href="#JoinandRSVP" class="btn btn-embossed btn-primary btn-social-twitter btn-danger back" id="rhc_rating_back" onclick="rhc_rating_openwindow(\'#rhc_rating_pos1\',\'#rhc_rating_pos2\');return false;">'.__('Back','rhcrating').'</a>';

					$this->return_data_content .= '<div class="clear"></div>';
				$this->return_data_content .= '</div>';
			
				$this->return_data_content .= '<div class="rhc_rating_alert_dialog span12 first" style="display: none;">';
					$this->return_data_content .= '<div class="alert alert-error">';
						$this->return_data_content .= '<button data-dismiss="alert" class="close fui-cross" type="button"></button>';
						$this->return_data_content .= '<div class="text"></div>';
						$this->return_data_content .= '</div>';
				$this->return_data_content .= '</div>';	
				
				
				$this->return_data_content .= '<div class="rhc_rating_success_dialog span12 first" style="display: none;">';
					$this->return_data_content .= '<div class="alert alert-success">';
			        	$this->return_data_content .= '<button data-dismiss="alert" class="close fui-cross" type="button"></button>';
			        	$this->return_data_content .= '<div class="text"></div>';
					$this->return_data_content .= '</div>';
				$this->return_data_content .= '</div>';	
				
				$this->return_data_content .= '<div class="fc-view-loading loading-events" style="display: none;"><div class="fc-view-loading-1 ajax-loader loading-events"><div class="fc-view-loading-2x xspinner icon-xspinner-3"></div></div></div>';
			
				$this->return_data_content .= '</div>';
		$this->return_data_content .= '</div>';
		// 4/3	
		
		// show all new line
		if(!empty($settings['rating_attend_show'])){
		$this->return_data_content .= '<div id="rhc_rating_pos3" class="columbox span12" style="margin-left:0px;">';
			$this->return_data_content .= '<div class="rhc-info-cell fe-cell-label rch_h1"><label class="fe-extrainfo-label">'.__('Reviews and Ratings','rhcrating').'</label></div>';
			$this->return_data_content .= '<div class="span12" id="comment_avatar_place" style="margin-bottom: 15px;margin-left: 0px;">';
			$this->return_data_content .= '</div>';	
			$this->return_data_content .= '<div style="clear: both; height: 20px;"><div class="showmore_div icon-arrow-down-3"></div></div>';
		$this->return_data_content .= '</div>';
		}
	}
	
	function create_box(){
		$tmp = '<div style="margin-bottom: 25px;" class="rhc fe-extrainfo-container se-ratingbox"><div class="fe-extrainfo-container2 row-fluid cit_rhc_rating_"><div class="fe-extrainfo-holder">';
		$tmp .= $this->return_data_content;
		$tmp .= '</div></div></div>';
		
		$this->return_data_content = $tmp;
	}	
	
	function return_data(){
		return $this->return_data_content;
	}
}

?>