<?php

// -----------------------------------------------------------------------
// This file is part of Igloo
//
// Copyright (C) 2003, 2006 Barnraiser
// http://www.barnraiser.org/
// info@barnraiser.org
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with program; see the file COPYING. If not, write to the Free
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301, USA.
// -----------------------------------------------------------------------

include "inc/header.inc.php";


//ACS check
if (!isset($rss_access_granted) && (!$user_acs->checkPermission($c_acs['arr_acs']['resource']['poll'], $c_acs['arr_acs']['privilege']['allow_access']))) { //resourece_id, Priv_id
	header("Location: " . $_SESSION['user_homepage'] . "?" . strip_tags(SID));
	exit;
}


// SETUP LANGUAGE --------------------------------------------------------
require_once($languagePath . 'common.lang.php');
require_once($languageArrayPath . 'arr_user.lang.php');


// SETUP POLL ------------------------------------------------------------
require_once('class/Poll.class.php');
$poll = new Poll($db, null);
$poll->rsp_level_id = $rsp_level_id;

if (isset($_REQUEST['poll_id'])) {
	$poll->poll_id = $_REQUEST['poll_id'];
}


//SETUP WIKI -------------------------------------------------------------
require_once('class/Wiki/Wiki.php');
$wiki = new Text_Wiki();


// SETUP COMMENTS --------------------------------------------------------
require_once('class/Comment.class.php');
$comment = new Comment($db, $c_node['rsp']['item_type']['poll']);

if (isset($me['user_id'])) {
	$comment->user_id = $me['user_id'];
}

$comment->wiki = $wiki;


// SETUP TAGS ------------------------------------------------------------
require_once('class/Tag.class.php');
$tag = new Tag($db, $c_node['rsp']['item_type']['poll']);

if (isset($me['user_id'])) {
	$tag->user_id = $me['user_id'];
}


// SETUP BOOKMARKS -------------------------------------------------------
if (isset($me['user_id'])) {
	require_once('class/Bookmark.class.php');
	$bookmark = new Bookmark($db, $c_node['rsp']['item_type']['poll'], $me['user_id']);
}


// PROCESS FORM ----------------------------------------------------------
if (isset($_REQUEST['view']) && $_REQUEST['view'] == "node") {
	unset($_REQUEST['view']);
}

