<?php
/*
	Copyright © Alexander Sunvas*
	http://eleanor-cms.ru
	a@eleanor-cms.ru
	*Pseudonym
*/

/*
	Перед использованием блока, рекомендуется выполнить запрос (замените el_ на свой префикс):
	ALTER TABLE `el_comments` DROP INDEX `status`, ADD INDEX `status` (`status`,`sortdate`)
*/
global$Eleanor;
$comments=$news=array();
$R=Eleanor::$Db->Query('SELECT `id`,`module`,`contid`,`date`,`author`,`author_id`,`text` FROM `'.P.'comments` WHERE `status`=1 ORDER BY `sortdate` DESC LIMIT 10');
while($a=$R->fetch_assoc())
{
	$module=array_keys($Eleanor->modules['ids'],$a['module']);
	if($module)
	{
		$module=reset($module);
		if(isset($Eleanor->modules['sections'][$module]))
			switch($Eleanor->modules['sections'][$module])
			{
				case'news':
					$comments[$a['id']]='news';
					$a['_module']=$module;
					$news[ $a['id'] ]=array_slice($a,1);
			}
	}
}

if($news)
{
	$data=array();
	foreach($news as $v)
		$data[]=$v['contid'];
	$R=Eleanor::$Db->Query('SELECT `id`,`title` FROM `'.P.'news_l` WHERE `id`'.Eleanor::$Db->In($data).' AND `language`IN(\'\',\''.Language::$main.'\') AND `lstatus`=1');
	$data=array();
	while($a=$R->fetch_assoc())
		$data[ $a['id'] ]=array_slice($a,1);

	foreach($news as $k=>$v)
		if(isset($data[ $v['contid'] ]))
		{
			$author=Eleanor::$Login->UserLink(htmlspecialchars_decode($v['author'],ELENT),$v['author_id']);
			$text=strip_tags(OwnBB::Parse($v['text']),'<img>');
			$text=trim($text);

			$news[$k]='<li><span>Автор:</span> '
				.($author ? '<a href="'.$author.'">'.$v['author'].'</a>' : $v['author'])
				.'<br />'.Strings::CutStr($text,150)
				.'<br /><span>В новости:</span> <a href="?'.Url::Query(array('module'=>$v['_module'],'id'=>$v['contid'],'cfind'=>$k)).'">'.$data[ $v['contid'] ]['title'].'</a></li>';
		}
		else
			unset($news[$k]);
}

$s='';
foreach($comments as $k=>$v)
	if(isset(${$v}[$k]))
		$s.=${$v}[$k];
if($s)
	echo'<ul class="last_comments">',$s,'</ul>';