<?
define('CFOR_MSG_PER_PAGE', 10);

define('CFOR_ERR_INVALID_ARGUMENTS',	401);
define('CFOR_ERR_MISSING_TI3A',			410);

if (!function_exists('rs2arr')) {
	function rs2arr(&$rs, $key_as_index=false, $single_row=false) {
	
		$new_arr = array();

		while ($r = $rs->row()) {

			$r = array_map('stripslashes', $r);

			if (count($r) == 1) {
				$value = current($r);
			} elseif (count($r) == 2 && $key_as_index !== false) {
				$tmp_r = $r;
				unset($tmp_r[$key_as_index]);
				$value = current($tmp_r);
			} else {
				$value = $r;
				
			}
			if ($key_as_index && isset($r[$key_as_index])) {
				$new_arr[$r[$key_as_index]] = $value;
			} else {
				$new_arr[] = $value;
			}		

			$rs->moveNext();
		}
		$rs->Close();

		return ($single_row) ? current($new_arr) : $new_arr;
	}
}

if (!function_exists('generate_query')) {
	function generate_query($tbl_name, $data, $key_field) {

		$fields = array();
		foreach ($data as $fld => $value) {
			if ($fld == $key_field) continue;
			$fields[] = $fld .' = ' . $value;
		}

		if (isset($data[$key_field]) && !empty($data[$key_field])) {
			//--- update
			$query = 'UPDATE '.$tbl_name.' SET ' . implode(', ',$fields) .' WHERE '.$key_field .' = '.$data[$key_field];
		} else {
			//--- insert
			$query = 'INSERT INTO '.$tbl_name.' SET ' . implode(', ',$fields);
		}

		return $query;
	}
}

require_once ('class.stdModule.php');

class customForum extends stdModule {

	function customForum() {
		
		$this->stdModule('cfor0100');

		$this->_settings->templates->forum_index	= "forum_index.tpl";
		$this->_settings->templates->list_topics	= "list_topics.tpl";
		$this->_settings->templates->new_topic		= "new_topic.tpl";

		$this->_settings->templates->list_messages	= "list_messages.tpl";
		$this->_settings->templates->new_message	= "new_message.tpl";

		$this->_variables->messages_per_page		= CFOR_MSG_PER_PAGE;

		$this->_variables->public->email->notification	= 'FORNOTIF';


		if (is_object($GLOBALS['g_obj_ti3a0365'])) {
			$this->_ti3a = &$GLOBALS['g_obj_ti3a0365'];
		} else {
			//$this->triggerError(CFOR_ERR_MISSING_TI3A);
		}

	}

	// TICH E-mail function
	function _send_email($code, $usr_id = null, $email_address = null, $variables = null, $attachment = null) {

		if (is_object($this->_tcc)) {

			$attachment = ($attachment) ? array($attachment) : null;

			$err = $this->_tcc->doCall("SEND_EMAIL", array(
					'code'			=> $code
				,	'org_id'		=> $this->_variables->public->org_id
				,	'usr_id'		=> $usr_id
				,	'lng_code'		=> $this->_variables->public->lng_code
				,	'address'		=> array('to' => $email_address)
				,	'variables'		=> $variables
				,	'attachments'	=> $attachment
				,	'debug'			=> false
				), $return , true);

			return $return;

		} else {

			$this->_debug('_send_email::error: no tcc object available', 1);
			trigger_error('no tcc object available in _send_email', E_USER_WARNING);

		}

	}


	function _updateStats($type, $id, $update) {
		
		switch ($type) {
			case 'for_tpc_count':
				$this->_db->Execute("UPDATE tbl_for_forum SET for_tpc_count = for_tpc_count ".$update." WHERE for_id = ".$id);
				break;
			case 'for_msg_id_last':
				$this->_db->Execute("UPDATE tbl_for_forum SET for_msg_id_last = ".$update." WHERE for_id = ".$id);
				break;
			case 'tpc_msg_count':
				$this->_db->Execute("UPDATE tbl_for_topic SET tpc_msg_count = tpc_msg_count ".$update." WHERE tpc_id = ".$id);
				break;
			case 'usr_msg_count':
				$this->_db->Execute("UPDATE tbl_for_user SET usr_msg_count = usr_msg_count ".$update." WHERE usr_id = ".$id);
				break;

			case 'tpc_msg_id_last':
				$this->_db->Execute("UPDATE tbl_for_topic SET tpc_msg_id_last = ".$update." WHERE tpc_id = ".$id);
				break;
			case 'all':
				$this->_db->Execute("UPDATE tbl_for_forum SET for_tpc_count = (select count(tpc_id) from tbl_for_topic where tpc_for_id = for_id)");
				$this->_db->Execute("UPDATE tbl_for_topic SET tpc_msg_count = (select count(msg_id) from tbl_for_message where msg_tpc_id = tpc_id)");
				$this->_db->Execute("UPDATE tbl_for_user SET usr_msg_count = (select count(msg_id) from tbl_for_message where msg_usr_id = usr_id)");
				break;
		}

	}

