<?php

// -----------------------------------------------------------------------
// This file is part of Igloo
// 
// Copyright (C) 2003, 2005 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($lastviewdate)) {
			$query .= "comment_create_datetime > " . $this->db->DBTimeStamp($lastviewdate) . " AND ";
		}
		
		$query .= "1=1";
		//echo $query;
		$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['igloo_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) {
		
		$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 . "_comment c, " . $this->db->prefix . "_user_profile p "
		;
		
		$query .= "WHERE ";
		
		$query .= "c.user_id=p.user_id AND p.profile_default=1 AND ";
		
		if (isset($this->user_id)) {
		    $query .= "c.user_id=" . $this->user_id . " AND ";
		}
		
		if (isset($this->no_user_id)) {
		    $query .= "c.user_id!=" . $this->no_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->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 ";
		}
		
		$query .= "1=1 ";
		//echo $query;
		if (isset($this->orderby)) {
			$query .= "order by " . $this->orderby;
		}
		else {
			$query .= "order by c.comment_create_datetime";
		}
		
		//echo $query;
		
		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;
				
				if (isset($this->wiki)) {
					$this->wiki->disableRule('wikilink');
					$temp1['comment_body'] = $this->wiki->transform($temp1['comment_body'], 'Xhtml');
				}
				else {
					$temp1['comment_body'] = nl2br($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['igloo_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['igloo_error_log'][] = array(12, null, null);
		}
		
		if (empty($GLOBALS['igloo_error_log'])) {
	
			if (isset($this->group_id)) {
			    $group_id = $this->group_id;
			}
			else {
				$group_id = "null";
			}
			
			$rec = array();
			$rec['item_id'] = $this->item_id;
			$rec['group_id'] = $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;
			
			$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['igloo_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['igloo_error_log'][] = array(1, 1, $error_info);
		}
	}//EO getComment
}
?>
