Location: PHPKode > scripts > Reviews > reviews/Xoriant_Reviews.php
<?php
class Xoriant_Reviews {

	private $hostname_conn;
	private $database_conn;
	private $username_conn;
	private $password_conn;
	private $conn;
	private static $instance;
	
	public function __construct() {
		if(self::$instance) {
			return self::$instance;
		} else {
			self::$instance = $this;
			$this->hostname_conn = "localhost";
			$this->database_conn = "classes";
			$this->username_conn = "user";
			$this->password_conn = "password";
			$this->conn = mysql_connect($this->hostname_conn, $this->username_conn, $this->password_conn); 
			if(!$this->conn) throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
			mysql_select_db($this->database_conn, $this->conn);
		}
	}
	
	public function display_table_fields($table_name) {
		$rs = @mysql_query("select * from ".$table_name." LIMIT 1");
		if(!$rs) {
			throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
		}
		$i = 0;
		$register_main_arr = array();
		while ($i < mysql_num_fields($rs)) {
			$meta = mysql_fetch_field($rs, $i);
			$register_arr[] = $meta->name;
			$i++;
		}
		return $register_arr;
	}
	public function phpinsert($table_name,$pk,$postarray) {
		$register_arr = array();
		$register_arr = $this->display_table_fields($table_name);
		$query = "insert into ".$table_name." set ";
		foreach($postarray as $key=>$value) {
			if(gettype($value)=="array") {
				$string = '';
				foreach($value as $val) {
					if(strlen($val)>0) { 
						$val = $this->processString($val);
						$string .= $val.'|';
					}
				}
				$string = substr($string,0,-1);
				if(in_array($key,$register_arr)) {
					$query .= $key."='".$string."',"; 
				}
			} else {
				if(in_array($key,$register_arr)) {
					$value = $this->processString($value);
					$query .= $key."='".$value."',";
				}
			}
		}
		$query = substr($query,0,-1);
		$result = @mysql_query($query);
		if(!$result) {
			throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
		}
		$uid = mysql_insert_id();
		return $uid;
	}
	
	public function phpedit($table_name,$pk,$postarray,$uid) {
		$register_arr = array();
		$register_arr = $this->display_table_fields($table_name);
		$query = "update ".$table_name." set ";
		foreach($postarray as $key=>$value) {
			if(gettype($value)=="array") {
				$string = '';
				foreach($value as $val) {
					if(strlen($val)>0) { 
						$val = $this->processString($val);
						$string .= $val.'|';
					}
				}
				$string = substr($string,0,-1);
				if(in_array($key,$register_arr)) {
					$query .= $key."='".$string."',"; 
				}
			} else {
				if(in_array($key,$register_arr)) {
					$value = $this->processString($value);
					$query .= $key."='".$value."',";
				}
			}
		}
		$query = substr($query,0,-1);
		$query .= " where ".$pk." = '".$uid."'";		
		$result = @mysql_query($query);
		if(!$result) {
			throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
		}
		return $uid;
	}
	
	private function processString($text) {
		return addslashes(stripslashes(trim($text)));
	}
	
	
	public function validateData($record) {		
		if(!isset($record['rating'])) {
			throw new Exception("Rating is empty. ");
		}	
		if(empty($record['review_title'])) {
			throw new Exception("Rating Title is empty. ");
		}
		return true;
	}
	
	public function checkRatingUniqueIp($record) {
		$sql = "SELECT count(*) FROM xoriant_reviews_ratings WHERE item_id = '".$this->processString($record['item_id'])."' and rating_ip = '".$this->processString($record['rating_ip'])."'";
		$rs = @mysql_query($sql);
		if(!$rs) {
			throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
		}
		$arr = mysql_fetch_array($rs);
		$count = $arr['cnt'];
		return $count;
	}
	
	public function checkRatingUniqueUser($record) {
		$sql = "SELECT count(*) as cnt FROM xoriant_reviews_ratings WHERE item_id = '".$this->processString($record['item_id'])."' and user_id = ".$this->processString($record['user_id'])."'";
		$rs = @mysql_query($sql);
		if(!$rs) {
			throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
		}
		$arr = mysql_fetch_array($rs);
		$count = $arr['cnt'];
		return $count;
	}
	
