<?php

/************************************************************************/
/* DOCEBO CMS - Learning Managment System                               */
/* ============================================                         */
/*                                                                      */
/* Copyright (c) 2004                                                   */
/* http://www.docebo.com                                                */
/*                                                                      */
/* 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 of the License.       */
/************************************************************************/

if (!defined("IN_DOCEBO")) die ("You can't access this file directly...");

if($GLOBALS['current_user']->isAnonymous()) die("You can't access");

$GLOBALS["page"]->addEnd('<link href="'.getPathTemplate("cms").'style/style-admin.css" rel="stylesheet" type="text/css" />', "page_head");

function loadUnreaded() {

	if(!isset($_SESSION['unreaded_forum'])) {

		//-find last access---------------------------------------------------------------
		$no_entry = false;
		$reLast = mysql_query("
		SELECT UNIX_TIMESTAMP(last_access)
		FROM ".$GLOBALS['prefix_cms']."_forum_timing
		WHERE idUser = '".getLogUserId()."'");
		if(mysql_num_rows($reLast)) {
			list($last_forum_access_time) = mysql_fetch_row($reLast);
		} else {
			$last_forum_access_time = 0;
			$no_entry = true;
		}
		$unreaded = array();
		$reUnreaded = mysql_query("
		SELECT t.idThread, t.idForum, m.generator, COUNT(m.idMessage)
		FROM ".$GLOBALS['prefix_cms']."_forumthread AS t JOIN ".$GLOBALS['prefix_cms']."_forummessage AS m
		WHERE t.idThread = m.idThread AND m.author <> '".getLogUserId()."' AND UNIX_TIMESTAMP(m.posted) >= '".$last_forum_access_time."'
		GROUP BY t.idThread, t.idForum, m.generator");

		while(list($id_thread, $id_forum, $is_generator, $how_much_mess) = mysql_fetch_row($reUnreaded)) {

			if($is_generator) {

				if(isset($unreaded[$id_forum]['new_thread']))
					$unreaded[$id_forum][$id_thread] = 'new_thread';
				else
					$unreaded[$id_forum][$id_thread] = 'new_thread';
			} else {
				if(isset($unreaded[$id_forum][$id_thread]))
					$unreaded[$id_forum][$id_thread] += $how_much_mess;
				else
					$unreaded[$id_forum][$id_thread] = $how_much_mess;
			}
			$_SESSION['unreaded_forum'] = $unreaded;
		}
		//-set as now the last forum access------------------------------------------------
		if($no_entry) {
			mysql_query("
			INSERT INTO  ".$GLOBALS['prefix_cms']."_forum_timing
			SET last_access = NOW(),
				idUser = '".getLogUserId()."'");
		} else {
			mysql_query("
			UPDATE ".$GLOBALS['prefix_cms']."_forum_timing
			SET  last_access = NOW()
			WHERE idUser = '".getLogUserId()."'");
		}
	}
}

function forum() {
	checkPerm('view');

	require_once($GLOBALS['where_framework'].'/lib/lib.newtypeone.php');
	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	$lang 		=& DoceboLanguage::CreateInstance('forum', 'cms');

	$mod_perm 	= checkPerm('mod', true);
	$moderate 	= checkPerm('moderate', true);
	$base_link 	= 'index.php?modname=forum&amp;op=forum';
	$acl_man 	=& $GLOBALS['current_user']->getAclManager();

	// Find and set unreaded message
	loadUnreaded();

	$tb = new typeOne( $GLOBALS['cms']['visuItem'], '', $lang->def('_ELEFORUM'));
	$tb->initNavBar('ini', 'link');
	$tb->setLink($base_link);

	$ini = $tb->getSelectedElement();

	// Construct query for forum display
	if($mod_perm) {

		$query_view_forum = "
		SELECT f.idForum, f.title, f.description, f.num_thread, f.num_post, f.locked, f.emoticons
		FROM ".$GLOBALS['prefix_cms']."_forum AS f
		ORDER BY f.sequence
		LIMIT $ini, ".$GLOBALS['cms']['visuItem'];

		$query_num_view = "
		SELECT COUNT(*) FROM ".$GLOBALS['prefix_cms']."_forum AS f";
	} else {

		$acl 	=& $GLOBALS['current_user']->getAcl();
		$all_user_idst = $acl->getSTGroupsST(getLogUserId());
		$all_user_idst[] = getLogUserId();

		$query_view_forum = "
		SELECT DISTINCT f.idForum, f.title, f.description, f.num_thread, f.num_post, f.locked, f.emoticons
		FROM ".$GLOBALS['prefix_cms']."_forum AS f
			LEFT JOIN ".$GLOBALS['prefix_cms']."_forum_access AS fa ON ( f.idForum = fa.idForum )
		WHERE
			( fa.idMember IS NULL OR fa.idMember IN (".implode($all_user_idst, ',')." )  ) ";

		$query_num_view = "
		SELECT COUNT( DISTINCT f.idForum )
		FROM ".$GLOBALS['prefix_cms']."_forum AS f
			LEFT JOIN ".$GLOBALS['prefix_cms']."_forum_access AS fa ON ( f.idForum = fa.idForum )
		WHERE
			( fa.idMember IS NULL OR fa.idMember IN (".implode($all_user_idst, ',')." )  ) ";

	}
	$re_forum = mysql_query($query_view_forum);
	list($tot_forum) = mysql_fetch_row(mysql_query($query_num_view));

	$re_last_post = mysql_query("
	SELECT f.idForum, m.idThread, m.posted, m.title, m.author
	FROM ".$GLOBALS['prefix_cms']."_forum AS f LEFT JOIN
		".$GLOBALS['prefix_cms']."_forummessage AS m ON ( f.last_post = m.idMessage )");
	while(list($idF_p, $idT_p, $posted, $title_p, $id_a) = mysql_fetch_row($re_last_post)) {

		if($posted !== NULL) {
			$last_post[$idF_p]['info'] = $GLOBALS['regset']->databaseToRegional($posted).'<br />'.substr(strip_tags($title_p), 0, 15).' ...';
			$last_post[$idF_p]['author'] = $id_a;
			$last_authors[] = $id_a;
		}
	}

	// find authors names
	if(isset($last_authors)) {
		$authors_names =& $acl_man->getUsers($last_authors);
	}
	// switch to one of the 2 visualization method
	if($GLOBALS['cms']['forum_as_table'] == 'on') {

		// show forum list in a table -----------------------------------------
		// table header
		$type_h = array('image', 'image', 'forumTitle', '', 'align_center', 'align_center', 'align_center');
		if($mod_perm) {
			$type_h[] = 'image'; $type_h[] = 'image'; $type_h[] = 'image'; $type_h[] = 'image'; $type_h[] = 'image';
		}
		$tb->setColsStyle($type_h);

		$cont_h = array(
			'<img src="'.getPathImage().'forum/forum.gif" title="'.$lang->def('_FREET').'" alt="'.$lang->def('_FREE').'" />',
			'<img src="'.getPathImage().'forum/emoticons.gif" title="'.$lang->def('_EMOTICONS').'" alt="'.$lang->def('_EMOTICONS').'" />',
			$lang->def('_TITLE'),
			$lang->def('_DESCRIPTION'),
			$lang->def('_NUMTHREAD'),
			$lang->def('_NUMPOST'),
			$lang->def('_LASTPOST')
		);
		if($mod_perm) {
			$cont_h[] = '<img src="'.getPathImage().'standard/down.gif" title="'.$lang->def('_DOWNFORUM').'" alt="'.$lang->def('_DOWN').'" />';
			$cont_h[] = '<img src="'.getPathImage().'standard/up.gif" title="'.$lang->def('_UPFORUM').'" alt="'.$lang->def('_UP').'" />';
			$cont_h[] = '<img src="'.getPathImage().'standard/moduser.gif" title="'.$lang->def('_MODGROUPST').'" alt="'.$lang->def('_MODGROUPS').'" />';
			$cont_h[] = '<img src="'.getPathImage().'standard/mod.gif" title="'.$lang->def('_MODFORUM').'" alt="'.$lang->def('_MOD').'" />';
			$cont_h[] = '<img src="'.getPathImage().'standard/rem.gif" title="'.$lang->def('_DELFORUM').'" alt="'.$lang->def('_DEL').'" />';
		}
		$tb->addHead($cont_h);

		// table body
		$i = 1;
		while(list($idF, $title, $descr, $num_thread, $num_post, $locked, $emoticons) = mysql_fetch_row($re_forum) ) {

			$c_css 			= '';
			$mess_notread 	= 0;
			$thread_notread = 0;
			// NOTES: status
			if($locked)	$status = '<img src="'.getPathImage().'forum/forum_locked.gif" alt="'.$lang->def('_LOCKED').'" />';
			elseif( isset($_SESSION['unreaded_forum'][$idF])) {

				if(isset($_SESSION['unreaded_forum'][$idF]) && is_array($_SESSION['unreaded_forum'][$idF])) {
					foreach($_SESSION['unreaded_forum'][$idF] as $k => $n_mess)
						if($n_mess != 'new_thread') $mess_notread += $n_mess;
						else $thread_notread += 1;
				}
				if($mess_notread > 0 || $thread_notread > 0) {
					$status = '<img src="'.getPathImage().'forum/forum_unreaded.gif" alt="'.$lang->def('_UNREADED').'" />';
					$c_css = ' class="text_bold"';
				} else {
					$status = '<img src="'.getPathImage().'forum/forum.gif" alt="'.$lang->def('_FREE').'" />';
				}
			} else $status = '<img src="'.getPathImage().'forum/forum.gif" alt="'.$lang->def('_FREE').'" />';

			// NOTES: other content
			$content = array( $status,
							'<img src="'.getPathImage('fw').'emoticons/'.$emoticons.'" title="'.$lang->def('_EMOTICONS').'" alt="'.$lang->def('_EMOTICONS').'" />',
							$title,
							$descr,
							$num_thread.( $thread_notread ? '<div class="forum_notread">'.$thread_notread.' '.$lang->def('_NEW').'</div>' : '' ),
							$num_post.( $mess_notread ? '<div class="forum_notread">'.$mess_notread.' '.$lang->def('_NEW').'</div>' : '' ) );
			if(isset($last_post[$idF])) {

				$author = $last_post[$idF]['author'];
				$content[] = $last_post[$idF]['info'].' ( '.$lang->def('_BY').': <span class="mess_author">'
							.( $authors_names[$author][ACL_INFO_LASTNAME].$authors_names[$author][ACL_INFO_FIRSTNAME] == '' ?
								$authors_names[$author][ACL_INFO_USERID] :
								$authors_names[$author][ACL_INFO_LASTNAME].' '.$authors_names[$author][ACL_INFO_FIRSTNAME] ).'</span> )';
			} else {

				$content[] = $lang->def('_NONE');
			}
			// NOTES: mod and perm
			if($mod_perm) {
				if($i != $tot_forum) $content[] = '<a href="index.php?modname=forum&amp;op=downforum&amp;idForum='.$idF.'">
					<img src="'.getPathImage().'standard/down.gif" title="'.$lang->def('_DOWNFORUM').'" alt="'.$lang->def('_DOWN').'" /></a>';
				else $content[] = '';

				if($i != 1) $content[] = '<a href="index.php?modname=forum&amp;op=moveupforum&amp;idForum='.$idF.'">
					<img src="'.getPathImage().'standard/up.gif" title="'.$lang->def('_UPFORUM').'" alt="'.$lang->def('_UP').'" /></a>';
				else $content[] = '';

				$content[] = '<a href="index.php?modname=forum&amp;op=modforumaccess&amp;idForum='.$idF.'&amp;load=1">
					<img src="'.getPathImage().'standard/moduser.gif" title="'.$lang->def('_MODGROUPST').'" alt="'.$lang->def('_MODGROUPS').'" /></a>';
				$content[] = '<a href="index.php?modname=forum&amp;op=modforum&amp;idForum='.$idF.'">
					<img src="'.getPathImage().'standard/mod.gif" title="'.$lang->def('_MODFORUM').'" alt="'.$lang->def('_MOD').'" /></a>';
				$content[] = '<a href="index.php?modname=forum&amp;op=delforum&amp;idForum='.$idF.'&amp;confirm=1" title="'.$lang->def('_DEL').' : '.$title.'">
					<img src="'.getPathImage().'standard/rem.gif" title="'.$lang->def('_DELFORUM').'" alt="'.$lang->def('_DEL').'" /></a>';
			}
			$tb->addBody( $content );
			++$i;
		}
		if($mod_perm) {

			$tb->addActionAdd('<a href="index.php?modname=forum&amp;op=addforum">
				<img src="'.getPathImage().'standard/add.gif" title="'.$lang->def('_ADDFORUMT').'" alt="'.$lang->def('_ADD').'" /> '
				.$lang->def('_ADDFORUM').'</a>');
			
			require_once($GLOBALS['where_framework'].'/lib/lib.dialog.php');	
			setupHrefDialogBox('a[href*=delforum]');
		}

		$GLOBALS['page']->add(
			getTitleArea($lang->def('_FORUM'), 'forum')
			.'<div class="std_block">'
			.$tb->getTable()
			.$tb->getNavBar($ini, $tot_forum)
			.'</div>', 'content' );
	} else {

		// second view styles
		$i = 1;
		$GLOBALS['page']->add(
			getTitleArea($lang->def('_FORUM'), 'forum')
			.'<div class="std_block">'
			.Form::openForm('search_forum', 'index.php?modname=forum&amp;op=search')
			.'<div class="search_mask form_line_l">'
			.'<label for="search_arg">'.$lang->def('_SEARCH_LABEL').'</label> '
			.Form::getInputTextfield(	'textfield_nowh',
										'search_arg',
										'search_arg',
										$lang->def('_SEARCH'),
										$lang->def('_SEARCH'), 255, '' )
			.' <input class="button_nowh" type="submit" id="search_button" name="search_button" value="'.$lang->def('_SEARCH').'" />'
			.'</div>'
			.Form::closeForm()
			, 'content');
		while( list($idF, $title, $descr, $num_thread, $num_post, $locked, $emoticons) = mysql_fetch_row( $re_forum ) ) {


			$c_css = '';
			$thread_notread = 0;
			$mess_notread = 0;
			// NOTES: status
			if($locked)	$status = '<img src="'.getPathImage().'forum/forum_locked.gif" alt="'.$lang->def('_LOCKED').'" />';
			elseif( isset($_SESSION['unreaded_forum'][$idF])) {

				if(isset($_SESSION['unreaded_forum'][$idF]) && is_array($_SESSION['unreaded_forum'][$idF])) {
					foreach($_SESSION['unreaded_forum'][$idF] as $k => $n_mess)
						if($n_mess != 'new_thread') $mess_notread += $n_mess;
						else $thread_notread += 1;
				}
				if($mess_notread > 0 || $thread_notread > 0) {
					$status = '<img src="'.getPathImage().'forum/forum_unreaded.gif" alt="'.$lang->def('_UNREADED').'" />';
					$c_css = ' class="text_bold"';
				} else {
					$status = '<img src="'.getPathImage().'forum/forum.gif" alt="'.$lang->def('_FREE').'" />';
				}
			} else {
				$status = '<img src="'.getPathImage().'forum/forum.gif" alt="'.$lang->def('_FREE').'" />';
			}

			$GLOBALS['page']->add(
				'<table class="forum_table" cellspacing="0" summary="'.$lang->def('_FORUM_INFORMATION').'">'
				.'<tr class="forum_header">'
					.'<th class="forum_title">'.$status.'&nbsp;'
					.'<img src="'.getPathImage('fw').'emoticons/'.$emoticons.'" title="'.$lang->def('_EMOTICONS').'" alt="'.$lang->def('_EMOTICONS').'" />'
					.'&nbsp;'
					//.'<a'.$c_css.' href="index.php?modname=forum&amp;op=thread&amp;idForum='.$idF.'">'.$title.'</a>'
					.$title
					.'</th>'
					.'<th class="image" nowrap="nowrap">'.$lang->def('_NUMTHREAD').'</th>'
					.'<th class="image" nowrap="nowrap">'.$lang->def('_NUMPOST').'</th>'
				.'</tr>'
				.'<tr>'
					.'<td>'.$descr.'</td>'
					.'<td class="image" nowrap="nowrap">'.$num_thread
						.( $thread_notread ? '<div class="forum_notread">'.$thread_notread.' '.$lang->def('_NEW').'</div>' : '' )
					.'</td>'
					.'<td class="image" nowrap="nowrap">'.$num_post
						.( $mess_notread ? '<div class="forum_notread">'.$mess_notread.' '.$lang->def('_NEW').'</div>' : '' )
					.'</td>'
				.'</tr>'
				.'<tr>'
					.'<td colspan="3">', 'content');

			if(isset($last_post[$idF])) {

				$author = $last_post[$idF]['author'];
				$GLOBALS['page']->add('<span class="forum_lastpost">'.$lang->def('_LASTPOST').' : '.$last_post[$idF]['info'].' ( '.$lang->def('_BY').': <span class="mess_author">'
					.( $authors_names[$author][ACL_INFO_LASTNAME].$authors_names[$author][ACL_INFO_FIRSTNAME] == '' ?
						$authors_names[$author][ACL_INFO_USERID] :
						$authors_names[$author][ACL_INFO_LASTNAME].' '.$authors_names[$author][ACL_INFO_FIRSTNAME] ).'</span> )'
						.'</span>'
					, 'content');
			} else {

				//$GLOBALS['page']->add($lang->def('_NONE'), 'content');
			}
			$GLOBALS['page']->add(
					'</td>'
				.'</tr>'
				.'<tr>'
					.'<td colspan="3" class="forum_manag">', 'content');
			if($mod_perm) {

				$GLOBALS['page']->add('<ul class="adjac_link">', 'content');
				if($i != $tot_forum) {

					$GLOBALS['page']->add('<li><a href="index.php?modname=forum&amp;op=downforum&amp;idForum='.$idF.'">
					<img src="'.getPathImage().'standard/down.gif" title="'.$lang->def('_DOWNFORUM').'" alt="'.$lang->def('_DOWN').'" /></a></li>'
					, 'content');
				}
				if($i != 1) {

					$GLOBALS['page']->add('<li><a href="index.php?modname=forum&amp;op=moveupforum&amp;idForum='.$idF.'">
					<img src="'.getPathImage().'standard/up.gif" title="'.$lang->def('_UPFORUM').'" alt="'.$lang->def('_UP').'" /></a></li>', 'content');
				} else {

					$GLOBALS['page']->add('<li><div style=" display: inline; margin: 0px 11px;"></div></li>', 'content');
				}
				$GLOBALS['page']->add(
					'<li><a href="index.php?modname=forum&amp;op=modforumaccess&amp;idF='.$idF.'">
						<img src="'.getPathImage().'standard/moduser.gif" title="'.$lang->def('_MODGROUPST').'" alt="'.$lang->def('_MODGROUPS').'" /></a></li>'
					.'<li><a href="index.php?modname=forum&amp;op=modforum&amp;idForum='.$idF.'">
						<img src="'.getPathImage().'standard/mod.gif" title="'.$lang->def('_MODFORUM').'" alt="'.$lang->def('_MOD').'" /></a></li>'
					.'<li><a href="index.php?modname=forum&amp;op=delforum&amp;idForum='.$idF.'">
						<img src="'.getPathImage().'standard/rem.gif" title="'.$lang->def('_DELFORUM').'" alt="'.$lang->def('_DEL').'" /></a></li>'
				, 'content');
				$GLOBALS['page']->add('</ul>', 'content');
			}
			$GLOBALS['page']->add('</td>'
				.'</tr>'
				.'</table>', 'content');
				$i++;
		}
		if($mod_perm) {

			$GLOBALS['page']->add(
				'<div class="add_container">'
				.'<a href="index.php?modname=forum&amp;op=addforum">'
				.'<img src="'.getPathImage().'standard/add.gif" title="'.$lang->def('_ADDFORUMT').'" alt="'.$lang->def('_ADD').'" /> '
				.$lang->def('_ADDFORUM').'</a></div>', 'content');
		}
		$GLOBALS['page']->add(
			$tb->getNavBar($ini, $tot_forum)
			.'</div>', 'content' );
	}
}

//---------------------------------------------------------------------------//

function addforum() {
	checkPerm('add');

	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	$lang =& DoceboLanguage::createInstance('forum', 'cms');

	$default = 'blank.gif';
	$GLOBALS['page']->add(
		getTitleArea($lang->def('_FORUM'), 'forum')
		.'<div class="std_block">'
		.getBackUi('index.php?modname=forum&amp;op=forum', $lang->def('_BACK'))
		.Form::openForm('addforumform', 'index.php?modname=forum&amp;op=insforum')
		.Form::openElementSpace()
		.Form::getTextfield($lang->def('_TITLE'), 'title', 'title', 255, $lang->def('_NOTITLE'))
		.Form::getTextarea($lang->def('_DESCRIPTION'), 'description', 'description', $lang->def('_DESCRIPTION'))
		.Form::openFormLine()
		.Form::getLabel('emoticons', $lang->def('_EMOTICONS'))
		.'<select class="dropdown" id="emoticons" name="emoticons">'
	, 'content');
	$templ = dir(getPathImage('fw').'emoticons/');
	while($elem = $templ->read()) {

		if(ereg('.gif', $elem)) {
			$GLOBALS['page']->add(
				'<option value="'.$elem.'" class="option_with_image" style="background-image: url(\''.getPathImage('fw').'emoticons/'.$elem.'\');"'
				.( $elem == $default ? ' selected="selected"' : '' )
				.'>'
				.$elem.'</option>'
			, 'content');
		}
	}
	closedir($templ->handle);
	$GLOBALS['page']->add(
		'</select>'
		.Form::closeFormLine()
		.Form::closeElementSpace()
		.Form::openButtonSpace()
		.Form::getButton('insforum', 'insforum', $lang->def('_INSERT'))
		.Form::getButton('undo', 'undo', $lang->def('_UNDO'))
		.Form::closeButtonSpace()
		.Form::closeForm()
		.'</div>', 'content');
}

function insforum() {
	checkPerm('mod');

	$lang =& DoceboLanguage::createInstance('forum', 'cms');

	if(isset($_POST['undo'])) jumpTo('index.php?modname=forum&op=forum');
	if($_POST['title'] == '') {
		$_POST['title'] = $lang->def('_NOTITLE');
	}

	// finding sequence
	list($seq) = mysql_fetch_row(mysql_query("
	SELECT MAX(sequence) + 1
	FROM ".$GLOBALS['prefix_cms']."_forum"));

	$ins_query = "
	INSERT INTO ".$GLOBALS['prefix_cms']."_forum
	( title, description, sequence, emoticons ) VALUES
	( '".$_POST['title']."',
		'".$_POST['description']."',
		'$seq',
		'".$_POST['emoticons']."' )";
	if(!mysql_query( $ins_query )) jumpTo('index.php?modname=forum&op=forum&result=err');


	require_once($GLOBALS['where_framework'].'/lib/lib.eventmanager.php');

	$acl_manager=$GLOBALS["current_user"]->getAclManager();
	$recipients=$acl_manager->getGroupAllUser($acl_manager->getGroupRegisteredId());

	$msg_composer = new EventMessageComposer('forum', 'cms');

	$msg_composer->setSubjectLangText('email', '_NEW_FORUM', false);
	$msg_composer->setBodyLangText('email', '_NEW_FORUM_BODY', array(	'[url]' => $GLOBALS['cms']['url'],
																		'[title]' => $_POST['title'],
																		'[text]' => $_POST['description'] ) );

	$msg_composer->setSubjectLangText('sms', '_NEW_FORUM_SMS', false);
	$msg_composer->setBodyLangText('sms', '_NEW_FORUM_BODY_SMS', array(	'[url]' => $GLOBALS['cms']['url'],
																		'[title]' => $_POST['title'],
																		'[text]' => $_POST['description'] ) );

	createNewAlert('CmsForumNewCategory',
						'forum',
						'addforum',
						1,
						"new forum category",
						$recipients,
						$msg_composer );


	jumpTo('index.php?modname=forum&op=forum&result=ok');
}

//---------------------------------------------------------------------------//

function modforum() {
	checkPerm('mod');

	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	$lang =& DoceboLanguage::createInstance('forum', 'cms');

	list($title, $text, $emoticons) = mysql_fetch_row(mysql_query("
	SELECT title, description, emoticons
	FROM ".$GLOBALS['prefix_cms']."_forum
	WHERE idForum = '".(int)$_GET['idForum']."'"));

	$GLOBALS['page']->add(
		getTitleArea($lang->def('_FORUM'), 'forum', $lang->def('_FORUM'))
		.'<div class="std_block">'
		.Form::openForm('addforumform', 'index.php?modname=forum&amp;op=upforum')
		.Form::openElementSpace()
		.Form::getHidden('idForum', 'idForum', (int)$_GET['idForum'])
		.Form::getTextfield($lang->def('_TITLE'), 'title', 'title', 255, $title)
		.Form::getTextarea($lang->def('_DESCRIPTION'), 'description', 'description', $text)
		.Form::openFormLine()
		.Form::getLabel('emoticons', $lang->def('_EMOTICONS'))
		.'<select id="emoticons" name="emoticons">'
	, 'content');
	$templ = dir(getPathImage('fw').'emoticons/');
	while($elem = $templ->read()) {

		if(ereg('.gif', $elem)) {
			$GLOBALS['page']->add(
				'<option value="'.$elem.'" class="option_with_image" style="background-image: url(\''.getPathImage('fw').'emoticons/'.$elem.'\');"'
				.( $elem == $emoticons ? ' selected="selected"' : '' )
				.'>'
				.$elem.'</option>'
			, 'content');
		}
	}
	closedir($templ->handle);
	$GLOBALS['page']->add(
		'</select>'
		.Form::closeFormLine()
		.Form::closeElementSpace()
		.Form::openButtonSpace()
		.Form::getButton('insforum', 'insforum', $lang->def('_SAVE'))
		.Form::getButton('undo', 'undo', $lang->def('_UNDO'))
		.Form::closeButtonSpace()
		.Form::closeForm()
		.'</div>', 'content');
}

function upforum() {
	checkPerm('mod');

	if(isset($_POST['undo'])) jumpTo('index.php?modname=forum&op=forum');
	if($_POST['title'] == '') $_POST['title'] = def('_NOTITLE', 'forum');

	$ins_query = "
	UPDATE ".$GLOBALS['prefix_cms']."_forum
	SET title = '".$_POST['title']."',
		description = '".$_POST['description']."',
		emoticons = '".$_POST['emoticons']."'
	WHERE idForum = '".(int)$_POST['idForum']."'";
	if(!mysql_query( $ins_query )) jumpTo('index.php?modname=forum&op=forum&result=err');
	jumpTo('index.php?modname=forum&op=forum&result=ok');
}

function moveforum($idForum, $direction) {
	checkPerm('mod');

	list( $seq ) = mysql_fetch_row(mysql_query("
	SELECT sequence
	FROM ".$GLOBALS['prefix_cms']."_forum
	WHERE idForum = '".(int)$idForum."'"));

	if($direction == 'up') {
		//move up
		mysql_query("
		UPDATE ".$GLOBALS['prefix_cms']."_forum
		SET sequence = '$seq'
		WHERE sequence = '".($seq - 1)."'");
		mysql_query("
		UPDATE ".$GLOBALS['prefix_cms']."_forum
		SET sequence = sequence - 1
		WHERE idForum = '".(int)$idForum."'");
	}
	if($direction == 'down') {
		//move down
		mysql_query("
		UPDATE ".$GLOBALS['prefix_cms']."_forum
		SET sequence = '$seq'
		WHERE sequence = '".($seq + 1)."'");
		mysql_query("
		UPDATE ".$GLOBALS['prefix_cms']."_forum
		SET sequence = sequence + 1
		WHERE idForum = '".(int)$idForum."'");
	}
	jumpTo('index.php?modname=forum&op=forum');
}

function changestatus() {
	checkPerm('mod');

	list( $lock ) = mysql_fetch_row(mysql_query("
	SELECT locked
	FROM ".$GLOBALS['prefix_cms']."_forum
	WHERE idForum = '".(int)$_GET['idForum']."'"));

	if($lock == 1) $new_status = 0;
	else $new_status = 1;

	mysql_query("
	UPDATE ".$GLOBALS['prefix_cms']."_forum
	SET locked = '$new_status'
	WHERE idForum = '".(int)$_GET['idForum']."'");
	jumpTo('index.php?modname=forum&op=thread&idForum='.(int)$_GET['idForum']);
}

//---------------------------------------------------------------------------//

function delforum() {
	checkPerm('del');

	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	$lang =& DoceboLanguage::createInstance('forum', 'cms');
	$id_forum = importVar('idForum', true, 0);

	list($title, $text, $seq) = mysql_fetch_row(mysql_query("
	SELECT title, description, sequence
	FROM ".$GLOBALS['prefix_cms']."_forum
	WHERE idForum = '".$id_forum."'"));

	if(isset($_POST['undo'])) jumpTo('index.php?modname=forum&op=forum');
	if(isset($_GET['confirm'])) {

		$re_thread = mysql_query("
		SELECT idThread
		FROM ".$GLOBALS['prefix_cms']."_forumthread
		WHERE idForum = '".(int)$_GET['idForum']."'");
		while(list($idT) = mysql_fetch_row($re_thread)) {

			if(!mysql_query("
			DELETE FROM ".$GLOBALS['prefix_cms']."_forummessage
			WHERE idThread = '$idT'")) jumpTo('index.php?modname=forum&op=forum&result=err_del');
			unsetNotify('thread', $idT);
		}
		if(!mysql_query("
		DELETE FROM ".$GLOBALS['prefix_cms']."_forumthread
		WHERE idForum= '".$id_forum."'")) jumpTo('index.php?modname=forum&op=forum&result=err_del');
		if(!mysql_query("
		DELETE FROM ".$GLOBALS['prefix_cms']."_forum_access
		WHERE idForum='".$id_forum."'")) jumpTo('index.php?modname=forum&op=forum&result=err_del');
		if(!mysql_query("
		DELETE FROM ".$GLOBALS['prefix_cms']."_forum
		WHERE idForum='".$id_forum."'")) jumpTo('index.php?modname=forum&op=forum&result=err_del');

		mysql_query("
		UPDATE ".$GLOBALS['prefix_cms']."_forum
		SET sequence = sequence - 1
		WHERE idForum = '".$id_forum."' AND sequence > '".$seq."'");

		unsetNotify('forum', $id_forum);
		jumpTo('index.php?modname=forum&op=forum&result=ok');
	} else {

		$GLOBALS['page']->add(
			getTitleArea($lang->def('_FORUM'), 'forum', $lang->def('_FORUM'))
			.'<div class="std_block">'
			.getDeleteUi($lang->def('_AREYOUSURE'),
				'<span class="text_bold">'.$lang->def('_TITLE').' :</span> '.$title.'<br />'
				.'<span class="text_bold">'.$lang->def('_DESCRIPTION').' :</span> '.$text,
				true,
				'index.php?modname=forum&amp;op=delforum&amp;idForum='.$_GET['idForum'].'&amp;confirm=1',
				'index.php?modname=forum&amp;op=forum' )
			.'</div>', 'content');
	}
}

//---------------------------------------------------------------------------//


function modforumaccess() {
	checkPerm('mod');

	require_once($GLOBALS['where_cms']."/lib/lib.simplesel.php");

	$out =& $GLOBALS['page'];
	$out->setWorkingZone("content");
	$lang =& DoceboLanguage::createInstance('forum', 'cms');

	$idForum=(int)importVar("idForum");


	$ssel=new SimpleSelector(true, $lang);

	$perm=array();
	$perm["view"]["img"]=getPathImage()."standard/view.gif";
	$perm["view"]["alt"]=$lang->def("_ALT_VIEW");
	$perm["write"]["img"]=getPathImage()."standard/add.gif";
	$perm["write"]["alt"]=$lang->def("_ADD");
	$perm["moderate"]["img"]=getPathImage()."forum/moderate.gif";
	$perm["moderate"]["alt"]=$lang->def("_ALT_MODERATE");
	$perm["upload"]["img"]=getPathImage()."forum/upload.gif";
	$perm["upload"]["alt"]=$lang->def("_ALT_UPLOAD");

	$ssel->setPermList($perm);

	$url="index.php?modname=forum&amp;op=modforumaccess&amp;idForum=".$idForum;
	$back_url="index.php?modname=forum&amp;op=forum";
	$ssel->setLinks($url, $back_url);

	$op=$ssel->getOp();

	if (($op == "main") || ($op == "manual_init") )
		$saved_data=loadForumSavedPerm($idForum);

	$page_body="";
	$full_page="";

	switch($op) {

		case "main": {
			$ssel->setSavedData($saved_data);
			$page_body=$ssel->loadSimpleSelector();
		} break;

		case "manual_init":{

			// Saving permissions of simple selector
			$save_info=$ssel->getSaveInfo();
			saveForumPerm($idForum, $save_info["selected"], $save_info["database"]);

			$ssel->setSavedData($saved_data);
			$full_page=$ssel->loadManualSelector($lang->def( '_FORUM_PERM' ));
		} break;
		case "manual": {
			$full_page=$ssel->loadManualSelector($lang->def( '_FORUM_PERM' ));
		} break;

		case "save_manual": {

			// Saving permissions of manual selector
			$save_info=$ssel->getSaveInfo();
			saveForumPerm($idForum, $save_info["selected"], $save_info["database"]);

			jumpTo(str_replace("&amp;", "&", $url));
		} break;

		case "save": {

			// Saving permissions of simple selector
			$save_info=$ssel->getSaveInfo();
			saveForumPerm($idForum, $save_info["selected"], $save_info["database"]);

			jumpTo(str_replace("&amp;", "&", $back_url));
		} break;

	}

	if (!empty($full_page))
		$out->add($full_page);

	if (!empty($page_body)) {
		// If we have only the page body, then better to add the area title.
		$ta_array=array();
		$ta_array["index.php?modname=forum&amp;op=forum"]=$lang->def("_FORUM");
		$ta_array[]=$lang->def( '_FORUM_PERM' );

		$out->add(getTitleArea($ta_array, 'forum', $lang->def('_FORUM')));
		$out->add("<div class=\"std_block\">\n");
		$out->add($page_body);
		$out->add("</div>\n");
	}
}


function saveForumPerm($idForum, $selected_items, $database_items) {

		$pl=getForumPermList();
		$acl_manager=& $GLOBALS['current_user']->getACLManager();
		foreach($pl as $key=>$val) {
			if ((isset($selected_items[$val])) && (is_array($selected_items[$val]))) {

				$role_id="/cms/forum/".$idForum."/".$val;
				$role=$acl_manager->getRole(false, $role_id);
				if (!$role)
					$idst=$acl_manager->registerRole($role_id, "");
				else
					$idst=$role[ACL_INFO_IDST];

				foreach($selected_items[$val] as $pk=>$pv) {
					if ((!isset($database_items[$val])) || (!is_array($database_items[$val])) ||
						(!in_array($pv, array_keys($database_items[$val])))) {
							$acl_manager->addToRole($idst, $pv);
					}
				}

				if ((isset($database_items[$val])) && (is_array($database_items[$val])))
					$to_rem=array_diff(array_keys($database_items[$val]), $selected_items[$val]);
				else
					$to_rem=array();
				foreach($to_rem  as $pk=>$pv) {
					$acl_manager->removeFromRole($idst, $pv);
				}

			}
		}

	include_once($GLOBALS['where_cms']."/lib/lib.reloadperm.php");
	setCmsReloadPerm();
}



function modforumaccess_ok() {
	checkPerm('mod');

	require_once($GLOBALS['where_framework']."/class.module/class.directory.php");

	$out =& $GLOBALS['page'];
	$out->setWorkingZone("content");
	$lang =& DoceboLanguage::createInstance('forum', 'cms');

	$mdir=new Module_Directory();
	$mdir->show_orgchart_selector=FALSE;

	$mdir->use_multi_sel=true;

	$perm=array();
	$perm["view"]["img"]=getPathImage()."standard/view.gif";
	$perm["view"]["alt"]=$lang->def("_ALT_VIEW");
	$perm["write"]["img"]=getPathImage()."standard/add.gif";
	$perm["write"]["alt"]=$lang->def("_ADD");
	$perm["moderate"]["img"]=getPathImage()."forum/moderate.gif";
	$perm["moderate"]["alt"]=$lang->def("_ALT_MODERATE");
	$perm["upload"]["img"]=getPathImage()."forum/upload.gif";
	$perm["upload"]["alt"]=$lang->def("_ALT_UPLOAD");


	$mdir->sel_extend->setExtraSel($perm);
	if (isset($_POST[DIRECTORY_ID]))
		$mdir->sel_extend->setPrintedItems($mdir->getPrintedItems($_POST));

	$idForum=(int)importVar("idForum");
	//$this->docman->requireFolderPerm($tree_id, "addfolder");


	$back_url="index.php?modname=forum&amp;op=forum";
	$out->add( getBackUi($back_url, $lang->def( '_BACK' ) ));


	if( isset($_POST['okselector']) ) {  // Saving permissions..

		$selected_items=$mdir->sel_extend->getSelectedItems(); //print_r($selected_items); die();
		$database_items=$mdir->sel_extend->getDatabaseItemsFromVar();

		saveForumPerm($idForum, $selected_items, $database_items);

		jumpTo(str_replace("&amp;", "&", $back_url));
	}
	else if( isset($_POST['cancelselector']) ) { // Undo..
		jumpTo(str_replace("&amp;", "&", $back_url));
	}
	else { // Show selector


		$url="index.php?modname=forum&amp;op=modforumaccess&amp;idForum=".$idForum."&amp;stayon=1";

		if( !isset($_GET['stayon']) ) {
			$saved_perm=loadForumSavedPerm($idForum);
			$mdir->sel_extend->grabSelectedItems($saved_perm);
			$mdir->sel_extend->setDatabaseItems($saved_perm);
		}
		else {
			$mdir->sel_extend->grabSelectedItems($_POST);
		}


		$mdir->loadSelector($url,
		$lang->def( '_FORUM_PERM' ), "", TRUE);
	}

}


function getForumPermList() {
	return array("view", "write", "moderate", "upload");
}


function loadForumSavedPerm($idForum) {
	$res=array();
	$pl=getForumPermList();
	$acl_manager=& $GLOBALS['current_user']->getACLManager();

	foreach($pl as $key=>$val) {

		$role_id="/cms/forum/".$idForum."/".$val;
		$role=$acl_manager->getRole(false, $role_id);

		if (!$role) {
			$res[$val]=array();
		}
		else {
			$idst=$role[ACL_INFO_IDST];
			$res[$val]=array_flip($acl_manager->getRoleMembers($idst));
		}
	}

	return $res;
}


function modforumaccess_old() {
	checkPerm('mod');

	require_once($GLOBALS['where_framework'].'/class.module/class.directory.php');
	$lang =& DoceboLanguage::createInstance('forum', 'cms');
	$out =& $GLOBALS['page'];
	$id_forum = importVar('idForum', true, 0);

	$aclManager = new DoceboACLManager();
	$user_select = new Module_Directory();
	$user_select->show_user_selector = TRUE;
	$user_select->show_group_selector = TRUE;
	$user_select->show_orgchart_selector = FALSE;

	if(isset($_POST['cancelselector'])) jumpTo('index.php?modname=forum&amp;op=forum');
	if(isset($_POST['okselector'])) {

		$user_selected 	= $user_select->getSelection($_POST);

		$query_reader = "
		SELECT idMember
		FROM ".$GLOBALS['prefix_cms']."_forum_access
		WHERE idForum = '".$id_forum."'";
		$re_reader = mysql_query($query_reader);
		$old_users = array();
		while(list($id_user) = mysql_fetch_row($re_reader)) {

			$old_users[] = $id_user;
		}
		$add_reader = array_diff($user_selected, $old_users);
		$del_reader = array_diff($old_users, $user_selected);

		if(is_array($add_reader)) {

			while(list(, $idst) = each($add_reader)) {

				$query_insert = "
				INSERT INTO ".$GLOBALS['prefix_cms']."_forum_access
				( idForum, idMember ) VALUES
				( 	'".$id_forum."',
					'".$idst."' )";
				mysql_query($query_insert);
			}
		}
		if(is_array($del_reader)) {

			while(list(, $idst) = each($del_reader)) {

				$query_delete = "
				DELETE FROM ".$GLOBALS['prefix_cms']."_forum_access
				WHERE idForum = '".$id_forum."' AND idMember = '".$idst."'";
				mysql_query($query_delete);
			}
		}
		jumpTo('index.php?modname=forum&amp;op=forum&amp;result=ok');
	}

	if(isset($_GET['load'])) {

		$query_reader = "
		SELECT idMember
		FROM ".$GLOBALS['prefix_cms']."_forum_access
		WHERE idForum = '".$id_forum."'";
		$re_reader = mysql_query($query_reader);
		$users = array();
		while(list($id_user) = mysql_fetch_row($re_reader)) {

			$users[] = $id_user;
		}
		$user_select->resetSelection($users);
	}
	$arr_idstGroup = $aclManager->getGroupsIdstFromBasePath('/lms/course/'.(int)$_SESSION['idCourse'].'/subscribed/');
	$user_select->setUserFilter('group',$arr_idstGroup);
	$user_select->setGroupFilter('path', '/lms/course/'.$_SESSION['idCourse'].'/group');
	$user_select->setPageTitle(getTitleArea(
		array('index.php?modname=forum&amp;op=forum' => $lang->def('_FORUM'),
		$lang->def('_FORUM_ACCESS')), 'forum'));
	$user_select->loadSelector('index.php?modname=forum&amp;op=modforumaccess&amp;idForum='.$id_forum,
			'',
			$lang->def('_CHOOSE_FORUM_ACCESS'),
			true,
			true );
}

//---------------------------------------------------------------------------//

function thread() {
	checkPerm('view');

	require_once($GLOBALS['where_framework'].'/lib/lib.newtypeone.php');
	require_once($GLOBALS['where_framework'].'/lib/lib.navbar.php');
	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');

	$lang =& DoceboLanguage::createInstance('forum', 'cms');

	$moderate 	= checkPerm('moderate', true);
	$mod_perm 	= checkPerm('mod', true);
	$id_forum 	= importVar('idForum', true, 0);
	$ord 		= importVar('ord');
	$jump_url	= 'index.php?modname=forum&amp;op=thread&amp;idForum='.$id_forum;
	$acl_man 	=& $GLOBALS['current_user']->getAclManager();

	list($title, $tot_thread, $locked_f) = mysql_fetch_row(mysql_query("
	SELECT title, num_thread, locked
	FROM ".$GLOBALS['prefix_cms']."_forum
	WHERE idForum = '$id_forum'"));

	$nav_bar 	= new NavBar('ini', $GLOBALS['cms']['visuItem'], $tot_thread, 'link');
	$ini 		= $nav_bar->getSelectedElement();
	$ini_page 	= $nav_bar->getSelectedPage();
	$nav_bar->setLink($jump_url.'&amp;ord='.$ord);

	$query_thread = "
	SELECT t.idThread, t.author AS thread_author, t.posted, t.title, t.num_post, t.num_view, t.locked, t.erased
	FROM ".$GLOBALS['prefix_cms']."_forumthread AS t LEFT JOIN
			".$GLOBALS['prefix_cms']."_forummessage AS m ON ( t.last_post = m.idMessage )
	WHERE t.idForum = '$id_forum' ";
	switch($ord) {
		case "obji"		: $query_thread .= " ORDER BY t.title DESC " ;	break;
		case "obj" 		: $query_thread .= " ORDER BY t.title " ;			break;
		case "authi"	: $query_thread .= " ORDER BY t.author DESC " ;	break;
		case "auth" 	: $query_thread .= " ORDER BY t.author " ;		break;
		case "posti" 	: $query_thread .= " ORDER BY m.posted " ;		break;
		case "post"		:
		default 		: {
			$ord = 'post';
			$query_thread .= " ORDER BY m.posted DESC " ;	break;
		}
	}
	$query_thread .= " LIMIT $ini, ".$GLOBALS['cms']['visuItem'];
	$re_thread = mysql_query($query_thread);

	$re_last_post = mysql_query("
	SELECT m.idThread, t.author AS thread_author, m.posted, m.title, m.author  AS mess_author, m.generator
	FROM ".$GLOBALS['prefix_cms']."_forumthread AS t LEFT JOIN
		".$GLOBALS['prefix_cms']."_forummessage AS m ON ( t.last_post = m.idMessage )
	WHERE t.idForum = '".$id_forum."'");
	while(list($idT_p, $id_ta, $posted, $title_p, $id_a, $is_gener) = mysql_fetch_row($re_last_post)) {

		$last_authors[$id_ta] = $id_ta;
		if($posted !== NULL) {
			$last_post[$idT_p]['info'] = $GLOBALS['regset']->databaseToRegional($posted).'<br />'.substr(strip_tags($title_p), 0, 15).' ...';
			$last_post[$idT_p]['author'] = $id_a;
			$last_authors[$id_a] = $id_a;
		}
	}
	if(isset($last_authors)) {
		$authors_names =& $acl_man->getUsers($last_authors);
	}
	$page_title = array(
		'index.php?modname=forum&amp;op=forum' => $lang->def('_FORUM'),
		$title
	);
	$GLOBALS['page']->add(
		 getTitleArea($page_title, 'forum')
		.'<div class="std_block">'
		.Form::openForm('search_forum', 'index.php?modname=forum&amp;op=search&amp;idForum='.$id_forum)
		.'<div class="search_mask form_line_l">'
		.'<label for="search_arg">'.$lang->def('_SEARCH_LABEL').'</label> '
		.Form::getInputTextfield(	'textfield_nowh',
									'search_arg',
									'search_arg',
									$lang->def('_SEARCH'),
									$lang->def('_SEARCH'), 255, '' )
		.' <input class="button_nowh" type="submit" id="search_button" name="search_button" value="'.$lang->def('_SEARCH').'" />'
		.'</div>'
		.Form::closeForm()
	, 'content');

	$tb = new TypeOne($GLOBALS['cms']['visuItem'], $lang->def('_THREAD_CAPTION'), $lang->def('_THRAD_SUMMARY'));

	$img_up 	= '<img src="'.getPathImage().'standard/ord_asc.gif" alt="'.$lang->def('_ORD_ASC').'" />';
	$img_down 	= '<img src="'.getPathImage().'standard/ord_desc.gif" alt="'.$lang->def('_ORD_DESC').'" />';

	$cont_h = array(
		'<img src="'.getPathImage().'forum/thread.gif" title="'.$lang->def('_FREET').'" alt="'.$lang->def('_FREE').'" />',
		'<a href="'.$jump_url.'&amp;ord='.( $ord == 'obj' ? 'obji' : 'obj' ).'" title="'.$lang->def('_ORD_THREAD').'">'
			.( $ord == 'obj' ? $img_up : ( $ord == 'obji' ? $img_down : '' ) ).$lang->def('_THREAD').'</a>',
		$lang->def('_NUMREPLY'),
		'<a href="'.$jump_url.'&amp;ord='.( $ord == 'auth' ? 'authi' : 'auth' ).'" title="'.$lang->def('_ORD_AUTHOR').'">'
			.( $ord == 'auth' ? $img_up : ( $ord == 'authi' ? $img_down : '' ) ).$lang->def('_AUTHOR').'</a>',
		$lang->def('_NUMVIEW'),
		//$lang->def('_POSTED'),
		'<a href="'.$jump_url.'&amp;ord='.( $ord == 'post' ? 'posti' : 'post' ).'" title="'.$lang->def('_ORD_POST').'">'
			.( $ord == 'post' ? $img_up : ( $ord == 'posti' ? $img_down : '' ) ).$lang->def('_LASTPOST').'</a>'
	);
	$type_h = array('image', '', 'align_center', 'align_center', 'image',
	//'align_center',
	'align_center');
	if($mod_perm) {

		$cont_h[] = '<img src="'.getPathImage().'standard/mod.gif" alt="'.$lang->def('_MOD').'" title="'.$lang->def('_MODTHREAD_TITLE').'" />';
		$type_h[] = 'image';
		$cont_h[] = '<img src="'.getPathImage().'standard/rem.gif" alt="'.$lang->def('_DEL').'" title="'.$lang->def('_DELTHREAD_TITLE').'" />';
		$type_h[] = 'image';
	}
	$tb->setColsStyle($type_h);
	$tb->addHead($cont_h);
	while(list($idT, $t_author, $posted, $title, $num_post, $num_view, $locked, $erased) = mysql_fetch_row($re_thread)) {

		$c_css = '';
		// thread author
		$t_author = ( $authors_names[$t_author][ACL_INFO_LASTNAME].$authors_names[$t_author][ACL_INFO_FIRSTNAME] == '' ?
				$authors_names[$t_author][ACL_INFO_USERID] :
				$authors_names[$t_author][ACL_INFO_LASTNAME].' '.$authors_names[$t_author][ACL_INFO_FIRSTNAME] );
		// last post author
		if(isset($last_post[$idT])) {

			$author = $last_post[$idT]['author'];
			$last_mess_write = $last_post[$idT]['info'].' ( '.$lang->def('_BY').': <span class="mess_author">'
				.( $authors_names[$author][ACL_INFO_LASTNAME].$authors_names[$author][ACL_INFO_FIRSTNAME] == '' ?
					$authors_names[$author][ACL_INFO_USERID] :
					$authors_names[$author][ACL_INFO_LASTNAME].' '.$authors_names[$author][ACL_INFO_FIRSTNAME] ).'</span> )';
		} else {
			$last_mess_write = $lang->def('_NONE');
		}
		// status of the thread
		if($erased) {
			$status = '<img src="'.getPathImage().'forum/thread_erased.gif" alt="'.$lang->def('_FREE').'" />';
		} elseif($locked) {
			$status = '<img src="'.getPathImage().'forum/thread_locked.gif" alt="'.$lang->def('_LOCKED').'" />';
		} elseif(isset($_SESSION['unreaded_forum'][$id_forum][$idT])) {

			$status = '<img src="'.getPathImage().'forum/thread_unreaded.gif" alt="'.$lang->def('_UNREADED').'" />';
			$c_css = ' class="text_bold"';
		} else {
			$status = '<img src="'.getPathImage().'forum/thread.gif" alt="'.$lang->def('_FREE').'" />';
		}
		$content = array($status);
		$content[] = ( $erased && !$mod_perm ?
					'<div class="forumErased">'.$lang->def('_ERASED').'</div>' :
					'<a'.$c_css.' href="index.php?modname=forum&amp;op=message&amp;idThread='.$idT.'">'.$title.'</a>');
		$content[] = $num_post
			.( isset($_SESSION['unreaded_forum'][$id_forum][$idT]) && $_SESSION['unreaded_forum'][$id_forum][$idT] != 'new_thread'
				? '<br />(<span class="forum_notread">'.$_SESSION['unreaded_forum'][$id_forum][$idT].' '.$lang->def('_NEW').')</span>'
				: ( isset($_SESSION['unreaded_forum'][$id_forum][$idT]) && $_SESSION['unreaded_forum'][$id_forum][$idT] == 'new_thread'
					? '<br />(<span class="forum_notread">'.$lang->def('_NEW_THREAD').')</span>'
					: '') );
		$content[] = $t_author;
		$content[] = $num_view;
		//$content[] = $GLOBALS['regset']->databaseToRegional($posted);
		$content[] = $last_mess_write;
		if($mod_perm) {

			$content[] = '<a href="index.php?modname=forum&amp;op=modthread&amp;idThread='.$idT.'" '
				.'title="'.$lang->def('_MODTHREAD_TITLE').' : '.strip_tags($title).'">'
				.'<img src="'.getPathImage().'standard/mod.gif" alt="'.$lang->def('_MOD').' : '.strip_tags($title).'" /></a>';
			$content[] = '<a href="index.php?modname=forum&amp;op=delthread&amp;idThread='.$idT.'" '
				.'title="'.$lang->def('_DELTHREAD_TITLE').' : '.strip_tags($title).'">'
				.'<img src="'.getPathImage().'standard/rem.gif" alt="'.$lang->def('_DEL').' : '.strip_tags($title).'" /></a>';
		}
		$tb->addBody($content);
	}
	if(!$locked_f || $mod_perm) {
		$tb->addActionAdd('<a href="index.php?modname=forum&amp;op=addthread&amp;idForum='.$id_forum.'">'
			.'<img src="'.getPathImage().'standard/add.gif" title="'.$lang->def('_ADDTHREADT').'" alt="'.$lang->def('_ADD').'" /> '
			.$lang->def('_ADDTHREAD').'</a>');
	}

	// NOTE: If notify request register it
	require_once($GLOBALS['where_framework'].'/lib/lib.usernotifier.php');

	$can_notify = usernotifier_getUserEventStatus(getLogUserId(), 'ForumNewThread');

	if(isset($_GET['notify']) && $can_notify) {
		if(issetNotify('forum', $id_forum, getLogUserId())) {
			$re = unsetNotify('forum', $id_forum, getLogUserId());
			$is_notify = !$re;
		} else {
			$re = setNotify('forum', $id_forum, getLogUserId());
			$is_notify = $re;
		}
		if($re) $GLOBALS['page']->add(getResultUi($lang->def('_NOTIFY_CHANGE_STATUS_CORRECT')), 'content');
		else $GLOBALS['page']->add(getErrorUi($lang->def('_NOTIFY_CHANGE_STATUS_FAILED')), 'content');
	} elseif($can_notify) {
		$is_notify = issetNotify('forum', $id_forum, getLogUserId());
	}


	$text_inner = '';
	if($can_notify) {

		$text_inner .= '<li><a href="index.php?modname=forum&amp;op=thread&amp;notify=1&amp;idForum='.$id_forum.'&amp;ini='.$ini_page.'" '
		.( !$is_notify ?
			'title="'.$lang->def('_NOTIFY_ME_FORUM_TITLE').'">'
			.'<img src="'.getPathImage().'forum/notify.gif" alt="'.$lang->def('_NOTIFY').'" /> '.$lang->def('_NOTIFY_ME_FORUM').'</a> '
			:
			'title="'.$lang->def('_UNNOTIFY_ME_FORUM_TITLE').'">'
			.'<img src="'.getPathImage().'forum/unnotify.gif" alt="'.$lang->def('_UNNOTIFY').'" /> '.$lang->def('_UNNOTIFY_ME_FORUM').'</a> '
		).'</li>';
	}
	if($moderate) {
		$text_inner .= '<li><a href="index.php?modname=forum&amp;op=modstatus&amp;idForum='.$id_forum.'">'
			.( $locked_f
				?'<img src="'.getPathImage().'forum/forum.gif" alt="'.$lang->def('_UNLOCKFORUMALT').'" /> '.$lang->def('_UNLOCKFORUM')
				: '<img src="'.getPathImage().'forum/forum_locked.gif" alt="'.$lang->def('_LOCKFORUMALT').'" /> '.$lang->def('_LOCKFORUM') )
			.'</a></li>';
	}
	if($text_inner != '') $GLOBALS['page']->add('<div class="forum_action_top"><ul class="adjac_link">'.$text_inner.'</ul></div>', 'content');
	$GLOBALS['page']->add($tb->getTable(), 'content');
	if($text_inner != '') $GLOBALS['page']->add('<div class="forum_action_bottom"><ul class="adjac_link">'.$text_inner.'</ul></div>', 'content');
	$GLOBALS['page']->add(
		$nav_bar->getNavBar($ini)
		.'</div>', 'content');
}

//---------------------------------------------------------------------------//

function addthread() {
	checkPerm('view');

	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	$lang =& DoceboLanguage::createInstance('forum', 'cms');
	$id_forum = importVar('idForum', true, 0);

	list($title) = mysql_fetch_row(mysql_query("
	SELECT title
	FROM ".$GLOBALS['prefix_cms']."_forum
	WHERE idForum = '".$id_forum."'"));

	$page_title = array(
		'index.php?modname=forum&amp;forum' => $lang->def('_FORUM'),
		'index.php?modname=forum&amp;op=thread&amp;idForum='.$id_forum => $title,
		$lang->def('_NEW_THREAD')
	);
	$GLOBALS['page']->add(
		getTitleArea($page_title, 'forum', $lang->def('_FORUM'))
		.'<div class="std_block">'
		.getBackUi('index.php?modname=forum&amp;op=thread&amp;idForum='.$id_forum, $lang->def('_BACK'))
		.Form::openForm('form_forum', 'index.php?modname=forum&amp;op=insthread', false, false, 'multipart/form-data')
		.Form::openElementSpace()

		.Form::getHidden('idForum', 'idForum', $id_forum)
		.Form::getTextfield($lang->def('_SUBJECT'), 'title', 'title', 255)
		.Form::getTextarea($lang->def('_TEXTOF'), 'textof', 'textof')
	, 'content');
	if(checkPerm('upload', true)) {

		$GLOBALS['page']->add(Form::getFilefield($lang->def('_UPLOAD'), 'attach', 'attach'), 'content');
	}
	$GLOBALS['page']->add(
		Form::closeElementSpace()
		.Form::openButtonSpace()
		.Form::getButton('post_thread', 'post_thread', $lang->def('_SEND'))
		.Form::getButton('undp', 'undo', $lang->def('_UNDO'))
		.Form::closeButtonSpace()
		.Form::closeForm()
		.'</div>'
	, 'content');
}

//---------------------------------------------------------------------------//

function save_file($file) {
	require_once($GLOBALS['where_framework'].'/lib/lib.upload.php');

	$path = $GLOBALS['where_files'].'/doceboCms/'.$GLOBALS['cms']['pathforum'];

	if($file['name'] != '') {

		$savefile = rand(0,100).'_'.time().'_'.$file['name'];
		if(!file_exists($path.$savefile)) {

			sl_open_fileoperations();
			if(!sl_upload($file['tmp_name'], $path.$savefile)) {

				$savefile = '';
			}
			sl_close_fileoperations();
			return $savefile;
		}
	}
	return '';
}

function delete_file( $name ) {
	require_once($GLOBALS['where_framework'].'/lib/lib.upload.php');

	$path = $GLOBALS['where_files'].'/doceboCms/'.$GLOBALS['cms']['pathforum'];
	if($name != '') return sl_unlink($path.$name);
}

function insthread() {
	checkPerm('view');

	$lang =& DoceboLanguage::createInstance('forum', 'cms');
	$id_forum = importVar('idForum', true , 0);
	if(isset($_POST['undo'])) jumpTo('index.php?modname=forum&op=thread&idForum='.$id_forum);

	list($forum_title) = mysql_fetch_row(mysql_query("
	SELECT title
	FROM ".$GLOBALS['prefix_cms']."_forum
	WHERE AND idForum = '".$id_forum."'"));

	$locked = false;
	if(!checkPerm('moderate', true)) {

		$query_view_forum = "
		SELECT idMember, locked
		FROM ".$GLOBALS['prefix_cms']."_forum AS f LEFT JOIN
				".$GLOBALS['prefix_cms']."_forum_access AS fa
					ON ( f.idForum = fa.idForum )
		WHERE AND f.idForum = '".$id_forum."'";
		$re_forum = mysql_query($query_view_forum);
		while(list($id_m, $lock_s) = mysql_fetch_row($re_forum)) {

			$locked = $lock_s;
			if($id_m != NULL) $members[] = $id_m;
		}
	}
	$continue = false;
	if(!isset($members)) $continue = true;
	else {
		$acl 	=& $GLOBALS['current_user']->getAcl();
		$all_user_idst = $acl->getSTGroupsST(getLogUserId());
		$all_user_idst[] = getLogUserId();

		$can_access = array();
		$can_access = array_intersect($members, $all_user_idst);
		if(!empty($can_access)) $continue = true;
	}
	if(!$continue) jumpTo('index.php?modname=forum&op=thread&idForum='.$id_forum.'&amp;result=err_cannotsee');
	if($locked) jumpTo('index.php?modname=forum&op=thread&idForum='.$id_forum.'&amp;result=err_lock');
	if($_POST['title'] == '') $_POST['title'] = substr(strip_tags($_POST['textof']), 0, 50).'...';

	$now = date("Y-m-d H:i:s");
	$ins_query = "
	INSERT INTO ".$GLOBALS['prefix_cms']."_forumthread
	( idForum, title, author, num_post, last_post, posted )
	VALUES (
		'".$id_forum."',
		'".$_POST['title']."',
		'".getLogUserId()."',
		 0,
		 0,
		 '".$now ."' )";
	if(!mysql_query($ins_query)) jumpTo('index.php?modname=forum&op=thread&idForum='.$id_forum.'&amp;result=err_ins');
	list($id_thread) = mysql_fetch_row(mysql_query("SELECT LAST_INSERT_ID()"));

	$name_file = '';
	if(($_FILES['attach']['name'] != '') && checkPerm('upload', true)) {

		$name_file = save_file($_FILES['attach']);
	}

	$ins_mess_query = "
	INSERT INTO ".$GLOBALS['prefix_cms']."_forummessage
	( idThread, title, textof, author, posted, answer_tree, attach, generator )
	VALUES (
		'".$id_thread."',
		'".$_POST['title']."',
		'".$_POST['textof']."',
		'".getLogUserId()."',
		'".$now ."',
		'/".$now ."',
		'".addslashes($name_file)."',
		'1' ) ";
	if(!mysql_query( $ins_mess_query )) {

		mysql_query("
		DELETE FROM ".$GLOBALS['prefix_cms']."_forumthread
		WHERE idThread = '$id_thread'");
		delete_file($name_file);

		jumpTo('index.php?modname=forum&op=thread&idForum='.$id_forum.'&amp;result=err_ins2');
	}
	list($id_message) = mysql_fetch_row(mysql_query("SELECT LAST_INSERT_ID()"));

	mysql_query("
	UPDATE ".$GLOBALS['prefix_cms']."_forumthread
	SET last_post = '$id_message'
	WHERE idThread = '$id_thread'");

	mysql_query("
	UPDATE ".$GLOBALS['prefix_cms']."_forum
	SET num_thread = num_thread + 1,
		num_post = num_post + 1,
		last_post = '$id_message'
	WHERE idForum = '$id_forum'");

	$course_name = $GLOBALS['course_descriptor']->getValue('name');

	launchNotify('forum', $id_forum, $lang->def('_NEW_THREAD'),
		$lang->def('_SUBJECT_NOTIFY_THREAD'),
		$lang->def('_NEW_THREAD_INSERT_IN_FORUM')."\n".$GLOBALS['cms']['url'].' : '.$course_name.' > '.$forum_title.' > '.$_POST['title']
	);

	jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread);
}

//---------------------------------------------------------------------------//

function modthread() {
	checkPerm('view');

	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	$lang =& DoceboLanguage::createInstance('forum', 'cms');
	$id_thread 	= importVar('idThread', true, 0);
	$ini 	= importVar('ini');

	$moderate 	= checkPerm('moderate', true);
	$mod_perm	= checkPerm('mod', true);
	$acl_man 	=& $GLOBALS['current_user']->getAclManager();

	// retrive info about message
	$mess_query = "
	SELECT idMessage, title, textof, author
	FROM ".$GLOBALS['prefix_cms']."_forummessage
	WHERE idThread = '".$id_thread."' AND generator = '1'";
	list($id_message, $title, $textof, $author) = mysql_fetch_row(mysql_query($mess_query));

	if(!$moderate && !$mod_perm && ($author == getLogUserId()) ) die("You can't access");

	// Some info about forum and thread
	$thread_query = "
	SELECT idForum
	FROM ".$GLOBALS['prefix_cms']."_forumthread
	WHERE idThread = '".$id_thread."'";
	list($id_forum) = mysql_fetch_row(mysql_query($thread_query));
	$forum_query = "
	SELECT title, locked
	FROM ".$GLOBALS['prefix_cms']."_forum
	WHERE idForum = '".$id_forum."'";
	list($forum_title, $locked_f) = mysql_fetch_row(mysql_query($forum_query));

	$page_title = array(
		'index.php?modname=forum&amp;op=forum' => $lang->def('_FORUM'),
		'index.php?modname=forum&amp;op=thread&amp;idForum='.$id_forum => $forum_title,
		$lang->def('_MOD_THREAD')
	);

	$GLOBALS['page']->add(
		getTitleArea($page_title, 'forum')
		.'<div class="std_block">'
		.( isset($_GET['search'])
			? getBackUi('index.php?modname=forum&op=search&amp;ini='.$ini, $lang->def('_BACK'))
			: getBackUi('index.php?modname=forum&amp;op=thread&amp;idForum='.$id_forum, $lang->def('_BACK'))
		)
		.Form::openForm('form_forum', 'index.php?modname=forum&amp;op=upthread', false, false, 'multipart/form-data')
		.Form::openElementSpace()

		.Form::getHidden('search', 'search', (isset($_GET['search']) ? '1' : '0' ) )
		.Form::getHidden('ini', 'ini', importVar('ini') )
		.Form::getHidden('idThread', 'idThread', $id_thread)
		.Form::getTextfield($lang->def('_SUBJECT'), 'title', 'title', 255, $title)
		.Form::getTextarea($lang->def('_TEXTOF'), 'textof', 'textof', $textof)
	, 'content');
	if(checkPerm('upload', true)) {

		$GLOBALS['page']->add(Form::getFilefield($lang->def('_UPLOAD'), 'attach', 'attach'), 'content');
	}
	$GLOBALS['page']->add(
		Form::closeElementSpace()
		.Form::openButtonSpace()
		.Form::getButton('post_thread', 'post_thread', $lang->def('_SAVE'))
		.Form::getButton('undo', 'undo', $lang->def('_UNDO'))
		.Form::closeButtonSpace()
		.Form::closeForm()
		.'</div>', 'content');
}

function upthread() {
	checkPerm('view');

	$id_thread 	= importVar('idThread', true, 0);
	$ini 	= importVar('ini');
	$moderate 	= checkPerm('moderate', true);
	$mod_perm	= checkPerm('mod', true);

	$lang =& DoceboLanguage::createInstance('forum', 'cms');

	// retrive info about message
	$mess_query = "
	SELECT idMessage, author, attach
	FROM ".$GLOBALS['prefix_cms']."_forummessage
	WHERE  idThread = '".$id_thread."' AND generator = '1'";
	list($id_message, $author, $attach) = mysql_fetch_row(mysql_query($mess_query));
	if(isset($_POST['undo'])) {

		if($_POST['search'] == 1) jumpTo('index.php?modname=forum&op=search&amp;ini='.$ini);
		else jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;ini='.$ini);
	}

	if(!$moderate && !$mod_perm && ($author == getLogUserId()) ) die("You can't access");

	list($id_forum, $locked_t, $erased_t) = mysql_fetch_row(mysql_query("
	SELECT idForum, locked, erased
	FROM ".$GLOBALS['prefix_cms']."_forumthread
	WHERE idThread = '".$id_thread."'"));

	if($locked_t ||$erased_t && (!$mod_perm && !$moderate)) {
		if($_POST['search'] == 1) jumpTo('index.php?modname=forum&op=search&amp;ini='.$ini);
		else jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;ini='.$ini.'&amp;result=err_lock');
	}
	if($_POST['title'] == '') $_POST['title'] = substr(strip_tags($_POST['textof']), 0, 50).'...';

	$now = date("Y-m-d H:i:s");

	//save attachment
	$name_file = $attach;
	if($_FILES['attach']['name'] != '' && checkPerm('upload', true) ) {

		delete_file($attach);
		$name_file = save_file($_FILES['attach']);
	}
	$upd_mess_query = "
	UPDATE ".$GLOBALS['prefix_cms']."_forummessage
	SET title = '".$_POST['title']."',
		textof = '".$_POST['textof']."',
		attach = '".addslashes($name_file)."',
		modified_by = '".getLogUserId()."',
		modified_by_on = '".$now."'
	WHERE idMessage = '".$id_message."'";
	if(!mysql_query($upd_mess_query)) {

		delete_file($name_file);
		if($_POST['search'] == 1) jumpTo('index.php?modname=forum&op=search&amp;ini='.$ini);
		else jumpTo('index.php?modname=forum&op=thread&idForum='.$id_forum.'&amp;result=err_ins');
	}

	mysql_query("
	UPDATE ".$GLOBALS['prefix_cms']."_forumthread
	SET title = '".$_POST['title']."'
	WHERE idThread = '".$id_thread."'");
	if($_POST['search'] == 1) jumpTo('index.php?modname=forum&op=search&amp;ini='.$ini);
	else jumpTo('index.php?modname=forum&op=thread&idForum='.$id_forum.'&amp;result=ok');
}

//---------------------------------------------------------------------------//

function delthread() {

	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	$lang =& DoceboLanguage::createInstance('forum', 'cms');
	$id_thread = importVar('idThread', true, 0);
	$ini = importVar('ini');

	$thread_query = "
	SELECT idForum, title, last_post
	FROM ".$GLOBALS['prefix_cms']."_forumthread
	WHERE idThread = '".$id_thread."'";
	list($id_forum, $thread_title, $last_post) = mysql_fetch_row(mysql_query($thread_query));

	if(isset($_POST['undo'])) {
		if($_POST['search'] == 1) jumpTo('index.php?modname=forum&op=search&amp;ini='.$ini);
		else jumpTo('index.php?modname=forum&op=thread&idForum='.$id_forum);
	}
	if(isset($_POST['confirm'])) {

		$forum_query = "
		SELECT last_post
		FROM ".$GLOBALS['prefix_cms']."_forum
		WHERE idForum = '".$id_forum."'";
		list($last_post_forum) = mysql_fetch_row(mysql_query($forum_query));

		$mess_query = "
		SELECT attach
		FROM ".$GLOBALS['prefix_cms']."_forummessage
		WHERE idThread = '".$id_thread."'";
		$re_mess = mysql_query($mess_query);
		while(list($file) = mysql_fetch_row($re_mess)) {

			if($file != '') delete_file($file);
		}
		$post_deleted = mysql_num_rows($re_mess);
		if(!mysql_query("
		DELETE FROM ".$GLOBALS['prefix_cms']."_forummessage
		WHERE idThread = '".$id_thread."'"))
			if($_POST['search'] == 1) jumpTo('index.php?modname=forum&op=search&amp;ini='.$ini);
			else jumpTo('index.php?modname=forum&op=thread&idForum='.$id_forum.'&amp;result=err_del');

		if(!mysql_query("
		UPDATE ".$GLOBALS['prefix_cms']."_forum
		SET num_thread = num_thread - 1,
			num_post = num_post - ".$post_deleted
		.( $last_post_forum == $last_post ? " , last_post = '' " : " " )
		." WHERE idForum = '".$id_forum."'"))
			if($_POST['search'] == 1) jumpTo('index.php?modname=forum&op=search&amp;ini='.$ini);
			else jumpTo('index.php?modname=forum&op=thread&idForum='.$id_forum.'&amp;result=err_del');

		if(!mysql_query("
		DELETE FROM ".$GLOBALS['prefix_cms']."_forumthread
		WHERE idThread = '".$id_thread."'"))
			if($_POST['search'] == 1) jumpTo('index.php?modname=forum&op=search&amp;ini='.$ini);
			else jumpTo('index.php?modname=forum&op=thread&idForum='.$id_forum.'&amp;result=err_del');

		unsetNotify('thread', $id_thread);
		if($_POST['search'] == 1) jumpTo('index.php?modname=forum&op=search');
		else jumpTo('index.php?modname=forum&op=thread&idForum='.$id_forum.'&amp;result=ok');
	} else {

		$forum_query = "
		SELECT title
		FROM ".$GLOBALS['prefix_cms']."_forum
		WHERE idForum = '".$id_forum."'";
		list($forum_title) = mysql_fetch_row(mysql_query($forum_query));

		$page_title = array(
			'index.php?modname=forum&amp;op=forum' => $lang->def('_FORUM'),
			'index.php?modname=forum&amp;op=thread&amp;idForum='.$id_forum => $forum_title,
			$lang->def('_DEL_THREAD')
		);
		$GLOBALS['page']->add(
			getTitleArea($page_title, 'forum')
			.'<div class="std_block">'
			.Form::openForm('del_thread', 'index.php?modname=forum&amp;op=delthread')
			.Form::getHidden('idThread', 'idThread', $id_thread)
			.Form::getHidden('search', 'search', (isset($_GET['search']) ? '1' : '0' ) )
			.Form::getHidden('ini', 'ini', importVar('ini') )
			.getDeleteUi(
				$lang->def('_AREYOUSURE_THREAD'),
				'<span>'.$lang->def('_TITLE').' :</span> '.$thread_title,
				false,
				'confirm',
				'undo'
			)
			.Form::closeForm()
			.'</div>', 'content');
	}
}

//---------------------------------------------------------------------------//

// XXX: distance
function loadDistance( $date ) {

	// yyyy-mm-dd hh:mm:ss
	// 0123456789012345678
	$year 	= substr($date, 0, 4);
	$month 	= substr($date, 5, 2);
	$day	= substr($date, 8, 2);

	$hour 	= substr($date, 11, 2);
	$minute = substr($date, 14, 2);
	$second	= substr($date,17 , 2);

	$distance = time() - mktime($hour, $minute, $second, $month, $day, $year);
	//second -> minutes
	$distance = (int)($distance / 60);
	//< 1 hour print minutes
	if( ($distance >= 0 ) && ($distance < 60) ) return $distance.' '.def('_MINUTES');

	//minutes -> hour
	$distance = (int)($distance / 60);
	if( ($distance >= 0 ) && ($distance < 60) ) return $distance.' '.def('_HOURS');

	//hour -> day
	$distance = (int)($distance / 24);
	if( ($distance >= 0 ) && ($distance < 30 ) ) return $distance.' '.def('_DAYS');

	//echo > 1 month
	return def('_ONEMONTH');
}

function message() {
	checkPerm('view');

	require_once($GLOBALS['where_framework'].'/lib/lib.newtypeone.php');
	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	$lang =& DoceboLanguage::createInstance('forum', 'cms');
	$id_thread = importVar('idThread', true, 0);

	$moderate 	= checkPerm('moderate', true);
	$mod_perm	= checkPerm('mod', true);
	$acl_man 	=& $GLOBALS['current_user']->getAclManager();

	$tb 	= new TypeOne($GLOBALS['cms']['visuItem'], $lang->def('_CAPTION_FORUM_MESSAGE'), $lang->def('_SUMMARY_FORUM_MESSAGE'));
	$tb->initNavBar('ini', 'link');
	$tb->setLink('index.php?modname=forum&amp;op=message&amp;idThread='.$id_thread);
	$ini 	= $tb->getSelectedElement();
	$ini_page = $tb->getSelectedPage();

	// Some info about forum and thread
	$thread_query = "
	SELECT idForum, title, num_post, locked, erased
	FROM ".$GLOBALS['prefix_cms']."_forumthread
	WHERE idThread = '".$id_thread."'";
	list($id_forum, $thread_title, $tot_message, $locked_t, $erased_t) = mysql_fetch_row(mysql_query($thread_query));
	$forum_query = "
	SELECT title, locked
	FROM ".$GLOBALS['prefix_cms']."_forum
	WHERE idForum = '".$id_forum."'";
	list($forum_title, $locked_f) = mysql_fetch_row(mysql_query($forum_query));
	++$tot_message;

	//set as readed if needed
	if(isset($_SESSION['unreaded_forum'][$id_forum][$id_thread])) unset($_SESSION['unreaded_forum'][$id_forum][$id_thread]);

	if( ($ini == 0) && (!isset($_GET['result'])) ) {
		mysql_query("
		UPDATE ".$GLOBALS['prefix_cms']."_forumthread
		SET num_view = num_view + 1
		WHERE idThread = '".$id_thread."'");
	}
	$page_title = array(
		'index.php?modname=forum&amp;op=forum' => $lang->def('_FORUM'),
		'index.php?modname=forum&amp;op=thread&amp;idForum='.$id_forum => $forum_title,
		$thread_title
	);
	if($erased_t && !$mod_perm && !$moderate) {

		$GLOBALS['page']->add(
			getTitleArea($page_title, 'forum')
			.'<div class="std_block">'
			.$lang->def('_CANNOTENTER')
			.'</div>', 'content');
		return;
	}
	// Who have semantic evaluation
	$re_sema = mysql_query("
	SELECT DISTINCT idmsg
	FROM ".$GLOBALS['prefix_cms']."_forum_sema");
	while(list($msg_sema) = mysql_fetch_row($re_sema)) $forum_sema[$msg_sema] = 1;

	// Find post
	$messages 		= array();
	$authors 		= array();
	$authors_names	= array();
	$authors_info	= array();
	$re_message = mysql_query("
	SELECT idMessage, posted, title, textof, attach, locked, author, modified_by, modified_by_on
	FROM ".$GLOBALS['prefix_cms']."_forummessage
	WHERE idThread = '".$id_thread."'
	ORDER BY posted
	LIMIT $ini, ".$GLOBALS['cms']['visuItem']);
	while($record = mysql_fetch_assoc($re_message)) {

		$messages[$record['idMessage']] 	= $record;
		$authors[$record['author']] 		= $record['author'];
		if($record['modified_by'] != 0) {
			$authors[$record['modified_by']] 	= $record['modified_by'];
		}
	}
	$authors_names =& $acl_man->getUsers($authors);
	$level_name = getLevels();

	// Retriving level and number of post of the authors
	if(!empty($authors)) {

		$re_num_post = mysql_query("
		SELECT u.idUser, u.level, COUNT(*)
		FROM ".$GLOBALS['prefix_cms']."_forummessage AS m, ".$GLOBALS['prefix_cms']."_courseuser AS u
		WHERE m.author = u.idUser AND m.author IN ( ".implode($authors, ',')." )
		GROUP BY u.idUser, u.level");
		while( list($id_u, $level_u, $num_post_a) = mysql_fetch_row($re_num_post) ) {

			$authors_info[$id_u] = array( 'num_post' => $num_post_a, 'level' => $level_name[$level_u] );
		}
	}
	$type_h = array('forum_sender', 'forum_text');
	$cont_h = array($lang->def('_AUTHOR'), $lang->def('_TEXTOF'));
	$tb->setColsStyle($type_h);
	$tb->addHead($cont_h);

	// Compose messagges display
	$path = $GLOBALS['cms']['url'].$GLOBALS['where_files_relative'].'/doceboCms/'.$GLOBALS['cms']['pathphoto'];
	while(list($id_message, $message_info) = each($messages)) {

		// sender info
		$m_author = $message_info['author'];
		
		if($authors_names[$m_author][ACL_INFO_AVATAR] != '') $img_size = @getimagesize($path.$authors_names[$m_author][ACL_INFO_AVATAR]);
		
		$sender = '<div class="forum_author">'
			.( $authors_names[$m_author][ACL_INFO_LASTNAME].$authors_names[$m_author][ACL_INFO_FIRSTNAME] == '' ?
				$authors_names[$m_author][ACL_INFO_USERID] :
				$authors_names[$m_author][ACL_INFO_LASTNAME].' '.$authors_names[$m_author][ACL_INFO_FIRSTNAME] )
			.'</div>'
			.'<div class="forum_level">'.$lang->def('_LEVEL').' : '.$authors_info[$m_author]['level'].'</div>'
			.( $authors_names[$m_author][ACL_INFO_AVATAR] != '' 
				? '<img class="forum_avatar'.( $img_size[0] > 150 || $img_size[1] > 150 ? ' image_limit' : '' ).'" src="'.$path.$authors_names[$m_author][ACL_INFO_AVATAR].'" alt="'.$lang->def('_AVATAR').'" />' 
				: '' )
			.'<div class="forum_numpost">'.$lang->def('_NUMPOST').' : '
			.( isset($authors_info[$m_author]['num_post'])
				? $authors_info[$m_author]['num_post']
				: 0 )
			.'</div>'
			.'<img src="'.getPathImage().'standard/user.gif" alt="&gt;" />&nbsp;'
			.'<a href="index.php?modname=forum&amp;op=viewprofile&amp;idMessage='.$id_message.'&amp;ini='.$ini_page.'">'.$lang->def('_VIEWPROFILE').'</a>';

		// msg info
		$msgtext = '';

		$msgtext .= '<div class="forum_post_posted">'
			.$lang->def('_POSTED').' : '.$GLOBALS['regset']->databaseToRegional($message_info['posted'])
			.' ( '.loadDistance($message_info['posted']).' )'
			.'</div>';
		if($message_info['locked']) {

			$msgtext .= '<div class="forum_post_locked">'.$lang->def('_LOCKEDMESS').'</div>';
		} else {
			if($message_info['attach'] != '') {

				$msgtext .= '<div class="forum_post_attach">'
					.'<a href="index.php?modname=forum&amp;op=download&amp;id='.$id_message.'">'
					.$lang->def('_ATTACHMENT').' : '
					.'<img src="'.getPathImage('fw').mimeDetect($message_info['attach']).'" alt="'.$lang->def('_ATTACHMENT').'" /></a>'
					.'</div>';
			}
			$msgtext .= '<div class="forum_post_title">'.$lang->def('_SUBJECT').' : '.$message_info['title'].'</div>';
			$msgtext .= '<div class="forum_post_text">'
						.str_replace('[quote]', '<blockquote class="forum_quote">', str_replace('[/quote]', '</blockquote>', $message_info['textof']))
						.'</div>';

			if($message_info['modified_by'] != 0) {

				$modify_by = $message_info['modified_by'];
				$msgtext .= '<div class="forum_post_modified_by">'
						.$lang->def('_MODIFY_BY').' : '
						.( $authors_names[$modify_by][ACL_INFO_LASTNAME].$authors_names[$modify_by][ACL_INFO_FIRSTNAME] == '' ?
							$authors_names[$modify_by][ACL_INFO_USERID] :
							$authors_names[$modify_by][ACL_INFO_LASTNAME].' '.$authors_names[$modify_by][ACL_INFO_FIRSTNAME] )
						.' '.$lang->def('_MODIFY_BY_ON').' : '
						.$GLOBALS['regset']->databaseToRegional($message_info['modified_by_on'])
						.'</div>';
			}

			if($authors_names[$m_author][ACL_INFO_SIGNATURE] != '') {
				$msgtext .= '<div class="forum_post_sign_separator"></div>'
					.'<div class="forum_post_sign">'
					.$authors_names[$m_author][ACL_INFO_SIGNATURE]
					.'</div>';
			}
		}
		$content = array($sender, $msgtext);
		$tb->addBody($content);

		// some action that you can do with this message
		$action = '';
		if($sema_perm) {
			if(isset($forum_sema[$id_message])) $img_sema = 'sema_check';
			else $img_sema = 'sema';
			$action .= '<a href="index.php?modname=forum&amp;op=editsema&amp;idMessage='.$id_message.'&amp;ini='.$ini_page.'" '
					.'title="'.$lang->def('_EDITSEMA_TITLE').' : '.strip_tags($message_info['title']).'">'
				.'<img src="'.getPathImage().'forum/'.$img_sema.'.gif" alt="'.$lang->def('_MOD').' : '.strip_tags($message_info['title']).'" /> '
				.$lang->def('_SEMATAG').'</a> ';
		}
		if($moderate || $mod_perm) {
			if($message_info['locked']) {

				$action .= '<a href="index.php?modname=forum&amp;op=moderatemessage&amp;idMessage='.$id_message.'&amp;ini='.$ini_page.'" '
						.'title="'.$lang->def('_DEMODERATE_MESSAGE').' : '.strip_tags($message_info['title']).'">'
					.'<img src="'.getPathImage().'forum/demoderate.gif" alt="'.$lang->def('_ALT_DEMODERATE').' : '.strip_tags($message_info['title']).'" /> '
					.$lang->def('_DEMODERATE').'</a> ';
			} else {

				$action .= '<a href="index.php?modname=forum&amp;op=moderatemessage&amp;idMessage='.$id_message.'&amp;ini='.$ini_page.'" '
						.'title="'.$lang->def('_MODERATE_MESSAGE').' : '.strip_tags($message_info['title']).'">'
					.'<img src="'.getPathImage().'forum/moderate.gif" alt="'.$lang->def('_ALT_MODERATE').' : '.strip_tags($message_info['title']).'" /> '
					.$lang->def('_MODERATE').'</a> ';
			}
		}
		if((!$locked_t && !$locked_f) || $mod_perm || $moderate) {
			$action .= '<a href="index.php?modname=forum&amp;op=addmessage&amp;idThread='.$id_thread.'&amp;idMessage='.$id_message.'&amp;ini='.$ini_page.'" '
					.'title="'.$lang->def('_REPLY_TITLE').' : '.strip_tags($message_info['title']).'">'
				.'<img src="'.getPathImage().'forum/reply.gif" alt="'.$lang->def('_ALT_REPLY').' : '.strip_tags($message_info['title']).'" /> '
				.$lang->def('_QUOTE').'</a>';
		}
		if($moderate || $mod_perm || ($m_author == getLogUserId()) ) {

			$action .= '<a href="index.php?modname=forum&amp;op=modmessage&amp;idMessage='.$id_message.'&amp;ini='.$ini_page.'" '
					.'title="'.$lang->def('_MOD_MESSAGE').' : '.strip_tags($message_info['title']).'">'
				.'<img src="'.getPathImage().'standard/mod.gif" alt="'.$lang->def('_MOD').' : '.strip_tags($message_info['title']).'" /> '
				.$lang->def('_MOD').'</a>'
				.'<a href="index.php?modname=forum&amp;op=delmessage&amp;idMessage='.$id_message.'&amp;ini='.$ini_page.'" '
					.'title="'.$lang->def('_DEL_MESSAGE').' : '.strip_tags($message_info['title']).'">'
				.'<img src="'.getPathImage().'standard/rem.gif" alt="'.$lang->def('_DEL').' : '.strip_tags($message_info['title']).'" /> '
				.$lang->def('_DEL').'</a> ';
		}
		$tb->addBodyExpanded($action, 'forum_action');
	}
	if( (!$locked_t && !$locked_f) || $mod_perm || $moderate ) {

		$tb->addActionAdd(
			'<a href="index.php?modname=forum&amp;op=addmessage&amp;idThread='.$id_thread.'&amp;ini='.$ini_page.'" title="'.$lang->def('_ADDMESSAGET').'">'
			.'<img src="'.getPathImage().'standard/add.gif" alt="'.$lang->def('_ADD').'" /> '
			.$lang->def('_REPLY_TO_THIS_THREAD').'</a>'
		);
	}
	$GLOBALS['page']->add(
		getTitleArea($page_title, 'forum')
		.'<div class="std_block">'
		.Form::openForm('search_forum', 'index.php?modname=forum&amp;op=search&amp;idThread='.$id_thread)
		.'<div class="search_mask form_line_l">'
		.'<label for="search_arg">'.$lang->def('_SEARCH_LABEL').'</label> '
		.Form::getInputTextfield(	'textfield_nowh',
									'search_arg',
									'search_arg',
									$lang->def('_SEARCH'),
									$lang->def('_SEARCH'), 255, '' )
		.' <input class="button_nowh" type="submit" id="search_button" name="search_button" value="'.$lang->def('_SEARCH').'" />'
		.'</div>'
		.Form::closeForm(), 'content');

	// NOTE: If notify request register it
	require_once($GLOBALS['where_framework'].'/lib/lib.usernotifier.php');

	$can_notify = usernotifier_getUserEventStatus(getLogUserId(), 'ForumNewResponse');

	if(isset($_GET['notify']) && $can_notify) {
		if(issetNotify('thread', $id_thread, getLogUserId())) {
			$re = unsetNotify('thread', $id_thread, getLogUserId());
			$is_notify = !$re;
		} else {
			$re = setNotify('thread', $id_thread, getLogUserId());
			$is_notify = $re;
		}
		if($re) $GLOBALS['page']->add(getResultUi($lang->def('_NOTIFY_CHANGE_STATUS_CORRECT')), 'content');
		else $GLOBALS['page']->add(getErrorUi($lang->def('_NOTIFY_CHANGE_STATUS_FAILED')), 'content');
	} elseif($can_notify) {
		$is_notify = issetNotify('thread', $id_thread, getLogUserId());
	}

	$text_inner = '';
	if($can_notify) {

		$text_inner .= '<li><a href="index.php?modname=forum&amp;op=message&amp;notify=1&amp;idThread='.$id_thread.'&amp;ini='.$ini_page.'" '
		.( !$is_notify ?
			'title="'.$lang->def('_NOTIFY_ME_THREAD_TITLE').'">'
			.'<img src="'.getPathImage().'forum/notify.gif" alt="'.$lang->def('_NOTIFY').'" /> '.$lang->def('_NOTIFY_ME_THREAD').'</a> '
			:
			'title="'.$lang->def('_UNNOTIFY_ME_THREAD_TITLE').'">'
			.'<img src="'.getPathImage().'forum/unnotify.gif" alt="'.$lang->def('_UNNOTIFY').'" /> '.$lang->def('_UNNOTIFY_ME_THREAD').'</a> '
		).'</li>';
	}
	if($moderate) {

		$text_inner .= '<li><a href="index.php?modname=forum&amp;op=modstatusthread&amp;idThread='.$id_thread.'&amp;ini='.$ini_page.'">'
			.( $locked_t
				?'<img src="'.getPathImage().'forum/thread.gif" alt="'.$lang->def('_FREE').'" /> '.$lang->def('_FREETHREAD')
				: '<img src="'.getPathImage().'forum/thread_locked.gif" alt="'.$lang->def('_LOCKTHREAD').'" /> '.$lang->def('_LOCKTHREAD') )
			.'</a></li>';
	}
	if($mod_perm) {

		$text_inner .= '<li><a href="index.php?modname=forum&amp;op=changeerased&amp;idThread='.$id_thread.'&amp;ini='.$ini_page.'">'
			.( $erased_t
				?'<img src="'.getPathImage().'forum/thread.gif" alt="'.$lang->def('_FREE').'" /> '.$lang->def('_UNERASE')
				: '<img src="'.getPathImage().'forum/thread_erased.gif" alt="'.$lang->def('_DEL').'" /> '.$lang->def('_ERASE') )
			.'</a></li>';
	}
	if($text_inner != '') {
		$GLOBALS['page']->add('<div class="forum_action_top"><ul class="adjac_link">'.$text_inner.'</ul></div>', 'content');
	}
	$GLOBALS['page']->add($tb->getTable(), 'content');
	if($text_inner != '') {
		$GLOBALS['page']->add('<div class="forum_action_bottom"><ul class="adjac_link">'.$text_inner.'</ul></div>'	, 'content');
	}
	$GLOBALS['page']->add(
		$tb->getNavBar($ini, $tot_message)
		.'</div>', 'content');
}

//---------------------------------------------------------------------------//

function moderatemessage() {
	if(!checkPerm('moderate', true) && !checkPerm('mod', true)) die("You can't access");

	list( $id_thread, $lock ) = mysql_fetch_row(mysql_query("
	SELECT idThread, locked
	FROM ".$GLOBALS['prefix_cms']."_forummessage
	WHERE idMessage = '".(int)$_GET['idMessage']."'"));

	if($lock == 1) $new_status = 0;
	else $new_status = 1;

	mysql_query("
	UPDATE ".$GLOBALS['prefix_cms']."_forummessage
	SET locked = '$new_status'
	WHERE idMessage = '".(int)$_GET['idMessage']."'");

	jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&ini='.$_GET['ini']);
}

function modstatusthread() {
	if(!checkPerm('moderate', true) && !checkPerm('mod', true)) die("You can't access");

	$id_thread 		= importVar('idThread', true, 0);

	list( $idF, $lock ) = mysql_fetch_row(mysql_query("
	SELECT idForum, locked
	FROM ".$GLOBALS['prefix_cms']."_forumthread
	WHERE idThread = '".$id_thread ."'"));

	if($lock == 1) $new_status = 0;
	else $new_status = 1;

	mysql_query("
	UPDATE ".$GLOBALS['prefix_cms']."_forumthread
	SET locked = '$new_status'
	WHERE idThread = '".$id_thread ."'");

	jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&ini='.$_GET['ini']);
}

function changeerase() {
	if(!checkPerm('moderate', true) && !checkPerm('mod', true)) die("You can't access");

	$id_thread 		= importVar('idThread', true, 0);

	list( $idF, $erased ) = mysql_fetch_row(mysql_query("
	SELECT idForum, erased
	FROM ".$GLOBALS['prefix_cms']."_forumthread
	WHERE idThread = '".$id_thread."'"));

	if($erased == 1) $new_status = 0;
	else $new_status = 1;

	mysql_query("
	UPDATE ".$GLOBALS['prefix_cms']."_forumthread
	SET erased = '$new_status'
	WHERE idThread = '".$id_thread."'");

	jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&ini='.$_GET['ini']);
}

//---------------------------------------------------------------------------//

function showMessageForAdd($id_thread, $how_much) {

	require_once($GLOBALS['where_framework'].'/lib/lib.newtypeone.php');
	$lang =& DoceboLanguage::createInstance('forum', 'cms');

	$acl_man 	=& $GLOBALS['current_user']->getAclManager();

	$tb 	= new TypeOne($GLOBALS['cms']['visuItem'], $lang->def('_CAPTION_FORUM_MESSAGE_ADD'), $lang->def('_SUMMARY_FORUM_MESSAGE_ADD'));

	// Find post
	$messages 		= array();
	$authors 		= array();
	$authors_names	= array();
	$authors_info	= array();
	$re_message = mysql_query("
	SELECT idMessage, posted, title, textof, attach, locked, author, modified_by
	FROM ".$GLOBALS['prefix_cms']."_forummessage
	WHERE idThread = '".$id_thread."'
	ORDER BY posted DESC
	LIMIT 0, ".$how_much);
	while($record = mysql_fetch_assoc($re_message)) {

		$messages[$record['idMessage']] 	= $record;
		$authors[$record['author']] 		= $record['author'];
		if($record['modified_by'] != 0) {
			$authors[$record['modified_by']] 	= $record['modified_by'];
		}
	}
	$authors_names =& $acl_man->getUsers($authors);
	$level_name = getLevels();

	// Retriving level and number of post of th authors
	$re_num_post = mysql_query("
	SELECT u.idUser, u.level, COUNT(*)
	FROM ".$GLOBALS['prefix_cms']."_forummessage AS m, ".$GLOBALS['prefix_cms']."_courseuser AS u
	WHERE m.author = u.idUser AND m.author IN ( ".implode($authors, ',')." )
	GROUP BY u.idUser, u.level");
	while( list($id_u, $level_u, $num_post_a) = mysql_fetch_row($re_num_post) ) {

		$authors_info[$id_u] = array( 'num_post' => $num_post_a, 'level' => $level_name[$level_u] );
	}
	$type_h = array('forum_sender', 'forum_text');
	$cont_h = array($lang->def('_AUTHOR'), $lang->def('_TEXTOF'));
	$tb->setColsStyle($type_h);
	$tb->addHead($cont_h);

	// Compose messagges display
	$path = $GLOBALS['cms']['url'].$GLOBALS['where_files_relative'].'/doceboCms/'.$GLOBALS['cms']['pathphoto'];
	while(list($id_message, $message_info) = each($messages)) {

		// sender info
		$m_author = $message_info['author'];
		
		if($authors_names[$m_author][ACL_INFO_AVATAR] != '') $img_size = @getimagesize($path.$authors_names[$m_author][ACL_INFO_AVATAR]);
		
		$sender = '<div class="forum_author">'
			.( $authors_names[$m_author][ACL_INFO_LASTNAME].$authors_names[$m_author][ACL_INFO_FIRSTNAME] == '' ?
				$authors_names[$m_author][ACL_INFO_USERID] :
				$authors_names[$m_author][ACL_INFO_LASTNAME].' '.$authors_names[$m_author][ACL_INFO_FIRSTNAME] )
			.'</div>'
			.'<div class="forum_level">'.$lang->def('_LEVEL').' : '.$authors_info[$m_author]['level'].'</div>'
			.( $authors_names[$m_author][ACL_INFO_AVATAR] != '' 
				? '<img class="forum_avatar'.( $img_size[0] > 150 || $img_size[1] > 150 ? ' image_limit' : '' ).'" src="'.$path.$authors_names[$m_author][ACL_INFO_AVATAR].'" alt="'.$lang->def('_AVATAR').'" />' 
				: '' )
			.'<div class="forum_numpost">'.$lang->def('_NUMPOST').' : '
			.( isset($authors_info[$m_author]['num_post'])
				? $authors_info[$m_author]['num_post']
				: 0 )
			.'</div>'
			.'<img src="'.getPathImage().'standard/user.gif" alt="&gt;" />&nbsp;'
			.'<a href="index.php?modname=forum&amp;op=viewprofile&amp;idMessage='.$id_message.'">'.$lang->def('_VIEWPROFILE').'</a>';

		// msg info
		$msgtext = '';

		$msgtext .= '<div class="forum_post_posted">'
			.$lang->def('_POSTED').' : '.$GLOBALS['regset']->databaseToRegional($message_info['posted'])
			.' ( '.loadDistance($message_info['posted']).' )'
			.'</div>';
		if($message_info['locked']) {

			$msgtext .= '<div class="forum_post_locked">'.$lang->def('_LOCKEDMESS').'</div>';
		} else {
			$msgtext .= '<div class="forum_post_title">'.$lang->def('_SUBJECT').' : '.$message_info['title'].'</div>';

			$msgtext .= '<div class="forum_post_text">'
				.str_replace('[quote]', '<blockquote class="forum_quote">', str_replace('[/quote]', '</blockquote>', $message_info['textof']))
				.'</div>';

			if($message_info['modified_by'] != 0) {

				$modify_by = $message_info['modified_by'];
				$msgtext .= '<div class="forum_post_modified_by">'
						.$lang->def('_MODIFY_BY').' : '
						.( $authors_names[$modify_by][ACL_INFO_LASTNAME].$authors_names[$modify_by][ACL_INFO_FIRSTNAME] == '' ?
							$authors_names[$modify_by][ACL_INFO_USERID] :
							$authors_names[$modify_by][ACL_INFO_LASTNAME].' '.$authors_names[$modify_by][ACL_INFO_FIRSTNAME] ).'</div>';
			}
			if($authors_names[$m_author][ACL_INFO_SIGNATURE] != '') {
				$msgtext .= '<div class="forum_post_sign_separator"></div>'
					.'<div class="forum_post_sign">'
					.$authors_names[$m_author][ACL_INFO_SIGNATURE]
					.'</div>';
			}
		}
		$content = array($sender, $msgtext);
		$tb->addBody($content);
	}
	$GLOBALS['page']->add($tb->getTable(), 'content');
}

function addmessage() {
	checkPerm('view');

	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	$lang =& DoceboLanguage::createInstance('forum', 'cms');
	$id_thread 		= importVar('idThread', true, 0);
	$id_message 	= importVar('idMessage', true, 0);
	$ini = importVar('ini');

	$moderate 	= checkPerm('moderate', true);
	$mod_perm	= checkPerm('mod', true);
	$acl_man 	=& $GLOBALS['current_user']->getAclManager();

	// Some info about forum and thread
	$thread_query = "
	SELECT idForum, title , locked, erased
	FROM ".$GLOBALS['prefix_cms']."_forumthread
	WHERE idThread = '".$id_thread."'";
	list($id_forum, $thread_title, $locked_t, $erased_t) = mysql_fetch_row(mysql_query($thread_query));
	$forum_query = "
	SELECT title, locked
	FROM ".$GLOBALS['prefix_cms']."_forum
	WHERE idForum = '".$id_forum."'";
	list($forum_title, $locked_f) = mysql_fetch_row(mysql_query($forum_query));

	$page_title = array(
		'index.php?modname=forum&amp;op=forum' => $lang->def('_FORUM'),
		'index.php?modname=forum&amp;op=thread&amp;idForum='.$id_forum => $forum_title,
		'index.php?modname=forum&amp;op=message&amp;idThread='.$id_thread.'&amp;ini='.$ini => $thread_title,
		$lang->def('_REPLY_TO_THIS_THREAD')
	);
	if($erased_t && !$mod_perm && !$moderate) {

		$GLOBALS['page']->add(
			getTitleArea($page_title, 'forum')
			.'<div class="std_block">'
			.$lang->def('_CANNOTENTER')
			.'</div>', 'content');
		return;
	}
	// retrive info about quoting
	if($id_message != 0) {

		$message_query = "
		SELECT title, textof
		FROM ".$GLOBALS['prefix_cms']."_forummessage
		WHERE idMessage = '".$id_message."'";
		list($m_title, $m_textof) = mysql_fetch_row(mysql_query($message_query));
	}
	$GLOBALS['page']->add(
		getTitleArea($page_title, 'forum')
		.'<div class="std_block">'
		.getBackUi('index.php?modname=forum&amp;op=message&amp;idThread='.$id_thread.'&amp;ini='.$ini, $lang->def('_BACK'))
		.Form::openForm('form_forum', 'index.php?modname=forum&amp;op=insmessage', false, false, 'multipart/form-data')
		.Form::openElementSpace()

		.Form::getHidden('idThread', 'idThread', $id_thread)
		.Form::getHidden('idMessage', 'idMessage', $id_message)
		.Form::getHidden('ini', 'ini', $ini)
		.Form::getTextfield($lang->def('_SUBJECT'), 'title', 'title', 255, ($id_message != '' ? $lang->def('_RE').' '.$m_title : '' ))
		.Form::getTextarea($lang->def('_TEXTOF'), 'textof', 'textof', ($id_message != '' ? '[quote]'.$m_textof.'[/quote]' : '' ))
	, 'content');
	if(checkPerm('upload', true)) {

		$GLOBALS['page']->add(Form::getFilefield($lang->def('_UPLOAD'), 'attach', 'attach'), 'content');
	}
	$GLOBALS['page']->add(
		Form::closeElementSpace()
		.Form::openButtonSpace()
		.Form::getButton('post_thread', 'post_thread', $lang->def('_SEND'))
		.Form::getButton('undo', 'undo', $lang->def('_UNDO'))
		.Form::closeButtonSpace()
	, 'content');
	showMessageForAdd($id_thread, 3);
	$GLOBALS['page']->add(
		Form::openButtonSpace()
		.Form::getButton('post_thread_2', 'post_thread', $lang->def('_SEND'))
		.Form::getButton('undo_2', 'undo', $lang->def('_UNDO'))
		.Form::closeButtonSpace()
		.Form::closeForm()
		.'</div>', 'content');
}

function insmessage() {
	checkPerm('view');

	$id_thread 	= importVar('idThread', true, 0);
	$id_message 	= importVar('idMessage', true, 0);
	$ini 	= importVar('ini');
	$moderate 	= checkPerm('moderate', true);
	$mod_perm	= checkPerm('mod', true);

	if(isset($_POST['undo'])) jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;ini='.$ini);

	$lang =& DoceboLanguage::createInstance('forum', 'cms');

	// Some info about forum and thread
	list($id_forum, $thread_title, $locked_t, $erased_t) = mysql_fetch_row(mysql_query("
	SELECT idForum, title, locked, erased
	FROM ".$GLOBALS['prefix_cms']."_forumthread
	WHERE idThread = '".$id_thread."'"));
	$forum_query = "
	SELECT title
	FROM ".$GLOBALS['prefix_cms']."_forum
	WHERE idForum = '".$id_forum."'";
	list($forum_title) = mysql_fetch_row(mysql_query($forum_query));

	$locked_f = false;
	if(!checkPerm('moderate', true)) {

		$query_view_forum = "
		SELECT idMember, locked
		FROM ".$GLOBALS['prefix_cms']."_forum AS f LEFT JOIN
				".$GLOBALS['prefix_cms']."_forum_access AS fa
					ON ( f.idForum = fa.idForum )
		WHERE f.idForum = '".$id_forum."'";
		$re_forum = mysql_query($query_view_forum);
		while(list($id_m, $lock_s, $erase_s) = mysql_fetch_row($re_forum)) {

			$locked_f = $lock_s;
			if($id_m != NULL) $members[] = $id_m;
		}
	}
	$continue = false;
	if(!isset($members)) $continue = true;
	else {
		$acl 	=& $GLOBALS['current_user']->getAcl();
		$all_user_idst = $acl->getSTGroupsST(getLogUserId());
		$all_user_idst[] = getLogUserId();

		$can_access = array();
		$can_access = array_intersect($members, $all_user_idst);
		if(!empty($can_access)) $continue = true;
	}
	if(!$continue) jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;ini='.$ini.'&amp;result=err_cannotsee');
	if($locked_f || $locked_t ||$erased_t && (!$mod_perm && !$moderate)) {
		jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;ini='.$ini.'&amp;result=err_lock');
	}
	if($_POST['title'] == '') $_POST['title'] = substr(strip_tags($_POST['textof']), 0, 50).'...';

	$now = date("Y-m-d H:i:s");

	//save attachment
	$name_file = '';
	if($_FILES['attach']['name'] != '' && checkPerm('upload', true) ) {
		$name_file = save_file($_FILES['attach']);
	}
	$answer_tree = '';
	if($id_message != 0) {

		list($answer_tree) = mysql_fetch_row(mysql_query("
		SELECT answer_tree
		FROM ".$GLOBALS['prefix_cms']."_forummessage
		WHERE idMessage = '".$id_message."'"));
	}
	$answer_tree .= '/'.$now;

	$ins_mess_query = "
	INSERT INTO ".$GLOBALS['prefix_cms']."_forummessage
	( idThread, title, textof, author, posted, answer_tree, attach ) VALUES
	( 	'".$id_thread."',
		'".$_POST['title']."',
		'".$_POST['textof']."',
		'".getLogUserId()."',
		'".$now."',
		'".$answer_tree."',
		'".addslashes($name_file)."' )";
	if(!mysql_query($ins_mess_query)) {

		delete_file($name_file);
		jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;ini='.$ini.'&amp;result=err_ins');
	}
	list($new_id_message) = mysql_fetch_row(mysql_query("SELECT LAST_INSERT_ID()"));

	mysql_query("
	UPDATE ".$GLOBALS['prefix_cms']."_forum
	SET num_post = num_post + 1,
		last_post = '".$new_id_message."'
	WHERE idForum = '".$id_forum."'");

	mysql_query("
	UPDATE ".$GLOBALS['prefix_cms']."_forumthread
	SET num_post = num_post + 1,
		last_post = '".$new_id_message."'
	WHERE idThread = '".$id_thread."'");

	$course_name = $GLOBALS['course_descriptor']->getValue('name');

	launchNotify('thread', $id_thread, $lang->def('_NEW_MESSAGE'),
		$lang->def('_SUBJECT_NOTIFY_MESSAGE'),
		$lang->def('_NEW_MESSAGE_INSERT_IN_THREAD')."\n".$GLOBALS['cms']['url']." : ".$course_name.' > '.$forum_title.' > '.$thread_title
	);
	jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;ini='.$ini.'&amp;result=ok');
}

//---------------------------------------------------------------------------//

function modmessage() {
	checkPerm('view');

	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	$lang =& DoceboLanguage::createInstance('forum', 'cms');
	$id_message 	= importVar('idMessage', true, 0);
	$ini 			= importVar('ini');

	$moderate 	= checkPerm('moderate', true);
	$mod_perm	= checkPerm('mod', true);
	$acl_man 	=& $GLOBALS['current_user']->getAclManager();

	// retrive info about message
	$mess_query = "
	SELECT idThread, title, textof, author
	FROM ".$GLOBALS['prefix_cms']."_forummessage
	WHERE  idMessage = '".$id_message."'";
	list($id_thread, $title, $textof, $author) = mysql_fetch_row(mysql_query($mess_query));

	if(!$moderate && !$mod_perm && ($author == getLogUserId()) ) die("You can't access");

	// Some info about forum and thread
	$thread_query = "
	SELECT idForum, title , locked, erased
	FROM ".$GLOBALS['prefix_cms']."_forumthread
	WHERE idThread = '".$id_thread."'";
	list($id_forum, $thread_title, $locked_t, $erased_t) = mysql_fetch_row(mysql_query($thread_query));
	$forum_query = "
	SELECT title, locked
	FROM ".$GLOBALS['prefix_cms']."_forum
	WHERE idForum = '".$id_forum."'";
	list($forum_title, $locked_f) = mysql_fetch_row(mysql_query($forum_query));

	$page_title = array(
		'index.php?modname=forum&amp;op=forum' => $lang->def('_FORUM'),
		'index.php?modname=forum&amp;op=thread&amp;idForum='.$id_forum => $forum_title,
		'index.php?modname=forum&amp;op=message&amp;idThread='.$id_thread.'&amp;ini='.$ini => $thread_title,
		$lang->def('_MOD_MESSAGE')
	);
	if($erased_t && !$mod_perm && !$moderate) {

		$GLOBALS['page']->add(
			getTitleArea($page_title, 'forum')
			.'<div class="std_block">'
			.$lang->def('_CANNOTENTER')
			.'</div>', 'content');
		return;
	}

	$GLOBALS['page']->add(
		getTitleArea($page_title, 'forum')
		.'<div class="std_block">'
		.getBackUi('index.php?modname=forum&amp;op=message&amp;idThread='.$id_thread.'&amp;ini='.$ini, $lang->def('_BACK'))
		.Form::openForm('form_forum', 'index.php?modname=forum&amp;op=upmessage', false, false, 'multipart/form-data')
		.Form::openElementSpace()

		.Form::getHidden('idMessage', 'idMessage', $id_message)
		.Form::getHidden('ini', 'ini', $ini)
		.Form::getTextfield($lang->def('_SUBJECT'), 'title', 'title', 255, $title)
		.Form::getTextarea($lang->def('_TEXTOF'), 'textof', 'textof', $textof)
	, 'content');
	if(checkPerm('upload', true)) {

		$GLOBALS['page']->add(Form::getFilefield($lang->def('_UPLOAD'), 'attach', 'attach'), 'content');
	}
	$GLOBALS['page']->add(
		Form::closeElementSpace()
		.Form::openButtonSpace()
		.Form::getButton('post_thread', 'post_thread', $lang->def('_SAVE'))
		.Form::getButton('undo', 'undo', $lang->def('_UNDO'))
		.Form::closeButtonSpace()
	, 'content');
	showMessageForAdd($id_thread, 3);
	$GLOBALS['page']->add(
		Form::openButtonSpace()
		.Form::getButton('post_thread_2', 'post_thread', $lang->def('_SAVE'))
		.Form::getButton('undo_2', 'undo', $lang->def('_UNDO'))
		.Form::closeButtonSpace()
		.Form::closeForm()
		.'</div>', 'content');
}

function upmessage() {
	checkPerm('view');

	$id_message 	= importVar('idMessage', true, 0);
	$ini 	= importVar('ini');
	$moderate 	= checkPerm('moderate', true);
	$mod_perm	= checkPerm('mod', true);

	$lang =& DoceboLanguage::createInstance('forum', 'cms');

	// retrive info about message
	$mess_query = "
	SELECT idThread, author, attach, generator
	FROM ".$GLOBALS['prefix_cms']."_forummessage
	WHERE  idMessage = '".$id_message."'";
	list($id_thread, $author, $attach, $is_generator) = mysql_fetch_row(mysql_query($mess_query));
	if(isset($_POST['undo'])) jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;ini='.$ini);

	if(!$moderate && !$mod_perm && ($author == getLogUserId()) ) die("You can't access");

	list($id_forum, $locked_t, $erased_t) = mysql_fetch_row(mysql_query("
	SELECT idForum, locked, erased
	FROM ".$GLOBALS['prefix_cms']."_forumthread
	WHERE idThread = '".$id_thread."'"));

	if($locked_t ||$erased_t && (!$mod_perm && !$moderate)) {
		jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;ini='.$ini.'&amp;result=err_lock');
	}
	if($_POST['title'] == '') $_POST['title'] = substr(strip_tags($_POST['textof']), 0, 50).'...';

	$now = date("Y-m-d H:i:s");

	//save attachment
	$name_file = $attach;
	if($_FILES['attach']['name'] != '' && checkPerm('upload', true) ) {

		delete_file($attach);
		$name_file = save_file($_FILES['attach']);
	}
	$upd_mess_query = "
	UPDATE ".$GLOBALS['prefix_cms']."_forummessage
	SET title = '".$_POST['title']."',
		textof = '".$_POST['textof']."',
		attach = '".addslashes($name_file)."',
		modified_by = '".getLogUserId()."',
		modified_by_on = '".$now."'
	WHERE idMessage = '".$id_message."'";
	if(!mysql_query($upd_mess_query)) {

		delete_file($name_file);
		jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;ini='.$ini.'&amp;result=err_ins');
	}

	if($is_generator) {
		mysql_query("
		UPDATE ".$GLOBALS['prefix_cms']."_forumthread
		SET title = '".$_POST['title']."'
		WHERE idThread = '".$id_thread."'");
	}
	jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;ini='.$ini.'&amp;result=ok');
}

//---------------------------------------------------------------------------//

function delmessage() {
	checkPerm('view');

	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	$lang =& DoceboLanguage::createInstance('forum', 'cms');

	$id_message 	= importVar('idMessage', true, 0);
	$moderate 		= checkPerm('moderate', true);
	$mod_perm		= checkPerm('mod', true);
	$ini 	= importVar('ini');

	$mess_query = "
	SELECT idThread, title, textof, author, attach, answer_tree
	FROM ".$GLOBALS['prefix_cms']."_forummessage
	WHERE  idMessage = '".$id_message."'";
	list($id_thread, $title, $textof, $author, $file, $answer_tree) = mysql_fetch_row(mysql_query($mess_query));

	if(!$moderate && !$mod_perm && ($author == getLogUserId()) ) die("You can't access");

	$thread_query = "
	SELECT idForum, title, num_post
	FROM ".$GLOBALS['prefix_cms']."_forumthread
	WHERE idThread = '".$id_thread."'";
	list($id_forum, $thread_title, $num_post) = mysql_fetch_row(mysql_query($thread_query));

	$forum_query = "
	SELECT title
	FROM ".$GLOBALS['prefix_cms']."_forum
	WHERE idForum = '".$id_forum."'";
	list($forum_title) = mysql_fetch_row(mysql_query($forum_query));

	if(isset($_POST['undo'])) jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;ini='.$ini);
	if(isset($_POST['confirm'])) {

		$new_answer_tree = substr($answer_tree, 0, -21);
		if(!mysql_query("
		UPDATE ".$GLOBALS['prefix_cms']."_forummessage
		SET answer_tree = CONCAT( '$new_answer_tree', SUBSTRING( answer_tree FROM ".strlen($answer_tree)." ) )
		WHERE answer_tree LIKE '".$answer_tree."/%'"))
			jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;result=err_del');

		if(!mysql_query("
		UPDATE ".$GLOBALS['prefix_cms']."_forum
		SET num_post = num_post - 1
			".( $num_post == 0 ? " ,num_thread = num_thread - 1 " : " " )."
		WHERE idForum = '".$id_forum."'"))
			jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;result=err_del');

		if($num_post == 0) {

			if(!mysql_query("
			DELETE FROM ".$GLOBALS['prefix_cms']."_forumthread
			WHERE idThread = '".$id_thread."'"))
				jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;result=err_del');
			unsetNotify('thread', $id_thread);
		} else {

			if(!mysql_query("
			UPDATE ".$GLOBALS['prefix_cms']."_forumthread
			SET num_post = num_post - 1
			WHERE idThread = '".$id_thread."'"))
				jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;result=err_del');
		}
		delete_file($file);

		if(!mysql_query("
		DELETE FROM ".$GLOBALS['prefix_cms']."_forummessage
		WHERE idMessage = '".$id_message."'"))
			jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;result=err_del');

		jumpTo('index.php?modname=forum&op=message&idThread='.$id_thread.'&amp;result=ok');
	} else {

		$page_title = array(
			'index.php?modname=forum&amp;op=forum' => $lang->def('_FORUM'),
			'index.php?modname=forum&amp;op=thread&amp;idForum='.$id_forum => $forum_title,
			'index.php?modname=forum&amp;op=message&amp;idThread='.$id_thread.'&amp;ini='.$ini => $thread_title,
			$lang->def('_DEL_MESSAGE')
		);
		$GLOBALS['page']->add(
			getTitleArea($page_title, 'forum')
			.'<div class="std_block">'
			.Form::openForm('del_thread', 'index.php?modname=forum&amp;op=delmessage')
			.Form::getHidden('idMessage', 'idMessage', $id_message)
			.Form::getHidden('ini', 'ini', $ini)
			.getDeleteUi(
				$lang->def('_AREYOUSURE_MESSAGE'),
				'<span>'.$lang->def('_SUBJECT').' :</span> '.$title.'<br />'
				.$textof,
				false,
				'confirm',
				'undo' )
			.Form::closeForm()
			.'</div>', 'content');
	}
}

//---------------------------------------------------------------------------//

function viewprofile() {
	checkPerm('view');

	require_once($GLOBALS['where_cms'].'/lib/lib.profile.php');
	$lang =& DoceboLanguage::createInstance('forum', 'cms');

	$id_message = importVar('idMessage');
	$ini = importVar('ini');

	list($id_thread, $idst_user) = mysql_fetch_row(mysql_query("
	SELECT idThread, author
	FROM ".$GLOBALS['prefix_cms']."_forummessage
	WHERE idMessage = '".(int)$_GET['idMessage']."'"));

	$page_title = array(
		'index.php?modname=forum&amp;op=forum' => $lang->def('_FORUM'),
		$lang->def('_VIEW_PROFILE')
	);
	$GLOBALS['page']->add(
		getTitleArea($page_title, 'forum')
		.'<div class="std_block">'
		.getBackUi('index.php?modname=forum&amp;op=message&amp;idThread='.$id_thread.'&amp;ini='.$ini, $lang->def('_BACK'))
		.getprofile($idst_user)
		.getBackUi('index.php?modname=forum&amp;op=message&amp;idThread='.$id_thread.'&amp;ini='.$ini, $lang->def('_BACK'))
		.'</div>', 'content');
}

//---------------------------------------------------------------------------//

function editsema() {
	checkPerm('sema');

	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');

	$id_message = importVar('idMessage');
	$ini = importVar('ini');

	$lang =& DoceboLanguage::createInstance('forum', 'cms');

	list($id_thread, $title, $textof, $idst_user) = mysql_fetch_row(mysql_query("
	SELECT idThread, title, textof, author
	FROM ".$GLOBALS['prefix_cms']."_forummessage
	WHERE idMessage = '".(int)$_GET['idMessage']."'"));

	$sema_values = array();
	$query_prev_entry = "
	SELECT *
	FROM ".$GLOBALS['prefix_cms']."_forum_sema
	WHERE idmsg = '".$id_message."'";
	$re_prev_entry = mysql_query($query_prev_entry);
	if (mysql_num_rows($re_prev_entry)) {

		while( $sema_info  = mysql_fetch_array($re_prev_entry)) {

			$sema_values[$sema_info['idsema']] = $sema_info['idsemaitem'];
		}
	}
	$query_sema = "
	SELECT *
	FROM ".$GLOBALS['prefix_cms']."_pagel_sema
	ORDER BY title";
	$re_sema = mysql_query($query_sema);

	$page_title = array(
		'index.php?modname=forum&amp;op=forum' => $lang->def('_FORUM'),
		$lang->def('_ASSIGN_SEMA_TAG')
	);
	$GLOBALS['page']->add(
		getTitleArea($page_title, 'forum')
		.'<div class="std_block">'
		.getBackUi('index.php?modname=forum&amp;op=message&amp;idThread='.$id_thread.'&amp;ini='.$ini, $lang->def('_BACK'))

		.Form::openForm('editsema', 'index.php?modname=forum&amp;op=savesema')
		.Form::openElementSpace()
		.Form::getHidden('idMessage', 'idMessage', $id_message)
		.Form::getHidden('ini', 'ini', $ini)
	, 'content');
	if(mysql_num_rows($re_sema) > 0) {

		while($pagel_sema = mysql_fetch_assoc($re_sema)) {

			$items = array();
			$query_item = "
			SELECT id, title
			FROM ".$GLOBALS['prefix_cms']."_pagel_sema_items
			WHERE idref = '".$pagel_sema['id']."'
			ORDER BY ord";
			$re_item = mysql_query($query_item);
			while(list($item_id, $text) = mysql_fetch_row($re_item)) {
				$items[$item_id] = $text;
			}
			$GLOBALS['page']->add(
				Form::getDropdown(	$pagel_sema['title'],
									'sema_'.$pagel_sema['id'],
									'sema['.$pagel_sema['id'].']',
									$items,
									( isset($sema_values[$pagel_sema['id']]) ? $sema_values[$pagel_sema['id']] : '' ) )
			, 'content');
		}
	}
	$GLOBALS['page']->add(
		Form::closeElementSpace()
		.Form::openButtonSpace()
		.Form::getButton('save', 'save', $lang->def('_SAVE'))
		.Form::getButton('undo', 'undo', $lang->def('_UNDO'))
		.Form::closeButtonSpace()
		.Form::closeForm()
		.'</div>', 'content');

}


//---------------------------------------------------------------------------//

function forumsearch() {
	checkPerm('view');

	if(isset($_POST['search_arg'])) {
		$_SESSION['forum']['search_arg'] = $_POST['search_arg'];
		$search_arg = importVar('search_arg');
	} else {
		$search_arg = $_SESSION['forum']['search_arg'];
	}
	$ord = importVar('ord');
	$mod_perm = checkPerm('mod');

	require_once($GLOBALS['where_framework'].'/lib/lib.newtypeone.php');
	require_once($GLOBALS['where_framework'].'/lib/lib.navbar.php');
	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');

	$lang =& DoceboLanguage::createInstance('forum', 'cms');

	$acl_man 	=& $GLOBALS['current_user']->getAclManager();

	if($mod_perm) {

		$query_view_forum = "
		SELECT DISTINCT idForum
		FROM ".$GLOBALS['prefix_cms']."_forum";
	} else {

		$acl 	=& $GLOBALS['current_user']->getAcl();
		$all_user_idst = $acl->getSTGroupsST(getLogUserId());
		$all_user_idst[] = getLogUserId();

		$query_view_forum = "
		SELECT DISTINCT f.idForum
		FROM ".$GLOBALS['prefix_cms']."_forum AS f
			LEFT JOIN ".$GLOBALS['prefix_cms']."_forum_access AS fa ON ( f.idForum = fa.idForum )
		WHERE
			( fa.idMember IS NULL OR fa.idMember IN (".implode($all_user_idst, ',')." )  ) ";

	}
	$forums = array();
	$re_forum = mysql_query($query_view_forum);
	while(list($id_f) = mysql_fetch_row($re_forum)) {
		$forums[] = $id_f;
	}
	if(empty($forums)) {

		$page_title = array(
			'index.php?modname=forum&amp;op=forum' => $lang->def('_FORUM'),
			$lang->def('_SEARCH_RESULT_FOR').' : '.$search_arg
		);
		$GLOBALS['page']->add(
			getTitleArea($page_title, 'forum')
			.'<div class="std_block">'
			.$lang->def('_NO_PLACEFORSEARCH')
			.'</div>', 'content');
	}
	$query_num_thread = "
	SELECT COUNT(DISTINCT t.idThread)
	FROM ".$GLOBALS['prefix_cms']."_forumthread AS t JOIN
			".$GLOBALS['prefix_cms']."_forummessage AS m
	WHERE t.idThread = m.idThread AND t.idForum IN ( ".implode($forums, ',')." )
		AND ( m.title LIKE '%".$search_arg."%' OR m.textof LIKE '%".$search_arg."%' ) ";
	list($tot_thread) = mysql_fetch_row(mysql_query($query_num_thread));

	$jump_url = 'index.php?modname=forum&amp;op=search';
	$nav_bar 	= new NavBar('ini', $GLOBALS['cms']['visuItem'], $tot_thread, 'link');
	$nav_bar->setLink($jump_url.'&amp;ord='.$ord);
	$ini 		= $nav_bar->getSelectedElement();
	$ini_page	= $nav_bar->getSelectedPage();

	$query_thread = "
	SELECT DISTINCT t.idThread, t.idForum, t.author AS thread_author, t.posted, t.title, t.num_post, t.num_view, t.locked, t.erased
	FROM ".$GLOBALS['prefix_cms']."_forumthread AS t JOIN
			".$GLOBALS['prefix_cms']."_forummessage AS m
	WHERE t.idThread = m.idThread AND t.idForum IN ( ".implode($forums, ',')." )
		AND ( m.title LIKE '%".$search_arg."%' OR m.textof LIKE '%".$search_arg."%' ) ";
	switch($ord) {
		case "obji"		: $query_thread .= " ORDER BY t.title DESC " ;	break;
		case "obj" 		: $query_thread .= " ORDER BY t.title " ;			break;
		case "authi"	: $query_thread .= " ORDER BY t.author DESC " ;	break;
		case "auth" 	: $query_thread .= " ORDER BY t.author " ;		break;
		case "posti" 	: $query_thread .= " ORDER BY m.posted " ;		break;
		case "post"		:
		default 		: {
			$ord = 'post';
			$query_thread .= " ORDER BY m.posted DESC " ;	break;
		}
	}
	$query_thread .= " LIMIT $ini, ".$GLOBALS['cms']['visuItem'];
	$re_thread = mysql_query($query_thread);
	echo mysql_error();
	$re_last_post = mysql_query("
	SELECT m.idThread, t.author AS thread_author, m.posted, m.title, m.author  AS mess_author, m.generator
	FROM ".$GLOBALS['prefix_cms']."_forumthread AS t LEFT JOIN
		".$GLOBALS['prefix_cms']."_forummessage AS m ON ( t.last_post = m.idMessage )
	WHERE t.idForum IN ( ".implode($forums, ',')." )");
	while(list($idT_p, $id_ta, $posted, $title_p, $id_a, $is_gener) = mysql_fetch_row($re_last_post)) {

		$last_authors[$id_ta] = $id_ta;
		if($posted !== NULL) {
			$last_post[$idT_p]['info'] = $GLOBALS['regset']->databaseToRegional($posted).'<br />'.substr(strip_tags($title_p), 0, 15).' ...';
			$last_post[$idT_p]['author'] = $id_a;
			$last_authors[$id_a] = $id_a;
		}
	}
	if(isset($last_authors)) {
		$authors_names =& $acl_man->getUsers($last_authors);
	}

	$page_title = array(
		'index.php?modname=forum&amp;op=forum' => $lang->def('_FORUM'),
		$lang->def('_SEARCH_RESULT_FOR').' : '.$search_arg
	);
	$GLOBALS['page']->add(
		 getTitleArea($page_title, 'forum')
		.'<div class="std_block">'
		.Form::openForm('search_forum', 'index.php?modname=forum&amp;op=search')
		.'<div class="search_mask form_line_l">'
		.'<label for="search_arg">'.$lang->def('_SEARCH_LABEL').'</label> '
		.Form::getInputTextfield(	'textfield_nowh',
									'search_arg',
									'search_arg',
									$search_arg,
									$lang->def('_SEARCH'), 255, '' )
		.' <input class="button_nowh" type="submit" id="search_button" name="search_button" value="'.$lang->def('_SEARCH').'" />'
		.'</div>'
		.Form::closeForm()
	, 'content');

	$tb = new TypeOne($GLOBALS['cms']['visuItem'], $lang->def('_THREAD_CAPTION'), $lang->def('_THRAD_SUMMARY'));

	$img_up 	= '<img src="'.getPathImage().'standard/ord_asc.gif" alt="'.$lang->def('_ORD_ASC').'" />';
	$img_down 	= '<img src="'.getPathImage().'standard/ord_desc.gif" alt="'.$lang->def('_ORD_DESC').'" />';

	$cont_h = array(
		'<img src="'.getPathImage().'forum/thread.gif" title="'.$lang->def('_FREET').'" alt="'.$lang->def('_FREE').'" />',
		'<a href="'.$jump_url.'&amp;ord='.( $ord == 'obj' ? 'obji' : 'obj' ).'" title="'.$lang->def('_ORD_THREAD').'">'
			.( $ord == 'obj' ? $img_up : ( $ord == 'obji' ? $img_down : '' ) ).$lang->def('_THREAD').'</a>',
		$lang->def('_NUMREPLY'),
		'<a href="'.$jump_url.'&amp;ord='.( $ord == 'auth' ? 'authi' : 'auth' ).'" title="'.$lang->def('_ORD_AUTHOR').'">'
			.( $ord == 'auth' ? $img_up : ( $ord == 'authi' ? $img_down : '' ) ).$lang->def('_AUTHOR').'</a>',
		$lang->def('_NUMVIEW'),
		//$lang->def('_POSTED'),
		'<a href="'.$jump_url.'&amp;ord='.( $ord == 'post' ? 'posti' : 'post' ).'" title="'.$lang->def('_ORD_POST').'">'
			.( $ord == 'post' ? $img_up : ( $ord == 'posti' ? $img_down : '' ) ).$lang->def('_LASTPOST').'</a>'
	);
	$type_h = array('image', '', 'align_center', 'align_center', 'image',
	//'align_center',
	'align_center');
	if($mod_perm) {

		$cont_h[] = '<img src="'.getPathImage().'standard/mod.gif" alt="'.$lang->def('_MOD').'" title="'.$lang->def('_MODTHREAD_TITLE').'" />';
		$type_h[] = 'image';
		$cont_h[] = '<img src="'.getPathImage().'standard/rem.gif" alt="'.$lang->def('_DEL').'" title="'.$lang->def('_DELTHREAD_TITLE').'" />';
		$type_h[] = 'image';
	}
	$tb->setColsStyle($type_h);
	$tb->addHead($cont_h);
	while(list($idT, $id_forum, $t_author, $posted, $title, $num_post, $num_view, $locked, $erased) = mysql_fetch_row($re_thread)) {

		$c_css = '';
		// thread author
		$t_author = ( $authors_names[$t_author][ACL_INFO_LASTNAME].$authors_names[$t_author][ACL_INFO_FIRSTNAME] == '' ?
				$authors_names[$t_author][ACL_INFO_USERID] :
				$authors_names[$t_author][ACL_INFO_LASTNAME].' '.$authors_names[$t_author][ACL_INFO_FIRSTNAME] );
		// last post author
		if(isset($last_post[$idT])) {

			$author = $last_post[$idT]['author'];
			$last_mess_write = $last_post[$idT]['info'].' ( '.$lang->def('_BY').': <span class="mess_author">'
				.( $authors_names[$author][ACL_INFO_LASTNAME].$authors_names[$author][ACL_INFO_FIRSTNAME] == '' ?
					$authors_names[$author][ACL_INFO_USERID] :
					$authors_names[$author][ACL_INFO_LASTNAME].' '.$authors_names[$author][ACL_INFO_FIRSTNAME] ).'</span> )';
		} else {
			$last_mess_write = $lang->def('_NONE');
		}
		// status of the thread
		if($erased) {
			$status = '<img src="'.getPathImage().'forum/thread_erased.gif" alt="'.$lang->def('_FREE').'" />';
		} elseif($locked) {
			$status = '<img src="'.getPathImage().'forum/thread_locked.gif" alt="'.$lang->def('_LOCKED').'" />';
		} elseif(isset($_SESSION['unreaded_forum'][$id_forum][$idT])) {

			$status = '<img src="'.getPathImage().'forum/thread_unreaded.gif" alt="'.$lang->def('_UNREADED').'" />';
			$c_css = ' class="text_bold"';
		} else {
			$status = '<img src="'.getPathImage().'forum/thread.gif" alt="'.$lang->def('_FREE').'" />';
		}
		$content = array($status);
		$content[] = ( $erased && !$mod_perm ?
					'<div class="forumErased">'.$lang->def('_ERASED').'</div>' :
					'<a'.$c_css.' href="index.php?modname=forum&amp;op=searchmessage&amp;idThread='.$idT.'&amp;ini_thread='.$ini_page.'">'
					.eregi_replace($search_arg, '<span class="filter_evidence">'.$search_arg.'</span>', $title).'</a>');
		$content[] = $num_post
			.( isset($_SESSION['unreaded_forum'][$id_forum][$idT]) && $_SESSION['unreaded_forum'][$id_forum][$idT] != 'new_thread'
				? '<br />(<span class="forum_notread">'.$_SESSION['unreaded_forum'][$id_forum][$idT].' '.$lang->def('_NEW').')</span>'
				: '' );
		$content[] = $t_author;
		$content[] = $num_view;
		//$content[] = $GLOBALS['regset']->databaseToRegional($posted);
		$content[] = $last_mess_write;
		if($mod_perm) {

			$content[] = '<a href="index.php?modname=forum&amp;op=modthread&amp;idThread='.$idT.'&amp;search=1&amp;ini='.$ini_page.'" '
				.'title="'.$lang->def('_MODTHREAD_TITLE').' : '.strip_tags($title).'">'
				.'<img src="'.getPathImage().'standard/mod.gif" alt="'.$lang->def('_MOD').' : '.strip_tags($title).'" /></a>';
			$content[] = '<a href="index.php?modname=forum&amp;op=delthread&amp;idThread='.$idT.'&amp;search=1&amp;ini='.$ini_page.'" '
				.'title="'.$lang->def('_DELTHREAD_TITLE').' : '.strip_tags($title).'">'
				.'<img src="'.getPathImage().'standard/rem.gif" alt="'.$lang->def('_DEL').' : '.strip_tags($title).'" /></a>';
		}
		$tb->addBody($content);
	}
	$GLOBALS['page']->add($tb->getTable(), 'content');

	$GLOBALS['page']->add(
		$nav_bar->getNavBar($ini)
		.'</div>', 'content');
}

function forumsearchmessage() {
	checkPerm('view');

	$search_arg = $_SESSION['forum']['search_arg'];

	require_once($GLOBALS['where_framework'].'/lib/lib.newtypeone.php');
	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	$lang =& DoceboLanguage::createInstance('forum', 'cms');
	$id_thread = importVar('idThread', true, 0);
	$ini_thread = importVar('ini_thread');

	$sema_perm 	= checkPerm('sema', true);
	$moderate 	= checkPerm('moderate', true);
	$mod_perm	= checkPerm('mod', true);
	$acl_man 	=& $GLOBALS['current_user']->getAclManager();

	$tb 	= new TypeOne($GLOBALS['cms']['visuItem'], $lang->def('_CAPTION_FORUM_MESSAGE_SEARCH'), $lang->def('_SUMMARY_FORUM_MESSAGE_SEARCH'));
	$tb->initNavBar('ini', 'link');
	$tb->setLink('index.php?modname=forum&amp;op=searchmessage&amp;idThread='.$id_thread.'&amp;ini_thread='.$ini_thread);
	$ini 	= $tb->getSelectedElement();
	$ini_page = $tb->getSelectedPage();

	// Some info about forum and thread
	$thread_query = "
	SELECT idForum, title, num_post, locked, erased
	FROM ".$GLOBALS['prefix_cms']."_forumthread
	WHERE idThread = '".$id_thread."'";
	list($id_forum, $thread_title, $tot_message, $locked_t, $erased_t) = mysql_fetch_row(mysql_query($thread_query));
	$forum_query = "
	SELECT title, locked
	FROM ".$GLOBALS['prefix_cms']."_forum
	WHERE idForum = '".$id_forum."'";
	list($forum_title, $locked_f) = mysql_fetch_row(mysql_query($forum_query));
	++$tot_message;

	//set as readed if needed
	if(isset($_SESSION['unreaded_forum'][$id_forum][$id_thread])) unset($_SESSION['unreaded_forum'][$id_forum][$id_thread]);

	if( ($ini == 0) && (!isset($_GET['result'])) ) {
		mysql_query("
		UPDATE ".$GLOBALS['prefix_cms']."_forumthread
		SET num_view = num_view + 1
		WHERE idThread = '".$id_thread."'");
	}
	$page_title = array(
		'index.php?modname=forum&amp;op=forum' => $lang->def('_FORUM'),
		'index.php?modname=forum&amp;op=search&amp;ini='.$ini_thread => $thread_title,
		$lang->def('_SEARCH_RESULT_FOR').' : '.$search_arg
	);
	if($erased_t && !$mod_perm && !$moderate) {

		$GLOBALS['page']->add(
			getTitleArea($page_title, 'forum')
			.'<div class="std_block">'
			.$lang->def('_CANNOTENTER')
			.'</div>', 'content');
		return;
	}
	// Who have semantic evaluation
	$re_sema = mysql_query("
	SELECT DISTINCT idmsg
	FROM ".$GLOBALS['prefix_cms']."_forum_sema");
	while(list($msg_sema) = mysql_fetch_row($re_sema)) $forum_sema[$msg_sema] = 1;

	// Find post
	$messages 		= array();
	$authors 		= array();
	$authors_names	= array();
	$authors_info	= array();
	$re_message = mysql_query("
	SELECT idMessage, posted, title, textof, attach, locked, author, modified_by, modified_by_on
	FROM ".$GLOBALS['prefix_cms']."_forummessage
	WHERE idThread = '".$id_thread."'
	ORDER BY posted
	LIMIT $ini, ".$GLOBALS['cms']['visuItem']);
	while($record = mysql_fetch_assoc($re_message)) {

		$messages[$record['idMessage']] 	= $record;
		$authors[$record['author']] 		= $record['author'];
		if($record['modified_by'] != 0) {
			$authors[$record['modified_by']] 	= $record['modified_by'];
		}
	}
	$authors_names =& $acl_man->getUsers($authors);
	$level_name = getLevels();

	// Retriving level and number of post of th authors
	$re_num_post = mysql_query("
	SELECT u.idUser, u.level, COUNT(*)
	FROM ".$GLOBALS['prefix_cms']."_forummessage AS m, ".$GLOBALS['prefix_cms']."_courseuser AS u
	WHERE m.author = u.idUser AND m.author IN ( ".implode($authors, ',')." )
	GROUP BY u.idUser, u.level");
	while( list($id_u, $level_u, $num_post_a) = mysql_fetch_row($re_num_post) ) {

		$authors_info[$id_u] = array( 'num_post' => $num_post_a, 'level' => $level_name[$level_u] );
	}
	$type_h = array('forum_sender', 'forum_text');
	$cont_h = array($lang->def('_AUTHOR'), $lang->def('_TEXTOF'));
	$tb->setColsStyle($type_h);
	$tb->addHead($cont_h);

	// Compose messagges display
	$path = $GLOBALS['cms']['url'].$GLOBALS['where_files_relative'].'/doceboCms/'.$GLOBALS['cms']['pathphoto'];
	while(list($id_message, $message_info) = each($messages)) {

		// sender info
		$m_author = $message_info['author'];
		
		if($authors_names[$m_author][ACL_INFO_AVATAR] != '') $img_size = @getimagesize($path.$authors_names[$m_author][ACL_INFO_AVATAR]);
		
		$sender = '<div class="forum_author">'
			.( $authors_names[$m_author][ACL_INFO_LASTNAME].$authors_names[$m_author][ACL_INFO_FIRSTNAME] == '' ?
				$authors_names[$m_author][ACL_INFO_USERID] :
				$authors_names[$m_author][ACL_INFO_LASTNAME].' '.$authors_names[$m_author][ACL_INFO_FIRSTNAME] )
			.'</div>'
			.'<div class="forum_level">'.$lang->def('_LEVEL').' : '.$authors_info[$m_author]['level'].'</div>'
			.( $authors_names[$m_author][ACL_INFO_AVATAR] != '' 
				? '<img class="forum_avatar'.( $img_size[0] > 150 || $img_size[1] > 150 ? ' image_limit' : '' ).'" src="'.$path.$authors_names[$m_author][ACL_INFO_AVATAR].'" alt="'.$lang->def('_AVATAR').'" />' 
				: '' )
			.'<div class="forum_numpost">'.$lang->def('_NUMPOST').' : '
			.( isset($authors_info[$m_author]['num_post'])
				? $authors_info[$m_author]['num_post']
				: 0 )
			.'</div>'
			.'<img src="'.getPathImage().'standard/user.gif" alt="&gt;" />&nbsp;'
			.'<a href="index.php?modname=forum&amp;op=viewprofile&amp;idMessage='.$id_message.'&amp;ini='.$ini_page.'">'.$lang->def('_VIEWPROFILE').'</a>';

		// msg info
		$msgtext = '';

		$msgtext .= '<div class="forum_post_posted">'
			.$lang->def('_POSTED').' : '.$GLOBALS['regset']->databaseToRegional($message_info['posted'])
			.' ( '.loadDistance($message_info['posted']).' )'
			.'</div>';
		if($message_info['locked']) {

			$msgtext .= '<div class="forum_post_locked">'.$lang->def('_LOCKEDMESS').'</div>';
		} else {
			if($message_info['attach'] != '') {

				$msgtext .= '<div class="forum_post_attach">'
					.'<a href="index.php?modname=forum&amp;op=download&amp;id='.$id_message.'">'
					.$lang->def('_ATTACHMENT').' : '
					.'<img src="'.getPathImage('fw').mimeDetect($message_info['attach']).'" alt="'.$lang->def('_ATTACHMENT').'" /></a>'
					.'</div>';
			}

			$textof = str_replace('[quote]', '<blockquote class="forum_quote">', str_replace('[/quote]', '</blockquote>', $message_info['textof']));
			$msgtext .= '<div class="forum_post_title">'.$lang->def('_SUBJECT').' : '
						.eregi_replace($search_arg, '<span class="filter_evidence">'.$search_arg.'</span>', $message_info['title'])
						.'</div>';
			$msgtext .= '<div class="forum_post_text">'
						.eregi_replace($search_arg, '<span class="filter_evidence">'.$search_arg.'</span>', $textof)
						.'</div>';

			if($message_info['modified_by'] != 0) {

				$modify_by = $message_info['modified_by'];
				$msgtext .= '<div class="forum_post_modified_by">'
						.$lang->def('_MODIFY_BY').' : '
						.( $authors_names[$modify_by][ACL_INFO_LASTNAME].$authors_names[$modify_by][ACL_INFO_FIRSTNAME] == '' ?
							$authors_names[$modify_by][ACL_INFO_USERID] :
							$authors_names[$modify_by][ACL_INFO_LASTNAME].' '.$authors_names[$modify_by][ACL_INFO_FIRSTNAME] )
						.' '.$lang->def('_MODIFY_BY_ON').' : '
						.$GLOBALS['regset']->databaseToRegional($message_info['modified_by_on'])
						.'</div>';
			}

			if($authors_names[$m_author][ACL_INFO_SIGNATURE] != '') {
				$msgtext .= '<div class="forum_post_sign_separator"></div>'
					.'<div class="forum_post_sign">'
					.$authors_names[$m_author][ACL_INFO_SIGNATURE]
					.'</div>';
			}
		}
		$content = array($sender, $msgtext);
		$tb->addBody($content);

		// some action that you can do with this message
		$action = '';
		if($sema_perm) {
			if(isset($forum_sema[$id_message])) $img_sema = 'sema_check';
			else $img_sema = 'sema';
			$action .= '<a href="index.php?modname=forum&amp;op=editsema&amp;idMessage='.$id_message.'&amp;ini='.$ini_page.'" '
					.'title="'.$lang->def('_EDITSEMA_TITLE').' : '.strip_tags($message_info['title']).'">'
				.'<img src="'.getPathImage().'forum/'.$img_sema.'.gif" alt="'.$lang->def('_MOD').' : '.strip_tags($message_info['title']).'" /> '
				.$lang->def('_SEMATAG').'</a> ';
		}
		if($moderate || $mod_perm) {
			if($message_info['locked']) {

				$action .= '<a href="index.php?modname=forum&amp;op=moderatemessage&amp;idMessage='.$id_message.'&amp;ini='.$ini_page.'" '
						.'title="'.$lang->def('_DEMODERATE_MESSAGE').' : '.strip_tags($message_info['title']).'">'
					.'<img src="'.getPathImage().'forum/demoderate.gif" alt="'.$lang->def('_ALT_DEMODERATE').' : '.strip_tags($message_info['title']).'" /> '
					.$lang->def('_DEMODERATE').'</a> ';
			} else {

				$action .= '<a href="index.php?modname=forum&amp;op=moderatemessage&amp;idMessage='.$id_message.'&amp;ini='.$ini_page.'" '
						.'title="'.$lang->def('_MODERATE_MESSAGE').' : '.strip_tags($message_info['title']).'">'
					.'<img src="'.getPathImage().'forum/moderate.gif" alt="'.$lang->def('_ALT_MODERATE').' : '.strip_tags($message_info['title']).'" /> '
					.$lang->def('_MODERATE').'</a> ';
			}
		}
		if((!$locked_t && !$locked_f) || $mod_perm || $moderate) {
			$action .= '<a href="index.php?modname=forum&amp;op=addmessage&amp;idThread='.$id_thread.'&amp;idMessage='.$id_message.'&amp;ini='.$ini_page.'" '
					.'title="'.$lang->def('_REPLY_TITLE').' : '.strip_tags($message_info['title']).'">'
				.'<img src="'.getPathImage().'forum/reply.gif" alt="'.$lang->def('_ALT_REPLY').' : '.strip_tags($message_info['title']).'" /> '
				.$lang->def('_QUOTE').'</a>';
		}
		if($moderate || $mod_perm || ($m_author == getLogUserId()) ) {

			$action .= '<a href="index.php?modname=forum&amp;op=modmessage&amp;idMessage='.$id_message.'&amp;ini='.$ini_page.'" '
					.'title="'.$lang->def('_MOD_MESSAGE').' : '.strip_tags($message_info['title']).'">'
				.'<img src="'.getPathImage().'standard/mod.gif" alt="'.$lang->def('_MOD').' : '.strip_tags($message_info['title']).'" /> '
				.$lang->def('_MOD').'</a>'
				.'<a href="index.php?modname=forum&amp;op=delmessage&amp;idMessage='.$id_message.'&amp;ini='.$ini_page.'" '
					.'title="'.$lang->def('_DEL_MESSAGE').' : '.strip_tags($message_info['title']).'">'
				.'<img src="'.getPathImage().'standard/rem.gif" alt="'.$lang->def('_DEL').' : '.strip_tags($message_info['title']).'" /> '
				.$lang->def('_DEL').'</a> ';
		}
		$tb->addBodyExpanded($action, 'forum_action');
	}
	if( (!$locked_t && !$locked_f) || $mod_perm || $moderate ) {

		$tb->addActionAdd(
			'<a href="index.php?modname=forum&amp;op=addmessage&amp;idThread='.$id_thread.'&amp;ini='.$ini_page.'" title="'.$lang->def('_ADDMESSAGET').'">'
			.'<img src="'.getPathImage().'standard/add.gif" alt="'.$lang->def('_ADD').'" /> '
			.$lang->def('_REPLY_TO_THIS_THREAD').'</a>'
		);
	}
	$GLOBALS['page']->add(
		getTitleArea($page_title, 'forum')
		.'<div class="std_block">'
		.Form::openForm('search_forum', 'index.php?modname=forum&amp;op=search&amp;idThread='.$id_thread)
		.'<div class="search_mask form_line_l">'
		.'<label for="search_arg">'.$lang->def('_SEARCH_LABEL').'</label> '
		.Form::getInputTextfield(	'textfield_nowh',
									'search_arg',
									'search_arg',
									$search_arg,
									$lang->def('_SEARCH'), 255, '' )
		.' <input class="button_nowh" type="submit" id="search_button" name="search_button" value="'.$lang->def('_SEARCH').'" />'
		.'</div>'
		.Form::closeForm(), 'content');

	if($moderate || $mod_perm) {
		$GLOBALS['page']->add(
			'<div class="forum_action_top">'
			.'<a href="index.php?modname=forum&amp;op=modstatusthread&amp;idThread='.$id_thread.'&amp;ini='.$ini_page.'">'
			.( $locked_t
				?'<img src="'.getPathImage().'forum/thread.gif" alt="'.$lang->def('_FREE').'" /> '.$lang->def('_FREETHREAD')
				: '<img src="'.getPathImage().'forum/thread_locked.gif" alt="'.$lang->def('_LOCKTHREAD').'" /> '.$lang->def('_LOCKTHREAD') )
			.'</a> '
			.'<a href="index.php?modname=forum&amp;op=changeerased&amp;idThread='.$id_thread.'&amp;ini='.$ini_page.'">'
			.( $erased_t
				?'<img src="'.getPathImage().'forum/thread.gif" alt="'.$lang->def('_FREE').'" /> '.$lang->def('_UNERASE')
				: '<img src="'.getPathImage().'forum/thread_erased.gif" alt="'.$lang->def('_DEL').'" /> '.$lang->def('_ERASE') )
			.'</a>'
			.'</div>' , 'content');
	}
	$GLOBALS['page']->add($tb->getTable(), 'content');
	if($moderate || $mod_perm) {
		$GLOBALS['page']->add(
			'<div class="forum_action_bottom">'
			.'<a href="index.php?modname=forum&amp;op=modstatusthread&amp;idThread='.$id_thread.'&amp;ini='.$ini_page.'">'
			.( $locked_t
				?'<img src="'.getPathImage().'forum/thread.gif" alt="'.$lang->def('_FREE').'" /> '.$lang->def('_FREETHREAD')
				: '<img src="'.getPathImage().'forum/thread_locked.gif" alt="'.$lang->def('_LOCKTHREAD').'" /> '.$lang->def('_LOCKTHREAD') )
			.'</a> '
			.'<a href="index.php?modname=forum&amp;op=changeerased&amp;idThread='.$id_thread.'&amp;ini='.$ini_page.'">'
			.( $erased_t
				?'<img src="'.getPathImage().'forum/thread.gif" alt="'.$lang->def('_FREE').'" /> '.$lang->def('_UNERASE')
				: '<img src="'.getPathImage().'forum/thread_erased.gif" alt="'.$lang->def('_DEL').'" /> '.$lang->def('_ERASE') )
			.'</a>'
			.'</div>' , 'content');
	}
	$GLOBALS['page']->add(
		$tb->getNavBar($ini, $tot_message)
		.'</div>', 'content');

}

//-XXX: notify functions-----------------------------------------------------//

/**
 * Register a new notify
 * @param string	$notify_is_a 	specifie if the notify is for a thread or for a forum
 * @param int		$id_notify 		specifie the id of the resource
 * @param int		$id_user 		the user
 *
 * @return	bool	true if success false otherwise
 */
function setNotify($notify_is_a, $id_notify, $id_user) {
	checkPerm('view');

	$query_notify = "
	INSERT INTO ".$GLOBALS['prefix_cms']."_forum_notifier
	( id_notify, id_user, notify_is_a ) VALUES (
		'".$id_notify."',
		'".$id_user."',
		'".( $notify_is_a == 'forum' ? 'forum' : 'thread' )."' )";
	return mysql_query($query_notify);
}

/**
 * Erase a register notify
 * @param string	$notify_is_a 	specifie if the notify is for a thread or for a forum
 * @param int		$id_notify 		specifie the id of the resource
 * @param int		$id_user 		the user
 *
 * @return	bool	true if success false otherwise
 */
function unsetNotify($notify_is_a, $id_notify, $id_user = false) {
	checkPerm('view');

	$query_notify = "
	DELETE FROM ".$GLOBALS['prefix_cms']."_forum_notifier
	WHERE id_notify = '".$id_notify."' AND
		notify_is_a = '".( $notify_is_a == 'forum' ? 'forum' : 'thread' )."' ";
	if($id_user !== false)  $query_notify .= " AND id_user = '".$id_user."'";
	return mysql_query($query_notify);
}

/**
 * Return if a user as set a notify for a resource
 * @param string	$notify_is_a 	specifie if the notify is for a thread or for a forum
 * @param int		$id_notify 		specifie the id of the resource
 * @param int		$id_user 		the user
 *
 * @return	bool	true if exists false otherwise
 */
function issetNotify($notify_is_a, $id_notify, $id_user) {
	checkPerm('view');

	$query_notify = "
	SELECT id_notify
	FROM ".$GLOBALS['prefix_cms']."_forum_notifier
	WHERE id_notify = '".$id_notify."' AND
		id_user = '".$id_user."' AND
		notify_is_a = '".( $notify_is_a == 'forum' ? 'forum' : 'thread' )."'";
	$re = mysql_query($query_notify);
	return ( mysql_num_rows($re) == 0 ? false : true );
}

/**
 * Return all the users registered notify
 * @param int		$id_user 		the user
 * @param string	$notify_is_a 	specifie if the notify is for a thread or for a forum
 *
 * @return	array	[thread]=>(  [id] => id, ...), [forum]=>(  [id] => id, ...)
 */
function getAllNotify($id_user, $notify_is_a = false) {
	checkPerm('view');

	$notify = array();
	$query_notify = "
	SELECT id_notify, notify_is_a
	FROM ".$GLOBALS['prefix_cms']."_forum_notifier
	WHERE id_user = '".$id_user."'";
	if($notify_is_a !== false) $query_notify .= " AND notify_is_a = '".( $notify_is_a == 'forum' ? 'forum' : 'thread' )."'";
	$re = mysql_query($query_notify);
	while(list($id_n, $n_is_a) = mysql_fetch_row($re)) {

		$notify[$n_is_a][$id_n] = $id_n;
	}
	return $notify;
}

function launchNotify($notify_is_a, $id_notify, $description, $subject, $body) {

	$recipients = array();
	$query_notify = "
	SELECT id_user
	FROM ".$GLOBALS['prefix_cms']."_forum_notifier
	WHERE id_notify = '".$id_notify."' AND
		notify_is_a = '".( $notify_is_a == 'forum' ? 'forum' : 'thread' )."' AND
		id_user <> '".getLogUserId()."'";
	if($notify_is_a !== false) $query_notify .= " AND notify_is_a = '".( $notify_is_a == 'forum' ? 'forum' : 'thread' )."'";
	$re = mysql_query($query_notify);
	while(list($id_user) = mysql_fetch_row($re)) {

		$recipients[] = $id_user;
	}

	if(!empty($recipients)) {

		require_once($GLOBALS['where_framework'].'/lib/lib.eventmanager.php');

		createNewAlert(		( $notify_is_a == 'forum' ? 'ForumNewThread' : 'ForumNewResponse' ),
							'forum',
							( $notify_is_a == 'forum' ? 'new_thread' : 'responce' ),
							1,
							$description,
							$recipients,
							$subject,
							$body );
	}
	return;
}

//---------------------------------------------------------------------------//

function forumDispatch($op) {

	switch($op) {
		case "forum" : {
			forum();
		};break;
		//-----------------------------------------------//
		case "addforum" : {
			addforum();
		};break;
		case "insforum" : {
			insforum();
		};break;
		//-----------------------------------------------//
		case "modforum" : {
			modforum();
		};break;
		case "upforum" : {
			upforum();
		};break;
		case "downforum" : {
			moveforum( $_GET['idForum'], 'down');
		};break;
		case "moveupforum" : {
			moveforum( $_GET['idForum'], 'up');
		};break;
		case "modstatus" : {
			changestatus();
		};break;
		//-----------------------------------------------//
		case "delforum" : {
			delforum();
		};break;
		//-----------------------------------------------//
		case "modforumaccess": {
			modforumaccess();
		};break;
		//-----------------------------------------------//
		case "thread" : {
			thread();
		};break;
		//-----------------------------------------------//
		case "addthread" : {
			addthread();
		};break;
		case "insthread" : {
			insthread();
		};break;
		//-----------------------------------------------//
		case "modthread" : {
			modthread();
		};break;
		case "upthread" : {
			upthread();
		};break;
		//-----------------------------------------------//
		case "delthread" : {
			delthread();
		};break;
		//-----------------------------------------------//
		case "message" : {
			message();
		};break;
		case "moderatemessage" : {
			moderatemessage();
		};break;
		case "modstatusthread" : {
			modstatusthread();
		};break;
		case "changeerased" : {
			changeerase();
		};break;
		//-----------------------------------------------//
		case "addmessage" : {
			addmessage();
		};break;
		case "insmessage" : {
			insmessage();
		};break;
		//-----------------------------------------------//
		case "modmessage" : {
			modmessage();
		};break;
		case "upmessage" : {
			upmessage();
		};break;
		//-----------------------------------------------//
		case "delmessage" : {
			delmessage();
		};break;
		//-----------------------------------------------//
		case "viewprofile" : {
			viewprofile();
		};break;
		//-----------------------------------------------//
		case "editsema" : {
			editsema();
		};break;
		case "savesema" : {
			savesema();
		};break;
		//-----------------------------------------------//
		case "download" : {
			checkPerm('view');
			require_once($GLOBALS['where_framework'].'/lib/lib.download.php' );

			//find file
			list($title, $attach) = mysql_fetch_row(mysql_query("
			SELECT title, attach
			FROM ".$GLOBALS['prefix_cms']."_forummessage
			WHERE idMessage='".(int)$_GET['id']."'"));
			if(!$attach) {
				$GLOBALS['page']->add( getErrorUi('Sorry, such file does not exist!'), 'content');
				return;
			}
			//recognize mime type
			$expFileName = explode('.', $attach);
			$totPart = count($expFileName) - 1;

			$path = $GLOBALS['where_files_relative'].'/doceboCms/'.$GLOBALS['cms']['pathforum'];
			//send file
			sendFile($path, $attach, $expFileName[$totPart]);
		};break;
		//-----------------------------------------------//
		case "search" : {
			forumsearch();
		};break;
		case "searchmessage" : {
			forumsearchmessage();
		};break;
		//-----------------------------------------------//
	}
}

?>
