<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
	$sort=array(
		0	=>	'user_id',
		1	=>	'user_id DESC',
		2	=>	'login',
		3	=>	'login DESC',
		4	=>	'u.group_id',
		5	=>	'u.group_id DESC',
		6	=>	'email',
		7	=>	'email DESC'
	);
	if (!isset($_GET['var2']) || !array_key_exists($_GET['var2'], $sort)) $_GET['var2']=0;
	
	$mod_template="users_list.tpl";
	
	LabConfig::$mcfg['users_at_page']=20;
	
	if (!empty($_GET['search'])) $_GET['var3']=$_GET['search'];

	if (!isset($_GET['var3'])) $_GET['var3']=null;
	
	if ($_SERVER['REQUEST_METHOD']=='POST')
	{
		User::checkToken();
		if (in_array(-1, $_POST['group_id'])) LabCMS::i()->error('Пользователь не может принадлежать этой группе');
		if (LabCMS::i()->no_errors)
		{
			foreach($_POST['group_id'] as $key=>$var)
			{
				Db::i()->query("UPDATE ?_users SET group_id=? WHERE user_id=?", $var, $key);
			}
			if (count($_POST['delete'])) Db::i()->query("UPDATE ?_users SET `status`=-1 WHERE user_id IN (?a)", array_keys($_POST['delete']));
			LabCMS::i()->reset_cache();
			User::setResetSession();
		}
	}
	
	$count=Db::i()->selectCell("SELECT COUNT(user_id) FROM ?_users WHERE user_id!=-1 AND `status`>=0 {AND (login LIKE ?} {OR email LIKE ?)}", ($_GET['var3']?'%'.$_GET['var3'].'%':DBSIMPLE_SKIP), ($_GET['var3']?'%'.$_GET['var3'].'%':DBSIMPLE_SKIP));
	
	$pager=new Paginator($count, LabConfig::$mcfg['users_at_page'], $_GET['var1']);
	$pager=$pager->getPages();
	
	$users=Db::i()->select("SELECT user_id, login, email, group_name, u.group_id FROM ?_users u JOIN ?_groups g ON u.group_id=g.group_id WHERE user_id!=-1 AND `status`>=0 {AND (login LIKE ?} {OR email LIKE ?)} ORDER BY ".$sort[$_GET['var2']]." LIMIT ?d, ?d", ($_GET['var3']?'%'.$_GET['var3'].'%':DBSIMPLE_SKIP), ($_GET['var3']?'%'.$_GET['var3'].'%':DBSIMPLE_SKIP), $pager['sql_start'], $pager['sql_limit']);

	Tpl::i()->assign('users', $users);
	Tpl::i()->assign('possible_groups', get_child_groups(0));
	Tpl::i()->assign('pager', $pager);
