<?php
class Groups
{
	//=================================================================
	// Uebersicht
	//=================================================================
	function Overview()
	{
		if(isset($_POST['new']) && $_POST['new']==1 && isset($_POST['Name']) && !empty($_POST['Name']))
		{
			$n = preg_replace('/([^-A-Za-zÀ-ÖØ-öø-ÿ0-9 ]*)/', '', $_POST['Name']);
			$GLOBALS['db']->Query("INSERT INTO ".PREFIX."_benutzer_gruppen (Id, Name_Intern, Name) VALUES ('','$n','$n');");
			$NewId = $GLOBALS['db']->InsertId();
			
			// Neue Berechtigungs-Gruppe anlegen
			$row_nr = $GLOBALS['db']->RowQuery("SELECT Rechte_Admin,Rechte FROM ".PREFIX."_berechtigungen WHERE Gruppe = '2' AND Sektion = '1'");
			$sql = $GLOBALS['db']->Query("SELECT Id FROM " . PREFIX . "_sektionen");
			while($row = $sql->fetchrow())
			{
				$GLOBALS['db']->Query("INSERT INTO ".PREFIX."_berechtigungen 
				(
					Id,
					Gruppe,
					Sektion,
					Rechte,
					Rechte_Admin
				) VALUES (
					'',
					'$NewId',
					'$row->Id',
					'$row_nr->Rechte',
					'$row_nr->Rechte_Admin'
				)");
			}
			
		}
		
		$groups = array();
		$sql = $GLOBALS['db']->Query("SELECT * FROM ".PREFIX."_benutzer_gruppen ORDER BY Id ASC");
		while($row = $sql->fetchrow())
		{
			$uc = $GLOBALS['db']->RowQuery("SELECT COUNT(Id) AS UCount FROM ".PREFIX."_benutzer WHERE Gruppe='$row->Id'");
			$row->uc = $uc;
			array_push($groups,$row);
		}
		
		$GLOBALS['tmpl']->assign('groups', $groups);
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Groups_Name']);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/groups/overview.tpl'));
	}
	
	//=================================================================
	// Gruppenrechte
	//=================================================================
	function GroupPermissions()
	{
		$sections = array();
		$sql = $GLOBALS['db']->Query("SELECT Name,Id FROM ".PREFIX."_sektionen ORDER BY Id ASC");
		while($row = $sql->fetchrow())
		{
			$row->Groups = $this->UserGroups();
			array_push($sections,$row);
		}
		
		$GLOBALS['tmpl']->assign('sections', $sections);
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Groups_PermissionsEdit']);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/groups/permission_overview.tpl'));
	}
	
	//=================================================================
	// Gruppenrechte
	//=================================================================
	function GroupPermissionsEdit($id,$area)
	{
		if(isset($_POST['save']) &&  $_POST['save'] == 1 && $id != 1)
		{
			$res_page  = (isset($_POST['Rechte']) && $_POST['Rechte']>=1) ? implode(',', $_POST['Rechte']) : '';
			$res_admin = (isset($_POST['Rechte_Admin']) && $_POST['Rechte_Admin']>=1) ? implode(',', $_POST['Rechte_Admin']) : '';
			
			if($id==2) $res_admin = '';
			$ws = (isset($_POST['setall']) && $_POST['setall']==1) ? 'Sektion!=0' : "Sektion='$area'";
			
			$GLOBALS['db']->RowQuery("UPDATE ".PREFIX."_berechtigungen SET Rechte='$res_page', Rechte_Admin='$res_admin' WHERE Gruppe='$id' AND {$ws}");
			echo "<script>
			window.opener.document.getElementById('group_" . $area . "_" . $id . "').innerHTML='" . $GLOBALS['config_vars']['Groups_PermsSaved'] . "';
			window.close();</script>
			";
		}
		
		// Sprachwerte leeren
		$GLOBALS['tmpl']->clear_config();
		$GLOBALS['tmpl']->config_load(BASE_DIR . 'lang/' . $_SESSION['admin_lang'] . '/perms.txt','Page');
		$perms_vars = $GLOBALS['tmpl']->get_config_vars();
		
		// Sprachwerte leeren
		$GLOBALS['tmpl']->clear_config();
		$GLOBALS['tmpl']->config_load(BASE_DIR . 'lang/' . $_SESSION['admin_lang'] . '/perms.txt','Admin');
		$admin_vars = $GLOBALS['tmpl']->get_config_vars();
		
		$res = $GLOBALS['db']->RowQuery("SELECT * FROM ".PREFIX."_berechtigungen WHERE Gruppe='$id' AND Sektion='$area'");
		$res->Page  = explode(',', $res->Rechte);
		$res->Admin = explode(',', $res->Rechte_Admin);
		
		// Sprache wieder laden
		$GLOBALS['tmpl']->config_load(BASE_DIR . 'lang/' . $_SESSION['admin_lang'] . '/admin.txt');
		$config_vars = $GLOBALS['tmpl']->get_config_vars();
		
		$GLOBALS['config_vars'] = $config_vars;
		$GLOBALS['perms_vars'] = $perms_vars;
		$GLOBALS['tmpl']->assign('perms_page', $perms_vars);
		$GLOBALS['tmpl']->assign('perms_admin',$admin_vars);
		$GLOBALS['tmpl']->assign('res', $res);
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Groups_PermissionsEdit']);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/groups/permission_edit.tpl'));
	}
	
	//=================================================================
	// Benutzergruppen listen
	//=================================================================
	function UserGroups()
	{
		$groups = array();
		$sql = $GLOBALS['db']->Query("SELECT Id,Name_Intern,Name FROM ".PREFIX."_benutzer_gruppen ORDER BY Id ASC");
		while($row = $sql->fetchrow())
		{
			array_push($groups,$row);
		}
		return $groups;
	}
	
	//=================================================================
	// Gruppe loeschen
	//=================================================================
	function GroupDel($id)
	{
		$GLOBALS['db']->Query("UPDATE ".PREFIX."_benutzer SET Gruppe = '3' WHERE Gruppe = '$id'");
		$GLOBALS['db']->Query("DELETE FROM ".PREFIX."_berechtigungen WHERE Gruppe = '$id'");
		$GLOBALS['db']->Query("DELETE FROM ".PREFIX."_benutzer_gruppen WHERE Id = '$id'");
		$GLOBALS['db']->Query("ALTER TABLE ".PREFIX."_benutzer_gruppen AUTO_INCREMENT =1");
		 
		header('Location:index.php?do=groups&sub=useroverview');
		exit;
	}
	
	//=================================================================
	// Gruppe bearbeiten
	//=================================================================
	function GroupEdit($id)
	{
		// Speichern
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			$Avdel = (isset($_POST['Avdel']) && $_POST['Avdel']==1) ? "Avatar = ''," : "";
			$Avatar = (isset($_POST['newImg_2']) && !empty($_POST['newImg_2'])) ? "Avatar = '" .  $_POST['newImg_2'] . "'," : "";
			
			
			$q = "UPDATE ".PREFIX."_benutzer_gruppen 
			SET 
				Name = '" . $_POST['Name'] . "',
				Name_Intern = '" . $_POST['Name_Intern'] . "',
				VatByCountry = '" . $_POST['VatByCountry'] . "',
				Rabatt = '" . $_POST['Rabatt'] . "',
				ShopAnzeige = '" . $_POST['ShopAnzeige'] . "',
				Avatar_Default = '" . $_POST['Avatar_Default'] . "',
				$Avdel 
				$Avatar 
				Avatar_B = '" . $_POST['Avatar_B'] . "',
				Avatar_H = '" . $_POST['Avatar_H'] . "',
				MaxPn = '" . $_POST['MaxPn'] . "',
				MaxPn_Zeichen = '" . $_POST['MaxPn_Zeichen'] . "',
				MaxAnlagen = '" . $_POST['MaxAnlagen'] . "',
				MaxZeichenPost = '" . $_POST['MaxZeichenPost'] . "',
				KoobiCode_Signatur = '" . $_POST['KoobiCode_Signatur'] . "',
				Signatur_Laenge = '" . $_POST['Signatur_Laenge'] . "',
				Signatur_Erlaubt = '" . $_POST['Signatur_Erlaubt'] . "' 
			WHERE 
				Id = '$id'";
				
			$q_guest = "UPDATE ".PREFIX."_benutzer_gruppen 
			SET 
				Name = '" . $_POST['Name'] . "',
				Name_Intern = '" . $_POST['Name_Intern'] . "',
				VatByCountry = '" . $_POST['VatByCountry'] . "',
				Rabatt = '" . $_POST['Rabatt'] . "',
				ShopAnzeige = '" . $_POST['ShopAnzeige'] . "',
				Avatar = '',
				MaxAnlagen = '" . $_POST['MaxAnlagen'] . "',
				MaxZeichenPost = '" . $_POST['MaxZeichenPost'] . "'
			WHERE 
				Id = '$id'";
			$GLOBALS['db']->Query(($id==2) ? $q_guest : $q);
			echo '<script>window.opener.location.reload(); window.close();</script>';
		}
		
		
		
		
		$res = $GLOBALS['db']->RowQuery("SELECT * FROM ".PREFIX."_benutzer_gruppen WHERE Id = '$id'");
		
		// Avatar
		if($res->Avatar!='' && file_exists(BASE_DIR . 'uploads/avatars/default/' . $res->Avatar))
		{
			$GLOBALS['tmpl']->assign('avatar', '<img src="../uploads/avatars/default/' . $res->Avatar . '" alt="" border="0" />');
		}
		
		// Ist Verzeichnis fuer den Upload beschreibbar?
		if(!is_writable(BASE_DIR . 'uploads/avatars/default/'))
		{
			$GLOBALS['tmpl']->assign('not_writable', 1);
		}
		
		$GLOBALS['tmpl']->assign('ispro', (VERSION == 'PRO' ? 1 : 0));
		$GLOBALS['tmpl']->assign('res', $res);
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Groups_Edit']);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/groups/groupform.tpl'));
		
	}
}
?>