if (isset($_POST['update_tag'])) {
	$frmdata['item_id'] = $frmdata['poll_id'];
	$tag->deleteItemTags($frmdata);
	$tag->insertTags($frmdata);
	
	return header("Location: pol_view.php?poll_id=". $frmdata['poll_id'] . "&" . strip_tags(SID));
	exit;
}
elseif (isset($_POST['preview'])) {
	unset($poll->rsp_level_id);
	$output_comment = $frmdata;
	$output_comment['comment_body'] = stripslashes($output_comment['comment_body']);
	$output_comment['comment_body'] = str_replace("\"", "'", $output_comment['comment_body']);
	$output_comment['display_comment_body'] = $wiki->transform($output_comment['comment_body'], 'Xhtml');
	
	$poll->poll_id = $_REQUEST['poll_id'];
	
	$output_poll = $poll->selPoll();
	
	$display = "preview_comment";
}
elseif (isset($_POST['insert'])) {
	$comment->item_id = $frmdata['poll_id'];
	$comment->item_user_id = $frmdata['item_user_id'];
	$comment->insertComment($frmdata);
	
	if (empty($GLOBALS['igloo_error_log'])) {
		//setup statistics
		require_once('class/Statistic.class.php');
		$statistic = new Statistic($db);
		//update stats
		$statistic->statistic_type_id = $c_node['statistic']['user']['total_comments'];
		$comment->user_id = $me['user_id'];
		unset($comment->item_id, $comment->rsp_item_type_id, $comment->section_item_id);
		$statistic->user_id = $me['user_id'];
		$statistic->setStatistic($comment->getCommentCount());

		return header("Location: pol_view.php?poll_id=". $frmdata['poll_id'] . "&" . strip_tags(SID));
		exit;
	}
}
elseif (isset($_POST['edit_again'])) {
	unset($poll->rsp_level_id);
	$output_comment = $frmdata;
	$output_comment['comment_body'] = stripslashes($output_comment['comment_body']);
	
	$poll->poll_id = $_REQUEST['poll_id'];
	
	$output_poll = $poll->selPoll();
}
elseif (isset($_REQUEST['bookmark']) && isset($me['user_id'])) { // bookmark options for this item
	$bookmark->item_id = $_REQUEST['poll_id'];
	
	if ($_REQUEST['bookmark'] == "ins_mark") {
		$bookmark->bookmark_type = $c_node['bookmark']['type']['watch'];
		
		if (!empty($_REQUEST['notify'])) {
			$bookmark->insertBookmark(1);
		}
		else {
			$bookmark->insertBookmark();
		}
	}
	elseif ($_REQUEST['bookmark'] == "del_mark") {
		$bookmark->bookmark_type = $c_node['bookmark']['type']['watch'];
		$bookmark->deleteBookmark();
	}
	elseif ($_REQUEST['bookmark'] == "ins_tip") {
		$bookmark->bookmark_type = $c_node['bookmark']['type']['recommend'];
		$bookmark->insertBookmark();
	}
	elseif ($_REQUEST['bookmark'] == "del_tip") {
		$bookmark->bookmark_type = $c_node['bookmark']['type']['recommend'];
		$bookmark->deleteBookmark();
	}
	
	header("Location: pol_view.php?poll_id=". $_REQUEST['poll_id'] . "&" . strip_tags(SID));
	exit;
}
elseif (isset($_REQUEST['view'])) {
	if ($_REQUEST['view'] == "my_network" && isset($me['user_id'])) {
		//get the latest relations polls
		unset($poll->rsp_level_id);
		$file->noimage = $c_node['file']['image']['thumb'][3]['noimage'];
		$file->thumb = $c_node['file']['image']['thumb'][3]['name'];
		$poll->file = $file;
		$my_network = $_SESSION['relations'];
		
		if (!empty($my_network)) {
			foreach($my_network as $key => $i):
				$poll->user_ids[] = $i['user_id_relation'];
				$user->user_ids[] = $i['user_id_relation'];
			endforeach;
		}
		else {
			$poll->user_ids[] = 0;
			$user->user_ids[] = 0;
		}
		
		$user->file = $file;
		$output_users = $user->selUsers();
		
		// get the poll published to network
		$poll->rsp_item_type_id = $c_node['rsp']['item_type']['poll'];
		$poll->rsp_level_id = $c_node['rsp']['rsp_level']['node'];
		$poll->degree_id = 1;
		$poll->limit = $c_node['display']['max_list_rows'];
		$network_polls = $poll->selPolls();
		
		//sort polls under people
		if (!empty($output_users)) {
			foreach ($output_users as $item_key => $i):
				foreach($network_polls as $content_key => $c):
					if ($i['user_id'] == $c['user_id']) {
						$output_users[$item_key]['polls'][] = $c;
						unset ($network_polls[$content_key]);
					}
				endforeach;
			endforeach;
		}
		
		$output_my_network_polls = $output_users;
		
		$lang['txt_intro_network'] = str_replace('SYS_KEYWORD_NUMBER', $c_node['display']['max_list_rows'], $lang['txt_intro_network']);
		$lang['txt_outro_network'] = str_replace('SYS_KEYWORD_NUMBER', $c_node['display']['max_list_rows'], $lang['txt_outro_network']);
		
		$menu_section = "network";
	}
	elseif ($_REQUEST['view'] == "my_polls" && isset($me['user_id'])) {
		//get my polls
		unset($poll->rsp_level_id);
		$poll->user_id = $me['user_id'];
		
		$output_polls = $poll->selPolls();
		
		$menu_section = "my";
	}
	elseif ($_REQUEST['view'] == "marked_polls" && isset($me['user_id'])) {
		//get my marked polls
		unset($poll->rsp_level_id);
		$bookmark->bookmark_type = $c_node['bookmark']['type']['watch'];
		$bookmark->rsp_item_type_id = $c_node['rsp']['item_type']['poll'];
		$watched_poll_ids = $bookmark->getBookmarks();
		
		if (!empty($watched_poll_ids)) {
			$poll->poll_ids = $watched_poll_ids;
			$output_polls = $poll->selPolls();
		}
		
		// remove subjects written by me
		if (!empty($output_polls)) {
			foreach($output_polls as $key => $i):
			echo $i['user_id'] . " ";
				if ($me['user_id'] == $i['user_id']) {
					unset ($output_polls[$key]);
				}
			endforeach;
		}
		
		$lang['txt_intro'] = $lang['txt_intro_marked'];
	}
	elseif ($_REQUEST['view'] == "tipped_polls" && isset($me['user_id'])) {
		//get my relations tipped polls
		unset($poll->rsp_level_id);
		$file->noimage = $c_node['file']['image']['thumb'][3]['noimage'];
		$file->thumb = $c_node['file']['image']['thumb'][3]['name'];
		$poll->file = $file;
		
		unset($bookmark->user_id);
		
		$my_network = $_SESSION['relations'];
		
		if (!empty($my_network)) {
			foreach($my_network as $key => $i):
				$bookmark->user_ids[] = $i['user_id_relation'];
				$user->user_ids[] = $i['user_id_relation'];
			endforeach;
		}
		else {
			$bookmark->user_ids[] = 0;
			$user->user_ids[] = 0;
		}
		
		$user->file = $file;
		$output_users = $user->selUsers();
		
		
		$bookmark->bookmark_type = $c_node['bookmark']['type']['recommend'];
		$bookmark->rsp_item_type_id = $c_node['rsp']['item_type']['poll'];
		$recommended_poll_ids = $bookmark->getBookmarks(1);
		
		if (!empty($recommended_poll_ids)) {
			foreach($recommended_poll_ids as $key => $i):
				$poll->poll_ids[] = $i['item_id'];
			endforeach;
			
			$recommended_polls = $poll->selPolls();
			
			//sort polls under people
			if (!empty($output_users) && !empty($recommended_polls)) {
				foreach ($output_users as $item_key => $i): // for each user we get their tipped items
					foreach($recommended_poll_ids as $bookmark_key => $b):
						if ($i['user_id'] == $b['user_id']) {
							// we get the poll
							foreach($recommended_polls as $content_key => $c):
								if ($c['poll_id'] == $b['item_id']) {
									$output_users[$item_key]['polls'][] = $c;
									unset ($recommended_polls[$content_key]);
								}
							endforeach;
						}
					endforeach;
				endforeach;
			}
			
		}
		$output_recommended_polls = $output_users;
		$lang['txt_intro'] = $lang['txt_intro_tipped'];
	}
	elseif ($_REQUEST['view'] == "voted" && isset($me['user_id'])) {
		//get my polls that i've voted on
		$poll->user_id = $me['user_id'];
		$output_polls = $poll->getUserPollParticipation();
		
		$lang['txt_intro'] = $lang['txt_intro_voted'];
	}
}
elseif (isset($_REQUEST['user_id'])) {
	//get polls for this user (if relation)
	unset($poll->rsp_level_id);
	$poll->user_id = $_REQUEST['user_id'];
	
	$output_polls = $poll->selPolls();
	
	// get the user
	$output_profile = $user->selProfile(null, $_REQUEST['user_id']);
	
	
	$file->noimage = $c_node['file']['image']['thumb'][1]['noimage'];
	$file->thumb = $c_node['file']['image']['thumb'][1]['name'];
	//setup file
	if (isset($output_profile['file_id'])) {
		$file->file_id = $output_profile['file_id'];
		$output_profile['image'] = $file->getFileRecord();
	}
	if (isset($output_profile) && empty($output_profile['image'])) {
		$output_profile['image']['file_title'] = "";
		$output_profile['image']['file'] = $file->noimage;
	}
}
elseif (isset($_REQUEST['poll_id'])) {
	unset($poll->rsp_level_id);
	$poll->poll_id = $_REQUEST['poll_id'];
	
	$output_poll = $poll->selPoll();
}
else { // get the latest item published to node
	$file->noimage = $c_node['file']['image']['thumb'][3]['noimage'];
	$file->thumb = $c_node['file']['image']['thumb'][3]['name'];
	$poll->file = $file;
	
	$poll->rsp_item_type_id = $c_node['rsp']['item_type']['poll'];
	$poll->rsp_status_id = $c_node['rsp']['status']['live'];
	
	$output_polls = $poll->selPolls();
	
	$lang['txt_intro_node'] = str_replace('SYS_KEYWORD_NUMBER', $c_node['display']['max_list_rows'], $lang['txt_intro_node']);
	
	$menu_section = "magazine";
	
	$_REQUEST['view'] = "node";
}


