<?php
	/**************************************************************************\
	* phpGroupWare - Todo                                                      *
	* http://www.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.todo.inc.php,v 1.7 2001/04/04 11:48:31 jengo Exp $ */

	class todo
	{
		var $db;
		var $grants;

		function todo()
		{
			global $phpgw;
			$this->db     = $phpgw->db;
			$this->grants = $phpgw->acl->get_grants('todo');
		}

		function check_record_rights($todo_id, $required_rights)
		{
			global $phpgw_info;

			$this->db->query("select todo_owner from phpgw_todo where todo_id='$todo_id'");
			$this->db->next_record();

			// Make sure this user has access to this item
			if (! $this->check_perms($this->grants[$this->db->f('todo_owner')],$required_rights) && $this->db->f('todo_owner') != $phpgw_info['user']['account_id'])
			{
				return False;
			}
			else
			{
				return True;
			}
		}

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

		function add($values)
		{
			$this->db->query("insert into phpgw_todo (todo_id_parent, todo_owner,todo_access,todo_cat,"
				. "todo_des,todo_pri,todo_status,todo_datecreated,todo_startdate, todo_enddate) values ("
				. $values['todo_id_parent'] . ", '" . $values['owner'] . "','" . $values['access'] . "','"
				. $values['cat'] . "','" . addslashes($values['des']) . "','" . $values['pri'] . "','"
				. $values['status'] . "','" . time() . "', " . $values['startdate'] . ", " . $values['enddate']
				. ")",__LINE__,__FILE__);
		}

		function delete($todo_id)
		{
			global $phpgw_info;
			$this->db->query("delete from phpgw_todo where todo_id='$todo_id' or todo_id_parent='"
				. "$todo_id' AND ((todo_access='public' and todo_owner != '"
				. $phpgw_info['user']['account_id'] . "') or (todo_owner='"
				. $phpgw_info['user']['account_id'] . "'))" ,__LINE__,__FILE__);
		}

		function edit($values)
		{
			$this->db->query("update phpgw_todo set todo_des='". addslashes($values['des'])
				. "', todo_pri='" . $values['pri'] . "', todo_status='" . $values['status']
				. "', todo_datecreated='" . time() . "', todo_access='" . $values['access']
				. "', todo_startdate='" . $values['startdate'] . "', todo_enddate='" . $values['enddate']
				. "', todo_cat='" . $values['cat'] . "' where todo_id='" . $values['todo_id']
				. "'",__LINE__,__FILE__);
		}
	}