<?php

require_code('ocf_forum_driver_helper');

require_css('news');
require_lang('news');

$max=array_key_exists('max',$map)?intval($map['max']):10;
$_title=array_key_exists('title',$map)?$map['title']:do_lang('POSTS');

$start=0;

$where='p_validated=1';

$where.=' AND EXISTS (SELECT * FROM '.$GLOBALS['FORUM_DB']->get_table_prefix().'group_category_access g WHERE g.category_name=p.p_cache_forum_id AND module_the_name=\'forums\' AND g.group_id='.strval(db_get_first_id()).')';

//$where.=not_like_spacer_posts('t.text_original');
$where.=' AND p_cache_forum_id<>101 AND p_cache_forum_id IS NOT NULL';

$order='p_time DESC,p.id DESC';

$select='p.*,text_parsed,text_original,l_time,t_cache_first_title';

$query='SELECT '.$select.' FROM '.$GLOBALS['FORUM_DB']->get_table_prefix().'f_posts p JOIN '.$GLOBALS['FORUM_DB']->get_table_prefix().'f_topics top ON top.id=p.p_topic_id LEFT JOIN '.$GLOBALS['FORUM_DB']->get_table_prefix().'f_read_logs l ON (p.p_topic_id=l_topic_id AND l_member_id='.strval(get_member()).') LEFT JOIN '.$GLOBALS['FORUM_DB']->get_table_prefix().'translate t ON t.id=p.p_post WHERE '.$where.' ORDER BY '.$order;
$rows=$GLOBALS['FORUM_DB']->query($query,$max,$start);
if (!is_array($rows)) return;
//$query_cnt='SELECT COUNT(*) FROM '.$GLOBALS['FORUM_DB']->get_table_prefix().'f_posts p LEFT JOIN '.$GLOBALS['FORUM_DB']->get_table_prefix().'translate t ON t.id=p.p_post WHERE '.$where;
//$count=$GLOBALS['FORUM_DB']->query_value_null_ok_full($query_cnt);

$out=new ocp_tempcode();
foreach ($rows as $row)
{
	$post_url=$GLOBALS['FORUM_DRIVER']->post_url($row['id'],'',true);

	$first_unread_url=mixed();
	require_code('ocf_topics');
	$has_read=ocf_has_read_topic($row['p_topic_id'],$row['p_time'],get_member(),$row['l_time']);
	if ((!$has_read) && (!match_key_match('_WILD:topicview:id='.strval($row['p_topic_id']))))
	{
		$first_unread_url=build_url(array('page'=>'topicview','id'=>$row['p_topic_id'],'type'=>'first_unread'),get_module_zone('topicview'));
		$first_unread_url->attach('#first_unread');
	}

	$date=get_timezoned_date($row['p_time'],false);

	$username=$GLOBALS['FORUM_DRIVER']->get_username($row['p_poster']);

	$message=new ocp_tempcode();
	if ((!is_null($row['text_parsed'])) && ($row['text_parsed']!='') && ($row['id']!=0))
	{
		if (!$message->from_assembly($row['text_parsed'],true))
			$message=get_translated_tempcode($row['id'],$GLOBALS['FORUM_DB']);
	} else $message=get_translated_tempcode($row['id'],$GLOBALS['FORUM_DB']);

	$out->attach(do_template('BLOCK_SIDE_CONTENT_COMMENTS_COMMENT',array(
		'POST_TITLE'=>escape_html($row['p_title']),
		'TOPIC_TITLE'=>escape_html($row['t_cache_first_title']),
		'POST'=>$message,
		'POST_URL'=>$post_url,
		'UNREAD_URL'=>$first_unread_url,
		'USERNAME'=>$username,
		'MEMBER_ID'=>strval($row['p_poster']),
		'DATE'=>$date,
		'_DATE'=>strval($row['p_time']),
	)));
}

$tpl=do_template('BLOCK_SIDE_CONTENT_COMMENTS',array(
	'TITLE'=>$_title,
	'CONTENT'=>$out,
));
$tpl->evaluate_echo();