// CHECK PERMISSION TO VIEW POLL ------------------------------------------
if (!empty($output_poll)) {
	// Are we allowed to see this poll?
	require_once($languageArrayPath . 'arr_rsp.lang.php');
	
	require_once('class/Rsp.class.php');
	$rsp = new Rsp($db, $c_node['rsp']['item_type']['poll']);
	$output_poll['rsp'] = $rsp->selRspItem($poll->poll_id);
	
	if (isset($me['user_id']) && $me['user_id'] == $output_poll['user_id']) {
		// is the author me?
		$allow_view = 1;
	}
	elseif (isset($rss_access_granted)) { // RSS Feed link
		$allow_view = 1;
	}
	elseif (!empty($output_poll['rsp'])) { // is the item set to public?
		foreach ($output_poll['rsp'] as $key => $i):
			if ($i['level_id'] == $c_node['rsp']['rsp_level']['public']) {
				$allow_view = 1;
				break;
			}
			elseif (!empty($_SESSION['s_md5']) && empty($i['degree_id']) && $i['level_id'] == $c_node['rsp']['rsp_level']['node']) { // is the item set to node and the user logged in?
				$allow_view = 1;
				break;
			}
			elseif (!empty($i['degree_id'])) { // is it set to network and are they in my network?
				foreach($_SESSION['relations'] as $key => $r):
					if ($output_blog['user_id'] == $r['user_id_relation']) {
						$allow_view = 1;
						break;
					}
					
					if (isset($allow_view)) {
						break;
					}
				endforeach;
			}
		endforeach;
	}
	
	if (!isset($allow_view)) {
		unset($output_poll);
	}
}


