<?php
class Poll
{
	//=================================================================
	// Uebersicht
	//=================================================================
	function Overview()
	{
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			foreach($_POST['poll'] as $pollid => $pid)
			{
				$time_s = explode('.', $_POST['Start'][$pollid]);
				$time_e = explode('.', $_POST['Ende'][$pollid]);
				$start  = mktime(0,0,1,$time_s[1],$time_s[0],$time_s[2]);
				$end    = mktime(23,59,59,$time_e[1],$time_e[0],$time_e[2]);
				if($start>$end) $start = $end-100;
				
				 $GLOBALS['db']->Query("UPDATE " . PREFIX . "_umfrage 
				 SET 
				 	Start = '$start',
					Ende = '$end' 
				 WHERE 
				 	Id = '{$pollid}'");
			}
		}
		
		$polls = array();
		$sql = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_umfrage WHERE Sektion='1' ORDER BY Id DESC");
		while($row = $sql->fetchrow())
		{
			$row->Users = count(explode(',', $row->IpLog))-1;
			$row->Comments = getCommentCount('poll', $row->Id);
			array_push($polls,$row);
		}
		
		$GLOBALS['tmpl']->assign('polls', $polls);
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Polls']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/poll/overview.tpl'));
	}
	
	//=================================================================
	// Neu
	//=================================================================
	function NewPoll()
	{
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			$Titel_1 = $_POST['Titel_1'];
			$Titel_2 = (empty($_POST['Titel_2'])) ? $Titel_1 : $_POST['Titel_2'];
			$Titel_3 = (empty($_POST['Titel_3'])) ? $Titel_1 : $_POST['Titel_3'];
			
			$time_s = explode('.', $_POST['Start']);
			$time_e = explode('.', $_POST['Ende']);
			$start  = mktime(0,0,1,$time_s[1],$time_s[0],$time_s[2]);
			$end    = mktime(23,59,59,$time_e[1],$time_e[0],$time_e[2]);
			if($start>$end) $start = $end-100;
			
			$query = "INSERT INTO " . PREFIX . "_umfrage
			(
				Id,
				Sektion,
				Titel_1,
				Titel_2,
				Titel_3,
				Gruppen,
				Start,
				Ende,
				Kommentare,
				Multi
			) VALUES (
				'',
				'" . AREA . "',
				'" . $Titel_1 . "',
				'" . $Titel_2 . "',
				'" . $Titel_3 . "',
				'" . implode(',',$_POST['Gruppen'])  . "',
				'" . $start  . "',
				'" . $end  . "',
				'" . $_POST['Kommentare'] . "',
				'" . $_POST['Multi'] . "' 
			)";
			$GLOBALS['db']->Query($query);
			$new_id = $GLOBALS['db']->InsertId();
			Logger('User ' . $_SESSION['user_name'] . ' create new Pollform (' . $new_id . ')', '3', $_SESSION['benutzer_id']);
			header('Location:index.php?do=poll&sub=edit&id=' . $new_id . '&noframes=1');
			exit;
		}
		
		$res->Start = stime();
		$res->Ende = mktime(0,0,1,date('m')+3,date('d'), date('Y'));
		$res->Multi = 0;
		$res->Kommentare = 1;
		
		
		$GLOBALS['tmpl']->assign('res', $res);
		$GLOBALS['tmpl']->assign('new', 1);
		$GLOBALS['tmpl']->assign('groups', UserGroups());
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Polls_new']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/poll/edit_new.tpl'));
	}
	
	//=================================================================
	// Bearbeiten
	//=================================================================
	function Edit($id)
	{	
		// Eigenschaften speichern
		if(isset($_POST['update_settings']) && $_POST['update_settings']==1)
		{
			$Titel_1 = $_POST['Titel_1'];
			$Titel_2 = (empty($_POST['Titel_2'])) ? $Titel_1 : $_POST['Titel_2'];
			$Titel_3 = (empty($_POST['Titel_3'])) ? $Titel_1 : $_POST['Titel_3'];
			
			$time_s = explode('.', $_POST['Start']);
			$time_e = explode('.', $_POST['Ende']);
			$start  = mktime(0,0,1,$time_s[1],$time_s[0],$time_s[2]);
			$end    = mktime(23,59,59,$time_e[1],$time_e[0],$time_e[2]);
			if($start>$end) $start = $end-100;
			
			$query = "UPDATE " . PREFIX . "_umfrage
					SET 
						Titel_1 = '" . $Titel_1 . "',
						Titel_2 = '" . $Titel_2 . "',
						Titel_3 = '" . $Titel_3 . "',
						Gruppen = '" . implode(',',$_POST['Gruppen'])  . "',
						Start = '" . $start  . "',
						Ende = '" . $end  . "',
						Kommentare = '" . $_POST['Kommentare'] . "',
						Multi = '" . $_POST['Multi'] . "' 
					WHERE 
						Id = '{$id}'";
			$GLOBALS['db']->Query($query);
			Logger('User ' . $_SESSION['user_name'] . ' update Pollform (' . $id . ')', '3', $_SESSION['benutzer_id']);
		}
		
		// Neue Auswahlmoeglichkeiten
		if(isset($_POST['new']) && $_POST['new']==1)
		{
			if(!empty($_POST['Frage_1']))
			{
				$Frage_1 = $_POST['Frage_1'];
				$Frage_2 = (empty($_POST['Frage_2'])) ? $Frage_1 : $_POST['Frage_2'];
				$Frage_3 = (empty($_POST['Frage_3'])) ? $Frage_1 : $_POST['Frage_3'];
				
				$query = "INSERT INTO " . PREFIX . "_umfrage_fragen 
				(
					Id,
					UmfrageId,
					Frage_1,
					Frage_2,
					Frage_3,
					Farbe,
					Position
				) VALUES (
					'',
					'$id',
					'$Frage_1',
					'$Frage_2',
					'$Frage_3',
					'$_POST[Farbe]',
					'$_POST[Position]'
				)";
				$GLOBALS['db']->Query($query);
			}
			
			Logger('User ' . $_SESSION['user_name'] . ' update Pollform (' . $id . ')', '3', $_SESSION['benutzer_id']);
			header('Location:index.php?do=poll&sub=edit&id=' . $id . 's&noframes=1');
			exit;
		}
		
		// Auswahl updaten
		if(isset($_POST['update']) && $_POST['update']==1)
		{
			foreach($_POST['poll'] as $cid => $pid)
			{
				if(!empty($_POST['Frage_1'][$cid]))
				{
					$Frage_1 = $_POST['Frage_1'][$cid];
					$Frage_2 = (empty($_POST['Frage_2'][$cid])) ? $Frage_1 : $_POST['Frage_2'][$cid];
					$Frage_3 = (empty($_POST['Frage_3'][$cid])) ? $Frage_1 : $_POST['Frage_3'][$cid];
					
					$query = "UPDATE " . PREFIX . "_umfrage_fragen 
					SET 
						Frage_1 = '" . $Frage_1 . "',
						Frage_2 = '" . $Frage_2 . "',
						Frage_3 = '" . $Frage_3 . "',
						Farbe = '" . $_POST['Farbe'][$cid]  . "',
						Position = '" . $_POST['Position'][$cid]  . "' 
					WHERE 
						Id = '{$cid}'";
					$GLOBALS['db']->Query($query);
				}
			}
			header('Location:index.php?do=poll&sub=edit&id=' . $id . 's&noframes=1');
			exit;
		}
		
		$res = $GLOBALS['db']->RowQuery("SELECT * FROM " . PREFIX . "_umfrage WHERE Sektion='1' AND Id='{$id}'");
		$res->Groups = explode(',', $res->Gruppen);
		
		$items = array();
		$sql = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_umfrage_fragen WHERE UmfrageId='{$res->Id}' ORDER BY Position ASC");
		while($row = $sql->fetchrow())
		{
			array_push($items,$row);
		}
		
		$GLOBALS['tmpl']->assign('items', $items);
		$GLOBALS['tmpl']->assign('groups', UserGroups());
		$GLOBALS['tmpl']->assign('res', $res);
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Polls_edit']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/poll/edit_new.tpl'));
	}
	
	//=================================================================
	// Loeschen
	//=================================================================
	function DeletePoll($id)
	{
		$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_umfrage WHERE Id='{$id}'");
		$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_umfrage_fragen WHERE UmfrageId='{$id}'");
		$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_kommentare WHERE Bereich='poll' AND Objekt_Id='{$id}'");
		Logger('User ' . $_SESSION['user_name'] . ' delete Pollform (' . $id . ')', '3', $_SESSION['benutzer_id']);
		header('Location:index.php?do=poll');
		exit;
	}
	
	//=================================================================
	// Oeffnen / Schliessen
	//=================================================================
	function OpenClose($id,$op='')
	{
		switch($op)
		{
			case '': default: case 'open': $dba = "SET Aktiv='1'"; break;
			case 'close': $dba = "SET Aktiv='0'"; break;
		}
		
		$GLOBALS['db']->Query("UPDATE " . PREFIX . "_umfrage SET Aktiv='0' WHERE Sektion='1'");
		$GLOBALS['db']->Query("UPDATE " . PREFIX . "_umfrage {$dba} WHERE Id='{$id}'");
		
		header('Location:index.php?do=poll');
		exit;
	}
	
	//=================================================================
	// Statistiken loeschen
	//=================================================================
	function DelStats($id)
	{
		$GLOBALS['db']->Query("UPDATE " . PREFIX . "_umfrage SET IpLog='',UserLog='' WHERE Id='{$id}'");
		$GLOBALS['db']->Query("UPDATE " . PREFIX . "_umfrage_fragen SET Hits='' WHERE UmfrageId='{$id}'");
		header('Location:index.php?do=poll');
		exit;
	}
}
?>