<?php
	/**************************************************************************\
	* phpGroupWare - Trouble Ticket System                                     *
	* 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: newticket.php,v 1.31.2.3.4.6 2004/09/30 13:27:27 fipsfuchs Exp $
	// $Source: /cvsroot/phpgroupware/tts/newticket.php,v $

	$submit = $_POST['submit'];
	$cancel = $_POST['cancel'];
	if($submit || $cancel)
	{
		$GLOBALS['phpgw_info']['flags'] = array(
			'noheader' => True,
			'nonavbar' => True
		);
	}

	$GLOBALS['phpgw_info']['flags']['currentapp']          = 'tts';
	$GLOBALS['phpgw_info']['flags']['enable_send_class']   = True;
	$GLOBALS['phpgw_info']['flags']['enable_config_class'] = True;
	$GLOBALS['phpgw_info']['flags']['enable_categories_class'] = True;
	$GLOBALS['phpgw_info']['flags']['enable_vfs_class'] = True;

	include('../header.inc.php');
 
	$GLOBALS['phpgw']->config->read_repository();

	if($cancel)
	{
		$GLOBALS['phpgw']->redirect($GLOBALS['phpgw']->link('/tts/index.php'));
	}

	if($submit)
	{	
		//File-Upload
		if ($HTTP_POST_FILES['attachment']['name'] != '')
		{	
			$basedir = $GLOBALS['basedir'] . "/tts";
			//Check if home/groupdirectory exists. If not, we create it
			if (!file_exists($basedir))
			{
				$GLOBALS['phpgw']->vfs->override_acl = 1;
				$GLOBALS['phpgw']->vfs->mkdir (array (
						'string' => $basedir,
						'relatives' => array (RELATIVE_ALL)));
				$GLOBALS['phpgw']->vfs->override_acl = 0;
			}			
			
			$GLOBALS['phpgw']->db->query("SELECT max(ticket_id) from phpgw_tts_tickets",__LINE__,__FILE__);
			$GLOBALS['phpgw']->db->next_record();
			$tid = ($GLOBALS['phpgw']->db->f('max(ticket_id)')) + 1;
			
			//$tid = $ticket_id = $phpgw->db->get_last_insert_id('phpgw_tts_tickets','ticket_id');			
			$attdir = $basedir . "/" . $tid;			

			$GLOBALS['phpgw']->vfs->override_acl = 1;
			$GLOBALS['phpgw']->vfs->mkdir (array (
						'string' => $attdir,
						'relatives' => array (RELATIVE_ALL)));
			$GLOBALS['phpgw']->vfs->override_acl = 0;
						
			$GLOBALS['phpgw']->vfs->override_acl = 1;
			$GLOBALS['phpgw']->vfs->cp(array (
						'from'	=> $HTTP_POST_FILES['attachment']['tmp_name'],
						'to'	=> $attdir . '/' . $HTTP_POST_FILES['attachment']['name'],
						'relatives'	=> array (RELATIVE_NONE|VFS_REAL, RELATIVE_ALL)));
			$GLOBALS['phpgw']->vfs->override_acl = 0;
			
			$GLOBALS['phpgw']->db->query("SELECT max(file_id) from phpgw_vfs",__LINE__,__FILE__);
			$GLOBALS['phpgw']->db->next_record();
			$file_id = $GLOBALS['phpgw']->db->f('max(file_id)');
		}

		$GLOBALS['phpgw']->db->query("insert into phpgw_tts_tickets (ticket_group,ticket_priority,ticket_owner,"
			. "ticket_assignedto,ticket_subject,ticket_category,ticket_billable_hours,"
			. "ticket_billable_rate,ticket_status,ticket_deadline,ticket_effort,ticket_type,ticket_platform,ticket_attachment,ticket_details) values ('"
      . (int) $_POST['ticket']['group'] . "','"
			. (int) $_POST['ticket']['priority'] . "','"
			. $GLOBALS['phpgw_info']['user']['account_id'] . "','"
			. (int) $_POST['ticket']['assignedto'] . "','"
			. $GLOBALS['phpgw']->db->db_addslashes($_POST['ticket']['subject']) . "','"
			. (int) $_POST['ticket']['category'] . "','"
			. (float) $_POST['ticket']['billable_hours'] . "','"
			. (float) $_POST['ticket']['billable_rate'] . "','O','"
			. (int) $_POST['ticket']['deadline']['year'] . "-" . $GLOBALS['phpgw']->db->db_addslashes($_POST['ticket']['deadline']['month']) . "-" . (int) $_POST['ticket']['deadline']['day'] . "','"
			. $GLOBALS['phpgw']->db->db_addslashes($_POST['ticket']['effort'])."','"
			. ((int) $_POST['ticket']['type'] ? (int) $_POST['ticket']['type'] : 0) . "','"
			. ((int) $_POST['ticket']['platform'] ? (int) $_POST['ticket']['platform'] : 0) . "','"
			. ((int) $file_id ? (int) $file_id : 0). "','"
			. $GLOBALS['phpgw']->db->db_addslashes($_POST['ticket']['details']) . "')",__LINE__,__FILE__);

		$ticket_id = $GLOBALS['phpgw']->db->get_last_insert_id('phpgw_tts_tickets','ticket_id');

		$historylog = createobject('phpgwapi.historylog','tts');
		$historylog->add('O',$ticket_id,' ','');

		if($GLOBALS['phpgw']->config->config_data['mailnotification'])
		{
			mail_ticket($ticket_id);
		}

		$GLOBALS['phpgw']->redirect($GLOBALS['phpgw']->link('/tts/viewticket_details.php','&ticket_id=' . $ticket_id));
	}

	$account_selected  = array();
	$entry_selected    = array();
	$priority_selected = array();
	$priority_comment  = array();

	if(!$submit)
	{
		$GLOBALS['phpgw']->preferences->read_repository();
		
		/*
		if($GLOBALS['phpgw_info']['user']['preferences']['tts']['groupdefault'])
		{
			$entry_selected[$GLOBALS['phpgw_info']['user']['preferences']['tts']['groupdefault']] = ' selected';
		}

		if($GLOBALS['phpgw_info']['user']['preferences']['tts']['assigntodefault'])
		{
			$account_selected[$GLOBALS['phpgw_info']['user']['preferences']['tts']['assigntodefault']] = ' selected';
		}

		if($GLOBALS['phpgw_info']['user']['preferences']['tts']['prioritydefault'])
		{
			$priority_selected[$GLOBALS['phpgw_info']['user']['preferences']['tts']['prioritydefault']] = ' selected';
		}
		*/
		$GLOBALS['phpgw']->template->set_file(array(
			'newticket'   => 'newticket.tpl'
		));
		$GLOBALS['phpgw']->template->set_block('newticket','options_select');
		$GLOBALS['phpgw']->template->set_block('newticket','form');

		$GLOBALS['phpgw']->template->set_var('lang_create_new_ticket',lang('Create new ticket'));
		$GLOBALS['phpgw']->template->set_var('form_action', $GLOBALS['phpgw']->link('/tts/newticket.php'));

		$GLOBALS['phpgw']->template->set_var('lang_category',lang('Category'));
		$GLOBALS['phpgw']->template->set_var('lang_group', lang('Group'));
		$GLOBALS['phpgw']->template->set_var('lang_subject', lang('Subject') );
		$GLOBALS['phpgw']->template->set_var('lang_nosubject', lang('No subject'));
		$GLOBALS['phpgw']->template->set_var('lang_details', lang('Details'));
		$GLOBALS['phpgw']->template->set_var('lang_priority', lang('Priority'));
		$GLOBALS['phpgw']->template->set_var('lang_lowest', lang('Lowest'));
		$GLOBALS['phpgw']->template->set_var('lang_medium', lang('Medium'));
		$GLOBALS['phpgw']->template->set_var('lang_highest', lang('Highest'));
		$GLOBALS['phpgw']->template->set_var('lang_addticket', lang('Add Ticket'));
		$GLOBALS['phpgw']->template->set_var('lang_clearform', lang('Clear Form'));
		$GLOBALS['phpgw']->template->set_var('lang_assignedto',lang('Assign to'));
		$GLOBALS['phpgw']->template->set_var('lang_submit',lang('Submit'));
		$GLOBALS['phpgw']->template->set_var('lang_cancel',lang('Cancel'));

		$GLOBALS['phpgw']->template->set_var('lang_billable_hours',lang('Billable hours'));
		$GLOBALS['phpgw']->template->set_var('lang_billable_hours_rate',lang('Billable hours rate'));

		$GLOBALS['phpgw']->template->set_var('row_off', $GLOBALS['phpgw_info']['theme']['row_off']);
		$GLOBALS['phpgw']->template->set_var('row_on', $GLOBALS['phpgw_info']['theme']['row_on']);
		$GLOBALS['phpgw']->template->set_var('th_bg', $GLOBALS['phpgw_info']['theme']['th_bg']);

		$GLOBALS['phpgw']->template->set_var('value_details', $ticket['details']); // check this line
		$GLOBALS['phpgw']->template->set_var('value_subject', $ticket['details']); // check this line
		$GLOBALS['phpgw']->template->set_var('value_billable_hours',($ticket['billable_hours']?$ticket['billable_hours']:'0.00'));
		$GLOBALS['phpgw']->template->set_var('value_billable_hours_rate',($ticket['billable_rate']?$ticket['billable_rate']:'0.00'));
		$GLOBALS['phpgw']->template->set_var('currency',$GLOBALS['phpgw_info']['user']['preferences']['common']['currency']);

		$GLOBALS['phpgw']->template->set_var('lang_type',lang('Type'));
		$GLOBALS['phpgw']->template->set_var('lang_deadline',lang('Deadline'));
		$GLOBALS['phpgw']->template->set_var('lang_effort',lang('Effort'));
		$GLOBALS['phpgw']->template->set_var('lang_platform',lang('Platform'));
		$GLOBALS['phpgw']->template->set_var('lang_attachment',lang('Attachment'));
		
		unset($s);
		$groups = CreateObject('phpgwapi.accounts');
		$group_list = array();
		$group_list = $GLOBALS['phpgw']->accounts->membership($GLOBALS['phpgw_info']['user']['account_id']);

		while(list($key,$entry) = each($group_list))
		{
			if($GLOBALS['phpgw_info']['user']['preferences']['tts']['groupdefault'] == $entry['account_id'])
			{
				$selected = 'selected';
			}
			else
			{
				$selected = '';
			}
			$s .= '<option value="' . $entry['account_id'] . '" ' . $selected
				. '>' . $entry['account_name'] . '</option>';
		}
		$GLOBALS['phpgw']->template->set_var('options_group',$s);

		$s = '<select name="ticket[category]">' . $GLOBALS['phpgw']->categories->formated_list('select','',$group,False) . '</select>';
		$GLOBALS['phpgw']->template->set_var('value_category',$s);

		unset($s);
		for($i=1; $i<=31; $i++)
		{
			$i < 10 ? $y = '0' . $i : $y = $i;
			$s .= '<option>' . $y . '</option>';
		}
		$GLOBALS['phpgw']->template->set_var('option_day','<select name="ticket[deadline][day]"><option selected></option>' . $s . '</select>');
		
		unset($s);
		for($i=1; $i<=12; $i++)
		{
			$i < 10 ? $y = '0' . $i : $y = $i;
			$s .= '<option>' . $y . '</option>';
		}
		$GLOBALS['phpgw']->template->set_var('option_month','<select name="ticket[deadline][month]"><option selected></option>' . $s . '</select>');
		
		unset($s);
		$accounts = $groups;
		$accounts->account_id = $group_id;
		$account_list = $accounts->get_list('accounts');		
		
		while(list($key,$entry) = each($account_list))
		{
			$GLOBALS['phpgw']->accounts->get_account_name($entry['account_id'],$lid,$fname,$lname);
			if(!$fname && !$lname)
				continue;
			$employees_list[$entry['account_id']] = $GLOBALS['phpgw']->common->display_fullname($lid,$fname,$lname);
			
			if(count($employees_list))
				{
					asort($employees_list);
					reset($employees_list);
				}
		}
		
		$select_employee_list = "<select name=\"ticket[assignedto]\" size=\"1\">\n";
		while (list($account_id,$account_name) = each($employees_list))
		{
			$select_employee_list .= '<option value="' . $account_id . '"';
			if ($GLOBALS['phpgw_info']['user']['preferences']['tts']['assigntodefault'] == $account_id)
			{
				$select_employee_list .= ' selected';
			}
			$select_employee_list .= '>'.$account_name."</option>\n";
		}
		$select_employee_list .= '</select>';
		
		$GLOBALS['phpgw']->template->set_var('value_assignedto',$select_employee_list);

		//auswahlfelder type und platform


        $platform = CreateObject('tts.subcategories',$GLOBALS['phpgw_info']['user']['account_id'],'tts');
		unset($s);
		$s = '<select name="ticket[platform]">' . $platform->formated_list('select','appandplatform','',False) . '</select>';
		$GLOBALS['phpgw']->template->set_var('value_platform',$s);
		
		
		$type = CreateObject('tts.subcategories',$GLOBALS['phpgw_info']['user']['account_id'],'tts');
		unset($s);
		$s = '<select name="ticket[type]">' . $type->formated_list('select','appandtype','',False) . '</select>';
		$GLOBALS['phpgw']->template->set_var('value_type',$s);
		
		//$GLOBALS['phpgw']->template->set_var('tts_account_lid','0');
		//$GLOBALS['phpgw']->template->set_var('tts_account_name',lang('None'));

		// Choose the correct priority to display
		$prority_selected[$ticket['priority']] = ' selected';
		$priority_comment[1]  = ' - '.lang('Lowest');
		$priority_comment[5]  = ' - '.lang('Medium');
		$priority_comment[10] = ' - '.lang('Highest');
		for($i=1; $i<=10; $i++)
		{
			if($GLOBALS['phpgw_info']['user']['preferences']['tts']['prioritydefault'] == $i)
			{
				$prio_selected = 'selected';
			}
			else
			{
				$prio_selected = '';
			}
			$priority_select .= '<option value="' . $i . '" ' . $prio_selected . '>' . $i . $priority_comment[$i] . '</option>';
		}
		$GLOBALS['phpgw']->template->set_var('value_priority','<select name="ticket[priority]">' . $priority_select . '</select>');

		$GLOBALS['phpgw']->template->set_var('tts_select_options','');
		$GLOBALS['phpgw']->template->set_var('tts_new_lstcategory','');
		$GLOBALS['phpgw']->template->set_var('tts_new_lstassignto','');

		$GLOBALS['phpgw']->template->pfp('out','form');
		$GLOBALS['phpgw']->common->phpgw_footer();
	}
	else
	{
	}
?>
