<?php

// -----------------------------------------------------------------------
// This file is part of AROUNDMe
// 
// Copyright (C) 2003 - 2006 Barnraiser
// http://www.barnraiser.org/
// info@barnraiser.org
// 
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option) any
// later version.
// 
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with program; see the file COPYING. If not, write to the Free
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301, USA.
// -----------------------------------------------------------------------



class Comment {
	// CLASS FUNCTIONS
	// Comment
	// getCommentCount
	// selComments
	// insertComment
	// getComment
	
	
	
	// Comment --------------------------------------------------------------------
	// the constuctor
	// tom calthrop - 23rd December 2004
	//
	function Comment($db, $rsp_item_type_id) {
		$this->db = $db;
		$this->rsp_item_type_id = $rsp_item_type_id;
	}//EO Comment

	
	
	// getCommentCount --------------------------------------------------------------------
	// returns a count if a particular item
	// tom calthrop - 23rd December 2004
	//
	function getCommentCount($item_id = null, $lastviewdate = null) {
		
		if (!isset($item_id) && isset($this->item_id)) {
		    $item_id = $this->item_id;
		}
		
		$query = "
			SELECT count(comment_id) as total 
			FROM " . $this->db->prefix . "_comment 
			where "
		;
		
		if (isset($this->rsp_item_type_id)) {
			$query .= "rsp_item_type_id=" . $this->rsp_item_type_id . " AND ";
		}
		
		if (isset($this->rsp_item_type_ids)) { //used for bookmarks
			$rsp_item_type_ids = "";
			foreach($this->rsp_item_type_ids as $key => $i):
				$rsp_item_type_ids .= $i;
				if (count($this->rsp_item_type_ids) > $key+1) {
					$rsp_item_type_ids .=",";
				}
			endforeach;
			$query .= "rsp_item_type_id in (" . $rsp_item_type_ids . ") AND ";
		}
		
		if (isset($this->group_id)) {
			$query .= "group_id=" . $this->group_id . " AND ";
		}
		
		if (isset($item_id)) {
			$query .= "item_id=" . $item_id . " and ";
		}
		
		if (isset($this->user_id)) {
			$query .= "user_id=" . $this->user_id . " and ";
		}
		
		if (isset($this->item_user_id)) {
			$query .= "item_user_id=" . $this->item_user_id . " and ";
		}
		
		if (isset($lastviewdate)) {
			$query .= "comment_create_datetime > " . $this->db->DBTimeStamp($lastviewdate) . " AND ";
		}
		
		$query .= "1=1";
		//echo $query . "<br /><br />";
		$result = $this->db->Execute($query);

		if (isset($result->fields['total'])) {
			return $result->fields['total'];
		}
		else {
			$error_info = "function=getCommentCount class=Comment.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
			$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
		}
	}//EO getCommentCount

	
	
