<?PHP
/******************************************************************************
 @Author:     Björn Brockmann, Paul Eppner (paul@twomoons.de)
			      Sven Bräutigam, Jürgen Brändle
 @Homepage    http://www.der-dirigent.de
  Last update : 2005-11-24       - Bugfix for multibyte charsets                        
  Part of     : Update to Version 01.00.02    
******************************************************************************/

/******************************************************************************
 1. Benötigte Funktionen und Klassen includieren
******************************************************************************/

include('inc/fnc.user.php');

/******************************************************************************
 2. Eventuelle Actions/ Funktionen abarbeiten
******************************************************************************/

$perm->check('1', 'area_user');
switch($action) {
	case 'activate':
		user_set_active($iduser, '1');
		break;
	case 'deactivate':
		user_set_active($iduser, '0');
		break;
	case 'delete':
		user_delete();
		break;
}

/******************************************************************************
 3. Eventuelle Dateien zur Darstellung includieren
******************************************************************************/

include('inc/inc.header.php');

/******************************************************************************
 4. Bildschirmausgabe aufbereiten und ausgeben
******************************************************************************/

// Allgemeine Einstellungen initialisieren
if (empty($ascdesc)) {
	$order = 'surname';
	$ascdesc = 'ASC';
}
// Gibt es User, die keiner Gruppe zugeordnet sind?
$sql = "SELECT u_g_id FROM ".$dedi_db['users_groups']." WHERE idgroup='1' LIMIT 0, 1";
$db->query($sql);
if ($db->affected_rows()) $non_available = true;
if ((!$non_available && $idgroup == '1') || empty($idgroup)) $idgroup = '2';
$base_url = $sess->url('main.php?area=user&idgroup='.$idgroup.'&order=%s&ascdesc=%s');


// Templatedatei laden
$tpl->loadTemplatefile('user.tpl');
$tmp['AREA'] = $dedi_lang['area_user'];
if (!empty($errno)) {
	$tpl -> setCurrentBlock('ERROR');
	$tpl_error['ERRORMESSAGE'] = $dedi_lang['err_'.$errno];
	$tpl->setVariable($tpl_error);
	$tpl->parseCurrentBlock();
}
// Tabellenformatierung
$tmp['CELLPADDING'] = $cellpadding;
$tmp['CELLSPACING'] = $cellspacing;
$tmp['BORDER'] = $border;

// Formulareinstellungen
$tmp['FORM_URL'] = $sess->url('main.php');
$tmp['ORDER'] = $order;
$tmp['ASCDESC'] = $ascdesc;

// Tabellenüberschrift
$tmp['LANG_LOGINNAME'] = '<a href ="'.sprintf($base_url, 'username', ($order == 'username' && ($ascdesc == 'ASC' || $ascdesc == '')) ? 'DESC' : 'ASC').'" onMouseOver="on(\''.$dedi_lang['gen_sort'].'\');return true;" onMouseOut="off()" ;return true;">'.$dedi_lang['user_loginname'].'</a>';
$tmp['LANG_NAME'] = '<a href ="'.sprintf($base_url, 'name', ($order == 'name' && ($ascdesc == 'ASC' || $ascdesc == '')) ? 'DESC' : 'ASC').'" onMouseOver="on(\''.$dedi_lang['gen_sort'].'\');return true;" onMouseOut="off()" ;return true;">'.$dedi_lang['user_name'].'</a>';
$tmp['LANG_SURNAME'] = '<a href ="'.sprintf($base_url, 'surname', ($order == 'surname' && ($ascdesc == 'ASC' || $ascdesc == '')) ? 'DESC' : 'ASC').'" onMouseOver="on(\''.$dedi_lang['gen_sort'].'\');return true;" onMouseOut="off()" ;return true;">'.$dedi_lang['user_surname'].'</a>';
$tmp['LANG_ACTIONS'] = $dedi_lang['user_action'];

$tpl->setVariable($tmp);
unset($tmp);

// Aktionen
$tpl->setCurrentBlock('SELECT_ACTIONLIST');
$tmp['ACTIONLIST_VALUE'] = 'user';
$tmp['ACTIONLIST_ENTRY'] = $dedi_lang['user_action'];
$tmp['ACTIONLIST_SELECTED'] = '';
$tpl->setVariable($tmp);
$tpl->parseCurrentBlock();
unset($tmp);

// User anlegen
$tmp['ACTIONLIST_VALUE'] = 'user_edit';
$tmp['ACTIONLIST_ENTRY'] = $dedi_lang['user_new'];
$tmp['ACTIONLIST_SELECTED'] = '';
$tpl->setVariable($tmp);
$tpl->parseCurrentBlock();
unset($tmp);

