<?php
  /**************************************************************************\
  * phpGroupWare -                                                           *
  * 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: editdirectory.php,v 1.2 2001/05/11 15:01:03 milosch Exp $ */

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

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

	$t = CreateObject('phpgwapi.Template',PHPGW_APP_TPL);
	$t->set_file(array('form' => 'directory_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=\"dir_id\" value=\"$dir_id\">\n";

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

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

		$directory_name   = addslashes($directory_name);
		$directory_basedn = addslashes($directory_basedn);
		$directory_rootdn = addslashes($directory_rootdn);

		if (! $error)
		{
			$phpgw->db->query("UPDATE phpgw_dj_directories SET"
				. " dir_name='"    . $dir_name
				. " WHERE dir_id=" . $dir_id);
		}
	}

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

	$phpgw->db->query("SELECT * FROM phpgw_dj_directories WHERE dir_id=".$dir_id);
	while ($phpgw->db->next_record())
	{
		$directory[] = array(
			'dir_id'   => $phpgw->db->f('dir_id'),
			'dir_name' => $phpgw->db->f('dir_name'),
		);
	}

	$t->set_var('title_directory',lang('Edit Directory'));
	$t->set_var('actionurl',$phpgw->link('/dj/editdirectory.php'));
	$t->set_var('deleteurl',$phpgw->link('/dj/deletedirectory.php',"dir_id=$dir_id&start=$start&query=$query&sort=$sort&order=$order&filter=$filter"));
	$t->set_var('doneurl',$phpgw->link('/dj/directories.php',"start=$start&query=$query&sort=$sort&order=$order&filter=$filter"));

	$t->set_var('hidden_vars',$hidden_vars);
	$t->set_var('lang_name',lang('Directory name'));

	$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'));

	$dir_id = $directory[0]['id'];

	$t->set_var('dir_name',$phpgw->strip_html($directory[0]['dir_name']));

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

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

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