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

		$X->NAVI[] 				= $X->ADMIN->module_name();
		$this->TABLE 			= '_admin_users';
		$this->LOG_ACTIVITY 	= false;
		$this->MULTILANG 		= false;

		if(IS_ROOT) {
			$this->USER_COND='1';
			$this->USER_GET_DISALLOW=false;
		} else {
			if(IS_SU) {
				$this->USER_COND='id>=2';
				$this->USER_GET_DISALLOW=(!empty($_GET['id']) && $_GET['id']<2);
			} else {
				$this->USER_COND='id>=5';
				$this->USER_GET_DISALLOW=(!empty($_GET['id']) && $_GET['id']<5);
			}
		}



		switch($_GET['action']) {
			default:	 			return $this->_list(); 		break;
			case 'form':		return $this->_form();		break;
			case 'save':		return $this->_save();		break;
			case 'delete':		return $this->_delete();	break;
			case 'override':	return $this->_override();	break;
		}
	}


	function _list() {
		global $X;

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

		$split=$X->DB->split('
			SELECT this.*
			FROM '.$this->TABLE.' this
			WHERE '.$this->USER_COND.'
			'.$X->DB->set_order($table).'
		', 0);


		$table['top_left'] = $X->FORM->dodaj('Dodaj użytkownika');
		$table['head']='
			'.$X->ADMIN->sortable_col($table,$split,'th').'
			'.$X->ADMIN->head_col('ID','id',30,'center').'
			'.$X->ADMIN->head_col('Login','login',150,'left').'
			'.$X->ADMIN->head_col('Opis','title',false,'left').'
			'.(IS_ROOT ? $X->ADMIN->head_col('Kolor',26,false,false):'').'
			'.$X->ADMIN->head_col('Ost. logowanie','last_login',120,'center').'
			'.$X->ADMIN->head_col('Ost. aktywność','last_activity',120,'center').'
			'.$X->ADMIN->head_col('Ost. IP','last_ip',100,'center').'
			'.$X->ADMIN->head_col('Akt','active',35,'center','Aktywny').'
			'.(IS_ROOT ? $X->ADMIN->head_col('Override',false,60,'center'):'').'
			'.$X->ADMIN->head_col(false,false,75,'center').'
			'.$X->ADMIN->head_col(false,false,70,'center').'
		';
		while($dane=$X->DB->fa($split['sql'])) {
			$rows.='
				<tr id="R:'.$dane['id'].'">
					'.$X->ADMIN->sortable_col($table,$split,'td').'
					<td class="center">'.$dane['id'].'</td>
					<td><b>'.$X->STR->outt($dane['login']).'</b></td>
					<td>'.$X->STR->outt($dane['title']).'</td>
					'.(IS_ROOT ? '<td class="center">'.(!empty($dane['css_color']) ? '<div class="color_picker" style="cursor:default; margin:0px;"><div style="background-color:#'.$dane['css_color'].'"></div></div>':'-').'</td>':'').'
					<td class="center">'.$X->STR->outt($dane['last_login']!=0 ? $dane['last_login']:'-').'</td>
					<td class="center"><b>'.$X->STR->out($dane['last_activity']!=0 ? $X->STR->addon('time_ago_tip',$dane['last_activity']):'-').'</b></td>
					<td class="center">'.$X->STR->outt(!empty($dane['last_ip']) ? $dane['last_ip']:'-').'</td>
					<td class="center">'.$X->FORM->ajax_checkbox($this->TABLE, $dane, 'active').'</td>
					'.(IS_ROOT ? '<td class="center">'.$X->FORM->button('<span class="ico w_user"></span>&nbsp;&nbsp;<span class="ico w_right"></span>', $X->URL->get('action=override&id='.$dane['id']),false,'button button_gfx_small2', ($dane['id']==$_SESSION['backend']['user_id'] || $dane['id']==1)).'</td>':'').'
					<td class="center">'.$X->FORM->button('Edytuj', $X->URL->get('action=form&id='.$dane['id'])).'</td>
					<td class="center">'.$X->FORM->button_potwierdzenie('Usuń', $X->URL->get('action=delete&id='.$dane['id']),false,false,false,($dane['id']==$_SESSION['backend']['user_id'] || $dane['id']==1)).'</td>
				</tr>
			';
		}
		return $X->ADMIN->listbox($this, $table, $rows, $split).$html;
	}


	function _form() {
		global $X;
		if($this->USER_GET_DISALLOW) $X->URL->go2('action&id');
		require(START_FORM);

		$REQ[]=array('text', 2, 'login', false, array('unique'=>1, 'table'=>$this->TABLE));
		$REQ[]=array('text', 2, 'title');
		if($new==1) $REQ[]=array('text', 3, 'new_password');

		if(!IS_ROOT) $mod_perm_cond='AND id IN (SELECT module_id FROM _modules2users WHERE user_id="'.$_SESSION['backend']['user_id'].'" )';


		$html='
			'.$X->ADMIN->form_actions($edit).'
			<table class="form padd3">
				<tr>
					<td width="150">'.$X->FORM->label('login','Login').'</td>
					<td>'.$X->FORM->input('login',400,$X->STR->outt($dane['login'])).'</td>
				</tr>
				<tr>
					<td>'.$X->FORM->label('title','Opis').'</td>
					<td>'.$X->FORM->input('title',400,$X->STR->outt($dane['title'])).'</td>
				</tr>
				<tr>
					<td>'.$X->FORM->label('new_password',($new==1 ? 'Hasło':'Nowe hasło')).'</td>
					<td>'.$X->FORM->password('new_password',400,$X->STR->outt($dane['new_password'])).'&nbsp;'.$X->FORM->button_OC('** / AZ','$(this).prev().prop(\'type\', $(this).prev().prop(\'type\')==\'password\' ? \'text\':\'password\');').'</td>
				</tr>
				<tr>
					<td>'.$X->FORM->label('active','Aktywny').'</td>
					<td>'.$X->FORM->checkbox('active',1,$dane['active']).'</td>
				</tr>
				<tr><td><br /></td></tr>
				'.($new==true && IS_ROOT ? '
					<tr>
						<td>'.$X->FORM->label('force_id','Force ID').'</td>
						<td>'.$X->FORM->input_center('force_id',30,$X->STR->outt($dane['force_id'])).'</td>
					</tr>
				':'').'
				<tr>
					<td>'.$X->FORM->label('is_su','Super User').'</td>
					<td>'.$X->FORM->checkbox('is_su',1,$dane['is_su']).'</td>
				</tr>
				<tr><td><br /></td></tr>
				<tr>
					<td class="top">'.$X->FORM->label('modules','Moduły').'</td>
					<td>'.$X->FORM->multiselect('modules',$X->FORM->get_options_tree('SELECT id,title FROM _modules WHERE parent_id="$" '.$mod_perm_cond.' ORDER BY pozycja, id ', '$', 0, $X->DB->get_array(' SELECT module_id FROM _modules2users WHERE user_id="'.$dane['id'].'" ')),400,0,true,true).'</td>
				</tr>
			</table>
			'.$X->ADMIN->form_actions($edit).'
		';
		return $X->ADMIN->formbox($X->FORM->get($FORM_NAME, $REQ, $html));
	}


	function _save() {
		global $X;
		if($this->USER_GET_DISALLOW) $X->URL->go2('action&id');
		if(empty($_GET['id']) && !empty($_POST['force_id'])) $FORCE_INSERT_ID=$_POST['force_id'];
		require(START_SAVE);
		array_push($SAVE, 'login', 'title', 'active');
		if(IS_ROOT) array_push($SAVE, 'is_su');


		if(!empty($_POST['new_password'])) {
			$_POST['password']=md5('d:'.$_POST['new_password'].'<>'.$_POST['new_password'].':h');
			$_POST['auth_checksum']=md5('dh::'.$_POST['new_password'].'@'.$X->STR->outt($_POST['login']).'<<'.(int)$_GET['id']);
			array_push($SAVE, 'password', 'auth_checksum');
		}

		$X->DB->save_multiselect('modules', array('table'=>'_modules2users', 'col'=>'module_id', 'key'=>'user_id', 'val'=>$_GET['id']));
		$X->DB->q(' UPDATE '.$this->TABLE.' SET '.$X->DB->save($SAVE).' WHERE id="'.$_GET['id'].'" ');


		$perm_array=$X->DB->get_array(' SELECT module_id FROM _modules2users WHERE user_id="'.$_GET['id'].'" ORDER BY module_id ');
		$is_su=$X->DB->fast(' SELECT is_su FROM '.$this->TABLE.' WHERE id="'.$_GET['id'].'" ');
		$perm_checksum=md5('mod_perm::'.(int)$_GET['id'].'>>'.$is_su.'>>'.@serialize($perm_array).'@dh');

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

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


	function _delete() {
		global $X;
		if($this->USER_GET_DISALLOW || $_GET['id']==1 || $_GET['id']==$_SESSION['backend']['user_id']) $X->URL->go2('action&id');
		require(START_DELETE);

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

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


	function _override() {
		global $X;
		if($this->USER_GET_DISALLOW || $_GET['id']==1) $X->URL->go2('action&id');

		$user=$X->DB->fast(' SELECT * FROM '.$this->TABLE.' WHERE id="'.$_GET['id'].'" ');

		$_SESSION['backend']['pre_override']['user_id'] 	= $_SESSION['backend']['user_id'];
		$_SESSION['backend']['pre_override']['css_color'] 	= $_SESSION['backend']['css_color'];
		$_SESSION['backend']['pre_override']['is_root'] 	= $_SESSION['backend']['is_root'];

		$_SESSION['backend']['user_id'] = $user['id'];
		$_SESSION['backend']['css_color'] = $user['css_color'];
		unset($_SESSION['backend']['is_root']);
		setcookie('css_color','',-1);

		$X->URL->go('./');
	}

}
?>