<?php
ob_start();
require_once("init.php");

if(!permission("adminpanel")){ exit(); }
$tmpl = new Koobi("templates/".ADMINTHEME."/poll/");
$tmpl->assign("theme", ADMINTHEME);
$tmpl->assign("langadmin", $langadmin);
$tmpl->assign("area", $area);
$tmpl->register_function("permission", "permission");

switch ($_REQUEST["action"]) {
	
	case "comment":
	// ===============================================
	// <<-- Newskommentare -->>
	// ===============================================
	if(!permission('pollcomments')) { echo error($langadmin['noperms']); exit(); }
	
	if($_REQUEST['subaction']=="save") {
		foreach ($_POST["title"] as $id => $title) {
			$sql = new dbc("UPDATE " . PREFIX . "_pollcomments SET title='".$_POST['title'][$id]."', comment='".$_POST['comment'][$id]."' WHERE id = $id");
		}
		
		foreach ($_POST["del"] as $id => $del) {
			$sql = new dbc("DELETE FROM " . PREFIX . "_pollcomments WHERE id = $id");
		}
		
		header("Location:poll.php?action=view&area=".$_REQUEST['area']."");
	}
	
	if($_REQUEST['subaction']=="del") {
		$sql =& new dbc("DELETE FROM ".PREFIX."_pollcomments WHERE id='".$_REQUEST['nid']."'");
	}
	
	$sql =& new dbc("SELECT * FROM ".PREFIX."_pollcomments WHERE pollid='".$_REQUEST['id']."' ORDER by id");
	
	
	$i = 0;
	while (	$row = $sql->fetchrow()) {
		$comments[$i]["comment"] = htmlspecialchars($row->comment);
		$comments[$i]["title"] = htmlspecialchars($row->title);
		$comments[$i]["ctime"] = mygmt($row->ctime);
		$comments[$i]["id"] = $row->id;
		$comments[$i]["author"] = fetchusername($row->autor);
		$i++;
	}
	$tmpl->assign("comments", $comments);
	$tmpl->display("pollcomments.tpl");
	
	break;
	
	// =======================================
	//  <<-- UEBERSICHT -->>
	// =======================================
	case "view":
	if( (!permission('addpoll')) && (!permission('editpoll')) && (!permission('pollcomments')) )  { echo error($langadmin['noperms']); exit(); }
	$polls = array();
	$r_poll = new dbc("SELECT id, title, start, end, active, place_id FROM " . PREFIX . "_poll WHERE area_id = " . $area);
	
	while ($poll = $r_poll->fetchrow()) {
		$poll->place_id = htmlspecialchars("{printpoll id='$poll->place_id'}");
		
		$sql_com = new dbc("SELECT pollid FROM ".PREFIX."_pollcomments WHERE pollid='".$poll->id."'");
		$poll->comms = $sql_com->numrows();
		
		array_push($polls, $poll);
	}
	
	
	
	$r_area = new dbc("SELECT area_id, area_name FROM " . PREFIX . "_areas WHERE area_id = " . $area);
	$row = $r_area->fetchrow();
	$tmpl->assign("area", $area);	
	$tmpl->assign("polls", $polls);
	$tmpl->display("poll_view.tpl");
	break;
	
	
	
	// =======================================
	//  <<-- NEUE/BEARBEITETE UMFRAGE -->>
	// =======================================
	case "new":
	if(!permission('addpoll')) { echo error($langadmin['noperms']); exit(); }
	$r_groups = new dbc("
	SELECT
	ugroup,
	groupname
	FROM
	" . PREFIX . "_usergroup
	");
	
	$groups = array();
	
	while ($group = $r_groups->fetchrow()) {
		array_push($groups, $group);
	}
	
	$tmpl->assign("groups", $groups);
	
	//if ($_REQUEST["submit"] == $langadmin['save']) {
		if ($_REQUEST['send'] == 1) {
			/*
			$errors = array();
			if ($_REQUEST["title"] == "") {
				array_push($errors, str_replace("{0}", $langadmin["title"], $langadmin["poll_error_field_required"]));
			}
			*/
			
			// ================================================
			//  start-/end-datum validierung
			// ================================================
			
			$start_date = 0;
			$end_date = 0;
			$is_error = 0;
			
			if(	
			($_REQUEST["s_year"] == $_REQUEST["e_year"]) && 
			($_REQUEST["s_mon"] == $_REQUEST["e_mon"]) && 
			($_REQUEST["s_day"] == $_REQUEST["e_day"]) 
			){
				$errors[] = $langadmin['startendsameerror'];
			}
			if ($_REQUEST["s_year"] > $_REQUEST["e_year"]) {
				$errors[] = $langadmin["poll_error_date"];
			} elseif ($_REQUEST["s_year"] == $_REQUEST["e_year"]) {
				// gleiches jahr
				if ($_REQUEST["s_mon"] > $_REQUEST["e_mon"]) {
					$errors[] = $langadmin["poll_error_date"];
				} elseif ($_REQUEST["s_mon"] == $_REQUEST["e_mon"]) {
					// gleicher monat
					if ($_REQUEST["s_day"] > $_REQUEST["e_day"]) {
						$errors[] = $langadmin['startisbiggererror'];
					} else {
						$start_date = mktime($_REQUEST["s_hour"], $_REQUEST["s_min"], 0, $_REQUEST["s_mon"], $_REQUEST["s_day"], $_REQUEST["s_year"]);
						$end_date = mktime($_REQUEST["e_hour"], $_REQUEST["e_min"], 0, $_REQUEST["e_mon"], $_REQUEST["e_day"], $_REQUEST["e_year"]);
					}
				} else {
					$start_date = mktime($_REQUEST["s_hour"], $_REQUEST["s_min"], 0, $_REQUEST["s_mon"], $_REQUEST["s_day"], $_REQUEST["s_year"]);
					$end_date = mktime($_REQUEST["e_hour"], $_REQUEST["e_min"], 0, $_REQUEST["e_mon"], $_REQUEST["e_day"], $_REQUEST["e_year"]);
				}
			} else {
				$start_date = mktime($_REQUEST["s_hour"], $_REQUEST["s_min"], 0, $_REQUEST["s_mon"], $_REQUEST["s_day"], $_REQUEST["s_year"]);
				$end_date = mktime($_REQUEST["e_hour"], $_REQUEST["e_min"], 0, $_REQUEST["e_mon"], $_REQUEST["e_day"], $_REQUEST["e_year"]);
			}
			
			
			// place_id kann nur dann gesetzt werden,
			// wenn die ausgewaehlte id nicht bereits an eine
			// aktive umfrage vergeben wurde
			$area = $_REQUEST['area_id']; 
			$r_place = new dbc("
			SELECT 
			place_id, 
			active 
			FROM 
			" . PREFIX . "_poll 
			WHERE 
			place_id = " . $_REQUEST['place'] . " AND
			area_id = $area AND id != '".$_REQUEST['poll_id']."'
			");
			
			$place = $r_place->fetchrow();
			
			
			if (($r_place->numrows()) && ($place->active==1) && ($_REQUEST["active"] == 1)) {
				$_REQUEST["active"] = 0;
				$errors[] = $langadmin['poll_error_place'];
			}
			
			
			if (count($errors)) {
				if($_REQUEST['edit'] == 1){
					$_REQUEST['area'] = $_REQUEST['area_id'];
					$tmpl->assign('area', $_REQUEST['area_id']);
					$tmpl->assign('editmode', 1);
					$tmpl->assign('editerror', 1);
					$tmpl->assign('poll_id', $_REQUEST['poll_id']);
				}
				if($_REQUEST['donew'] == 1){
					$_REQUEST['area'] = $_REQUEST['area_id'];
				}
				
				$tmpl->assign('title', $_REQUEST["title"]);
				$tmpl->assign('errors', $errors);
				$tmpl->assign('year', date("Y"));
				$tmpl->display("poll_new.tpl");
			} else {
				$group_id = (is_array($_REQUEST["group"])) ? implode(",", $_REQUEST["group"]) : $_REQUEST["group"];
				
				//if ($_REQUEST["poll_id"] == -1) {
					if ($_REQUEST['donew'] == 1) {
						$query = "
						INSERT INTO 
						" . PREFIX . "_poll (
						id, 
						area_id, 
						title,
						start,
						end,
						active,
						group_id,
						can_comment,
						place_id
						) 
						VALUES (
						'', 
						" . $_REQUEST["area_id"] . ", 
						'" . $_REQUEST["title"] . "',
						'$start_date',
						'$end_date',
						" . $_REQUEST["active"] . ",
						'$group_id',
						" . $_REQUEST["can_comment"] . ",
						" . $_REQUEST["place"] . "
						)";
						
						$new = new dbc($query);
						$id = $new->getid();
						header("Location:poll.php?action=show_items&id=".$id."&area=".$_REQUEST["area_id"]."");
					} 
					if ($_REQUEST['edit'] == 1) {
						
						
						$query = "
						UPDATE
						" . PREFIX . "_poll
						SET
						title = '" . $_REQUEST["title"] . "',
						start = '$start_date',
						end = '$end_date',
						active = " . $_REQUEST["active"] . ",
						group_id = '$group_id',
						can_comment = " . $_REQUEST["can_comment"] . ",
						place_id = " . $_REQUEST["place"] . "
						WHERE
						id = " . $_REQUEST["poll_id"];
						$update = new dbc($query);
						header("Location: poll.php?action=view&area=".$_REQUEST['area_id']."");
					}
					
					
				}
				
			} else {
				$tmpl->assign("year", date("Y"));
				$tmpl->assign('endyear', date("Y")+6);
				$tmpl->assign('startyear', date("Y")-1);
				$tmpl->display("poll_new.tpl");
			}
			
			break;
			
			
			
			// =======================================
			//  <<-- BEARBEITEN -->>
			// =======================================
			case "edit":
			if(!permission('editpoll')) { echo error($langadmin['noperms']); exit(); }
			// =======================================
			//  <<-- UMFRAGEPUNKT EINFUEGEN -->>
			// =======================================
			if ($_REQUEST['dowhat'] == "add") {
				
				$tmpl->assign("color_keys", array_keys($langadmin["colors"]));
				$tmpl->assign("colors", $langadmin["colors"]);
				$tmpl->assign("id", $_REQUEST["id"]);
				$tmpl->display("poll_new_item.tpl");
			}
			
			
			// =======================================
			//  <<-- UMFRAGE SAMT ITEMS LOESCHEN -->>
			// =======================================
			if ($_REQUEST['dowhat'] == "del") {
				$sql = new dbc("DELETE FROM " . PREFIX . "_poll_item WHERE poll_id = " . $_REQUEST["id"]);
				$sql = new dbc("DELETE FROM " . PREFIX . "_poll WHERE id = " . $_REQUEST["id"]);
				header("Location: poll.php?action=view&area=".$_REQUEST['area']."");
			}
			
			
			
			// =======================================
			//  <<-- UMFRAGEPUNKT BEARBEITEN -->>
			// =======================================
			if ($_REQUEST['dowhat'] == "edit") {
				$query = "
				SELECT 
				id, 
				title, 
				start, 
				end, 
				active, 
				group_id, 
				can_comment,
				place_id
				FROM 
				" . PREFIX . "_poll 
				WHERE 
				id = " . $_REQUEST["id"];
				
				$r_poll = new dbc($query);
				$poll = $r_poll->fetchrow();
				
				$poll->group_id = ($poll->group_id == "") ? array() : explode(",", $poll->group_id);
				
				$start_date = array(
				"year" => date("Y",$poll->start),
				"mon" => date("m",$poll->start),
				"day" => date("d",$poll->start),
				"hour" => date("H",$poll->start),
				"min" => date("i",$poll->start)
				);
				
				$end_date = array(
				"year" => date("Y", $poll->end),
				"mon" => date("m", $poll->end),
				"day" => date("d", $poll->end),
				"hour" => date("H",$poll->end),
				"min" => date("i",$poll->end)
				);
				
				$poll->start_date = $start_date;
				$poll->end_date = $end_date;
				
				$r_groups = new dbc("
				SELECT
				ugroup,
				groupname
				FROM
				" . PREFIX . "_usergroup
				");
				
				$groups = array();
				
				while ($group = $r_groups->fetchrow()) {
					array_push($groups, $group);
				}
				
				$tmpl->assign("groups", $groups);
				$tmpl->assign("poll", $poll);
				$tmpl->assign("year", date("Y")-2);
				$tmpl->assign('editmode', 1);
				$tmpl->display("poll_new.tpl");
			}
			
			
			
			
			case "edit_item":
			if(!permission('editpoll')) { echo error($langadmin['noperms']); exit(); }
			
			if($_REQUEST['subaction'] == "edit"){
				$r_item = new dbc("SELECT id, title, color FROM " . PREFIX . "_poll_item WHERE id = " . $_REQUEST["id"]);
				
				$tmpl->assign("color_keys", array_keys($langadmin["colors"]));
				$tmpl->assign("colors", $langadmin["colors"]);
				$tmpl->assign("item", $r_item->fetchrow());
				$tmpl->assign("area_id", $r_item->fetchrow());
				$tmpl->display("poll_new_item.tpl");
			}
			
			if($_REQUEST['subaction'] == "delete"){
				$sql = new dbc("DELETE FROM " . PREFIX . "_poll_item WHERE id = " . $_REQUEST["id"]);
				header("Location:poll.php?action=show_items&id=".$_REQUEST['redirid']."&area=".$_REQUEST['area']."");
			}
			
			break;
			
			// =======================================
			//  <<-- HIER WIRD SORTIERT -->>
			// =======================================
			case "sort":
			if(!permission('editpoll')) { echo error($langadmin['noperms']); exit(); }
			foreach ($_POST["posi"] as $id => $posi) {
				if( ($posi != "") && (is_numeric($posi)) ) {
					$query = "UPDATE " . PREFIX . "_poll_item SET posi=$posi WHERE id = '$id'";
					$result = new dbc($query);
				}
			}
			
			foreach ($_POST["color"] as $id => $color) {
				if( ($color != "") ) {
					$query = "UPDATE " . PREFIX . "_poll_item SET color='".$color."' WHERE id = '$id'";
					$result = new dbc($query);
				}
			}
			
			foreach ($_POST["title"] as $id => $title) {
				if( ($title != "") ) {
					$query = "UPDATE " . PREFIX . "_poll_item SET title='".$title."' WHERE id = '$id'";
					$result = new dbc($query);
				}
			}
			
			foreach ($_POST["hits"] as $id => $hits) {
				if( ($hits != "") ) {
					$query = "UPDATE " . PREFIX . "_poll_item SET hits='".$hits."' WHERE id = '$id'";
					$result = new dbc($query);
				}
			}
			header("Location:poll.php?action=show_items&id=".$_REQUEST['redirid']."&area=".$_REQUEST['area']."");
			break;
			
			// =======================================
			//  <<-- UMFRAGEPUNKTE ANZEIGEN -->>
			// =======================================
			case "show_items":
			if(!permission('viewpoll')) { echo error($langadmin['noperms']); exit(); }
			$r_poll = new dbc("SELECT id, title FROM " . PREFIX . "_poll WHERE id = " . $_REQUEST["id"]);
			
			$r_poll_items = new dbc("SELECT id, title, color, posi,hits FROM " . PREFIX . "_poll_item WHERE poll_id = " . $_REQUEST["id"]. " ORDER BY posi ASC");
			$poll_items = array();
			
			while ($poll_item = $r_poll_items->fetchrow()) {
				$poll_item->title = htmlspecialchars($poll_item->title);
				array_push($poll_items, $poll_item);
			}
			
			$langadmin["colors"] = ",".$langadmin["colors"];
			$colors = explode(",",$langadmin["colors"]);
			while (list($key, $value) = each ($colors)) {
				if($value){
					$cols[$value]['val'] = $value;
					//$colors[$value]['key'] = $key;
				}
			}
			
			//$tmpl->assign("color_keys", array_keys($langadmin["colors"]));
			$tmpl->assign("colors", $langadmin["colors"]);
			$tmpl->assign("cols", $cols);
			$tmpl->assign("poll", $r_poll->fetchrow());
			$tmpl->assign("area", $area);
			$tmpl->assign("items", $poll_items);
			$tmpl->display("poll_items.tpl");
			break;
			
			
			
			// =======================================
			//  <<-- NEUEN UMFRAGEPUNKT UEBERNEHMEN -->>
			// =======================================
			case "save_item":
			if(!permission('editpoll')) { echo error($langadmin['noperms']); exit(); }
			if ($_REQUEST["title"] == "") {
				
				$tmpl->assign("error", str_replace("{0}", $langadmin["title"], $langadmin["poll_error_field_required"]));
				$tmpl->assign("color_keys", array_keys($langadmin["colors"]));
				$tmpl->assign("colors", $langadmin["colors"]);
				$tmpl->assign("id", $_REQUEST["id"]);
				$tmpl->assign("iserror", 1);
				$tmpl->assign("area", $_REQUEST["area_id"]);
				$tmpl->display("poll_new_item.tpl");
			} else {
				if ($_REQUEST["item_id"] == -1) {
					
					$sql = new dbc("
					INSERT INTO
					" . PREFIX . "_poll_item (id, poll_id, title, color, posi)
					VALUES (
					'',
					" . $_REQUEST["id"] . ",
					'" . $_REQUEST["title"] . "',
					'" . $_REQUEST["color"] . "',
					'" . $_REQUEST["posi"] . "'
					)
					");
					header("Location:poll.php?action=view&area=".$_REQUEST['area_id']."");
				} else {
					$sql = new dbc("
					UPDATE
					" . PREFIX . "_poll_item
					SET
					title = '" . $_REQUEST["title"] . "',
					color = '" . $_REQUEST["color"] . "'
					WHERE
					id = " . $_REQUEST["item_id"]
					);
				}
				
				header("Location:poll.php?action=show_items&id=".$_REQUEST['redirid']."&area=".$_REQUEST['area_id']."");
			}
			
			break;
		}
		?>
		