<?php session_start();
		if (isset($_SESSION['logged']) && $_SESSION['logged'] == 'yes') {
			include ('scripts/classes.php');if ($_SESSION['lang']=="EN"){$lang="EN";include ('lang/en.php');}elseif ($_SESSION['lang']=="GR"){$lang="GR";include ('lang/gr.php'); }else {$lang="EN"; include ('lang/en.php'); }
			$mods = getCLSModules('POS'); foreach ($mods as $cls_modules){extract($cls_modules);} if ($mod_name == "POS") $_POS=1; //echo $_POS;
			$user = $_SESSION['user'];
			$global = getGlobalSettings(); foreach ($global as $glo_settings){extract($glo_settings);}	
			$mods = getCLSModules('POS'); foreach ($mods as $modules){extract($modules);} if ($mod_name == true) $_POS=1; //echo $_POS;				
			$page_title = $lbl_page_title_user; //Assign Page Title
			$mode = $lbl_mode_manage_users; //Assign Mode
?>	
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $page_title ?></title>
<link rel="shortcut icon" href="img/favicon.png" />
<link href="cms1.css" rel="stylesheet" type="text/css" /> 
</head>
<body>
<script>top.document.title=self.document.title;</script>
	<div>
		<fieldset>
		<legend><?php echo $mode ?></legend>
			<div class="align-left">
			<?php
    ################################################################################
    ## +---------------------------------------------------------------------------+
    ## | 1. Creating & Calling:                                                    | 
    ## +---------------------------------------------------------------------------+
    ##  *** define a relative (virtual) path to datagrid.class.php file
    ##  *** (relatively to the current file)
    ##  *** RELATIVE PATH ONLY ***     
      define ("DATAGRID_DIR", "datagrid/");                     /* Ex.: "datagrid/" */ 
      define ("PEAR_DIR", "datagrid/pear/");                    /* Ex.: "datagrid/pear/" */
    
      require_once(DATAGRID_DIR.'datagrid.class.php');
      require_once(PEAR_DIR.'PEAR.php');
      require_once(PEAR_DIR.'DB.php');
    
    ##
    ##  *** creating variables that we need for database connection 

      // includes database connection parameters
      include_once('datagrid/config.inc.php');
    
      ob_start();
      $db_conn = DB::factory('mysql');  /* don't forget to change on appropriate db type */
      $result_conn = $db_conn->connect(DB::parseDSN('mysql://'.$DB_USER.':'.$DB_PASS.'@'.$DB_HOST.'/'.$DB_NAME));
      if(DB::isError($result_conn)){ die($result_conn->getDebugInfo()); }  
	  
    ##  *** put a primary key on the first place 
      $sql = "SELECT
			users.id,
			users.name as name,
			username,
			email,
			password,
			block,
			sendEmail,
			usertype,
			gid,
			DATE_FORMAT(registerDate, '%d-%m-%Y %H:%i') as registerDate,
			lastvisitDate,
			users.active,
			params,
			core_printers.alias as printer_name,
			usertypes.name as usertype_name,
            BRA.branch_name as branch,
			'<img src=\"img/unlock.gif\" alt=\"Unlock\" />' as un_lock
            FROM users
			LEFT OUTER JOIN usertypes ON users.usertype= usertypes.id
			LEFT OUTER JOIN core_printers ON users.default_printer=core_printers.id 
            LEFT OUTER JOIN cls_branches BRA ON users.branch_id=BRA.id 
			WHERE username not in ('webnail', 'system')
			";

    ##  *** set needed options and create a new class instance 
      $debug_mode = false;        /* display SQL statements while processing */    
      $messaging = true;          /* display system messages on a screen */ 
      $unique_prefix = "use_";    /* prevent overlays - must be started with a letter */
      $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix, DATAGRID_DIR);
	  //webnail get current mode
	  $mode = isset($_GET[$unique_prefix.'mode']) ? $_GET[$unique_prefix.'mode'] : ""; 
	  
    ##  *** set encoding and collation (default: utf8/utf8_unicode_ci)
      $dg_encoding = "utf8";
      $dg_collation = "utf8_unicode_ci";
      $dgrid->SetEncoding($dg_encoding, $dg_collation);
    ##  *** set data source with needed options
      $default_order_field = "id";
      $default_order_type = "ASC";
      $dgrid->DataSource($db_conn, $sql, $default_order_field, $default_order_type);
      
    
    ## +---------------------------------------------------------------------------+
    ## | 2. General Settings:                                                      | 
    ## +---------------------------------------------------------------------------+
	##  *** allow multirow operations
        $multirow_option = false;
        $dgrid->AllowMultirowOperations($multirow_option);
        $multirow_operations = array(
            "edit"  => array("view"=>false),
            "delete"  => array("view"=>false),
            "details" => array("view"=>false), 
        );
        $dgrid->SetMultirowOperations($multirow_operations);
		//webnail
		if ($_SESSION['lang']=="EN"){$dg_language = "en";}elseif ($_SESSION['lang']=="GR"){	$dg_language = "gk";}else{$dg_language = "en";}
		$dgrid->SetInterfaceLang($dg_language);  
         ##  *** set modes for operations ("type" => "link|button|image")
    ##  *** "view" - view mode | "edit" - add/edit/details modes
    ##  *** "byFieldValue"=>"fieldName" - make the field to be a link to edit mode page
    $modes = array(
         "add"	  =>array("view"=>true, "edit"=>false, "type"=>"button", "show_add_button"=>"outside"),
         "edit"	  =>array("view"=>true, "edit"=>true,  "type"=>"image" ),
         "cancel"  =>array("view"=>true, "edit"=>true,  "type"=>"button"),
         "details" =>array("view"=>true, "edit"=>true, "type"=>"image"),
         "delete"  =>array("view"=>false, "edit"=>false,  "type"=>"image"),

    );
    $dgrid->SetModes($modes);   
    ##  *** set CSS class for datagrid
    ##  *** "default|blue|gray|green|pink|empty|x-blue|x-gray|x-green" or your own css style
     $css_class = "default";
     $dgrid->SetCssClass($css_class);
    ##  *** set DataGrid caption
    $dg_caption = "";
      $dgrid->SetCaption($dg_caption);
    
    ## +---------------------------------------------------------------------------+
    ## | 3. Printing & Exporting Settings:                                         | 
    ## +---------------------------------------------------------------------------+
    ##  *** set printing option: true(default) or false 
      $printing_option = true;
      $dgrid->AllowPrinting($printing_option);
	  $exporting_option = true;
		$exporting_directory = "exports/";               
		$export_all = true;
		$dgrid->AllowExporting($exporting_option, $exporting_directory, $export_all);
		$exporting_types = array("excel"=>"false", "pdf"=>"false", "xml"=>"false");
		$dgrid->AllowExportingTypes($exporting_types);
    
    ## +---------------------------------------------------------------------------+
    ## | 4. Sorting & Paging Settings:                                             | 
    ## +---------------------------------------------------------------------------+
    ##  *** set paging option: true(default) or false 
      $paging_option = true;
      $rows_numeration = false;
      $numeration_sign = "#";       
      $dgrid->AllowPaging($paging_option, $rows_numeration, $numeration_sign);
    ##  *** set paging settings
      $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
      $top_paging = array();
      $pages_array = array("20"=>"20", "25"=>"25", "50"=>"50", "100"=>"100", "250"=>"250", "500"=>"500", "1000"=>"1000");
      $default_page_size = 20;
      $dgrid->SetPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size);
    
    ## +---------------------------------------------------------------------------+
    ## | 5. Filter Settings:                                                       | 
    ## +---------------------------------------------------------------------------+
    ##  *** set filtering option: true or false(default)
      $filtering_option = true;
      $show_search_type = true;
      $dgrid->AllowFiltering($filtering_option, $show_search_type);
    ##  *** set additional filtering settings
    ##  *** tips: use "," (comma) if you want to make search by some words, for ex.: hello, bye, hi
      $filtering_fields = array(
      $lbl_grid_username=>array("type"=>"textbox", "table"=>"users", "field"=>"username", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"like", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),	  
      $lbl_grid_user_name=>array("type"=>"textbox", "table"=>"users", "field"=>"name", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"like", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
//$lbl_grid_usertype=>array("type"=>"dropdownlist", "table"=>"usertypes", "field"=>"name", "condition"=>"id<>1", "show_operator"=>"false", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),    
//$lbl_grid_user_active=>array("type"=>"checkbox", "table"=>"users", "field"=>"active", "condition"=>"", "show_operator"=>"false", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),   
	 );
      $dgrid->SetFieldsFiltering($filtering_fields);
    
    ## +---------------------------------------------------------------------------+
    ## | 6. View Mode Settings:                                                    | 
    ## +---------------------------------------------------------------------------+
    ##  *** set view mode table properties
      $vm_table_properties = array("width"=>"100%");
      $dgrid->SetViewModeTableProperties($vm_table_properties);  
    ##  *** set columns in view mode
    ##  *** Ex.: "on_js_event"=>"onclick='alert(\"Yes!!!\");'"
    ##  ***      "barchart" : number format in SELECT SQL must be equal with number format in max_value
      if ($_POS==1 and $print_receipt == 1 ){$printer="true";}else{$printer="false";};
	 // echo $printer;
	  $vm_colimns = array(
        "id" => array("header"=>$lbl_grid_code, "type"=>"label", "align"=>"left", "width"=>"20px", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"true", "tooltip_type"=>"floating", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"",  "field_data"=>"", "rel"=>"", "title"=>""),
        "username"     => array("header"=>$lbl_grid_username, "type"=>"label", "target"=>"_self", "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"true", "tooltip_type"=>"floating", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
		"name" => array("header"=>$lbl_grid_user_name, "type"=>"label",      "align"=>"left", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),	
		//"email" => array("header"=>$lbl_grid_user_email, "type"=>"label",      "align"=>"left", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),		
		"usertype_name" => array("header"=>$lbl_grid_usertype, "type"=>"label",      "align"=>"left", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),				
        "branch" => array("header"=>_BRANCH, "type"=>"label",      "align"=>"left", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
        "printer_name" => array("header"=>$lbl_grid_default_printer, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "sort_type"=>"string", "sort_by"=>"", "visible"=>$printer, "on_js_event"=>""),		
		"registerDate" => array("header"=>$lbl_grid_created_on, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),	
		"active"=>array("header"=>$lbl_grid_user_active, "type"=>"checkbox",  "align"=>"center", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "true_value"=>1, "false_value"=>0),
    	'un_lock'=>array("header"=>$lbl_grid_custom_unlock, "type"=>"link", "align"=>"center", "summarize"=>"false", "visible"=>"true", "on_js_event"=>"onClick=\"return confirmSubmit()\"", "field_key"=>"id", "field_key_1"=>"field_name_1", "field_data"=>"field_name_2", "rel"=>"", "target"=>"_self", "href"=>"users.php?use_mode=unlock&use_rid={0}", "sortable"=>"false"),			   					      				
      );
      $dgrid->SetColumnsInViewMode($vm_colimns);
    
    
    ## +---------------------------------------------------------------------------+
    ## | 7. Add/Edit/Details Mode Settings:                                        | 
    ## +---------------------------------------------------------------------------+
    ##  *** set add/edit mode table properties
      $em_table_properties = array("width"=>"100%");
      $dgrid->SetEditModeTableProperties($em_table_properties);
    ##  *** set details mode table properties
      $dm_table_properties = array("width"=>"100%");
      $dgrid->SetDetailsModeTableProperties($dm_table_properties);
    ##  ***  set settings for add/edit/details modes
      $table_name  = "users";
      $primary_key = "id";
      $condition   = "";
      $dgrid->SetTableEdit($table_name, $primary_key, $condition);
	  $em_columns=array();
   	if ($mode=="add"){	  
	?>   
		<script type="text/javascript">
			window.location = "user_add.php"
		</script>
	<?php exit();														
	  }elseif ($mode=="details"){
	  $em_columns = array(
            "id"   =>array("header"=>$lbl_grid_code, "type"=>"label"),			
            "username"=>array("header"=>$lbl_grid_username, "type"=>"label"),	
            "name" =>array("header"=>$lbl_grid_user_name, "type"=>"label"),
            "email"=>array("header"=>$lbl_grid_user_email, "type"=>"label"), 
			"usertype"=>array("header"=>$lbl_grid_usertype, "type"=>"foreign_key"),
            "branch_id"=>array("header"=>_BRANCH, "type"=>"foreign_key"),  
			"default_printer"=>array("header"=>$lbl_grid_default_printer, "type"=>"foreign_key", "visible"=>$printer), 
			"registerDate" =>array("header"=>$lbl_grid_created_on, "type"=>"datetimedmy", "value"=>date("d-m-Y H:i:s")),					
            "active" =>array("header"=>$lbl_grid_user_active,  "type"=>"checkbox"),														
      );	   
	  }elseif ($mode=="edit"){
	  $em_columns = array(
            "id"   =>array("header"=>$lbl_grid_code, "type"=>"label", "width"=>"30px", "title"=>"", "readonly"=>"true", "visible"=>"true"),			
            "username"=>array("header"=>$lbl_grid_username, "type"=>"label", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"true", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
			"name"=>array("header"=>$lbl_grid_user_name, "type"=>"textbox", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
            "email"=>array("header"=>$lbl_grid_user_email, "type"=>"textbox", "req_type"=>"re", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),	
            "usertype"=>array("header"=>$lbl_grid_usertype, "type"=>"textbox", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1",  "visible"=>"true"),	
            "branch_id"=>array("header"=>_BRANCH, "type"=>"textbox", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1",  "visible"=>"true"),
            "default_printer"=>array("header"=>$lbl_grid_default_printer, "type"=>"textbox", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1",  "visible"=>$printer),																								
      );	
	  }  
      $dgrid->SetColumnsInEditMode($em_columns);
    ##  *** set auto-generated columns in edit mode
      $auto_column_in_edit_mode = false;
      $dgrid->SetAutoColumnsInEditMode($auto_column_in_edit_mode);
    ##  *** set foreign keys for add/edit/details modes (if there are linked tables)
    ##  *** Ex.: "field_name"=>"CONCAT(field1,','field2) as field3" 
    ##  *** Ex.: "condition"=>"TableName_1.FieldName > 'a' AND TableName_1.FieldName < 'c'"
    ##  *** Ex.: "on_js_event"=>"onclick='alert(\"Yes!!!\");'"
      $foreign_keys = array(
        "usertype"=>array("table"=>"usertypes", "field_key"=>"id", "field_name"=>"name", "condition"=>"mask<>0", "view_type"=>"dropdownlist", "order_by_field"=>"mask", "order_type"=>"ASC", "on_js_event"=>""),
        "branch_id"=>array("table"=>"cls_branches", "field_key"=>"id", "field_name"=>"branch_name", "view_type"=>"dropdownlist", "order_by_field"=>"id", "order_type"=>"ASC", "on_js_event"=>""),
        "default_printer"=>array("table"=>"core_printers", "field_key"=>"id", "field_name"=>"alias", "condition"=>"active=1", "view_type"=>"dropdownlist", "order_by_field"=>"", "order_type"=>"ASC", "on_js_event"=>""),												
      ); 
      $dgrid->SetForeignKeysEdit($foreign_keys);
     
    ## +---------------------------------------------------------------------------+
    ## | 8. Bind the DataGrid:                                                     | 
    ## +---------------------------------------------------------------------------+
    ##  *** bind the DataGrid and draw it on the screen
      $dgrid->Bind();        
      ob_end_flush();
	  
?>
<script>
function confirmSubmit()
{
    var agree=confirm("<?php echo $lbl_confirm_unlock_user ?>");
    if (agree) return true; else return false; 
}
</script>
<?php if(isset($_GET[$unique_prefix.'mode']) && ($_GET[$unique_prefix.'mode'] == "unlock") ){
    // PREPARE THE IDs selected FOR A QUERY
   	$rid = $_GET['use_rid'];	
	mysqli_query ($mysqli, "UPDATE users SET password = password(username) WHERE id='$rid'") or die(mysqli_error($mysqli));
   ?>
   	<script>alert('<?php echo $lbl_user_unlocked ?>');</script>
<?php } ?>

			</div>
		</fieldset>
	</div>
</body>
</html>
<?php
} else {
    header('Location: login.php');
}
?>
