<?php

// -----------------------------------------------------------------------
// This file is part of aroundme
// 
// Copyright (C) 2003, 2005 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., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
// -----------------------------------------------------------------------

$menu_section = "magazine";

include "inc/header.inc.php";
require_once($c_node['dir']['class_path'] . 'Cms.class.php');
require_once($c_node['dir']['class_path'] . 'Poll.class.php');
require_once($c_node['dir']['class_path'] . 'Forum.class.php');
require_once($c_node['dir']['class_path'] . 'Library.class.php');
require_once($c_node['dir']['class_path'] . 'Blog.class.php');
require_once($c_node['dir']['class_path'] . 'Wiki/Wiki.php');
require_once($c_node['dir']['class_path'] . 'Comment.class.php');


//append language setup
require_once($languageArrayPath . 'arr_genre.lang.php');


if (isset($_REQUEST['blog_id'])) {
    $blog_id = $_REQUEST['blog_id'];
}

if (isset($_REQUEST['subject_id'])) {
    $subject_id = $_REQUEST['subject_id'];
}

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

if (isset($_REQUEST['library_id'])) {
    $library_id = $_REQUEST['library_id'];
}

if (isset($_REQUEST['section_id'])) {
    $section_id = $_REQUEST['section_id'];
}

//setup file
$file->noimage = $c_node['file']['image']['thumb'][2]['noimage'];
$file->thumb = $c_node['file']['image']['thumb'][2]['name'];


//setup comment
$comment = new Comment($db, null, $date);
//$comment->user_id = $me['user_id'];

		
//setup Wiki
$wiki =& new Text_Wiki();
$wiki->disableRule('wikilink');



//process requests
if (isset($_POST['preview'])) {
	$output_comment = $data;
	$output_comment['display_comment_body'] = htmlspecialchars(nl2br($output_comment["comment_body"]));
	
	$display = "preview_comment";

}
elseif (isset($_POST['insert'])) {
	$comment->item_id = $_REQUEST['comment_item_id'];
	$comment->user_id = $me['user_id'];
	
	if (isset($blog_id)) {
		if (isset($section_id) && $section_id == $c_acs['arr_section']['user']) {
		    $comment->cms_item_type_id = $c_node['cms']['item_type']['user_blog'];
		}
		elseif (isset($section_id) && $section_id == $c_acs['arr_section']['group']) {
		    $comment->cms_item_type_id = $c_node['cms']['item_type']['group_blog'];
		}
		
		$link = "blog_id=". $blog_id;
	}
	elseif (isset($subject_id)) {
		$comment->cms_item_type_id = $c_node['cms']['item_type']['group_forum_subject'];
		$link = "subject_id=". $subject_id;
	}
	elseif (isset($poll_id)) {
		if (isset($section_id) && $section_id == $c_acs['arr_section']['user']) {
		    $comment->cms_item_type_id = $c_node['cms']['item_type']['user_poll'];
		}
		elseif (isset($section_id) && $section_id == $c_acs['arr_section']['group']) {
		    $comment->cms_item_type_id = $c_node['cms']['item_type']['group_poll'];
		}
		
		$link = "poll_id=". $poll_id;
	}
	elseif (isset($library_id)) {
		if (isset($section_id) && $section_id == $c_acs['arr_section']['user']) {
		    $comment->cms_item_type_id = $c_node['cms']['item_type']['user_library'];
		}
		elseif (isset($section_id) && $section_id == $c_acs['arr_section']['group']) {
		    $comment->cms_item_type_id = $c_node['cms']['item_type']['group_library'];
		}
		
		$link = "library_id=". $library_id;
	}
		
		
	$comment->insertComment($data);
		
	if (empty($GLOBALS['am_error_log'])) {
		//update user stats
		//setup statistics
		require_once($c_node['dir']['class_path'] . 'Statistic.class.php');
		$statistic = new Statistic($db, $c_acs['arr_section']['user'], $date);
		$statistic->section_item_id = $me['user_id'];
		$statistic->statistic_type_id = $c_node['statistic']['user']['total_comments'];
		$statistic->section_id = $c_acs['arr_section']['user'];
		$statistic->setStatistic($comment->getCommentCount());
		
		//goto the item
		return header("Location: mag_item.php?" . $link . "&" . SID);
		exit;
	}
}
elseif (isset($_POST['edit_again'])) {
	$output_comment = $data;
}