	// selComments --------------------------------------------------------------------
	// returns a count if a particular item
	// tom calthrop - 23rd December 2004
	//
	// The group query looks complex. It returns the group membership profile, but
	// for comments posted like a comment received from splash 
	// by a non member we need the default profile
	//
	function selComments($item_id = null, $search=null) {
		
		$query = "
			SELECT c.comment_id, c.comment_parent_id, c.comment_body, c.user_id, c.item_id, 
			c.comment_create_datetime, c.rsp_item_type_id, 
			p.profile_nic, p.profile_hometown, p.profile_create_datetime "
		;
		
		$query .= "
			FROM " . $this->db->prefix . "_user_profile p, " . $this->db->prefix . "_comment c "
		;
		
		if (isset($this->group_id)) {
		    $query .= "
				JOIN " . $this->db->prefix . "_group_member gm ON c.user_id=gm.user_id AND gm.group_id=" . $this->group_id . " ";
			;
		}
		
		$query .= "WHERE ";
		
		$query .= "c.user_id=p.user_id AND p.profile_default=1 AND ";
		
		if (isset($this->group_id)) {
		    $query .= "
				(gm.profile_id=p.profile_id OR (c.user_id=p.user_id AND p.profile_default=1)) AND 
				c.group_id=" . $this->group_id . " AND "
			;
		}
		else {
		    $query .= "c.user_id=p.user_id AND p.profile_default=1 AND c.group_id is null AND ";
		}
		
		if (isset($this->user_id)) {
		    $query .= "c.user_id=" . $this->user_id . " AND ";
		}
		elseif (isset($this->user_ids)) {
			$user_ids = "";
			foreach($this->user_ids as $key => $i):
				$user_ids .= $i;
				if (count($this->user_ids) > $key+1) {
				    $user_ids .=",";
				}
			endforeach;
			$query .="c.user_id in (" . $user_ids . ") AND ";
		}
		
		if (isset($this->no_user_id)) {
		    $query .= "c.user_id!=" . $this->no_user_id . " AND ";
		}
		
		if (isset($this->item_user_id)) {
			$query .= "c.item_user_id=" . $this->item_user_id . " AND ";
		}
		
		if (isset($this->rsp_item_type_id)) {
			$query .= "c.rsp_item_type_id=" . $this->rsp_item_type_id . " AND ";
		}
		
		if (isset($this->no_comment_parent_id)) {
			$query .= "c.comment_parent_id is NULL AND ";
		}
		
		if (isset($this->item_ids)) {
			$item_ids = "";
			foreach($this->item_ids as $key => $i):
				$item_ids .= $i;
				if (count($this->item_ids) > $key+1) {
				    $item_ids .=",";
				}
			endforeach;
			$query .="c.item_id in (" . $item_ids . ") AND ";
		}
		elseif (!isset($item_id) && isset($this->item_id)) {
		    $query .= "c.item_id=" . $this->item_id . " AND ";
		}
		elseif (isset($item_id)) {
			$query .= "c.item_id=" . $item_id . " AND ";
		}
		
		if (isset($search)) {
			$query .= "
				c.comment_body like " . $this->db->qstr("%" . $search . "%", get_magic_quotes_gpc()) . " AND "
			;
		}
		
		if (isset($this->search_from_date)) {
			$query .= "
				c.comment_create_datetime>" . $this->db->DBTimeStamp($this->search_from_date) . " AND "
			;
		}
		
		$query .= "1=1 ";
		if (isset($this->order_by)) {
			$query .= "order by " . $this->order_by;
		}
		else {
			$query .= "order by c.comment_create_datetime";
		}
		
		if (isset($this->limit)) {
			$result = $this->db->SelectLimit($query, $this->limit);
		} else {
			$result = $this->db->Execute($query);
		}

		if (isset($result->fields)) {
			$temp = array();

			while (!$result->EOF) {
				$temp1 = $result->fields;
				
				$temp1['comment_body_raw'] = $temp1['comment_body'];
				
				if (isset($this->wiki)) {
					$this->wiki->disableRule('wikilink');
					
					if (!isset($this->wikiformat)) {
						$this->wikiformat = "Xhtml";
					}
						
					$temp1['comment_body'] = $this->wiki->transform($temp1['comment_body'], $this->wikiformat);
				}
				else {
					$temp1['comment_body'] = nl2br($temp1['comment_body']);
				}
				
				//format search results
				if (isset($search)) {
					$temp1['comment_body'] = searchTextFormat($search, $temp1['comment_body']);
				}
				
				array_push($temp, $temp1);
				$result->MoveNext();
			}

			$result->Close();
			return $temp;
			
		}
		else {
			$error_info = "function=selComments class=Comment.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
			$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
		}
	}//EO selComments
	
	
	
	// insertComment --------------------------------------------------------------------
	// inserts a comment
	// tom calthrop - 24th December 2004
	//
	function insertComment($data) {
		if (empty($data['comment_body'])) {
		    $GLOBALS['am_error_log'][] = array(12, null, null);
		}
		
		if (empty($GLOBALS['am_error_log'])) {
	
			if (empty($data['group_id'])) {
				$data['group_id'] = "null";
			}
			
			if (empty($data['comment_parent_id'])) {
				$data['comment_parent_id'] = "null";
			}
			
			// we add a check for duplicate entries
			$query = "
				SELECT comment_id 
				FROM " . $this->db->prefix . "_comment 
				WHERE 
				user_id=" . $this->user_id . " AND 
				comment_body=" . $this->db->qstr("%" . $data['comment_body'] . "%", get_magic_quotes_gpc()) . " AND 
				item_id=" . $this->item_id
			;
			
			$result = $this->db->SelectLimit($query, 1);
			
			if (!isset($result->fields['comment_id'])) {
				
				$rec = array();
				$rec['item_id'] = $this->item_id;
				$rec['group_id'] = $data['group_id'];
				$rec['rsp_item_type_id'] = $this->rsp_item_type_id;
				$rec['comment_body'] = $data['comment_body'];
				$rec['comment_create_datetime'] = time();
				$rec['user_id'] = $this->user_id;
				$rec['item_user_id'] = $this->item_user_id;
				$rec['comment_parent_id'] = $data['comment_parent_id'];
				
				
			
				$table = $this->db->prefix . "_comment";
				
				$query = $this->db->GetInsertSQL($table, $rec, get_magic_quotes_gpc());
				$result = $this->db->Execute($query);
				
				if (empty($result)) {
					$error_info = "function=insertComment class=Comment.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
					$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
				}
			}
		}
	}//EO insertComment

	
	
	// getComment --------------------------------------------------------------------
	// returns a row
	// tom calthrop - 23rd December 2004
	//
	function getComment($item_id = null, $profile_type=null) {
		
		if (!isset($item_id) && isset($this->item_id)) {
		    $item_id = $this->item_id;
		}
		
		$query = "
			SELECT comment_id, comment_create_datetime 
			FROM " . $this->db->prefix . "_comment 
			WHERE 
			rsp_item_type_id=" . $this->rsp_item_type_id . " AND 
			item_id=" . $item_id . " 
			order by comment_create_datetime desc"
		;
		
		$result = $this->db->SelectLimit($query, 1);

		if (isset($result->fields)) {
			$temp = $result->fields;
			
			$result->Close();
			return $temp;
			
		}
		else {
			$error_info = "function=getComment class=Comment.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
			$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
		}
	}//EO getComment
}
?>
