<?php

	$model['l'] = array(
		'user_id'		=> false,
		'user_active'	=> false,
		'password'		=> false,
		'last_login_ip'	=> false,
		'last_login'	=> false,
		'name_prepend'	=> false,
		'name_append'	=> false,
		'is_responsible'=> false,
		'comment'		=> false
	);
	
	$model['c'] = array(
		'user_id'		=> false,
		'last_login_ip'	=> false,
		'last_login'	=> false,
		'name_prepend'	=> false,
		'name_append'	=> false
	);
	$crud = new Crud("users",$model);
	
	if (isset($REQUEST_URI[2]) && is_numeric($REQUEST_URI[2])) {
		if($REQUEST_URI[3] == 'delete') {
			$crud->delete($REQUEST_URI[2]);
		} elseif($REQUEST_URI[3] == 'deactivate') {
			$crud->deactivate($REQUEST_URI[2]);
		} elseif($REQUEST_URI[3] == 'activate') {
			$crud->deactivate($REQUEST_URI[2],'activate');
		} elseif($REQUEST_URI[3] == 'edit') {	//todo: RENDER FORM
			$crud->edit($REQUEST_URI[2]);
		}
	}
	if (isset($_POST) && !empty($_POST)) {
		
		//MANUALY DO THE VALIDATION AND INSERT/UPDATE
		$is_active 		= ($_POST['f_user_active'] == 'on') ? 1: 0;
		$is_responsible = ($_POST['f_is_responsible'] == 'on') ? 1: 0;
		$sql = "INSERT INTO users SET 
				user_active = '{$is_active}',
				login_name = '{$_POST['f_login_name']}',
				user_mail = '{$_POST['f_user_mail']}',
				password = '".md5($_POST['f_password'])."',
				user_name = '{$_POST['f_user_name']}',
				user_surname = '{$_POST['f_user_surname']}',
				comment = '{$_POST['f_comment']}',
				phone_nr = '{$_POST['f_phone_nr']}',
				is_responsible = '{$is_responsible}'";
		$db->save($sql);
	}
	
	
	$crud->renderTable();
?>