if (isset($blog_id)) {
	
	$blog = new Blog($db, null, $date, $file);
	$blog->blog_id = $blog_id;
	$output_blog = $blog->getBlog();
	
	
	if ($output_blog['section_id'] == $c_acs['arr_section']['user']) {
	    $comment->cms_item_type_id = $c_node['cms']['item_type']['user_blog'];
		$wiki->setRenderConf('xhtml', 'image', 'base', $c_node['dir']['asset_path'] . 'user/');
		$wiki->setRenderConf('xhtml', 'shockwaveflash', 'base', $c_node['dir']['asset_path'] . 'user/');
	}
	elseif ($output_blog['section_id'] == $c_acs['arr_section']['group']) {
	    $comment->cms_item_type_id = $c_node['cms']['item_type']['group_blog'];
		$wiki->setRenderConf('xhtml', 'image', 'base', $c_node['dir']['asset_path'] . 'group/');
		$wiki->setRenderConf('xhtml', 'shockwaveflash', 'base', $c_node['dir']['asset_path'] . 'group/');
	}
	
	$output_blog['blog_body_display'] = $wiki->transform($output_blog['blog_body'], 'Xhtml');
	
	$comment->item_id = $blog_id;
	$comment->section_item_id = $output_blog['user_id'];
	$output_comments = $comment->getComments($blog_id);
	
	if (isset($output_blog['user_id'])) {
		$user->user_id = $output_blog['user_id'];
    	$output_author = $user->getProfile();
	}
	
	if (!empty($output_author['profile_allow_comments']) && !empty($output_blog['blog_allow_comments'])) {
		$allow_add_comment = true;
	}
}


if (isset($subject_id)) {
	
	$forum = new Forum($db, null, $date, $file);
	$forum->subject_id = $subject_id;
	$output_subject = $forum->getSubject();
	
	$comment->cms_item_type_id = $c_node['cms']['item_type']['group_forum_subject'];
	
	$comment->item_id = $subject_id;
	$comment->section_item_id = $output_subject['user_id'];
	$output_comments = $comment->getComments($subject_id);
	
	if (isset($output_subject['user_id'])) {
		$user->user_id = $output_subject['user_id'];
    	$output_author = $user->getProfile();
	}
	
	if (!empty($output_author['profile_allow_comments'])) {
		$allow_add_comment = true;
	}
}


if (isset($poll_id)) {
	
	$poll = new Poll($db, null, $date);
	$poll->poll_id = $poll_id;
	$output_poll = $poll->getPoll();
	$output_poll['current_answer'] = $poll->pollUserCurrentVote($me['user_id']);
	
	if (isset($output_poll)) {
		$output_poll_history = $poll->pollUserVoteHistory($me['user_id']);
	}
	
	$comment->cms_item_type_id = $c_node['cms']['item_type']['group_poll'];
	$comment->item_id = $poll_id;
	$comment->section_item_id = $output_poll['section_item_id'];
	$output_comments = $comment->getComments($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;
	}
	
	if (isset($output_poll['user_id'])) {
		$user->user_id = $output_poll['user_id'];
    	$output_author = $user->getProfile();
	}
	
	if (!empty($output_author['profile_allow_comments'])) {
		$allow_add_comment = true;
	}
}


if (isset($library_id)) {
	
	$library = new Library($db, null, $date, $file);
	$library->library_id = $library_id;
	$output_library_item = $library->getPageID();
	
	if ($output_library_item['section_id'] == $c_acs['arr_section']['user']) {
	    $comment->cms_item_type_id = $c_node['cms']['item_type']['user_library'];
		$wiki->setRenderConf('xhtml', 'image', 'base', $c_node['dir']['asset_path'] . 'user/');
		$wiki->setRenderConf('xhtml', 'shockwaveflash', 'base', $c_node['dir']['asset_path'] . 'user/');
	}
	elseif ($output_library_item['section_id'] == $c_acs['arr_section']['group']) {
	    $comment->cms_item_type_id = $c_node['cms']['item_type']['group_library'];
		$wiki->setRenderConf('xhtml', 'image', 'base', $c_node['dir']['asset_path'] . 'group/');
		$wiki->setRenderConf('xhtml', 'shockwaveflash', 'base', $c_node['dir']['asset_path'] . 'group/');
	}
	
	$output_library_item['library_body'] = $wiki->transform($output_library_item['library_body'], 'Xhtml');
	
	
	$comment->item_id = $library_id;
	$comment->section_item_id = $output_library_item['user_id'];
	$output_comments = $comment->getComments($library_id);
	
	if (isset($output_library_item['user_id'])) {
		$user->user_id = $output_library_item['user_id'];
    	$output_author = $user->getProfile();
	}
	
	if (!empty($output_author['profile_allow_comments']) && !empty($output_library_item['library_allow_comments'])) {
		$allow_add_comment = true;
	}
}


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

if (!empty($output_subject)) {
	$body->set('subject', $output_subject);
}

if (!empty($output_poll)) {
	$body->set('poll', $output_poll);
}

if (!empty($output_library_item)) {
	$body->set('library', $output_library_item);
}

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

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

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

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

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

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

$template_pageLink = $lang['txt_magazine_name'];
include "inc/footer.inc.php";
?>
