<?php session_start();
		if (isset($_SESSION['logged']) && $_SESSION['logged'] == 'yes') {
		include ('../scripts/classes.php'); //Retrieve functions
		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');}

		$array = getGlobalSettings(); foreach ($array as $settings){extract($settings);}
		$page_title = $lbl_page_title_customers; //Assign Page Title
		$mode=$lbl_mode_manage_customers; //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" /> 
<link rel="stylesheet" href="../scripts/jkit/css/jkit.css" />
</head>
<body>
<script>top.document.title=self.document.title;</script>
	<div>
	<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
			cls_customers.id,
			barcode_id,
			group_id,
			erp_code,
			cls_customer_groups.name,
			first_name,
			last_name,
            EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(), birth_date)))) AS age,
			DATE_FORMAT(cls_customers.birth_date, '%d-%m-%Y') AS birth_date,
			company,
			b_address,
			b_address_2,
			b_city,
			b_zipcode,
			b_country,
			b_state,
			phone,
			phone2,
			fax,
			comments,
			industry,
			last_transaction_date as last_trans_date,
			DATE_FORMAT(cls_customers.date_created, '%d-%m-%Y') AS date_created, 
			DATE_FORMAT(cls_customers.last_transaction_date, '%d-%m-%Y (%H:%i)') AS last_transaction_date,
			is_active,
			credits,
			score,
			turnover,
			redeem_amount,
            email,
            facebook,
            image,
            custom_1,
            custom_2,
            custom_3,
            '<img src=\"../img/select.gif\" alt=\"$lbl_discharge\">' AS pick_me
            FROM cls_customers 
			LEFT OUTER JOIN cls_customer_groups ON cls_customers.group_id = cls_customer_groups.id 
			";
            //DATE_FORMAT(a.date_created, '%d-%m-%Y %H:%i') as date_created,

    ##  *** 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 = "cas_";    /* prevent overlays - must be started with a letter */
      $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix, DATAGRID_DIR);
	  //webnail get current mode
	  $mode = isset($_REQUEST[$unique_prefix.'mode']) ? $_REQUEST[$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 = "DESC";
      $dgrid->DataSource($db_conn, $sql, $default_order_field, $default_order_type);
    ## +---------------------------------------------------------------------------+
    ## | 2. General Settings:                                                      | 
    ## +---------------------------------------------------------------------------+
    ## +-- PostBack Submission Method ---------------------------------------------+
    ##  *** defines postback submission method for DataGrid: AJAX, POST or GET(default)
    $postback_method = "ajax";
    $dgrid->SetPostBackMethod($postback_method);
    ##  *** 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"=>false, "edit"=>false, "type"=>"button", "show_add_button"=>"outside"),
         "edit"	  =>array("view"=>false, "edit"=>false,  "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 = false;
		$dgrid->AllowExporting($exporting_option, $exporting_directory, $export_all);
		$exporting_types = array("excel"=>"true", "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("15"=>"15", "20"=>"20", "50"=>"50", "100"=>"100");
      $default_page_size = 15;
      $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_last_name=>array("type"=>"textbox", "table"=>"cls_customers", "field"=>"last_name", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"like", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),	
      $lbl_grid_first_name=>array("type"=>"textbox", "table"=>"cls_customers", "field"=>"first_name", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"like", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),  
      $lbl_grid_barcode=>array("type"=>"textbox", "table"=>"cls_customers", "field"=>"barcode_id", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"like", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
      $lbl_grid_cat_group=>array("type"=>"dropdownlist", "table"=>"cls_customer_groups", "field"=>"name", "show_operator"=>"false", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),           
	 );
      $dgrid->SetFieldsFiltering($filtering_fields);
    
    $fill_from_array = array("0"=>$lbl_no, "1"=>$lbl_yes);
    $about_us_array = array(""=>_SELECT_VALUE, _INTERNET_SEARCH =>_INTERNET_SEARCH, _SAW_THE_SIGN=>_SAW_THE_SIGN, _REFERENCE=>_REFERENCE, _SOCIAL_MEDIA=>_SOCIAL_MEDIA, _DO_NOT_REMEMBER=>_DO_NOT_REMEMBER); #WI 1.4.6
    ## +---------------------------------------------------------------------------+
    ## | 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
      $vm_colimns = array(
        'pick_me'=>array("header"=>_PICK, "type"=>"link", "align"=>"center", "summarize"=>"false", "visible"=>"true", "on_js_event"=>"onClick=\"parent.PassParam('{0}','{1}', '{2}')\"", "field_key"=>"id", "field_key_1"=>"last_name", "field_key_2"=>"first_name", "field_data"=>"field_name_2", "rel"=>"", "target"=>"_self", "href"=>"", "sortable"=>"false"),
        "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"=>""), 	  
        "barcode_id" => array("header"=>$lbl_grid_barcode, "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"=>""),
        "last_name"     => array("header"=>$lbl_grid_last_name, "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"=>""),
		"first_name" => array("header"=>$lbl_grid_first_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"=>""),		
		"name" => array("header"=>$lbl_grid_cat_group, "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"=>""),		
		//"credits" => array("header"=>$lbl_grid_customer_credits, "type"=>"label",      "align"=>"right", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
		//"score" => array("header"=>$lbl_grid_score, "type"=>"label",      "align"=>"right", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
		//"turnover" => array("header"=>$lbl_grid_turnover, "type"=>"money", "align"=>"right", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"","sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"2", "dec_separator"=>",", "thousands_separator"=>"."),
		//"redeem_amount" => array("header"=>$lbl_grid_redeem_amount, "type"=>"money", "align"=>"right", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"","sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"2", "dec_separator"=>",", "thousands_separator"=>"."),		
		"age" => array("header"=>_AGE, "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"=>""),	
		//"birth_date" => array("header"=>$lbl_grid_birth_date, "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"=>""),
        //"last_transaction_date" => array("header"=>$lbl_last_transaction_date, "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"=>""),			
		//"is_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),	      				
        "is_active"=>array("header"=>$lbl_grid_user_active, "type"=>"enum", "align"=>"left", "text_length"=>"-1", "tooltip"=>"true", "tooltip_type"=>"floating",  "sort_type"=>"numeric", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "source"=>$fill_from_array),
      );
      $dgrid->SetColumnsInViewMode($vm_colimns);
    
    
    ## +---------------------------------------------------------------------------+
    ## | 7. Add/Edit/Details Mode Settings:                                        | 
    ## +---------------------------------------------------------------------------+
    ##  *** set add/edit mode table properties
	$em_columns= array();
      $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  = "cls_customers";
      $primary_key = "id";	
      $condition   = "";
      $dgrid->SetTableEdit($table_name, $primary_key, $condition);      
  												
	  if ($mode=="details"){
	   			//Reference Match Query
	  $em_columns = array(
            "image"  =>array("header"=>_IMAGE, "type"=>"image", "req_type"=>"st", "width"=>"300px", "title"=>_IMAGE, "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "target_path"=>"img/customers/", "max_file_size"=>"30K", "image_width"=>"120px", "image_height"=>"120px", "resize_image"=>"false", "resize_width"=>"120", "resize_height"=>"120", "magnify"=>"true", "magnify_type"=>"magnifier", "magnify_power"=>"2",  "host"=>"local", "allow_downloading"=>"false", "allowed_extensions"=>"jpg"),
            "id"   =>array("header"=>$lbl_grid_code, "type"=>"label"),			
            "last_name"=>array("header"=>$lbl_grid_last_name, "type"=>"label"),	
            "first_name" =>array("header"=>$lbl_grid_first_name, "type"=>"label"),
            "barcode_id"=>array("header"=>$lbl_grid_barcode, "type"=>"label"), 
			"company"=>array("header"=>$lbl_grid_company, "type"=>"label"),
			"industry"=>array("header"=>$lbl_grid_industry, "type"=>"label"),
			"group_id"=>array("header"=>$lbl_grid_cat_group, "type"=>"foreign_key"), 
			"birth_date" =>array("header"=>$lbl_grid_birth_date, "type"=>"datedmy", "value"=>date("d-m-Y")),	
			"date_created" =>array("header"=>$lbl_grid_created_on, "type"=>"datetimedmy", "value"=>date("d-m-Y H:i:s")),	
			"last_transaction_date" =>array("header"=>$lbl_last_transaction_date, "type"=>"datetimedmy", "value"=>date("d-m-Y H:i:s")),			
			"credits"=>array("header"=>$lbl_grid_customer_credits, "type"=>"label"),
			"score"=>array("header"=>$lbl_grid_score, "type"=>"label"),
			"turnover" => array("header"=>$lbl_grid_turnover, "type"=>"money", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"2", "dec_separator"=>",", "thousands_separator"=>"."),
			"redeem_amount" => array("header"=>$lbl_grid_redeem_amount, "type"=>"money", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"2", "dec_separator"=>",", "thousands_separator"=>"."),			
			"b_address"=>array("header"=>$lbl_grid_address, "type"=>"label"),
			"b_address_2"=>array("header"=>_MUNICIPALITY, "type"=>"label"),
			"b_city"=>array("header"=>$lbl_grid_city, "type"=>"label"),
			"b_zipcode"=>array("header"=>$lbl_grid_zipcode, "type"=>"label"),
			"b_state"=>array("header"=>$lbl_grid_state, "type"=>"label"),
			"phone"=>array("header"=>_MOBILE_PHONE, "type"=>"label"),
			"phone2"=>array("header"=>$lbl_grid_phone."(2)", "type"=>"label"),
			"fax"=>array("header"=>$lbl_grid_fax, "type"=>"label"),
            "email" =>array("header"=>_EMAIL,  "type"=>"link",   "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "field_key"=>"email", "field_key_1"=>"0", "field_data"=>"email", "rel"=>"", "title"=>"", "target"=>"_self",  "href"=>"mailto:{0}"),
			"facebook" =>array("header"=>_FACEBOOK_ACCOUNT,  "type"=>"link", "visible"=>"true",  "field_key"=>"facebook", "field_key_1"=>"0", "field_data"=>"facebook", "title"=>_FACEBOOK_ACCOUNT, "target"=>"_new", "href"=>"{0}"),
            "is_active" =>array("header"=>$lbl_grid_user_active,  "type"=>"checkbox"),
            "erp_code"=>array("header"=>$lbl_grid_erp_code, "type"=>"label"),
			"comments"=>array("header"=>$lbl_grid_comments, "type"=>"label"),
            //"custom_1"=>array("header"=>_PHOTOTYPE, "type"=>"label"),
            "custom_2"   =>array("header"=>_HOW_YOU_LEARNED_ABOUT_US, "type"=>"label"),
            "custom_3"=>array("header"=>_REFERENCE_CUSTOMER, "type"=>"hidden", "autocomplete"=>"false", "req_type"=>"sy", "width"=>"50px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false"),
            "delimiter_2"=>$res_cust_link,
      );	   	
	  }  
      $dgrid->SetColumnsInEditMode($em_columns);
      $auto_column_in_edit_mode = false;
      $dgrid->SetAutoColumnsInEditMode($auto_column_in_edit_mode);
      $foreign_keys = array(
        "group_id"=>array("table"=>"cls_customer_groups", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "order_by_field"=>"id", "order_type"=>"ASC", "on_js_event"=>""),	
        "b_state"=>array("table"=>"core_regions", "field_key"=>"name", "field_name"=>"name", "view_type"=>"dropdownlist", "order_by_field"=>"priority_order", "order_type"=>"ASC", "on_js_event"=>""),						
      ); 
      $dgrid->SetForeignKeysEdit($foreign_keys);
    ## +---------------------------------------------------------------------------+
    ## | 8. Bind the DataGrid:                                                     | 
    ## +---------------------------------------------------------------------------+
    ##  *** bind the DataGrid and draw it on the screen
      $dgrid->initFilteringState = "opened";
	  $dgrid->firstFieldFocusAllowed = true;
	  $dgrid->Bind();        
      ob_end_flush();
?>

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