// Usergruppen auflisten
$tpl->setCurrentBlock('SELECT_SHOWLIST');
$sql = "SELECT idgroup, name, is_active, is_sys_admin FROM ".$dedi_db['groups']." ORDER BY name";
$db->query($sql);
while ($db->next_record()) {
	// Standardusergruppe ausblenden
	if ($db->f('idgroup') != '1' || ($db->f('idgroup') == '1' && $non_available)) {
		$tmp['SHOWLIST_VALUE'] = $db->f('idgroup');
		$tmp['SHOWLIST_ENTRY'] = $db->f('name');
		if ($idgroup == $db->f('idgroup')) $tmp['SHOWLIST_SELECTED'] = ' selected';
		$tmp['SHOWLIST_STYLE'] = ($db->f('is_sys_admin') == '1') ? ' style="color:darkgreen;"' : '';
		if (empty($tmp['SHOWLIST_STYLE']) && $db->f('is_active') == '0') $tmp['SHOWLIST_STYLE'] = ' style="color:red;"';
		$tpl->setVariable($tmp);
		$tpl->parseCurrentBlock();
		unset($tmp);
	}
}

// User auflisten
$tpl->setCurrentBlock('ENTRY');
$sql = "SELECT * FROM ".$dedi_db['users']." A LEFT JOIN ".$dedi_db['users_groups']." B USING(user_id) WHERE B.idgroup='$idgroup' AND A.username!= '' ORDER BY $order $ascdesc";
$db->query($sql);
while ($db->next_record()) {
	$tmp['LOGINNAME'] = htmlspecialchars($db->f('username'));
	$tmp['NAME'] = htmlspecialchars($db->f('name'));
	$tmp['SURNAME'] = htmlspecialchars($db->f('surname'));
	$tmp['BUTTON_SENDMAIL'] = ($db->f('email') != '') ? '<a href="mailto:'.htmlspecialchars($db->f('email')).'" onMouseOver="on(\''.$dedi_lang['user_sendmail'].'\');return true;" onMouseOut="off()" ;return true;"><img src="tpl/'.$cfg_dedi['skin'].'/img/but_email.gif" border="0" alt="'.$dedi_lang['user_sendmail'].'" title="'.$dedi_lang['user_sendmail'].'" width="24" height="11"></a>' : '<img src="tpl/'.$cfg_dedi['skin'].'/img/space.gif" width="24" height="11" border="0">';
	$tmp['BUTTON_EDIT'] = '<a href="'.$sess->url("main.php?area=user_edit&idgroup=$idgroup&order=$order&ascdesc=$ascdesc&iduser=".$db->f('user_id')).'" onMouseOver="on(\''.$dedi_lang['user_edit'].'\');return true;" onMouseOut="off()" ;return true;"><img src="tpl/'.$cfg_dedi['skin'].'/img/but_edit.gif" border="0" alt="'.$dedi_lang['user_edit'].'" title="'.$dedi_lang['user_edit'].'" width="18" height="14"></a>';
	if ($db->f('is_deletable') == '1') {
                	$tmp['BUTTON_AKTIVE'] = ($db->f('is_active') == '0') ? '<a href="'.sprintf($base_url, $order, $ascdesc).'&action=activate&iduser='.$db->f('user_id').'" onMouseOver="on(\''.$dedi_lang['user_on'].'\');return true;" onMouseOut="off()" ;return true;"><img src="tpl/'.$cfg_dedi['skin'].'/img/but_offline.gif" border="0" alt="'.$dedi_lang['user_on'].'" title="'.$dedi_lang['user_on'].'" width="13" height="13"></a>' : '<a href="'.sprintf($base_url, $order, $ascdesc).'&action=deactivate&iduser='.$db->f('user_id').'" onMouseOver="on(\''.$dedi_lang['user_off'].'\');return true;" onMouseOut="off()" ;return true;"><img src="tpl/'.$cfg_dedi['skin'].'/img/but_online.gif" border="0" alt="'.$dedi_lang['user_off'].'" title="'.$dedi_lang['user_off'].'" width="13" height="13"></a>';
		$tmp['BUTTON_DELETE'] = '<a href="'.sprintf($base_url, $order, $ascdesc).'&action=delete&iduser='.$db->f('user_id').'" onMouseOver="on(\''.$dedi_lang['user_delete'].'\');return true;" onMouseOut="off();return true;" onClick="return delete_confirm()"><img src="tpl/'.$cfg_dedi['skin'].'/img/but_delete.gif" alt="'.$dedi_lang['user_delete'].'" title="'.$dedi_lang['user_delete'].'" width="13" height="13" border="0"></a>';
	} else {
                	$tmp['BUTTON_AKTIVE'] = '<img src="tpl/'.$cfg_dedi['skin'].'/img/space.gif" width="13" height="13" border="0">';
		$tmp['BUTTON_DELETE'] = '<img src="tpl/'.$cfg_dedi['skin'].'/img/space.gif" width="13" height="13" border="0">';
	}
	$tpl->setVariable($tmp);
	$tpl->parseCurrentBlock();
	unset($tmp);
}
if (!$db->affected_rows()) {
	$tpl -> setCurrentBlock('EMPTY');
	$tmp['LANG_NOUSERS'] = $dedi_lang['user_nousers'];
	$tpl->setVariable($tmp);
	$tpl->parse('EMPTY');
	unset($tmp);
}
?>