if (!empty($output_poll)) {
	if (isset($me['user_id'])) {
		$output_poll['current_answer']= $poll->pollUserCurrentVote($me['user_id']);
		$output_poll_history = $poll->pollUserVoteHistory($me['user_id']);
	}
	
	$wiki_path = "lib_view.php?page=";
	$wiki_new_path = "lib_edit.php?page=";
	$wiki_image_base = "get_file.php?file=";
	
	$wiki->setRenderConf('xhtml', 'wikilink', 'view_url', $wiki_path);
	$wiki->setRenderConf('xhtml', 'wikilink', 'new_url', $wiki_new_path);
	$wiki->setRenderConf('xhtml', 'image', 'base', $wiki_image_base);
	$output_poll['poll_body_display'] = $wiki->transform($output_poll['poll_body'], 'Xhtml');
	
	// GET AUTHOR
	$output_author = $user->selProfile(null, $output_poll['user_id']);
	
	// GET BOOKMARK --------------------------------------------------
	if (isset($bookmark)) {
		$bookmark->item_id = $poll->poll_id;
		$bookmark->bookmark_type = $c_node['bookmark']['type']['watch'];
		if ($bookmark->getBookmark()) {
			$bookmark->updateBookmark();
			$output_poll['bookmark']['watch'] = 1;
		}
		
		$bookmark->bookmark_type = $c_node['bookmark']['type']['recommend'];
		if ($bookmark->getBookmark()) {
			$bookmark->updateBookmark();
			$output_poll['bookmark']['recommend'] = 1;
		}
	}
	
	//get comments
	unset($comment->user_id);
	$output_comments = $comment->selComments($poll->poll_id);
	
	//get users vote for each comment
	if (isset($output_comments)) {
		foreach($output_comments as $key => $i):
			$output_comments[$key]['vote'] = $poll->pollUserVoteHistory($i['user_id']);
		endforeach;
	}
	
	//get poll image
	if (isset($output_poll['file_id'])) {
		$file->thumb = $c_node['file']['image']['thumb'][1]['name'];
		$file->file_id = $output_poll['file_id'];
		$output_poll['image'] = $file->getFileRecord();
	}
	
	
	//get tags
	$tag->item_id = $poll->poll_id;
	$output_my_tags = $tag->selTags();
	unset($tag->user_id);
	$output_item_tags = $tag->selTags(1);
	
	$datetime = date("Y-m-d H:i:s");
	
	// if the poll is live or ended we do not allow editing
	if ($output_poll['poll_start_datetime'] <= $datetime) {
	    $output_poll['poll_allow_edit'] = 1;
	}
	
	// if the poll is live allow comments to be added.
	if (($output_poll['poll_start_datetime'] <= $datetime) && ($datetime <= $output_poll['poll_end_datetime'])) {
	    $output_poll['poll_allow_comments'] = 1;
	}
	
	$menu_section = "magazine";
}



