<?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 ('../../../en.php');}
		$lang = $_SESSION['lang'];

		$array = getGlobalSettings(); foreach ($array as $settings){extract($settings);}
		$array_sms = getSMSSettings(); foreach ($array_sms as $sms_settings){extract($sms_settings);} 
		$page_title = $lbl_page_title_campaign;
		$mode = $lbl_mode_manage;                                      
?>	
<!DOCTYPE html SYSTEM "-//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 
       ##  *** put a primary key on the first place  
      $sql = "SELECT
            sms_outbox.id,	
            sms_outbox.batch_id,
            message_id,
            sms_outbox.phone,
            sms_outbox.status,
            sms_statuses.description AS status_desc,
            DATE_FORMAT(time_sent, '%d-%m-%Y %H:%i') AS time_sent,
            sender,
            message,
            message_url,
            scheduled_time,
            source_module,
            DATE_FORMAT(sms_outbox.created_on, '%d-%m-%Y %H:%i') AS created_on,
            users.id,
            users.username AS created_by,
            DATE_FORMAT(sms_outbox.updated_on, '%d-%m-%Y %H:%i') AS updated_on,
            sms_outbox.updated_by,
            success_flag,
            sms_outbox.campaign_id AS campaign_id,
            sms_campaign_header.description AS campaign_desc,
            message,
            message_url,
            CONCAT (cls_customers.last_name, ' ', cls_customers.first_name) AS cust_name,
            IF (sms_statuses.is_final = 0, '<img src=\"../../../img/get_status.gif\" alt=\"Get_Status\" />', '') AS inquiry
            FROM sms_outbox
            LEFT OUTER JOIN users ON sms_outbox.created_by= users.id 	
            LEFT OUTER JOIN sms_campaign_header ON sms_outbox.campaign_id= sms_campaign_header.id  	
            LEFT OUTER JOIN sms_statuses ON sms_outbox.status= sms_statuses.code
            LEFT OUTER JOIN cls_customers ON sms_outbox.customer_id= cls_customers.id
            WHERE sms_statuses.language = '$lang' and (sms_statuses.provider = '$primary_gateway_provider'
            OR sms_statuses.provider = 'webnail')
			";
  
    ##  *** 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 = "det_";    /* prevent overlays - must be started with a letter */
      $dgrid= new DataGrid($debug_mode, $messaging, $unique_prefix, DATAGRID_DIR);
    ##  *** 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 = "sms_outbox.id";
      $default_order_type = "DESC";
      $dgrid->DataSource($db_conn, $sql, $default_order_field, $default_order_type);
	  //webnail get current mode
	 //$mode = isset($_GET[$unique_prefix.'mode']) ? $_GET[$unique_prefix.'mode'] : "";       
     $mode = isset($_REQUEST[$unique_prefix.'mode']) ? $_REQUEST[$unique_prefix.'mode'] : ""; 
        
    ## +---------------------------------------------------------------------------+
    ## | 2. General Settings:                                                      | 
    ## +---------------------------------------------------------------------------+
	##  *** allow multirow operations
         $postback_method = "ajax";
        $dgrid->SetPostBackMethod($postback_method);
        $multirow_option = false;
        $dgrid->AllowMultirowOperations($multirow_option);
        $multirow_operations = array(
            "edit"  => array("view"=>false),
            "delete"  => array("view"=>false),
            "details" => array("view"=>false), 
			"clone" => array("view"=>false),
        );
        $dgrid->SetMultirowOperations($multirow_operations);
		//webnail language handling
		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"=>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 = 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 = true;
      $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("25"=>"25", "50"=>"50", "100"=>"100", "250"=>"250");
      $default_page_size = 25;
      $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_list_campaign=>array("type"=>"dropdownlist", "table"=>"sms_campaign_header", "field"=>"description", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),	
      $lbl_grid_filter_status=>array("type"=>"dropdownlist", "table"=>"sms_statuses", "field"=>"description", "filter_condition"=>"(provider='".$primary_gateway_provider."' OR provider='webnail')", "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""), 
      $lbl_grid_list_phone=>array("type"=>"textbox", "table"=>"sms_outbox", "field"=>"phone", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"like", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
      $lbl_grid_customer=> array("type"=>"textbox", "autocomplete"=>"false", "maxresults"=>"10", "shownoresults"=>"true", "table"=>"cls_customers", "field"=>"last_name", "show_operator"=>"false", "default_operator"=>"like%", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"", "on_js_event"=>""),    
      //$lbl_grid_sent_on_from=>array("type"=>"calendar", "table"=>"sms_outbox", "field"=>"created_on", "field_type"=>"from", "show_operator"=>"false", "default_operator"=>">=", "calendar_type"=>"floating","case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
      //$lbl_grid_sent_on_to=>array("type"=>"calendar", "table"=>"sms_outbox", "field"=>"created_on", "field_type"=>"to", "show_operator"=>"false", "default_operator"=>"<=", "calendar_type"=>"floating", "case_sensitive"=>"false", "comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
      $lbl_grid_message_id=>array("type"=>"textbox", "table"=>"sms_outbox", "field"=>"message_id", "condition"=>"", "show_operator"=>"false", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "default_operator"=>"=", "on_js_event"=>""),    	
      $lbl_grid_batch_id=>array("type"=>"textbox", "table"=>"sms_outbox", "field"=>"batch_id", "condition"=>"", "show_operator"=>"false", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "default_operator"=>"=", "on_js_event"=>""), 
      //$lbl_grid_route_id=>array("type"=>"textbox", "table"=>"sms_outbox", "field"=>"route_id", "condition"=>"", "show_operator"=>"false", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "default_operator"=>"=", "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
  
	  $vm_colimns = array(
	    "sms_outbox.id" => array("header"=>"id", "type"=>"label", "align"=>"left", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"false", "on_js_event"=>"",  "rel"=>""),
//      "message_id"     => array("header"=>$lbl_grid_message_id, "type"=>"label", "field_key_0"=>"category","field_data"=>"categ", "target"=>"_self", "align"=>"left", "width"=>"30px", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"true", "tooltip_type"=>"floating", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
		"phone" => array("header"=>$lbl_grid_phone, "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"=>""),
        "cust_name" => array("header"=>$lbl_grid_customer, "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"=>""),	
		"status_desc" => array("header"=>$lbl_grid_status, "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"=>""),									
		"time_sent" => array("header"=>$lbl_grid_time_sent, "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"=>""),			
		"sender" => array("header"=>$lbl_grid_sender, "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"=>""),	
		"campaign_desc" => array("header"=>$lbl_campaign, "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"=>""),		
//		"priority_queue" => array("header"=>$lbl_grid_queue, "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"=>""),	
/*		"delivery_delay" => array("header"=>$lbl_grid_delivery_delay, "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"=>""),	*/
//		"scheduled_time" => array("header"=>$lbl_grid_scheduled_time, "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"=>""),			
//		"source_module" => array("header"=>$lbl_grid_source_module, "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"=>""),	
//		"success_flag" => array("header"=>$lbl_grid_success_flag, "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"=>""),											
		"created_on" => array("header"=>$lbl_grid_sent_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"=>""),							
		"created_by" => array("header"=>$lbl_grid_sent_by, "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"=>""),	
//      "batch_id"  => array("header"=>$lbl_grid_batch_id, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "tooltip"=>"true", "tooltip_type"=>"floating", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"",  "rel"=>""),			
    	'inquiry'=>array("header"=>_UPDATE_STATUS, "type"=>"link", "align"=>"center", "summarize"=>"false", "visible"=>"true", "on_js_event"=>"", "field_key"=>"message_id", "field_key_1"=>"field_name_1", "field_data"=>"field_name_2", "rel"=>"", "target"=>"_self", "href"=>"sms_get_status.php?msgid={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
      		//webnail language handling
	  
	  $table_name  = "sms_outbox";
      $primary_key = "id";
      $condition   = "";
      $dgrid->SetTableEdit($table_name, $primary_key, $condition);	     
	  $em_columns = array(
	  		"id"   =>array("header"=>$lbl_grid_code, "type"=>"label", "width"=>"30px"),
            "phone"   =>array("header"=>$lbl_grid_phone, "type"=>"label", "width"=>"30px"),			
            "status_desc"=>array("header"=>$lbl_grid_status, "type"=>"label"),	
			"customer_id"=>array("header"=>$lbl_grid_customer, "type"=>"label"),
            "campaign_id" =>array("header"=>$lbl_grid_campaign_id, "type"=>"label"),
            "time_sent"=>array("header"=>$lbl_grid_time_sent, "type"=>"datetimedmy", "value"=>date("d-m-Y H:i:s")),
			"sender"=>array("header"=>$lbl_grid_sender, "type"=>"label"), 
			"priority_queue"=>array("header"=>$lbl_grid_queue, "type"=>"label"),
			"delivery_delay"=>array("header"=>$lbl_grid_delivery_delay, "type"=>"label"),
			"scheduled_time"=>array("header"=>$lbl_grid_scheduled_time, "type"=>"label"),
			"source_module"=>array("header"=>$lbl_grid_source_module, "type"=>"label"),
			"success_flag"=>array("header"=>$lbl_grid_success_flag, "type"=>"label"),
			"message_id"=>array("header"=>$lbl_grid_message_id, "type"=>"label"),
			"message"=>array("header"=>$lbl_grid_message, "type"=>"label"),
			"message_url"=>array("header"=>$lbl_grid_message_url, "type"=>"label"),
            //"route_id"=>array("header"=>$lbl_grid_route_id, "type"=>"label"),			
            "batch_id"=>array("header"=>$lbl_grid_batch_id, "type"=>"label"),
			"created_on" =>array("header"=>$lbl_grid_sent_on, "type"=>"datetimedmy", "value"=>date("d-m-Y H:i:s")),	
            "created_by" =>array("header"=>$lbl_grid_sent_by,  "type"=>"foreign_key"),	
            "updated_on" =>array("header"=>$lbl_grid_updated_on, "type"=>"datetimedmy", "value"=>date("d-m-Y H:i:s")),			
            "updated_by" =>array("header"=>$lbl_grid_updated_by,  "type"=>"foreign_key"),														
      );	   	

      $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(
        "campaign_id"=>array("table"=>"sms_campaign_header", "field_key"=>"id", "field_name"=>"description", "view_type"=>"dropdownlist", "order_by_field"=>"id", "order_type"=>"ASC", "on_js_event"=>""),	
        "created_by"=>array("table"=>"users", "field_key"=>"id", "field_name"=>"username", "view_type"=>"dropdownlist", "order_by_field"=>"id", "order_type"=>"ASC", "on_js_event"=>""),		
        "updated_by"=>array("table"=>"users", "field_key"=>"id", "field_name"=>"username", "view_type"=>"dropdownlist", "order_by_field"=>"id", "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>
		</fieldset>
	</div>
</body>
</html>
<?php
} else {
    header('Location: login.php');
}
?>