<?php

// -----------------------------------------------------------------------
// This file is part of aroundme
// 
// 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., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
// -----------------------------------------------------------------------

class Bookmark {
	// CLASS FUNCTIONS
	// Bookmark
	// getBookmarks
	// getBookmark
	// insertBookmark
	// updateBookmark
	// deleteBookmark
	// getBookmarkLastViewDate
	
	
	
	// Bookmark --------------------------------------------------------------------
	// the constuctor
	// tom calthrop - 25th Jan 2004
	//
	function Bookmark($db, $cms_item_type_id, $date, $user_id) {
		$this->db = $db;
		$this->cms_item_type_id = $cms_item_type_id;
		$this->date = $date;
		$this->user_id = $user_id;
	}
	
	
	
	// getSubjectBookmarks --------------------------------------------------------------------
	// returns multiple rows from the DB
	// the library needs the name and not the id, hence the $return_names var is set to 1 for names
	// tom calthrop - 25th Jan 2004
	//
	function getBookmarks() { //bookmark_name is used for libraryPageName
		$query = "
			SELECT item_id 
			FROM " . $this->db->prefix . "_bookmark 
			WHERE 
			user_id=" . $this->user_id . " and "
		;
		
		if (isset($this->bookmark_type)) {
		    $query .= "bookmark_type=" . $this->bookmark_type . " and ";
		}
		
		if (isset($this->cms_item_type_id)) {
		    $query .= "cms_item_type_id=" . $this->cms_item_type_id . " and ";
		}
		
		$query .= "1=1";
		
		$result = $this->db->Execute($query);

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

			while (!$result->EOF) {
				array_push($temp, $result->fields['item_id']);
				
				$result->MoveNext();
			}

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

	
	// getBookmark --------------------------------------------------------------------
	// gets the bookmark (returns true if found)
	// tom calthrop - 11th December 2004
	//
	function getBookmark() {
		
		$query = "
			SELECT user_id 
			FROM " . $this->db->prefix . "_bookmark 
			WHERE 
			user_id=" . $this->user_id . " and 
			cms_item_type_id=" . $this->cms_item_type_id . " and 
			bookmark_type=" . $this->bookmark_type . " and 
			item_id=" . $this->item_id . " and "
		;
		
		$query .= "1=1";
		
		$result = $this->db->Execute($query);
			
		if (isset($result->fields['user_id'])) {
			return true;
		}
		else {
			return false;
		}
	} //EO getBookmark
	
	
	
	// insertBookmark --------------------------------------------------------------------
	// inserts the bookmark
	// tom calthrop - 11th December 2004
	//
	function insertBookmark() {
		$datetime = date("Y-m-d H:i:s");
		$datetime = $this->db->DBDate($datetime);
		
	    $query = "
			INSERT INTO " . $this->db->prefix . "_bookmark (
			item_id, 
			cms_item_type_id, 
			bookmark_type, 
			user_id,
			bookmark_lastview_datetime
			) VALUES (
			" . $this->item_id . ", 
			" . $this->cms_item_type_id . ", 
			" . $this->bookmark_type . ", 
			" . $this->user_id . ", 
			" . $datetime . ")"
		;
		
		$result = $this->db->Execute($query);
		
		if (empty($result)) {
			$error_info = "function=insertBookmark class=Bookmark.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
			$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
		}
	} //EO insertBookmark
	
	
	
	// updateBookmark --------------------------------------------------------------------
	// updates the bookmark_lastview_datetime
	// tom calthrop - 11th December 2004
	//
	function updateBookmark() {
		$datetime = date("Y-m-d H:i:s");
		$datetime = $this->db->DBDate($datetime);
		
	    $query = "
			UPDATE " . $this->db->prefix . "_bookmark SET 
			bookmark_lastview_datetime=" . $datetime . " 
			WHERE
			cms_item_type_id=" . $this->cms_item_type_id . " and 
			bookmark_type=" . $this->bookmark_type . " and 
			user_id=" . $this->user_id . " and 
			item_id=" . $this->item_id
		;
		
		$result = $this->db->Execute($query);
		
		if (empty($result)) {
			$error_info = "function=updateBookmark class=Bookmark.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
			$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
		}
	} //EO updateBookmark
	
	
	
	// deleteBookmark --------------------------------------------------------------------
	// deletes a  bookmark
	// tom calthrop - 11th December 2004
	//
	function deleteBookmark($del_arr = null) {
		
		if (empty($del_arr)) {
		    $del_arr = array($this->item_id);
		}
		
		$rownums = count($del_arr);
		
		for ($i=0; $i < $rownums; $i++) {
		    $query = "
				DELETE FROM " . $this->db->prefix . "_bookmark 
				WHERE
				cms_item_type_id=" . $this->cms_item_type_id . " and 
				bookmark_type=" . $this->bookmark_type . " and 
				user_id=" . $this->user_id . " and 
				item_id=" . $del_arr[$i]
			;
			
			$result = $this->db->Execute($query);
			
			if (empty($result)) {
				$error_info = "function=deleteBookmark class=Bookmark.class.php -- " . $query . " -- " . $this->db->ErrorMsg();
				$GLOBALS['am_error_log'][] = array(1, 1, $error_info);
			}
		}
	} //EO deleteBookmark
	
	
	// getBookmarkLastViewDate --------------------------------------------------------------------
	// gets the bookmarks last view date
	// tom calthrop - 11th December 2004
	//
	function getBookmarkLastViewDate($item_id = null) {
		
		if (!isset($item_id) && isset($this->item_id)) {
		    $item_id = $this->item_id;
		}
		
		$query = "
			SELECT bookmark_lastview_datetime 
			FROM " . $this->db->prefix . "_bookmark 
			WHERE 
			user_id=" . $this->user_id . " and 
			bookmark_type=" . $this->bookmark_type . " and 
			cms_item_type_id=" . $this->cms_item_type_id . " and 
			item_id=" . $item_id
		;
		
		$result = $this->db->Execute($query);
			
		if (isset($result->fields['bookmark_lastview_datetime'])) {
			return $result->fields['bookmark_lastview_datetime'];
		}
		else {
			return false;
		}
	} //EO getBookmarkLastViewDate
	
}
?>