	function getUser($usr_id) {
		if (!is_numeric($usr_id) || $usr_id <= 0) return $this->triggerError(CFOR_ERR_INVALID_ARGUMENTS);

		$rs = $this->_db->query("SELECT * FROM tbl_for_user WHERE usr_id = ".$usr_id);
		return rs2arr($rs, false, true);
	}

	function getCategory($cat_id) {
		if (!is_numeric($cat_id) || $cat_id <= 0) return $this->triggerError(CFOR_ERR_INVALID_ARGUMENTS);

		$rs = $this->_db->Query("SELECT *
								FROM tbl_for_category
								WHERE cat_id = ".$cat_id);
		return rs2arr($rs, false, true);
	}

	function getForum($for_id) {
		if (!is_numeric($for_id) || $for_id <= 0) return $this->triggerError(CFOR_ERR_INVALID_ARGUMENTS);

		$rs = $this->_db->Query("SELECT cat_id, cat_name, tbl_for_forum.*
								FROM tbl_for_forum 
								LEFT JOIN tbl_for_category on cat_id = for_cat_id
								WHERE for_id = ".$for_id);
		return rs2arr($rs, false, true);
	}

	function getTopic($tpc_id) {
		if (!is_numeric($tpc_id) || $tpc_id <= 0) return $this->triggerError(CFOR_ERR_INVALID_ARGUMENTS);

		$rs = $this->_db->Query("SELECT cat_id, cat_name, for_id, for_name, for_desc, tpc_id, tpc_title
								FROM tbl_for_topic
								LEFT JOIN tbl_for_forum ON for_id = tpc_for_id
								LEFT JOIN tbl_for_category on cat_id = for_cat_id
								WHERE tpc_id = ".$tpc_id);
		return rs2arr($rs, false, true);
	}
	
	function getBinaries($bin_id=null, $msg_id=null) {
		if (!is_numeric($bin_id) && !is_numeric($msg_id)) return $this->triggerError(CFOR_ERR_INVALID_ARGUMENTS);
		
		if (is_numeric($bin_id)) {
			$sql = "SELECT * FROM tbl_for_binary WHERE bin_id = ".$bin_id;
		} else {
			$sql = "SELECT bin_id, bin_title, bin_filename, bin_mime, bin_size FROM tbl_for_binary WHERE bin_msg_id = ".$msg_id;
		}

		$rs = $this->_db->Query($sql);
		if (is_numeric($bin_id)) {
			$return = $rs->Row();
			$rs->Close();
		} else {
			$return = rs2arr($rs);
		}
		return $return;

	}
	
	function downloadBinary($bin_id) {
		if (!is_numeric($bin_id)) return $this->triggerError(CFOR_ERR_INVALID_ARGUMENTS);

		$bin = $this->getBinaries($bin_id);

		if ($bin) {

			Header("Pragma: public");
			Header("Expires: ".gmdate("D, d M Y H:i:s", mktime() + 2678400));
			Header("Cache-Control: max-age=3600, must-revalidate, post-check=0, pre-check=0");
			Header('Accept-Ranges: bytes');
			Header("Content-type: ". $bin['bin_mime']);
			Header("Content-Length: ".$bin['bin_size']);
			if (strpos($bin['bin_mime'], 'image/') !== false) {
				Header('Content-Disposition: inline; filename="'.$bin['bin_filename'].'"');
			} else {
				Header('Content-Disposition: attachment; filename="'.$bin['bin_filename'].'"');
			}
			Header("Connection: close");

			echo ($bin['bin_data']);
			
			exit();			

		}
	
	}


	function setCategory($data) {
		$sql_data = array();
		$cat_id = (isset($data['cat_id']) && is_numeric($data['cat_id'])) ? $data['cat_id'] : null;

		if (!$cat_id) {
			$sql_data['cat_org_id'] = $data['cat_org_id'];
			$sql_data['cat_created'] = 'NOW()';
		} else {
			$sql_data['cat_id'] = $cat_id;
		}

		if (isset($data['cat_name'])) $sql_data['cat_name'] = "'". mysql_escape_string($data['cat_name'])."'";
		if (isset($data['cat_order'])) $sql_data['cat_order'] = $data['cat_order'];

		if (count($sql_data)) {
			$sql_data['cat_changed'] = 'NOW()';
			
			$sql = generate_query('tbl_for_category', $sql_data, 'cat_id');
			$this->_db->Execute($sql);

			if (!$cat_id) $cat_id = $this->_db->lastInsertID();
			return $cat_id;
		}
	}

	function setForum($data) {
		$sql_data = array();
		$for_id = (isset($data['for_id']) && is_numeric($data['for_id'])) ? $data['for_id'] : null;

		if (!$for_id) {
			$sql_data['for_created'] = 'NOW()';
		} else {
			$sql_data['for_id'] = $for_id;
		}

		if (isset($data['for_cat_id'])) $sql_data['for_cat_id'] = $data['for_cat_id'];
		if (isset($data['for_name'])) $sql_data['for_name'] = "'". mysql_escape_string($data['for_name'])."'";
		if (isset($data['for_desc'])) $sql_data['for_desc'] = "'". mysql_escape_string($data['for_desc'])."'";

		if (isset($data['for_state'])) $sql_data['for_state'] = $data['for_state'] ? '1' : '0';
		if (isset($data['for_auth'])) $sql_data['for_auth'] = $data['for_auth'] ? '1' : '0';
		if (isset($data['for_order'])) $sql_data['for_order'] = $data['for_order'];

		if (count($sql_data)) {
			$sql_data['for_changed'] = 'NOW()';
			
			$sql = generate_query('tbl_for_forum', $sql_data, 'for_id');
			$this->_db->Execute($sql);

			if (!$for_id) $for_id = $this->_db->lastInsertID();
			return $for_id;
		}
	}

	function setTopic($data) {
		$sql_data = array();
		$tpc_id = (isset($data['tpc_id']) && is_numeric($data['tpc_id'])) ? $data['tpc_id'] : null;

		if (!$tpc_id) {

			//--- fields for new topic
			$sql_data['tpc_for_id'] = $data['tpc_for_id'];
			$sql_data['tpc_usr_id'] = $data['tpc_usr_id'];
			$sql_data['tpc_state'] = "'open'";
			$sql_data['tpc_msg_count'] = '0';
			$sql_data['tpc_created'] = 'NOW()';

		} else {
			$sql_data['tpc_id'] = $tpc_id;
		}
		
		if (isset($data['tpc_title'])) $sql_data['tpc_title'] = "'". mysql_escape_string($data['tpc_title'])."'";
		if (isset($data['tpc_state'])) $sql_data['tpc_state'] = "'". $data['tpc_state'].'"';

		if (count($sql_data)) {
			$sql_data['tpc_changed'] = 'NOW()';
			
			$sql = generate_query('tbl_for_topic', $sql_data, 'tpc_id');
			$this->_db->Execute($sql);

			if (!$tpc_id) $tpc_id = $this->_db->lastInsertID();
			return $tpc_id;

		}

	}

	function setMessage($data) {
		$sql_data = array();
		$msg_id = (isset($data['msg_id']) && is_numeric($data['msg_id'])) ? $data['msg_id'] : null;

		if (!$msg_id) {

			//--- fields for new message
			$sql_data['msg_tpc_id'] = $data['msg_tpc_id'];
			$sql_data['msg_usr_id'] = $data['msg_usr_id'];
			$sql_data['msg_created'] = 'NOW()';

		} else {
			$sql_data['msg_id'] = $msg_id;
		}
		
		if (isset($data['msg_text'])) $sql_data['msg_text'] = "'". mysql_escape_string($data['msg_text'])."'";

		if (count($sql_data)) {
			$sql_data['msg_changed'] = 'NOW()';
			
			$sql = generate_query('tbl_for_message', $sql_data, 'msg_id');
			$this->_db->Execute($sql);

			if (!$msg_id) $msg_id = $this->_db->lastInsertID();
			return $msg_id;
		}

	}
	
	function setBinary($data) {
		$sql_data = array();
		$bin_id = (isset($data['bin_id']) && is_numeric($data['bin_id'])) ? $data['bin_id'] : null;

		if (!$bin_id) {

			//--- fields for new message
			$sql_data['bin_created'] = 'NOW()';

		} else {
			$sql_data['bin_id'] = $bin_id;
		}
		
		if (isset($data['bin_msg_id']))		$sql_data['bin_msg_id']		= $data['bin_msg_id'];
		if (isset($data['bin_title']))		$sql_data['bin_title']		= "'". mysql_escape_string($data['bin_title'])."'";
		if (isset($data['bin_filename']))	$sql_data['bin_filename']	= "'". mysql_escape_string($data['bin_filename'])."'";
		if (isset($data['bin_tmp_name']))		$sql_data['bin_data']		= "'". addslashes(file_get_contents($data['bin_tmp_name']))."'";
		if (isset($data['bin_mime']))		$sql_data['bin_mime']		= "'". mysql_escape_string($data['bin_mime'])."'";
		if (isset($data['bin_size']))		$sql_data['bin_size']		= $data['bin_size'];

		if (count($sql_data)) {
			$sql_data['bin_changed'] = 'NOW()';
			
			$sql = generate_query('tbl_for_binary', $sql_data, 'bin_id');
			$this->_db->Execute($sql);

			if (!$bin_id) $bin_id = $this->_db->lastInsertID();
			return $bin_id;
		}
	
	}

	function saveMessage($tpc_id, $data) {
		if (!is_numeric($tpc_id) || $tpc_id <= 0) return $this->triggerError(CFOR_ERR_INVALID_ARGUMENTS);

		$topic_info = $this->getTopic($tpc_id);

		$message = array();
		$message['msg_usr_id'] = $this->_variables->public->usr_id;
		$message['msg_tpc_id'] = $tpc_id;
		$message['msg_text'] = $data['msg_text'];
		
		if ($new_msg_id = $this->setMessage($message)) {

			$this->_updateStats('tpc_msg_count', $tpc_id, '+1');
			$this->_updateStats('tpc_msg_id_last', $tpc_id, $new_msg_id);
			$this->_updateStats('for_msg_id_last', $topic_info['for_id'], $new_msg_id);
			$this->_updateStats('usr_msg_count', $this->_variables->public->usr_id, '+1');

			//--- check uploads
			if (isset($_FILES['upload']) && is_array($_FILES['upload'])) {
			
				$upload_count = count($_FILES['upload']['name']);
				for ($i = 0; $i < $upload_count; $i++) {

					if (is_uploaded_file($_FILES['upload']['tmp_name'][$i]) && $_FILES['upload']['error'][$i] == UPLOAD_ERR_OK) {

						$data = array(
									'bin_msg_id'	=> $new_msg_id
								,	'bin_filename'	=> $_FILES['upload']['name'][$i]
								,	'bin_mime'		=> $_FILES['upload']['type'][$i]
								,	'bin_tmp_name'	=> ($_FILES['upload']['tmp_name'][$i])
								,	'bin_size'		=> $_FILES['upload']['size'][$i]
						);
						$this->setBinary($data);

					}
				}

			}

			return $new_msg_id;
		
		} else {

			$this->triggerError(CFOR_ERR_SAVING_MESSAGE);

		}


	}
	
	function saveTopic($for_id, $data) {
		if (!is_numeric($for_id) || $for_id <= 0) return $this->triggerError(CFOR_ERR_INVALID_ARGUMENTS);

		$topic = array();
		$topic['tpc_for_id'] = $for_id;
		$topic['tpc_usr_id'] = $this->_variables->public->usr_id;
		$topic['tpc_title'] = $data['tpc_title'];

		if ($new_tpc_id = $this->setTopic($topic)) {
		
			$this->_updateStats('for_tpc_count', $for_id, '+1');

			$message = array();
			$message['msg_usr_id'] = $this->_variables->public->usr_id;
			$message['msg_tpc_id'] = $new_tpc_id;
			$message['msg_text'] = $data['msg_text'];
			
			$this->_updateStats('tpc_msg_count', $new_tpc_id, '+1');

			if ($new_msg_id = $this->setMessage($message)) {
	
				$this->_updateStats('tpc_msg_id_last', $new_tpc_id, $new_msg_id);
				$this->_updateStats('for_msg_id_last', $for_id, $new_msg_id);
				$this->_updateStats('usr_msg_count', $this->_variables->public->usr_id, '+1');

				//--- check uploads
				if (isset($_FILES['upload']) && is_array($_FILES['upload'])) {
				
					$upload_count = count($_FILES['upload']['name']);
					for ($i = 0; $i < $upload_count; $i++) {

						if (is_uploaded_file($_FILES['upload']['tmp_name'][$i]) && $_FILES['upload']['error'][$i] == UPLOAD_ERR_OK) {

							$data = array(
										'bin_msg_id'	=> $new_msg_id
									,	'bin_filename'	=> $_FILES['upload']['name'][$i]
									,	'bin_mime'		=> $_FILES['upload']['type'][$i]
									,	'bin_tmp_name'	=> ($_FILES['upload']['tmp_name'][$i])
									,	'bin_size'		=> $_FILES['upload']['size'][$i]
							);
							$this->setBinary($data);

						}
					}

				}
				
				return $new_tpc_id;
			
			} else {

				$this->triggerError(CFOR_ERR_SAVING_MESSAGE);

			}

		} else {

			$this->triggerError(CFOR_ERR_SAVING_TOPIC);

		}

	}

	function printMessagesByMsg($msg_id) {
		
		$sql = "SELECT topic.msg_tpc_id, COUNT(message.msg_id) AS offset
				FROM tbl_for_message AS topic
				LEFT JOIN tbl_for_message AS message ON message.msg_tpc_id = topic.msg_tpc_id AND message.msg_id <= topic.msg_id
				WHERE topic.msg_id = ".$msg_id ."
				GROUP BY topic.msg_tpc_id";

		$rs = $this->_db->query($sql);


		$page_offset = ceil($rs->Row('offset')/$this->_variables->messages_per_page) - 1;
		$this->printMessages($rs->Row('msg_tpc_id'), $page_offset);
		
		$rs->Close();

	}

	function printMessages($tpc_id, $offset = 0) {

		if (!is_numeric($tpc_id) || $tpc_id <= 0) return $this->triggerError(CFOR_ERR_INVALID_ARGUMENTS);

		$this->_smrt->assign('topic', $this->getTopic($tpc_id));

		$msg_offset = $offset >= 0 
							? $this->_variables->messages_per_page * $offset
							: 0;

		//--- get message
		$rs = $this->_db->Query("SELECT SQL_CALC_FOUND_ROWS tbl_for_message.*, COUNT(bin_id) as bin_count
								FROM tbl_for_message
								LEFT JOIN tbl_for_binary ON bin_msg_id = msg_id
								WHERE msg_tpc_id = ".$tpc_id." 
								GROUP BY msg_id
								ORDER BY msg_created", 
								$msg_offset, $this->_variables->messages_per_page);
		$rs_tmp = $this->_db->query("SELECT FOUND_ROWS()");
		while ($r = $rs->Row()) {
			$r['user'] = $this->getUser($r['msg_usr_id']);
			$r['binaries'] = $r['bin_count'] ? $this->getBinaries(null, $r['msg_id']) : array();
			$this->_smrt->append('messages', $r);
			$rs->MoveNext();
		}
		$rs->Close();

		$page_browser = array(	'current_offset'	=> $msg_offset
							,	'current_page'		=> $offset+1
							,	'messages_per_page' => $this->_variables->messages_per_page
							,	'total_messages'	=> $rs_tmp->Row('FOUND_ROWS()')
							,	'total_pages'		=> ceil($rs_tmp->Row('FOUND_ROWS()') / $this->_variables->messages_per_page)
							);
		$rs_tmp->Close();

		$this->_smrt->assign('page_browser', $page_browser);

		$this->assignTemplateGlobals();	
		$this->_smrt->display($this->_settings->templates->list_messages);
				
	}

	function printTopics($for_id) {
		if (!is_numeric($for_id) || $for_id <= 0) return $this->triggerError(CFOR_ERR_INVALID_ARGUMENTS);

		$this->_smrt->assign('forum', $this->getForum($for_id));

		//--- get topics
		$rs = $this->_db->Query("SELECT tbl_for_topic.* 
									,	msg_id as last_msg_id
									,	msg_usr_id as last_msg_usr_id
									,	UNIX_TIMESTAMP(msg_created) as last_msg_created
								FROM tbl_for_topic 
								LEFT JOIN tbl_for_message ON msg_id = tpc_msg_id_last
								WHERE tpc_for_id = ".$for_id." 
								ORDER BY last_msg_created DESC");
		while ($r = $rs->Row()) {
			$r['user'] = $this->getUser($r['tpc_usr_id']);
			$this->_smrt->append('topics', $r);
			$rs->MoveNext();
		}
		$rs->Close();

		$this->assignTemplateGlobals();	
		$this->_smrt->display($this->_settings->templates->list_topics);

	}

	function printNewMessage($tpc_id) {
		if (!is_numeric($tpc_id) || $tpc_id <= 0) return $this->triggerError(CFOR_ERR_INVALID_ARGUMENTS);

		$this->_smrt->assign('topic', $this->getTopic($tpc_id));

		
		$this->assignTemplateGlobals();	
		$this->_smrt->display($this->_settings->templates->new_message);
	}

	function printNewTopic($for_id) {
		if (!is_numeric($for_id) || $for_id <= 0) return $this->triggerError(CFOR_ERR_INVALID_ARGUMENTS);

		$this->_smrt->assign('forum', $this->getForum($for_id));

		
		$this->assignTemplateGlobals();	
		$this->_smrt->display($this->_settings->templates->new_topic);
	}


	function getForumIndex($no_check=false) {

		$sql = "SELECT 
					cat_id
				,	cat_name
				,	for_id
				,	for_name
				,	for_desc
				,	for_auth
				,	for_state
				,	for_tpc_count
				,	msg_tpc_id AS last_tpc_id
				,	msg_id AS last_msg_id
				,	UNIX_TIMESTAMP(msg_created) as last_msg_created
				FROM tbl_for_category
				LEFT JOIN tbl_for_forum ON for_cat_id = cat_id
				LEFT JOIN tbl_for_message ON msg_id = for_msg_id_last
				WHERE cat_org_id = ".$this->_variables->public->org_id."
				".(!$no_check ? "AND for_state = 1" : '')."
				ORDER BY cat_order";

		$forum_index = array();
		$rs = $this->_db->Query($sql);
		while ($r = $rs->Row()) {
			if (!isset($forum_index[$r['cat_id']])) {
				$forum_index[$r['cat_id']] = array(	'cat_id'	=> $r['cat_id']
												,	'cat_name'	=> $r['cat_name']
												,	'forums'	=> array());
			}		

			//--- todo: authorization check

			if (is_numeric($r['for_id'])) {
				$forum_index[$r['cat_id']]['forums'][] = array (	'for_id'	=> $r['for_id']
																,	'for_name'	=> $r['for_name']
																,	'for_desc'	=> $r['for_desc']
																,	'for_state'	=> $r['for_state']
																,	'for_auth'	=> $r['for_auth']
																,	'for_tpc_count'	=> $r['for_tpc_count']
																,	'last_tpc_id'	=> $r['last_tpc_id']
																,	'last_msg_id'	=> $r['last_msg_id']
																,	'last_msg_created'	=> $r['last_msg_created']);
			}

			$rs->moveNext();
		}
		$rs->Close();

		return $forum_index;
	
	}

	function printForumIndex() {
	
		$this->_smrt->assign('forum_index', $this->getForumIndex());

		$this->assignTemplateGlobals();	
		$this->_smrt->display($this->_settings->templates->forum_index);

	}

	function sendNotificationMail($tpc_id) {
		if (!is_numeric($tpc_id) || $tpc_id <= 0) return $this->triggerError(CFOR_ERR_INVALID_ARGUMENTS);

		global $g_obj_ti3a0365;

		$topic = $this->getTopic($tpc_id);

		$q = array(
				array(I3A_FLD, 'USR_ID')
			,	array(I3A_FLD, 'USR_DEF_CODE', " = 'MEM'")
			,	array(I3A_FLD, 'MBR_ACTIVE', ' = 1')
			,	array(I3A_GRP, 'YV', true)
		);

		$g_obj_ti3a0365->searchUsers($users, $q);

		if (count($users)) {
				
			$email_vars = array('TOPIC_ID'		=> $topic['tpc_id']
							,	'TOPIC_TITLE'	=> $topic['tpc_title']
							,	'TOPIC_URL'		=> 'http://'.$_SERVER['HTTP_HOST'].'/forum/list_messages/'.$topic['tpc_id'].'/');

			foreach ($users as $user) {
				$this->_send_email($this->_variables->public->email->notification, $user['USR_ID'], null, $email_vars);
			}
		}
		
	}

}

?>