<?php
	header('Content-Type: text/html; charset=utf-8');

	include('../../../inc/start.php');
	include('../../../inc/protect.php');

	if(!isset($_POST['token']) || $_SESSION['token'] != clean($_POST['token'], null))
		exit(json_encode(array('status' => 'error', 'text' => 'Неверный токен')));

	$limit = isset($_POST['limit']) ? intval($_POST['limit']) : 3;
	$limit = $limit > 2 ? $limit : 3;

	$tpl = new Template;
	$tpl->dir = '../tpl/ajax/';

	$events = $pdo->query('SELECT `link`, `author` FROM `events` WHERE `type`="3" ORDER BY `date` DESC LIMIT '.$limit); 
			 $events->setFetchMode(PDO::FETCH_OBJ);

	$tpl->result['content'] = '';

	while($event = $events->fetch())
	{
		$users = $pdo->query('SELECT `login`, `avatar` FROM `users` WHERE `id`="'.$event->author.'" LIMIT 1'); 
				$users->setFetchMode(PDO::FETCH_OBJ);

		$user = $users->fetch();

		$tpl->load_template('lastsignup.tpl');
			$tpl->set('{link}', $event->link);
			$tpl->set('{login}', $user->login);
			$tpl->set('{avatar}', $user->avatar);
		$tpl->compile('content');
	}

	echo $tpl->result['content'];
?>