if (!empty($output_polls)) {
	unset($comment->user_id, $comment->section_item_id, $comment->rsp_item_type_id);
	$comment->rsp_item_type_id = $c_node['rsp']['item_type']['poll'];
	
	foreach($output_polls as $key => $i):
		if (!empty($i['poll_allow_comments'])) {
			$output_polls[$key]['comment_total'] = $comment->getCommentCount($i['poll_id']);
		}
	endforeach;
	
	$tag->rsp_item_type_id = $c_node['rsp']['item_type']['poll'];
	foreach ($output_polls as $key => $i):
		$tag->item_id = $i['poll_id'];
		$output_polls[$key]['tags'] = $tag->selTags();
	endforeach;
}


//move to template
if (!empty($output_comments)) {
	$body->set('comments', $output_comments);
}

if (!empty($output_comment)) {
	$body->set('comment', $output_comment);
}

if (isset($display)) {
	$body->set('display', $display);
}

if (!empty($_REQUEST['view'])) {
	$body->set('view', $_REQUEST['view']);
}

if (!empty($output_poll)) {
	$body->set('poll', $output_poll);
	$body->set('rsp_config', $c_node['rsp']); 
	$lang['nav_breadcrumb_note'] = $output_poll['poll_question'];
}

if (!empty($output_poll_history)) {
	$body->set('poll_history', $output_poll_history);
}

if (!empty($output_poll_usage)) {
   $body->set('poll_usage', $output_poll_usage);
}

if (!empty($output_author)) {
	$body->set('author', $output_author);
}

if (!empty($output_my_tags)) {
	$body->set('my_tags', $output_my_tags);
}

if (!empty($output_item_tags)) {
	$body->set('item_tags', $output_item_tags);
}

if (!empty($output_polls)) {
   $body->set('polls', $output_polls);
   unset($lang['nav_breadcrumb']);
}

if (!empty($output_recommended_polls)) {
	$body->set('recommended_polls', $output_recommended_polls);
}

$body->set('batch_enabled', $c_node['batch']['enabled']);
$body->set('rspconfig', $c_node['rsp']);

include "inc/footer.inc.php";

?>
