<?php
	/**************************************************************************\
	* phpGroupWare - Todo list                                                 *
	* http://www.phpgroupware.org                                              *
	* This program is part of the GNU project, see http://www.gnu.org/         *
	*                                                                          *
	* Written by Joseph Engo <jengo@phpgroupware.org>                          *
	*            Bettina Gille [ceb@phpgroupware.org]                          *
	* -----------------------------------------------                          *
	* Copyright 2000 - 2003 Free Software Foundation, Inc                      *
	*                                                                          *
	*  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.ui.inc.php 21063 2010-03-25 22:46:41Z Caeies $ */

	class ui
	{
		var $grants;
		var $historylog;
		var $t;
		var $public_functions = array
		(
			'show_list'	=> True,
			'view'      => True,
			'add'       => True,
			'edit'      => True,
			'delete'	=> True,
			'matrix'	=> True
		);

		function ui()
		{
			$this->bo     = CreateObject('todo.bo',True);
			$this->nextmatchs = CreateObject('phpgwapi.nextmatchs');
			$this->historylog = CreateObject('phpgwapi.historylog','todo');
			$this->historylog->types = array(
				'A' => lang('Entry added'),
				'C' => lang('Category changed'),
				'S' => lang('Start date changed'),
				'E' => lang('End date changed'),
				'U' => lang('Urgency changed'),
				's' => lang('Status changed'),
				'T' => lang('Title changed'),
				'D' => lang('Description changed'),
				'a' => lang('Access changed'),
				'P' => lang('Parent changed')
			);

			$this->historylog->alternate_handlers = array(
				'S' => '$GLOBALS[\'phpgw\']->common->show_date',
				'E' => '$GLOBALS[\'phpgw\']->common->show_date',
				'C' => '$GLOBALS[\'phpgw\']->categories->id2name'
			);

			$this->cats       = CreateObject('phpgwapi.categories');
			$GLOBALS['phpgw']->categories = $this->cats;
			$this->sbox       = CreateObject('phpgwapi.sbox');
			$this->matrix     = CreateObject('phpgwapi.matrixview');
			$this->account    = $GLOBALS['phpgw_info']['user']['account_id'];
			$this->t          = CreateObject('phpgwapi.Template',$GLOBALS['phpgw']->common->get_tpl_dir('todo'));
			$this->grants     = $GLOBALS['phpgw']->acl->get_grants('todo');

			$this->start      = $this->bo->start;
			$this->query      = $this->bo->query;
			$this->filter     = $this->bo->filter;
			$this->order      = $this->bo->order;
			$this->sort       = $this->bo->sort;
			$this->cat_id     = $this->bo->cat_id;
		}

		function save_sessiondata()
		{
			$data = array
			(
				'start'		=> $this->start,
				'query'		=> $this->query,
				'filter'	=> $this->filter,
				'order'		=> $this->order,
				'sort'		=> $this->sort,
				'cat_id'	=> $this->cat_id
			);
			$this->bo->save_sessiondata($data);
		}

		function set_app_langs()
		{
			$this->t->set_var('th_bg',$GLOBALS['phpgw_info']['theme']['th_bg']);
			$this->t->set_var('row_on',$GLOBALS['phpgw_info']['theme']['row_on']);
			$this->t->set_var('row_off',$GLOBALS['phpgw_info']['theme']['row_off']);

			$this->t->set_var('lang_category',lang('Category'));
			$this->t->set_var('lang_select',lang('Select'));
			$this->t->set_var('lang_descr',lang('Description'));
			$this->t->set_var('lang_title',lang('Title'));
			$this->t->set_var('lang_none',lang('None'));
			$this->t->set_var('lang_nobody',lang('Nobody'));
			$this->t->set_var('lang_urgency',lang('Urgency'));
			$this->t->set_var('lang_completed',lang('Completed'));
			$this->t->set_var('lang_start_date',lang('Start Date'));
			$this->t->set_var('lang_end_date',lang('End Date'));
			$this->t->set_var('lang_date_due',lang('date due'));
			$this->t->set_var('lang_access',lang('Private'));
			$this->t->set_var('lang_parent',lang('Parent project'));
			$this->t->set_var('lang_submit',lang('Submit'));
			$this->t->set_var('lang_cancel',lang('Cancel'));
			$this->t->set_var('lang_save',lang('Save'));
			$this->t->set_var('lang_done',lang('Done'));
			$this->t->set_var('lang_assigned',lang('Assigned to'));
			$this->t->set_var('lang_owner',lang('Created by'));
		}

		function show_list()
		{
			$GLOBALS['phpgw_info']['flags']['app_header'] = lang('todo') . ': ' . lang('list todos');

			$GLOBALS['phpgw']->common->phpgw_header();
			echo parse_navbar();
			echo $this->show_list_body(True);
		}

		function show_list_body($show_page_header=True)
		{
			$this->t->set_file(
				Array(
					'todo_list_t' => 'list.tpl'
				)
			);
			$this->t->set_block('todo_list_t','page_header','page_header');
			$this->t->set_block('todo_list_t','table_header','table_header');
			$this->t->set_block('todo_list_t','todo_list','todo_list');
			$this->t->set_block('todo_list_t','table_footer','table_footer');
			$this->t->set_block('todo_list_t','page_footer','page_footer');
			
			$body = '';
			$this->set_app_langs();

			$this->t->set_var('lang_all',lang('All'));

			if (!$this->start)
			{
				$this->start = 0;
			}

			$todo_list = $this->bo->_list($this->start, True, $this->query,$this->filter,$this->order,$this->sort,$this->cat_id,'all');

// --------------------- nextmatch variable template-declarations ------------------------

			if($show_page_header)
			{
				$left = $this->nextmatchs->left('/index.php',$this->start,$this->bo->total_records,'&menuaction=todo.ui.show_list');
				$right = $this->nextmatchs->right('/index.php',$this->start,$this->bo->total_records,'&menuaction=todo.ui.show_list');
				$this->t->set_var('left',$left);
				$this->t->set_var('right',$right);

				$this->t->set_var('total_matchs',$this->nextmatchs->show_hits($this->bo->total_records,$this->start));

				$this->t->set_var('cat_action',$GLOBALS['phpgw']->link('/index.php','menuaction=todo.ui.show_list'));
				$this->t->set_var('categories',$this->cats->formated_list('select','all',$this->cat_id,'True'));
				$this->t->set_var('filter_action',$GLOBALS['phpgw']->link('/index.php','menuaction=todo.ui.show_list'));
				$this->t->set_var('filter_list',$this->nextmatchs->filter(1,array('yours' => 1,'filter' => $this->filter)));
				$this->t->set_var('search_action',$GLOBALS['phpgw']->link('/index.php','menuaction=todo.ui.show_list'));
				$this->t->set_var('search_list',$this->nextmatchs->search(array('search_obj' => 1,'query' => $this->query)));

				$body .= $this->t->fp('out','page_header');
			}

// ------------------------- end nextmatch template --------------------------------------

// ---------------- list header variable template-declarations --------------------------

			$this->t->set_var('sort_status',$this->nextmatchs->show_sort_order($this->sort,'todo_status',$this->order,'/todo/index.php',lang('Status')));
			$this->t->set_var('sort_urgency',$this->nextmatchs->show_sort_order($this->sort,'todo_pri',$this->order,'/todo/index.php',lang('Urgency')));
			$this->t->set_var('sort_title',$this->nextmatchs->show_sort_order($this->sort,'todo_title',$this->order,'/todo/index.php',lang('title')));
			$this->t->set_var('sort_sdate',$this->nextmatchs->show_sort_order($this->sort,'todo_startdate',$this->order,'/todo/index.php',lang('start date')));
			$this->t->set_var('sort_edate',$this->nextmatchs->show_sort_order($this->sort,'todo_enddate',$this->order,'/todo/index.php',lang('end date')));
			$this->t->set_var('sort_owner',$this->nextmatchs->show_sort_order($this->sort,'todo_owner',$this->order,'/todo/index.php',lang('created by')));
			$this->t->set_var('h_lang_sub',lang('Add Sub'));
			$this->t->set_var('h_lang_view',lang('View'));
			$this->t->set_var('h_lang_edit',lang('Edit'));
			$this->t->set_var('h_lang_delete',lang('Delete'));

			$body .= $this->t->fp('out','table_header');

// -------------- end header declaration --------------------------------------- 

			for ($i=0;$i<count($todo_list);$i++)
			{
				$this->nextmatchs->template_alternate_row_color($this->t);
				$level = $todo_list[$i]['level'];

				$title = $GLOBALS['phpgw']->strip_html($todo_list[$i]['title']); 

				if (!$title)
				{
					$words = split(' ',$GLOBALS['phpgw']->strip_html($todo_list[$i]['descr']));
					$title = "$words[0] $words[1] $words[2] $words[3] ...";
				}

				if ($level <= 0)
				{
					$title = '<font color="#0000EE"><b>' . $title . '</b></font>';
				}
				else
				{
					$space = '&nbsp;&nbsp;';
					$spaceset = str_repeat($space,$level);
					$title = $spaceset . $title;
				}

				switch ($todo_list[$i]['pri'])
				{
					case 1: $pri = lang('Low'); break;
					case 2: $pri = '<b>' . lang('normal') . '</b>'; break;
					case 3: $pri = '<font color="#CC0000"><b>' . lang('high') . '</b></font>'; break;
				}

				if ($todo_list[$i]['edate_epoch'] == 0)
				{
					$datedueout = '&nbsp;';
				}
				else
				{
					$datedue = $todo_list[$i]['edate_epoch'];
					$month	= $GLOBALS['phpgw']->common->show_date(time(),'n');
					$day	= $GLOBALS['phpgw']->common->show_date(time(),'d');
					$year	= $GLOBALS['phpgw']->common->show_date(time(),'Y');
					$currentdate = mktime(2,0,0,$month,$day,$year);

					$datedue = $datedue + (60*60) * $GLOBALS['phpgw_info']['user']['preferences']['common']['tz_offset'];
					if (($currentdate >= $datedue) && ($todo_list[$i]['status'] < 100))
					{
						$datedueout =  '<font color="#CC0000"><b>';
					}
					else
					{
						$datedueout = '';
					}

					$datedueout .= $todo_list[$i]['edate'];
					if ($currentdate >= $datedue)
					{
						$datedueout .= '</b></font>';
					}
				}

// --------------- template declaration for list records -------------------------------------

 				$this->t->set_var(array
				(
					'status'		=> $todo_list[$i]['status'],
					'pri'			=> $pri,
					'title'			=> $title,
					'datecreated'	=> $todo_list[$i]['sdate'],
					'datedue'		=> $datedueout,
					'owner'			=> $todo_list[$i]['owner']
				));

				$this->t->set_var('view','<a href="' . $GLOBALS['phpgw']->link('/index.php','menuaction=todo.ui.view&todo_id=' . $todo_list[$i]['id'])
					. '">' . lang('View') . '</a>');

				if ($this->bo->check_perms($this->grants[$todo_list[$i]['owner_id']],PHPGW_ACL_EDIT))
				{
					$this->t->set_var('edit','<a href="' . $GLOBALS['phpgw']->link('/index.php','menuaction=todo.ui.edit&todo_id=' . $todo_list[$i]['id'])
											. '">' . lang('Edit') . '</a>');
				}
				else
				{
					$this->t->set_var('edit','&nbsp;');
				}

				if ($this->bo->check_perms($this->grants[$todo_list[$i]['owner_id']],PHPGW_ACL_DELETE))
				{
					$this->t->set_var('delete','<a href="' . $GLOBALS['phpgw']->link('/index.php','menuaction=todo.ui.delete&todo_id=' . $todo_list[$i]['id'])
												. '">' . lang('Delete') . '</a>');
				}
				else
				{
					$this->t->set_var('delete','&nbsp;');
				}

				if ($this->bo->check_perms($this->grants[$todo_list[$i]['owner_id']],PHPGW_ACL_ADD))
				{
					$this->t->set_var('subadd', '<a href="' . $GLOBALS['phpgw']->link('/index.php','menuaction=todo.ui.add&parent=' . $todo_list[$i]['id']
																. '&cat_id=' . $this->cat_id) . '">' . lang('Add Sub') . '</a>');
				}
				else
				{
					$this->t->set_var('subadd','&nbsp;');
				}

				$body .= $this->t->fp('out','todo_list');
			}

			$body .= $this->t->fp('out','table_footer');

// ------------------------- end record declaration ------------------------

// --------------- template declaration for Add Form --------------------------

			if($show_page_header)
			{
				if ($this->cat_id && $this->cat_id != 0)
				{
					$cat = $this->cats->return_single($this->cat_id);
				}

				if ($cat[0]['app_name'] == 'phpgw' || $cat[0]['owner'] == '-1' || !$this->cat_id)
				{
					$this->t->set_var('add','<form method="POST" action="' . $GLOBALS['phpgw']->link('/index.php','menuaction=todo.ui.add&cat_id=' . $this->cat_id)
						. '"><input type="submit" name="Add" value="' . lang('Add') .'"></form>');
				}
				else
				{
					if ($this->bo->check_perms($this->grants[$cat[0]['owner']],PHPGW_ACL_ADD) || $cat[0]['owner'] == $GLOBALS['phpgw_info']['user']['account_id'])
					{
						$this->t->set_var('add','<form method="POST" action="' . $GLOBALS['phpgw']->link('/index.php','menuaction=todo.ui.add&cat_id=' . $this->cat_id)
						. '"><input type="submit" name="Add" value="' . lang('Add') .'"></form>');
					}
					else
					{
						$this->t->set_var('add','');
					}
				}

// ----------------------- end Add form declaration ----------------------------

// ------------ get actual date and year for matrixview arguments --------------

				$year = date('Y');
				$month = date('m');

				$this->t->set_var('matrixview',$GLOBALS['phpgw']->common->check_owner($GLOBALS['phpgw_info']['user']['account_id'],'/index.php',
					lang('View matrix of actual month'),'menuaction=todo.ui.matrix&month=' . $month . '&year=' . $year));
									
				$body .= $this->t->fp('out','page_footer');
			}

			$this->save_sessiondata();
			return $body;
		}

		function formatted_user($selected = '',$type)
		{
			if (!$selected)
			{
				$selected = $this->account;
			}

			if (! is_array($selected))
			{
				$selected = explode(',',$selected);
			}

			$employees = $this->bo->employee_list($type);

			while (list($null,$account) = each($employees))
			{
				$user_list .= '<option value="' . $account['account_id'] . '"';
				if (in_array($account['account_id'],$selected))
				{
					$user_list .= ' selected';
				}
				if ($account['account_type'] == 'u')
				{
					$user_list .= '>' . $account['account_firstname'] . ' ' . $account['account_lastname']
										. ' [' . $account['account_lid'] . ']'; 
				}
				else
				{
					$user_list .= '>' . $account['account_firstname'] . '&nbsp;' . lang('Group');
				}
				$user_list .= '</option>' . "\n";
			}
			return $user_list;
		}

		function formatted_todo($selected = '')
		{
			$todos = $this->bo->_list($start, False, $query, $filter, $sort, $order, $cat_id, $tree);

			for ($i=0;$i<count($todos);$i++)
			{
				$todo_select .= '<option value="' . $todos[$i]['id'] . '"';
				if ($todos[$i]['id'] == $selected)
				{
					$todo_select .= ' selected';
				}
				if (! $todos[$i]['title'])
				{
					$words = split(' ',$GLOBALS['phpgw']->strip_html($todos[$i]['descr']));
					$title = "$words[0] $words[1] $words[2] $words[3] ...";
					$todo_select .= '>' . $title;
				}
				else
				{
					$todo_select .= '>' . $GLOBALS['phpgw']->strip_html($todos[$i]['title']);
				}
				$todo_select .= '</option>';
			}
			return $todo_select;
		}

		function add()
		{
			$cat_id	= get_var('cat_id',array('GET','POST'));
			$parent	= get_var('parent',array('GET','POST'));
			$values	= get_var('values',array('POST'));

			if ($_POST['new_parent'])
			{
				$parent = $_POST['new_parent'];
			}

			if ($_POST['new_cat'])
			{
				$cat_id = $_POST['new_cat'];
			}

			if ($_POST['submit'])
			{
				$values['cat'] = $cat_id;
				$values['parent'] = $parent;

				$error = $this->bo->check_values($values);
				if (is_array($error))
				{
					$this->t->set_var('error',$GLOBALS['phpgw']->common->error_list($error));
				}
				else
				{
					$this->bo->save($values);
					$GLOBALS['phpgw']->redirect_link('/index.php','menuaction=todo.ui.show_list&cat_id=' . $cat_id);
					$GLOBALS['phpgw']->common->phpgw_exit();
				}
			}

			$GLOBALS['phpgw_info']['flags']['app_header'] = lang('todo') . ': ' . ($parent?lang('Add sub project'):lang('Add main project'));

			$GLOBALS['phpgw']->common->phpgw_header();
			echo parse_navbar();

			$this->t->set_file('todo_add','form.tpl');
			$this->t->set_block('todo_add','add','addhandle');
			$this->t->set_block('todo_add','edit','edithandle');

			$this->set_app_langs();
			$this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/index.php','menuaction=todo.ui.add'));

			$this->t->set_var('cat_list',$this->cats->formated_list('select','all',$cat_id,'True'));
			$this->t->set_var('todo_list',$this->formatted_todo($parent));

			$this->t->set_var('descr',$values['descr']);

			$this->t->set_var('pri_list',$this->sbox->getPriority('values[pri]'));
			$this->t->set_var('stat_list',$this->sbox->getPercentage('values[status]',0));

			if (!$values['smonth'])
			{
				$values['smonth'] = date('m',time());
			}

			if (!$values['sday'])
			{
				$values['sday'] = date('d',time());
			}

			if (!$values['syear'])
			{
				$values['syear'] = date('Y',time());
			}

			$this->t->set_var('start_select_date',$GLOBALS['phpgw']->common->dateformatorder($this->sbox->getYears('values[syear]',$values['syear']),
										$this->sbox->getMonthText('values[smonth]',$values['smonth']),$this->sbox->getDays('values[sday]',$values['sday'])));
			$this->t->set_var('end_select_date',$GLOBALS['phpgw']->common->dateformatorder($this->sbox->getYears('values[eyear]',$values['eyear']),
										$this->sbox->getMonthText('values[emonth]',$values['emonth']),$this->sbox->getDays('values[eday]',$values['eday'])));

			$this->t->set_var('selfortoday','<input type="checkbox" name="values[seltoday]" value="True">');

			$this->t->set_var('daysfromstartdate','<input type="text" name="values[daysfromstart]" size="3" maxlength="3">');

			$this->t->set_var('access_list', '<input type="checkbox" name="values[access]" value="True"' . ($values['access'] == 'private'?' checked':'') . '>');

			$this->t->set_var('lang_selfortoday',lang('or: select for today:'));
			$this->t->set_var('lang_daysfromstartdate',lang('or: days from startdate:'));
			$this->t->set_var('lang_submit',lang('Submit'));
			$this->t->set_var('lang_reset',lang('Clear form'));
			$this->t->set_var('edithandle','');
			$this->t->set_var('addhandle','');
			$this->t->pfp('out','todo_add');
			$this->t->pfp('addhandle','add');
		}

		function view()
		{
			$todo_id = get_var('todo_id',array('GET'));

			$GLOBALS['phpgw_info']['flags']['app_header'] = lang('todo') . ': ' . lang('view todo');

			$GLOBALS['phpgw']->common->phpgw_header();
			echo parse_navbar();

			$values = $this->bo->read($todo_id);
			$this->t->set_file('_view','view.tpl');

			$this->set_app_langs();

			$this->t->set_var('value_title',$GLOBALS['phpgw']->strip_html($values['title']));
			$this->t->set_var('value_descr',nl2br($GLOBALS['phpgw']->strip_html($values['descr'])));
			$this->t->set_var('value_category',$this->cats->id2name($values['cat']));

			$sdate = $values['sdate'] - $this->bo->datetime->tz_offset;
			$this->t->set_var('value_start_date',$GLOBALS['phpgw']->common->show_date($sdate,$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']));

			if ($values['edate'] && $values['edate'] != 0)
			{
				$edate = $values['edate'] - $this->bo->datetime->tz_offset;
				$this->t->set_var('value_end_date',$GLOBALS['phpgw']->common->show_date($edate,$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']));
			}

			$parent_values = $this->bo->read($values['parent']);
			$this->t->set_var('value_parent',$GLOBALS['phpgw']->strip_html($parent_values['title']));

			$this->t->set_var('value_completed',$values['status']);

			$cached_data = $this->bo->cached_accounts($values['owner']);
			$this->t->set_var('owner',$GLOBALS['phpgw']->common->display_fullname($cached_data[$values['owner']]['lid'],
									$cached_data[$values['owner']]['firstname'],$cached_data[$values['owner']]['lastname']));

			switch ($values['pri'])
			{
				case 1: $pri = lang('Low'); break;
				case 2: $pri = lang('normal'); break;
				case 3: $pri = '<font color="CC0000"><b>' . lang('high') . '</b></font>'; break;
			}

			$this->t->set_var('value_urgency',$pri);

			$this->t->set_var('lang_access',lang('Access'));
			$this->t->set_var('access',lang($values['access']));

			$this->t->set_var('history',$this->historylog->return_html(array(),'','', $todo_id));
			$this->t->set_var('done_action',$GLOBALS['phpgw']->link('/index.php','menuaction=todo.ui.show_list&cat_id=' . $values['cat']));
			$this->t->pfp('out','_view');
		}

		function edit()
		{
			$todo_id	= get_var('todo_id',array('GET','POST'));
			$cat_id		= get_var('cat_id',array('GET','POST'));
			$parent		= get_var('parent',array('POST','GET'));
			$values		= get_var('values',array('POST'));

			if ($_POST['new_parent'])
			{
				$parent = $_POST['new_parent'];
			}

			if ($_POST['new_cat'])
			{
				$cat_id = $_POST['new_cat'];
			}

            if ($_POST['submit'])
            {
                $values['cat'] = $cat_id;
                $values['parent'] = $parent;
                $values['id'] = $todo_id;

				$error = $this->bo->check_values($values);
				if (is_array($error))
				{
					$this->t->set_var('error',$GLOBALS['phpgw']->common->error_list($error));
				}
				else
				{
					$this->bo->save($values,'edit');
					$GLOBALS['phpgw']->redirect_link('/index.php','menuaction=todo.ui.show_list&cat_id=' . $cat_id);
					$GLOBALS['phpgw']->common->phpgw_exit();
				}
			}

			$GLOBALS['phpgw_info']['flags']['app_header'] = lang('todo') . ': ' . ($parent?lang('Edit sub project'):lang('Edit main project'));

			$GLOBALS['phpgw']->common->phpgw_header();
			echo parse_navbar();

			$this->t->set_file(array('todo_edit' => 'form.tpl'));
			$this->t->set_block('todo_edit','add','addhandle');
			$this->t->set_block('todo_edit','edit','edithandle');

			$this->set_app_langs();
			$this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/index.php','menuaction=todo.ui.edit&todo_id=' . $todo_id));

			$values = $this->bo->read($todo_id);

			$this->t->set_var('cat_list',$this->cats->formated_list('select','all',$values['cat'],'True'));
			$this->t->set_var('todo_list',$this->formatted_todo($values['parent']));

			$this->t->set_var('descr',$GLOBALS['phpgw']->strip_html($values['descr']));
			$this->t->set_var('title',$GLOBALS['phpgw']->strip_html($values['title']));

			$this->t->set_var('pri_list',$this->sbox->getPriority('values[pri]',$values['pri']));
			$this->t->set_var('stat_list',$this->sbox->getPercentage('values[status]',$values['status']));

			if ($values['sdate'] == 0)
			{
				$values['sday'] = 0;
				$values['smonth'] = 0;
				$values['syear'] = 0;
			}
			else
			{
				$values['sday'] = date('d',$values['sdate']);
				$values['smonth'] = date('m',$values['sdate']);
				$values['syear'] = date('Y',$values['sdate']);
			}

			$this->t->set_var('start_select_date',$GLOBALS['phpgw']->common->dateformatorder($this->sbox->getYears('values[syear]',$values['syear']),
										$this->sbox->getMonthText('values[smonth]',$values['smonth']),$this->sbox->getDays('values[sday]',$values['sday'])));

			if ($values['edate'] == 0)
			{
				$values['eday'] = 0;
				$values['emonth'] = 0;
				$values['eyear'] = 0;
			}
			else
			{
				$values['eday'] = date('d',$values['edate']);
				$values['emonth'] = date('m',$values['edate']);
				$values['eyear'] = date('Y',$values['edate']);
			}

			$this->t->set_var('end_select_date',$GLOBALS['phpgw']->common->dateformatorder($this->sbox->getYears('values[eyear]',$values['eyear']),
										$this->sbox->getMonthText('values[emonth]',$values['emonth']),$this->sbox->getDays('values[eday]',$values['eday'])));

			$this->t->set_var('selfortoday','&nbsp;');
			$this->t->set_var('lang_selfortoday','&nbsp;');
			$this->t->set_var('lang_daysfromstartdate','&nbsp;');
			$this->t->set_var('daysfromstartdate','&nbsp;');

			$this->t->set_var('access_list', '<input type="checkbox" name="values[access]" value="True"' . ($values['access'] == 'private'?' checked':'') . '>');

			if ($this->bo->check_perms($this->grants[$values['owner']],PHPGW_ACL_DELETE) || $values['owner'] == $GLOBALS['phpgw_info']['user']['account_id'])
			{
				$this->t->set_var('delete','<form method="POST" action="' . $GLOBALS['phpgw']->link('/index.php','menuaction=todo.ui.delete&todo_id=' . $values['id'])
                                    . '"><input type="submit" value="' . lang('Delete') .'"></form>');
			}
			else
			{
				$this->t->set_var('delete','&nbsp;');
			}

			$this->t->set_var('lang_submit',lang('Submit'));
			$this->t->set_var('edithandle','');
			$this->t->set_var('addhandle','');
			$this->t->pfp('out','todo_edit');
			$this->t->pfp('edithandle','edit');
		}

		function delete()
		{
			$todo_id = get_var('todo_id',array('GET','POST'));

			if ($_POST['confirm'])
			{
				if ($_POST['subs'])
				{
					$this->bo->delete($todo_id,True);
				}
				else
				{
					$this->bo->delete($todo_id);
				}
				$GLOBALS['phpgw']->redirect_link('/index.php','menuaction=todo.ui.show_list');
			}

			$GLOBALS['phpgw_info']['flags']['app_header'] = lang('todo') . ': ' . lang('delete todo');

			$GLOBALS['phpgw']->common->phpgw_header();
			echo parse_navbar();

			$this->t->set_file(array('todo_delete' => 'delete.tpl'));
			$this->t->set_var('actionurl',$GLOBALS['phpgw']->link('/index.php','menuaction=todo.ui.delete&todo_id=' . $todo_id));

			$exists = $this->bo->exists($todo_id);

			if ($exists)
			{
				$this->t->set_var('lang_subs',lang('Do you also want to delete all sub projects ?'));
				$this->t->set_var('subs','<input type="checkbox" name="subs" value="True">');
			}
			else
			{
				$this->t->set_var('lang_subs','');
				$this->t->set_var('subs', '');
			}

			$this->t->set_var('nolink',$GLOBALS['phpgw']->link('/index.php','menuaction=todo.ui.show_list'));
			$this->t->set_var('deleteheader',lang('Are you sure you want to delete this entry'));
			$this->t->set_var('lang_no',lang('No'));
			$this->t->set_var('lang_yes',lang('Yes'));

			$this->t->pfp('out','todo_delete');
		}

		function matrix()
		{
			$month	= get_var('month',array('POST','GET'));
			$year	= get_var('year',array('POST','GET'));
			$o		= get_var('o',array('POST','GET'));

			$GLOBALS['phpgw_info']['flags']['app_header'] = lang('todo') . ': ' . lang('matrix view');

			$GLOBALS['phpgw']->common->phpgw_header();
			echo parse_navbar();

			$colors = array
			(
				'#CC0033',
				'#006600',
				'#00CCFF',
				'#FF6600',
				'#0000FF'
			);

			$this->matrix->matrixview($month,$year);

			$entries = $this->bo->_list($start,False,$query,$filter,$order,$sort,$cat_id,'mains');

			for ($i=0;$i<count($entries);$i++)
			{
				$o++;
				$ind = $o % count($colors);

				if ($entries[$i]['sdate_epoch'] > 0 && $entries[$i]['edate_epoch'] > 0)
				{
					$startd = date('Y',$entries[$i]['sdate_epoch']) . date('m',$entries[$i]['sdate_epoch']) . date('d',$entries[$i]['sdate_epoch']);
					$endd = date('Y',$entries[$i]['edate_epoch']) . date('m',$entries[$i]['edate_epoch']) . date('d',$entries[$i]['edate_epoch']);
					$this->matrix->setPeriod($GLOBALS['phpgw']->strip_html($entries[$i]['title']),$startd,$endd,$colors[$ind]);

					$subentries = $this->bo->_list($start,False,$query,$filter,$order,$sort,$cat_id,'subs',$entries[$i]['id']);
					for ($j=0;$j<count($subentries);$j++)
					{
						if ($subentries[$j]['sdate_epoch'] > 0 && $subentries[$j]['edate_epoch'] > 0)
						{
							$startd = date('Y',$subentries[$j]['sdate_epoch']) . date('m',$subentries[$j]['sdate_epoch']) . date('d',$subentries[$j]['sdate_epoch']);                                                        
							$endd = date('Y',$subentries[$j]['edate_epoch']) . date('m',$subentries[$j]['edate_epoch']) . date('d',$subentries[$j]['edate_epoch']);
							$this->matrix->setPeriod($GLOBALS['phpgw']->strip_html($subentries[$j]['title']),$startd,$endd,$colors[$ind]);
						}
					}
				}
			}
			$this->matrix->out($GLOBALS['phpgw']->link('/index.php','menuaction=todo.ui.matrix'));
		}
	}
?>
