<?php
	/***************************************************************************\
	* phpGroupWare - Notes														*
	* http://www.phpgroupware.org												*
	* Written by Bettina Gille [ceb@phpgroupware.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 of the License, or (at your	*
	* option) any later version.												*
	\***************************************************************************/
	/* $Id: class.notes.inc.php,v 1.2.2.1 2001/06/25 00:36:09 skeeter Exp $ */

	class notes
	{
		var $db;
		var $db2;
		var $grants;
		var $total_records;

		function notes()
		{
			global $phpgw;

			$this->db				= $phpgw->db;
			$this->db2				= $this->db;
//			$this->total_records	= $this->db->num_rows();
			$this->grants			= $phpgw->acl->get_grants('notes');
//			$this->read_notes		= $this->read_notes($start, $limit, $search, $filter, $cat_id);
		}

		function check_perms($has, $needed)
		{
			return (!!($has & $needed) == True);
		}

		function read_notes($start, $limit = True, $search = '', $filter = '',$cat_id = '')
		{
			global $phpgw,$phpgw_info;

			if (! $filter)
			{
				$filter = 'none';
			}

			if ($filter != 'private')
			{
				if ($filter != 'none')
				{
					$filtermethod = " note_access like '%,$filter,%' ";
				}
				else
				{
					$filtermethod = " ( note_owner=" . $phpgw_info['user']['account_id'];
					if (is_array($this->grants))
					{
						$grants = $this->grants;
						while (list($user) = each($grants))
						{
							$public_user_list[] = $user;
						}
						reset($public_user_list);
						$filtermethod .= " OR (note_access='public' AND note_owner in(" . implode(',',$public_user_list) . ")))";
					}
					else
					{
						$filtermethod .= ' )';
					}
				}
			}
			else
			{
				$filtermethod = ' note_owner=' . $phpgw_info['user']['account_id'] . ' ';
			}

			if ($cat_id)
			{
				$filtermethod .= " AND note_category='$cat_id' ";
			}

			if ($search)
			{
				$searchmethod = " AND note_content like '%$search%'";
			}

			$sql = "SELECT * FROM phpgw_notes WHERE $filtermethod $searchmethod ORDER BY note_date DESC";

			$this->db2->query($sql,__LINE__,__FILE__);
			$this->total_records = $this->db2->num_rows();
			$this->db->query($sql. " " . $this->db->limit($start),__LINE__,__FILE__);

			$i = 0;
			while ($this->db->next_record())
			{
				$notes[$i]['id']		= $this->db->f('note_id');
				$notes[$i]['owner']		= $this->db->f('note_owner');
				$notes[$i]['access']	= $this->db->f('note_access');
				$notes[$i]['date']		= $this->db->f('note_date');
				$notes[$i]['category']	= $this->db->f('note_category');
				$notes[$i]['content']	= $this->db->f('note_content');
				$i++;
			}
			return $notes;
		}
	}
?>