	public function checkConfirmUniqueIp($record) {
		$sql = "SELECT count(*) as cnt FROM xoriant_reviews_ratings_confirm WHERE item_id = '".$this->processString($record['item_id'])."' and confirm_ip = '".$this->processString($record['confirm_ip'])."' and rating_id = '".$this->processString($record['rating_id'])."'";
		$rs = @mysql_query($sql);
		if(!$rs) {
			throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
		}
		$arr = mysql_fetch_array($rs);
		$count = $arr['cnt'];
		return $count;
	}
	
	public function checkConfirmUniqueUser($record) {
		$sql = "SELECT count(*) as cnt FROM xoriant_reviews_ratings_confirm WHERE item_id = '".$this->processString($record['item_id'])."' and confirm_user_id = '".$this->processString($record['confirm_user_id'])."' and rating_id = '".$this->processString($record['rating_id'])."'";
		$rs = @mysql_query($sql);
		if(!$rs) {
			throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
		}
		$arr = mysql_fetch_array($rs);
		$count = $arr['cnt'];
		return $count;
	}
	
	public function showRating($item_id) {
		$sql = "SELECT item_id, AVG(rating) as avg_rating, COUNT(rating) as counter, SUM(rating) as sumrating FROM xoriant_reviews_ratings WHERE item_id = '".$item_id."' GROUP BY item_id";
		$rs = @mysql_query($sql);
		if(!$rs) {
			throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
		}
		$arr = mysql_fetch_array($rs);
		return $arr;
	}
	
	public function showReviews($item_id, $max=-1, $pageNum=0) {
		$sql = "SELECT item_id, rating_id, AVG(confirm) as avg_rating, COUNT(confirm) as counter, SUM(confirm) as total FROM xoriant_reviews_ratings_confirm WHERE item_id = '".$item_id."' GROUP BY rating_id";
		$rs = @mysql_query($sql);
		if(!$rs) {
			throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
		}
		while($arr = mysql_fetch_array($rs)) {
			$confirm[$arr['rating_id']] = $arr;
		}
		
		if($max=="-1") {
			$sql = "SELECT * FROM xoriant_reviews_ratings WHERE item_id = '".$item_id."' order by rating_date desc";
			$return['sql'] = $sql;
			$rs = @mysql_query($sql);
			if(!$rs) {
				throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
			}
			$return['totalRows'] = mysql_num_rows($rs);
			$return['max'] = $max;
			$return['pageNum'] = $pageNum;	
			$startRow = $pageNum * $max;
			$return['startRow'] = $startRow;
			$totalPages = 0;	
			$return['totalPages'] = $totalPages;
		} else {
			$sql = "SELECT count(*) as cnt FROM xoriant_reviews_ratings WHERE item_id = '".$item_id."' order by rating_date desc";
			$return['sqlCnt'] = $sql;
			$rs = @mysql_query($sql);
			if(!$rs) {
				throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
			}
			$arr = mysql_fetch_array($rs);
			$totalRows = $arr['cnt'];
			$return['totalRows'] = $totalRows;
			
			if(!$max) $max = 10;
			$return['max'] = $max;
			if(!$pageNum) $pageNum = 0;	
			$return['pageNum'] = $pageNum;	
			$startRow = $pageNum * $max;
			$return['startRow'] = $startRow;
			$totalPages = ceil($totalRows/$max)-1;	
			$return['totalPages'] = $totalPages;
				
			$sql = "SELECT * FROM xoriant_reviews_ratings WHERE item_id = '".$item_id."'";
			$sql .= " Limit ".$startRow.", ".$max;
			$return['sql'] = $sql;
			$rs = @mysql_query($sql);
			if(!$rs) {
				throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
			}
		}
		while($arr = mysql_fetch_array($rs)) {
			$return['fields'][$arr['rating_id']] = $arr;
			$return['confirm'][$arr['rating_id']] = $confirm[$arr['rating_id']];
		}
		return $return;
	}
	
	public function showConfirm($rating_id, $item_id) {
		$sql = "SELECT item_id, rating_id, AVG(confirm) as avg_rating, COUNT(confirm) as counter, SUM(confirm) as total FROM xoriant_reviews_ratings_confirm WHERE item_id = '".$item_id."' and rating_id = '".$rating_id."' GROUP BY rating_id";
		$rs = @mysql_query($sql);
		if(!$rs) {
			throw new Exception(mysql_error()." on line number ".__LINE__." of file ".__FILE__);
		}
		$arr = mysql_fetch_array($rs);
		return $arr;
	}
}
?>
Return current item: Reviews