<? 
$CLS='login_history';
class login_history
{
	function init() {
		global $X;

		$X->NAVI[] 				= $X->ADMIN->module_name();
		$this->TABLE 			= '_admin_users_login';

		switch($_GET['action']) {
			default:	 				return $this->_list(); 			break;
			case 'delete':			return $this->_delete();		break;
			case 'delete_all':	return $this->_delete_all();	break;
			case 'batch':			return $this->_batch();			break;
		}
	}


	function _list() {
		global $X;

		$table['order_by']	= 'id';
		$table['order_dir']	= 'desc';

		$X->ADMIN->text_filter($this, 'id', 'ID', '=');
		$X->ADMIN->text_filter($this, 'login', 'Login');
		$X->ADMIN->text_filter($this, 'password', 'Hasło');
		$X->ADMIN->text_filter($this, 'ip', 'IP');
		$X->ADMIN->select_filter($this, 'user_id', 'Użytkownik', $X->UTIL->array_extend(array(''=>'-- Wszyscy --'), $X->DB->get_array(' SELECT id,login FROM _admin_users ORDER BY id ',true)));
		$X->ADMIN->select_filter($this, 'login_fail', 'Typy logowań', array(''=>'-- Wszystkie --', 0=>'Poprawne', 1=>'Błędne'));
		$X->ADMIN->filter_form($this, $table, 225);


		$split=$X->DB->split('
			SELECT this.*
			FROM '.$this->TABLE.' this
			WHERE 1 '.($_SESSION['backend']['is_root']!=1 ? 'AND id>=5':'').'
			'.$this->FILTER_SQL.'
			'.$X->DB->set_order($table).'
		', 20);


		$table['top_left']=$this->FILTER;
		$table['top_right']=$X->FORM->button_potwierdzenie('<span class="ico w_no"></span><span>&nbsp;Usuń wszystkie</span>',$X->URL->get('action=delete_all'),false,false,'button button_gfx');
		$table['bottom_left']=$X->ADMIN->batch_actions($split);
		$table['head']='
			'.$X->ADMIN->sortable_col($table,$split,'th').'
			'.$X->ADMIN->batch_col('th').'
			'.$X->ADMIN->head_col('ID','id',30,'center').'
			'.$X->ADMIN->head_col('OK','login_fail',34,'center').'
			'.$X->ADMIN->head_col('Login','login',150,'left').'
			'.$X->ADMIN->head_col('Hasło','password',false,'left').'
			'.$X->ADMIN->head_col('Data','date',120,'center').'
			'.$X->ADMIN->head_col('IP','ip',100,'center').'
			'.$X->ADMIN->head_col('Browser','browser',170,'left').'
			'.$X->ADMIN->head_col(false,false,66,'center').'
		';
		while($dane=$X->DB->fa($split['sql'])) {
			$rows.='
				<tr id="R:'.$dane['id'].'">
					'.$X->ADMIN->sortable_col($table,$split,'td').'
					'.$X->ADMIN->batch_col('td',$dane['id']).'
					<td class="center">'.$dane['id'].'</td>
					<td class="center"><span class="ico b_'.($dane['login_fail']==1 ? 'no':'yes').'"></span></td>
					<td><b>'.$X->STR->outt($dane['login']).'</b></td>
					<td>'.($dane['login_fail']==0 ? '<i class="gray3">'.$X->STR->outt($dane['password']).'</i>' : '<b>'.$X->STR->outt($dane['password']).'</b>').'</td>
					<td>'.$X->STR->outt($dane['date']).'</td>
					<td class="center" '.$X->FORM->tip($X->STR->outt($dane['host']),true).'>'.$X->STR->outt($dane['ip']).'</td>
					<td '.$X->FORM->tip($X->STR->outt($dane['user_agent']),true).'>'.$X->STR->outt($dane['browser']).'</td>
					<td class="center">'.$X->FORM->button_potwierdzenie('Usuń', $X->URL->get('action=delete&id='.$dane['id'])).'</td>
				</tr>
			';
		}
		return $X->ADMIN->listbox($this, $table, $rows, $split).$html;
	}


	function _delete($id=false) {
		require(START_DELETE);

		$X->DB->q(' DELETE FROM '.$this->TABLE.' WHERE id="'.$_GET['id'].'" ');

		if(!is_numeric($id)) $X->URL->go2('action&id');
	}


	function _delete_all() {
		require(START_DELETE);

		$X->DB->q(' TRUNCATE TABLE '.$this->TABLE.' ');
		$X->URL->go2('action&id');
	}


	function _batch() {
		global $X;

		if(count($_POST['batch'])>0) foreach($_POST['batch'] as $b_id) {
			if($_POST['batch_action']=='delete') $this->_delete($b_id);
		}
		$X->URL->go2('action&id');
	}

}
?>