<?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
*/

class admin_users_main extends ModuleTools
{
	public function _default()
	{
		if (!access('read')) return LabCMS::i()->error403();
		if ($_SERVER['REQUEST_METHOD']=='POST') {
			if (!access('edit')) return LabCMS::i()->error403();
			User::checkToken();
			$actions=Factory::create('admin_users.actions');
			$actions->deleteUsers(array_keys($_POST['delete']));
		}
		
		if (!empty($_GET['search'])) $_GET['var3']=$_GET['search'];
		$list=Factory::create('admin_users.list');
		$page=isset($_GET['var1'])?$_GET['var1']:1;
		$oderby=isset($_GET['var2'])?$_GET['var2']:0;
		$filter=isset($_GET['var3'])?$_GET['var3']:false;
		$users=$list->getUsers($page, $oderby, $filter);
		
		self::_assign('users', $users);
		self::_assign('pager', $list->pager);
		
		self::_setTpl('users_list');
	}
	
	public function lc_add()
	{
		if (!access('add')) return LabCMS::i()->error403();
		$forms=Factory::create('admin_users.forms');
		$form=$forms->addUser();
		
		if ($form->validate()) {
			$actions=Factory::create('admin_users.actions');
			if ($actions->addUser($form->getValue()))
				refresh();
		}
		
		self::_assign('form', $this->_renderForm($form));
		addBreadcrumb('Добавление пользователя');
		self::_setTpl('form');
	}
	
	public function lc_edit()
	{
		if (!access('edit')) return LabCMS::i()->error403();
		
		$user=User::getUser();
		$user->loadUserDataById($_GET['var1']);
		if ($user->isGuest()) return self::_error(_m('Пользователь не найден'));
		
		$forms=Factory::create('admin_users.forms');
		$form=$forms->editUser($user);
		
		if ($form->validate()) {
			$actions=Factory::create('admin_users.actions');
			if ($actions->updateUser($user, $form->getValue()))
				refresh();
		}
		
		self::_assign('form', $this->_renderForm($form));
		addBreadcrumb('Редактирование пользователя');
		self::_setTpl('form');
	}
	
	public function lc_clones()
	{
		if (!access('read')) return LabCMS::i()->error403();
		
		$clones=Factory::create('admin_users.clones');
		$list=$clones->getClonesByIP($_GET['var1'], $_GET['var2']);
		self::_assign('clones', $list);
		self::_setTpl('clones');
		addBreadcrumb('Поиск клонов по IP');
	}
	
	public function lc_clones_ec()
	{
		if (!access('read')) return LabCMS::i()->error403();
		
		$clones=Factory::create('admin_users.clones');
		$list=$clones->getClonesByID($_GET['var1']);
		self::_assign('clones', $list);
		self::_setTpl('clones_ec');
		addBreadcrumb('Поиск клонов по cookie');
	}
	
	public function lc_delete_by_real_id()
	{
		if (!access('edit')) return LabCMS::i()->error403();
		User::checkToken();
		$actions=Factory::create('admin_users.actions');
		if ($actions->deleteUserByRealId($_GET['var1']))
			refresh('', _url($_GET['var2'], 'clones_ec'));
	}
	
	public function lc_delete_by_ip()
	{
		if (!access('edit')) return LabCMS::i()->error403();
		User::checkToken();
		$actions=Factory::create('admin_users.actions');
		if ($actions->deleteUserByIP($_GET['var1']))
			refresh('', _url(array($_GET['var2'], $_GET['var3']), 'clones'));

	}
	
	public function lc_mark_as_unique()
	{
		if (!access('edit')) return LabCMS::i()->error403();
		User::checkToken();
		$actions=Factory::create('admin_users.actions');
		if ($actions->markAsUnique($_GET['var1']))
			refresh('', _url($_GET['var2'], 'clones_ec'));
	}
}