<?php
	/**************************************************************************\
	* phpGroupWare - Inventory                                                 *
	* 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: editdist.php,v 1.14 2001/05/24 03:00:16 bettina Exp $ */

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

	if (! $id)
	{
		Header('Location: ' . $phpgw->link('/inv/listdist.php','sort=' . $sort . '&order=' . $order . '&query=' . $query . '&start=' . $start
										. '&filter=' . $filter . '&cat_id=' . $cat_id));
		$phpgw->common->phpgw_exit();
	}

	$phpgw_info['flags']['currentapp'] = 'inv';
	include('../header.inc.php');
	$d = CreateObject('phpgwapi.contacts');

	if ($submit)
	{
		$fields = Array();
		$fields['org_name']			= $company;
		$fields['org_unit']			= $department;
		$fields['n_given']			= $firstname;
		$fields['n_family']			= $lastname;
		$fields['industry_type']	= $industry_type;
		$fields['software']			= $software;
		$fields['email']			= $email;
		$fields['tel_work']			= $wphone;
		$fields['tel_fax']			= $fax;
		$fields['tel_pager']		= $pager;
		$fields['note']				= $notes;
		if ($url == 'http://') { $url = ''; }
		$fields['url']				= $url;
		if ($url_mirror == 'http://') { $url_mirror = ''; }
		$fields['url_mirror']		= $url_mirror;
		if ($ftp == 'ftp://') { $ftp = ''; }
		$fields['ftp']				= $ftp;
		if ($ftp_mirror == 'ftp://') { $ftp_mirror = ''; }
		$fields['ftp_mirror']		= $ftp_mirror;

		if ($access) { $access = 'private'; }
		else { $access = 'public'; }

		$d->update($id,$phpgw_info['user']['account_id'],$fields,$access,$cat_id,$tid='n');
		Header('Location: ' . $phpgw->link('/inv/listdist.php','cat_id=' . $cat_id . '&order=' . $order . '&sort=' . $sort . '&filter=' . $filter
											. '&start=' . $start));
		$phpgw->common->phpgw_exit();
	}

    $t = CreateObject('phpgwapi.Template',PHPGW_APP_TPL);
	$t->set_file(array('form' => 'dist_form.tpl'));
	$t->set_block('form','add','addhandle');
	$t->set_block('form','edit','edithandle');

	$hidden_vars = '<input type="hidden" name="sort" value="' . $sort . '">' . "\n"
				. '<input type="hidden" name="order" value="' . $order . '">' . "\n"
				. '<input type="hidden" name="query" value="' . $query . '">' . "\n"
				. '<input type="hidden" name="start" value="' . $start . '">' . "\n"
				. '<input type="hidden" name="cat_id" value="' . $cat_id . '">' . "\n"
				. '<input type="hidden" name="id" value="' . $id . '">' . "\n"
				. '<input type="hidden" name="filter" value="' . $filter . '">' . "\n";

	$t->set_var('hidden_vars',$hidden_vars);

	$fields =  array('org_name' => 'org_name',
					'org_unit' => 'org_unit',
					'n_given'  => 'n_given',
					'n_family' => 'n_family',
				'industry_type' => 'industry_type',
					'software' => 'software',
						'email' => 'email',
					'tel_work' => 'tel_work', 
					'tel_fax' => 'tel_fax',
					'tel_pager' => 'tel_pager',
						'note' => 'note',
						'url' => 'url',
				'url_mirror' => 'url_mirror',
						'ftp' => 'ftp',
				'ftp_mirror' => 'ftp_mirror');

	$fields = $d->read_single_entry($id,$fields,$access,$cat_id,$tid='n');

	$t->set_var('lang_action',lang('Edit distributor'));
	$t->set_var('actionurl',$phpgw->link('/inv/editdist.php','id=' . $id));
	$t->set_var('deleteurl',$phpgw->link('/inv/deletedist.php','id=' . $id));
	$t->set_var('lang_company',lang('Company name'));
	$t->set_var('lang_department',lang('Department'));
	$t->set_var('lang_software',lang('Software'));
	$t->set_var('lang_url',lang('WWW'));
	$t->set_var('lang_url_mirror',lang('WWW mirror'));                                                                                                                                                   
	$t->set_var('lang_industry_type',lang('Industry type')); 
	$t->set_var('lang_firstname',lang('Firstname')); 
	$t->set_var('lang_lastname',lang('Lastname'));
	$t->set_var('lang_notes',lang('Notes')); 
	$t->set_var('lang_pager',lang('Pager'));                                                                                                                               
	$t->set_var('lang_ftp',lang('FTP'));
	$t->set_var('lang_ftp_mirror',lang('FTP mirror'));
	$t->set_var('lang_email',lang('Email'));
	$t->set_var('lang_phone',lang('Phone'));
	$t->set_var('lang_fax',lang('Fax'));
	$t->set_var('lang_contact',lang('Contact'));
	$t->set_var('lang_access',lang('Private'));
	$t->set_var('lang_cats',lang('Category'));
	$t->set_var('lang_select_cats',lang('Select category'));

	$id = $fields[0]['id'];
	$company = $fields[0]['org_name'];
	$department = $fields[0]['org_unit'];
	$firstname = $fields[0]['n_given'];
	$lastname = $fields[0]['n_family'];
	$industry_type = $fields[0]['industry_type'];
	$software = $fields[0]['software'];
	$email = $fields[0]['email'];
	$wphone = $fields[0]['tel_work'];
	$fax = $fields[0]['tel_fax'];
	$pager = $fields[0]['tel_pager'];
	$url = $fields[0]['url'];
	$url_mirror = $fields[0]['url_mirror'];
	$ftp = $fields[0]['ftp'];
	$ftp_mirror = $fields[0]['ftp_mirror'];
	$notes = $fields[0]['notes'];
	$access = $fields[0]['access'];

	$c = CreateObject('phpgwapi.categories');
	$c->app_name = 'addressbook';
	$t->set_var('cats_list',$c->formated_list('select','all',$cat_id,'True'));
	$t->set_var('company',$company);
	$t->set_var('department',$department);
	$t->set_var('firstname',$firstname);
	$t->set_var('lastname',$lastname);
	$t->set_var('industry_type',$industry_type);
	$t->set_var('software',$software);
	$t->set_var('email',$email);
	$t->set_var('wphone',$wphone);     
	$t->set_var('fax',$fax);
	$t->set_var('pager',$pager);
	if (! ereg('http://',$url)) { $url = 'http://'. $url; }
	$t->set_var('url',$url);
	if (! ereg('http://',$url_mirror)) { $url_mirror = 'http://'. $url_mirror; }
	$t->set_var('url_mirror',$url_mirror);
	if (! ereg('ftp://',$ftp)) { $ftp = 'ftp://'. $ftp; }
	$t->set_var('ftp',$ftp);
	if (! ereg('ftp://',$ftp_mirror)) { $ftp_mirror = 'ftp://'. $ftp_mirror; }
	$t->set_var('ftp_mirror',$ftp_mirror);
	$t->set_var('notes',$notes);

	if ($access=='private') { $t->set_var('access', '<input type="checkbox" name="access" value="True" checked>'); }
	else { $t->set_var('access', '<input type="checkbox" name="access" value="True">'); }

	$t->set_var('lang_edit',lang('Edit'));
	$t->set_var('lang_delete',lang('Delete'));
	$t->set_var('edithandle','');
	$t->set_var('addhandle','');

	$t->pparse('out','form');
	$t->pparse('edithandle','edit');

	$phpgw->common->phpgw_footer();
?>
