<?php
class Contactform
{
	//=================================================================
	// Uebersicht
	//=================================================================
	function Overview()
	{
		$cforms = array();
		$sql = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_kontakt_form ORDER BY Titel1 ASC");
		while($row = $sql->fetchrow())
		{
			array_push($cforms,$row);
		}
		
		$GLOBALS['tmpl']->assign('cforms', $cforms);
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['ContactForms']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/contactforms/overview.tpl'));
		
	}
	
	
	//=================================================================
	// Bearbeiten
	//=================================================================
	function  Edit($id)
	{
		// Neues Feld
		if(isset($_POST['new']) && $_POST['new']==1)
		{
			if(!empty($_POST['Name1']))
			{
				$Name1 = $_POST['Name1'];
				$Name2 = (empty($_POST['Name2'])) ? $Name1 : $_POST['Name2'];
				$Name3 = (empty($_POST['Name3'])) ? $Name1 : $_POST['Name3'];
				
				switch($_POST['Typ'])
				{
					case 'dropdown': case 'checkbox': case 'radio': 
						if(empty($_POST['Werte'])) $_POST['Werte'] = 'Option 1,Option 2,Option 3';
						$_POST['Pflicht'] = '0';
						$_POST['Email'] = '0';
						$_POST['Zahl'] = '0';	
					break;
					
					case 'textarea':
						$_POST['Zahl'] = '0';
						$_POST['Email'] = '0';
					break;
				}
				
				// DB-Eintrag
				$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_kontakt_form_felder 
				(
					Id,
					Form_Id,
					Typ,
					Pflicht,
					Posi,
					Zahl,
					Email,
					Werte,
					Name1,
					Name2,
					Name3
				) VALUES (
					'',
					'$id',
					'" . $_POST['Typ'] . "',
					'" . $_POST['Pflicht'] . "',
					'" . $_POST['Posi'] . "',
					'" . $_POST['Zahl'] . "',
					'" . $_POST['Email'] . "',
					'" . $_POST['Werte'] . "',
					'$Name1',
					'$Name2',
					'$Name3' 
				)");
			}
			
			Logger('User ' . $_SESSION['user_name'] . ' add new Contact-Form-Field (' . $id . ')', '3', $_SESSION['benutzer_id']);
			header('Location:index.php?do=contactforms&sub=edit&id=' . $id . '&noframes=1');
			exit;
		}
		
		// Speichern
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			// Eigenschaften
			@$E_Name1 = $_POST['E_Titel1'];
			@$E_Name2 = (empty($_POST['E_Titel2'])) ? $E_Name1 : $_POST['E_Titel2'];
			@$E_Name3 = (empty($_POST['E_Titel3'])) ? $E_Name1 : $_POST['E_Titel3'];
				
			$GLOBALS['db']->Query("UPDATE " . PREFIX . "_kontakt_form 
			SET
				Titel1 = '$E_Name1',
				Titel2 = '$E_Name2',
				Titel3 = '$E_Name3',
				Email = '" . trim($_POST['E_Email']) . "',
				Anlage = '" . $_POST['E_Anlage'] . "',
				Aktiv = '" . $_POST['E_Aktiv'] . "',
				Gruppen = '" . @implode(',', $_POST['E_Gruppen']) . "' 
			WHERE
				Id = '$id'");
			
			Logger('User ' . $_SESSION['user_name'] . ' update Contact-Form (' . $id . ')', '3', $_SESSION['benutzer_id']);
			
			// Felder
			foreach($_POST['Name1'] as $cid => $fieldid)
			{
				if(!empty($_POST['Name1'][$cid]))
				{
					$Name1 = $_POST['Name1'][$cid];
					$Name2 = (empty($_POST['Name2'][$cid])) ? $Name1 : $_POST['Name2'][$cid];
					$Name3 = (empty($_POST['Name3'][$cid])) ? $Name1 : $_POST['Name3'][$cid];
					
					switch($_POST['Typ'][$cid])
					{
						case 'dropdown': case 'checkbox': case 'radio': 
							if(empty($_POST['Werte'][$cid])) $_POST['Werte'][$cid] = 'Option 1,Option 2,Option 3';
							$_POST['Pflicht'][$cid] = '0';
							$_POST['Email'][$cid] = '0';
							$_POST['Zahl'][$cid] = '0';	
						break;
						
						case 'textarea':
							$_POST['Zahl'][$cid] = '0';
							$_POST['Email'][$cid] = '0';
						break;
					}
					
					
					$GLOBALS['db']->Query("UPDATE " . PREFIX . "_kontakt_form_felder 
					SET 
						Typ = '" . $_POST['Typ'][$cid] . "',
						Pflicht = '" . $_POST['Pflicht'][$cid] . "',
						Posi = '" . $_POST['Posi'][$cid] . "',
						Zahl = '" . $_POST['Zahl'][$cid] . "',
						Email = '" . $_POST['Email'][$cid] . "',
						Werte = '" . $_POST['Werte'][$cid] . "',
						Name1 = '$Name1',
						Name2 = '$Name2',
						Name3 = '$Name3' 
					WHERE
						Id = '$cid'
					");
					
					// Loeschen
					if(@$_POST['del'][$cid]==1)
					{
						$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_kontakt_form_felder WHERE Id = '$cid'");
					}
				}
			}
			header('Location:index.php?do=contactforms&sub=edit&id=' . $id . '&noframes=1');
			exit;
		}
		
		// Form
		$res = $GLOBALS['db']->RowQuery("SELECT * FROM " . PREFIX . "_kontakt_form WHERE Id = '$id'");
		$res->Groups = explode(',',$res->Gruppen);
		
		// Felder
		$fields = array();
		$sql = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_kontakt_form_felder WHERE Form_Id = '$id' ORDER BY Posi ASC");
		while($row = $sql->fetchrow())
		{
			array_push($fields,$row);
		}
		
		
		$GLOBALS['tmpl']->assign('groups', UserGroups());
		$GLOBALS['tmpl']->assign('res', $res);
		$GLOBALS['tmpl']->assign('fields', $fields);
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['ContactFormEdit']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/contactforms/edit_new.tpl'));
	}
	
	//=================================================================
	// Kopieren
	//=================================================================
	function CopyForm($id)
	{
		$res = $GLOBALS['db']->RowQuery("SELECT * FROM " . PREFIX . "_kontakt_form WHERE Id = '$id'");
		$sql = $GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_kontakt_form
		(
			Id,
			Datum,
			Autor,
			Titel1,
			Titel2,
			Titel3,
			Email,
			Anlage,
			Aktiv,
			Gruppen
		) VALUES (
			'',
			'" . stime() . "',
			'$res->Autor',
			'" . $res->Titel1 . " - " . $GLOBALS['config_vars']['ContactForms_copy_C'] . "',
			'$res->Titel2',
			'$res->Titel3',
			'$res->Email',
			'$res->Anlage',
			'$res->Aktiv',
			'$res->Gruppen'
		)");
		
		$newid = $GLOBALS['db']->InsertId();
		
		Logger('User ' . $_SESSION['user_name'] . ' copy Contact-Form ('.$id.')', '3', $_SESSION['benutzer_id']);
		
		$query = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_kontakt_form_felder WHERE Form_Id = '$id'");
		while($row = $query->fetchrow())
		{
			$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_kontakt_form_felder 
			(
				Id,
				Form_Id,
				Typ,
				Pflicht,
				Posi,
				Zahl,
				Email,
				Werte,
				Name1,
				Name2,
				Name3
			) VALUES (
				'',
				'$newid',
				'$row->Typ',
				'$row->Pflicht',
				'$row->Posi',
				'$row->Zahl',
				'$row->Email',
				'$row->Werte',
				'$row->Name1',
				'$row->Name2',
				'$row->Name3' 
			)");
		}
		header('Location:index.php?do=contactforms');
		exit;
	}
	
	//=================================================================
	// Loeschen
	//=================================================================
	function DeleteForm($id)
	{
		$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_kontakt_form WHERE Id = '$id'");
		$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_kontakt_form_felder WHERE Form_Id = '$id'");
		Logger('User ' . $_SESSION['user_name'] . ' delete Contact-Form (' . $id . ')', '3', $_SESSION['benutzer_id']);
		header('Location:index.php?do=contactforms');
		exit;
	}
	
	//=================================================================
	// Neu anlegen
	//=================================================================
	function NewForm()
	{
		if(isset($_POST['new']) && $_POST['new']==1)
		{
			// Form neu
			// Eigenschaften
			@$E_Name1 = $_POST['E_Titel1'];
			@$E_Name2 = (empty($_POST['E_Titel2'])) ? $E_Name1 : $_POST['E_Titel2'];
			@$E_Name3 = (empty($_POST['E_Titel3'])) ? $E_Name1 : $_POST['E_Titel3'];
			
			$sql = $GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_kontakt_form
			(
				Id,
				Datum,
				Autor,
				Titel1,
				Titel2,
				Titel3,
				Email,
				Anlage,
				Aktiv,
				Gruppen
			) VALUES (
				'',
				'" . stime() . "',
				'" . $_SESSION['benutzer_id'] . "',
				'" . $E_Name1 . "',
				'" . $E_Name2 . "',
				'" . $E_Name3 . "',
				'" . $_POST['E_Email'] . "',
				'" . $_POST['E_Anlage'] . "',
				'" . $_POST['E_Aktiv'] . "',
				'" . implode(',', $_POST['E_Gruppen']) . "'
			)");
			$newid = $GLOBALS['db']->InsertId();
			
			Logger('User ' . $_SESSION['user_name'] . ' create new Contact-Form (' . $newid . ')', '3', $_SESSION['benutzer_id']);
			
			// Felder neu	
			$Name1 = $_POST['Name1'];
			$Name2 = (empty($_POST['Name2'])) ? $Name1 : $_POST['Name2'];
			$Name3 = (empty($_POST['Name3'])) ? $Name1 : $_POST['Name3'];
			
			switch($_POST['Typ'])
			{
				case 'dropdown': case 'checkbox': case 'radio': 
					if(empty($_POST['Werte'])) $_POST['Werte'][$cid] = 'Option 1,Option 2,Option 3';
					$_POST['Pflicht'] = '0';
					$_POST['Email'] = '0';
					$_POST['Zahl'] = '0';	
				break;
				
				case 'textarea':
					$_POST['Zahl'] = '0';
					$_POST['Email'] = '0';
				break;
			}
			$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_kontakt_form_felder 
			(
				Id,
				Form_Id,
				Typ,
				Pflicht,
				Posi,
				Zahl,
				Email,
				Werte,
				Name1,
				Name2,
				Name3
			) VALUES (
				'',
				'$newid',
				'" . $_POST['Typ'] . "',
				'" . $_POST['Pflicht'] . "',
				'" . $_POST['Posi'] . "',
				'" . $_POST['Zahl'] . "',
				'" . $_POST['Email'] . "',
				'" . $_POST['Werte'] . "',
				'" . $Name1 . "',
				'" . $Name2 . "',
				'" . $Name3 . "'
			)");
			
			header('Location:index.php?do=contactforms&sub=edit&id=' . $newid . '&noframes=1');
			exit;
		}
		
		$GLOBALS['tmpl']->assign('new', 1);
		$GLOBALS['tmpl']->assign('groups', UserGroups());
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['ContactForms_new']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/contactforms/edit_new.tpl'));
	}
	
}
?>
