<?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 17479 2006-10-24 06:53:06Z skwashd $ */

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

	$submit = $_POST['submit'];
	$dir_id = $_POST['dir_id'] ? $_POST['dir_id'] : $_GET['dir_id'];
	$dir_name = $_POST['dir_name'] ? $_POST['dir_name'] : $_GET['dir_name'];

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

	$GLOBALS['phpgw']->template->set_file(array('form' => 'directory_form.tpl'));
	$GLOBALS['phpgw']->template->set_block('form','add','addhandle');
	$GLOBALS['phpgw']->template->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 (!$dir_name) { $error[$errorcount++] = lang('Please enter a name for that directory !'); }

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

		$dir_name   = addslashes($dir_name);

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

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

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

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

	$GLOBALS['phpgw']->template->set_var('hidden_vars',$hidden_vars);
	$GLOBALS['phpgw']->template->set_var('lang_name',lang('Directory name'));

	$GLOBALS['phpgw']->template->set_var('lang_done',lang('Done'));
	$GLOBALS['phpgw']->template->set_var('lang_default',lang('Default'));
	$GLOBALS['phpgw']->template->set_var('lang_edit',lang('Edit'));
	$GLOBALS['phpgw']->template->set_var('lang_delete',lang('Delete'));

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

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

	$GLOBALS['phpgw']->template->set_var('edithandle','');
	$GLOBALS['phpgw']->template->set_var('addhandle','');

	$GLOBALS['phpgw']->template->pparse('out','form');
	$GLOBALS['phpgw']->template->pparse('edithandle','edit');

	$GLOBALS['phpgw']->common->phpgw_footer();
?>
