<?php
  /**************************************************************************\
  * phpGroupWare - Admin                                                     *
  * 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: editserver.php,v 1.3 2001/05/11 14:32:50 milosch Exp $ */

	$phpgw_info["flags"]["currentapp"] = 'eldaptir';
	include('../header.inc.php');

	if(!$phpgw->acl->check('run',1,'admin'))
	{
		echo lang('access not permitted');
		$phpgw->common->phpgw_footer();
		$phpgw->common->phpgw_exit();

	}

	if (! $server_id) {
		Header('Location: ' . $phpgw->link('/eldaptir/servers.php',"sort=$sort&order=$order&query=$query&start=$start"
			. "&filter=$filter"));
	}

	$t = CreateObject('phpgwapi.Template',PHPGW_APP_TPL);
	$t->set_file(array('form' => 'server_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=\"filter\" value=\"$filter\">\n"
		. "<input type=\"hidden\" name=\"server_id\" value=\"$server_id\">\n";

	if ($submit) {
		$errorcount = 0;
		if (!$server_name) { $error[$errorcount++] = lang('Please enter a name for that server!'); }

		$phpgw->db->query("SELECT count(*) from phpgw_eldaptir_servers WHERE name='$server_name' AND id !='$server_id'");
		$phpgw->db->next_record();
		if ($phpgw->db->f(0) != 0) { $error[$errorcount++] = lang('That server name has been used already !'); }

		$server_name   = addslashes($server_name);
		$server_basedn = addslashes($server_basedn);
		$server_rootdn = addslashes($server_rootdn);
/*
		if($server_rootpw)
		{
			$server_rootpw = $phpgw->common->encrypt_password($server_rootpw);
		}
*/
		if ($is_default == 'on')
		{
			$default_checked = True;
		}
		else
		{
			$default_checked = 0;
		}

		if (! $error)
		{
			if($server_rootpw)
			{
				if($default_checked) $phpgw->db->query("UPDATE phpgw_eldaptir_servers SET is_default=0");
				$phpgw->db->query("UPDATE phpgw_eldaptir_servers SET"
					. " name='"       . $server_name
					. "',basedn='"    . $server_basedn
					. "',rootdn='"    . $server_rootdn
					. "',rootpw='"    . $server_rootpw
					. "',is_default=" . $default_checked
					. " WHERE id="    . $server_id);
			}
			else
			{
				if($default_checked) $phpgw->db->query("UPDATE phpgw_eldaptir_servers SET is_default=0");
				$phpgw->db->query("UPDATE phpgw_eldaptir_servers SET"
					. " name='"       . $server_name
					. "',basedn='"    . $server_basedn
					. "',rootdn='"    . $server_rootdn
					. "',is_default=" . $default_checked
					. " WHERE id="    . $server_id);
			}
		}
	}

	if ($errorcount) { $t->set_var('message',$phpgw->common->error_list($error)); }
	if (($submit) && (! $error) && (! $errorcount)) { $t->set_var('message',lang("Server $server_name has been updated !")); }
	if ((! $submit) && (! $error) && (! $errorcount)) { $t->set_var('message',''); }

	$phpgw->db->query("SELECT * FROM phpgw_eldaptir_servers WHERE id=".$server_id);
	while ($phpgw->db->next_record())
	{
		$servers[] = array(
			'id'          => $phpgw->db->f('id'),
			'name'        => $phpgw->db->f('name'),
			'basedn'      => $phpgw->db->f('basedn'),
			'rootdn'      => $phpgw->db->f('rootdn'),
			'is_default'  => $phpgw->db->f('is_default')
		);
	}

	$t->set_var('title_servers',lang('Edit Server'));
	$t->set_var('actionurl',$phpgw->link('/eldaptir/editserver.php'));
	$t->set_var('deleteurl',$phpgw->link('/eldaptir/deleteserver.php',"server_id=$server_id&start=$start&query=$query&sort=$sort&order=$order&filter=$filter"));
	$t->set_var('doneurl',$phpgw->link('/eldaptir/servers.php',"start=$start&query=$query&sort=$sort&order=$order&filter=$filter"));

	$t->set_var('hidden_vars',$hidden_vars);
	$t->set_var('lang_name',lang('Server name'));
	$t->set_var('lang_basedn',lang('Server basedn'));
	$t->set_var('lang_rootdn',lang('Server rootdn'));
	$t->set_var('lang_rootpw',lang('Server rootpw'));

	$t->set_var('lang_done',lang('Done'));
	$t->set_var('lang_default',lang('Default'));
	$t->set_var('lang_edit',lang('Edit'));
	$t->set_var('lang_delete',lang('Delete'));

	$server_id = $servers[0]['id'];

	$t->set_var('server_name',$phpgw->strip_html($servers[0]['name']));
	$t->set_var('server_basedn',$phpgw->strip_html($servers[0]['basedn']));
	$t->set_var('server_rootdn',$phpgw->strip_html($servers[0]['rootdn']));
	$t->set_var('server_rootpw','');

	if ($servers[0]['is_default'])
	{
		$t->set_var('is_default',' checked');
	}
	else
	{
		$t->set_var('is_default','');
	}

	$t->set_var('edithandle','');
	$t->set_var('addhandle','');

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

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