Connector Template

Base Import Index

<?php
require_once(dirname(__FILE__).'/../connect.php');
/**
Created by connect.php
Variables
	$dbo                         **Always
	$jobs_obj                    **Always
	$user_obj                    **Always
	$file_importer_locations_obj **Always
	$source_obj                  **load+refresh
	$con_obj                     **load+refresh
	$dat_obj                     **load+refresh
Functions
	saveInfo($con_info,$dat_info,$jobs_obj,$dbo)
**/
ini_set('display_errors', 'On');
date_default_timezone_set('America/New_York');
$output = array();
if ($_REQUEST['mode']) {
	switch ($_REQUEST['mode']) {
	/**
		Add new Wizard
	**/
	case "con":
		$html = create_connection(array(
			"user_obj" => $user_obj
		));
		if(isset($html['error'])) {
			$output = array('status'=>'fail');
			$output['message'] = $html;
		} else {
			$output = array('status'=>'success');
			$output['message'] = array(
				"steps" => 3,
				"html" => $html
			);
		}
		break;
	/**
		Add new Wizard
	**/
	case "dat":
		$html = start_connection(array(
			"user_obj" => $user_obj,
		));
		if(isset($html['error'])) {
			$output = array('status'=>'fail');
			$output['message'] = $html;
		} else {
			$output = array('status'=>'success');
			$output['message'] = array(
				"steps" => 2,
				"html" => $html
			);
		}
		break;
	/**
		Initial Table Load
	**/
	case "load":
		$output = load_data(array(
			"user_obj"   => $user_obj,
			"source_obj" => $source_obj,
			"con_obj"    => $con_obj,
			"dat_obj"    => $dat_obj,
			"jobs_obj"   => $jobs_obj,
			"db_obj"     => $dbo,
		));
		break;
	/**
		Reoccuring Table Load
	**/
	case "refresh":
		$output = refresh_data(array(
			"user_obj"   => $user_obj,
			"source_obj" => $source_obj,
			"con_obj"    => $con_obj,
			"dat_obj"    => $dat_obj,
			"jobs_obj"   => $jobs_obj,
			"db_obj"     => $dbo,
		));
		break;
	}
}
 
function create_connection($utils = array()) {
	$html = array();
	if ($_REQUEST['step'] == 1 || $_REQUEST['step'] == 'final') {
		$html[] = array(
			"display"=>"Host",
			"id"=>"apicon_host",
			"name"=>"apicon_host",
			"helptext"=>"",
			"required"=>"1",
			"readonly"=>"0",
			"type"=>"text",
			"value"=>""
		);
	}
	if ($_REQUEST['step'] == 2 || $_REQUEST['step'] == 'final') {
		$html[] = array(
			"display"=>"Path",
			"id"=>"apicon_path",
			"name"=>"apicon_path",
			"helptext"=>"Only change if a special case.",
			"required"=>"1",
			"readonly"=>"0",
			"type"=>"text",
			"value"=>"/"
		);
		$options = array();
		$options[] = API_make_options("0","No",0);
		$options[] = API_make_options("1","Yes",0);
		$html[] = array(
			"display"=>"Binary",
			"id"=>"apicon_binary",
			"name"=>"apicon_binary",
			"helptext"=>"",
			"required"=>"1",
			"readonly"=>"0",
			"type"=>"select",
			"options"=>$options,
			"value"=>"0"
		);
	}
	return $html;
}
 
function start_connection($utils = array()) {
	$html = array();
	if ($_REQUEST['step'] == 1 || $_REQUEST['step'] == 'final') {
		$html[] = array(
			"display"=>"Name",
			"id"=>"apicon_name",
			"name"=>"apicon_name",
			"helptext"=>"",
			"required"=>"1",
			"readonly"=>"0",
			"type"=>"text",
			"value"=>""
		);
	}
	if ($_REQUEST['step'] == 2 || $_REQUEST['step'] == 'final') {
		$options[] = API_make_options("0","No",0);
		$options[] = API_make_options("1","Yes",0);
		$options[] = API_make_options("2","Move to Historic folder",0);
		$html[] = array(
			"display"=>"Delete Files",
			"id"=>"apidat_del",
			"name"=>"apidat_del",
			"helptext"=>"This will automatically remove files from the ftp to prevent the files from getting picked up again.",
			"required"=>"1",
			"readonly"=>"0",
			"type"=>"select",
			"options"=>$options,
			"value"=>"1"
		);
		$options = array();
		$options[] = API_make_options("0","No",0);
		$options[] = API_make_options("1","Yes",0);
		$html[] = array(
			"display"=>"Truncate Table Before Import",
			"id"=>"apidat_truncate",
			"name"=>"apidat_truncate",
			"helptext"=>"This will permanently delete all records in the table before importing.",
			"required"=>"1",
			"readonly"=>"0",
			"type"=>"select",
			"options"=>$options,
			"value"=>"0"
		);
		$html[] = array(
			"display"=>"Skip Row(s)",
			"id"=>"apidat_skiprows",
			"name"=>"apidat_skiprows",
			"helptext"=>"Enter the number of rows, if any, that need to be skipped to get to the 'Header Row'.",
			"required"=>"0",
			"readonly"=>"0",
			"type"=>"text",
			"value"=>""
		);
		$html[] = array(
			"display"=>"Are there Custom Field types that need to be run?",
			"id"=>"apidat_customfields",
			"name"=>"apidat_customfields",
			"helptext"=>"Only select Yes if there will be custom field types that need executed.",
			"required"=>"1",
			"readonly"=>"0",
			"type"=>"select",
			"options"=>$options,
			"value"=>"0"
		);
		if(isset($utils['user_obj']) && $utils['user_obj']->getAdmin() >= 3){
			$options = array();
			$options[] = API_make_options('default','UTF-8',0);
			$options[] = API_make_options('UTF-16','UTF-16',0);
			$options[] = API_make_options('UTF-16LE','UTF-16LE',0);
			$options[] = API_make_options('UTF-16BE','UTF-16BE',0);
			$options[] = API_make_options('ISO-8859-1','ISO-8859-1',0);
			$options[] = API_make_options('Windows-1251','Windows-1251',0);
			$options[] = API_make_options('GB2312','GB2312',0);
			$options[] = API_make_options('Shift JIS','Shift JIS',0);
			$options[] = API_make_options('Windows-1252','Windows-1252',0);
			$options[] = API_make_options('GBK','GBK',0);
			$options[] = API_make_options('EUC-JP','EUC-JP',0);
			$options[] = API_make_options('ISO-8859-2','ISO-8859-2',0);
			$options[] = API_make_options('EUC-KR','EUC-KR',0);
			$options[] = API_make_options('Windows-1256','Windows-1256',0);
			$options[] = API_make_options('ISO-8859-15','ISO-8859-15',0);
			$options[] = API_make_options('ISO-8859-9','ISO-8859-9',0);
			$options[] = API_make_options('Windows-1250','Windows-1250',0);
			$options[] = API_make_options('Windows-1254','Windows-1254',0);
			$options[] = API_make_options('Big5','Big5',0);
			$options[] = API_make_options('Windows-874','Windows-874',0);
			$options[] = API_make_options('US-ASCII','US-ASCII',0);
			$html[] = array(
				"display"=>"File Encoding",
				"id"=>"apidat_file_encoding",
				"name"=>"apidat_file_encoding",
				"helptext"=>"",
				"required"=>"1",
				"readonly"=>"0",
				"type"=>"select",
				"options"=>$options,
				"value"=>"0"
			);
		}
		else{
			$html[] = array(
				"display"=>"File Encoding",
				"id"=>"apidat_file_encoding",
				"name"=>"apidat_file_encoding",
				"helptext"=>"",
				"required"=>"1",
				"readonly"=>"0",
				"type"=>"hidden",
				"hidden"=>"1",
				"value"=>"default"
			);
		}
		$options = array();
		$options[] = API_make_options("ins_all","Import All Records",0);
		$options[] = API_make_options("ins_new_only","Import Only New Records",0);
		$options[] = API_make_options("merge","Import New records and Update existing (Merge)",0);
		$html[] = array(
			"display"=>"Type of Import",
			"id"=>"apidat_import_type",
			"name"=>"apidat_import_type",
			"helptext"=>"",
			"required"=>"1",
			"readonly"=>"0",
			"type"=>"select",
			"options"=>$options,
			"value"=>"0"
		);
		$html[] = array(
			"display"=>"Key Field",
			"id"=>"apidat_key_field",
			"name"=>"apidat_key_field",
			"helptext"=>"Enter the numeric index of the column you would like to use as a key.",
			"required"=>"0",
			"readonly"=>"0",
			"type"=>"text",
			"value"=>""
		);
		$html[] = array(
			"display"=>"File Pattern",
			"id"=>"apidat_pattern",
			"name"=>"apidat_pattern",
			"helptext"=>"This will be used to match files for subsequent data pulls. Please use % for wildcards. Leave blank to import the same file(s) everytime.",
			"required"=>"0",
			"readonly"=>"0",
			"type"=>"text",
			"value"=>""
		);
		$html[] = array(
			"display"=>"",
			"id"=>"apidat_script1",
			"name"=>"",
			"helptext"=>"",
			"required"=>"",
			"readonly"=>"",
			"type"=>"code",
			"value"=>'<script type="text/javascript">
			$("#apidat_truncate").on("change", function(event) {
				if($("#apidat_truncate").val()=="1") {
					$("#apidat_customfields").parent().show();
					$("#apidat_import_type").parent().hide();
					$("#apidat_import_type").val("ins_all");
					$("#apidat_key_field").parent().hide();
					$("#apidat_key_field").val("");
					$("#apidat_key_field").attr("required","0");
				}
				else {
					$("#apidat_customfields").parent().show();
					$("#apidat_import_type").parent().show();
					$("#apidat_import_type").val("ins_all");
					$("#apidat_key_field").parent().hide();
					$("#apidat_key_field").val("");
					$("#apidat_key_field").attr("required","0");
				}
			});
			$("#apidat_import_type").on("change", function(event) {
				if($("#apidat_import_type").val()=="ins_all") {
					$("#apidat_truncate").parent().show();
					$("#apidat_customfields").parent().show();
					$("#apidat_key_field").parent().hide();
					$("#apidat_key_field").val("");
					$("#apidat_key_field").attr("required","0");
				}
				else if($("#apidat_import_type").val()=="ins_new_only") {
					$("#apidat_truncate").parent().hide();
					$("#apidat_customfields").parent().show();
					$("#apidat_key_field").parent().show();
					$("#apidat_key_field").val("");
					$("#apidat_key_field").attr("required",1);
				}
				else if($("#apidat_import_type").val()=="merge") {
					$("#apidat_truncate").parent().hide();
					$("#apidat_customfields").parent().hide();
					$("#apidat_customfields").val(0);
					$("#apidat_key_field").parent().show();
					$("#apidat_key_field").val("");
					$("#apidat_key_field").attr("required",1);
				}
			});
			$("#apidat_key_field").parent().hide();
			</script>'
		);
	}
	return $html;
}
function load_data($utils = array()) {
	$pass = $utils['con_obj']['apicon_password'];
	if (is_array($utils['con_obj']['apicon_ftp_password'])) {
		$pass = decode_value(rawurldecode($utils['con_obj']['apicon_ftp_password']['hash']));
	} else {
		$pass = rawurldecode($pass);
	}
 
	foreach ($files_to_import as $filename) {
		$file = $con->getFile($filename);
		if(!in_array($filename, $file_list)) {
			continue; //make sure the file still exists. if not skip
		}
		if(!isset($file[0])){
			continue;
		}
		$file['directory'] = '/var/local/tmmdata/import/';// <- please use this as default directory
		$file['guid']      = uniqid()."_"; // please use a unique id whe naming file to prevent overwriting
		$file['filename']  = 'filename.txt'; // name of file
		// full actual filename = $file['directory'].$file['guid'].$file['filename']
		if(isset($utils['dat_obj']['apidat_truncate']) && $utils['dat_obj']['apidat_truncate'] == 1){
			$file['import_options']['truncate'] = true;
		}
		if(isset($utils['dat_obj']['apidat_skiprows']) && $utils['dat_obj']['apidat_skiprows'] > 0){
			$file['import_options']['skip'] = $utils['dat_obj']['apidat_skiprows'];
		}
		if(isset($utils['dat_obj']['apidat_customfields']) && $utils['dat_obj']['apidat_customfields'] == 1){
			$file['import_options']['custom_fields'] = true;
		}
		if(isset($utils['dat_obj']['apidat_import_type'])
			&& isset($utils['dat_obj']['apidat_key_field'])
			&& $utils['dat_obj']['apidat_import_type'] != 'ins_all'
		){
			$file['import_options'][$utils['dat_obj']['apidat_import_type']] = $utils['dat_obj']['apidat_key_field'];
		}
		if(isset($utils['dat_obj']['apidat_file_encoding']) && $utils['dat_obj']['apidat_file_encoding'] != 'default'){
			$file['import_options']['file_encoding'] = $utils['dat_obj']['apidat_file_encoding'];
		}
		$files[] = $file;//['filename'];
	}
	return array(
		'status' => 'pass',
		'message'=> "Successfully downloaded " . count($files) . " files.",
		'files' => $files,
	);
}
function refresh_data($utils = array()) {
	$pass = $utils['con_obj']['apicon_password'];
	if (is_array($utils['con_obj']['apicon_ftp_password'])) {
		$pass = decode_value(rawurldecode($utils['con_obj']['apicon_ftp_password']['hash']));
	} else {
		$pass = rawurldecode($pass);
	}
 
	foreach ($files_to_import as $filename) {
		$file = $con->getFile($filename);
		if(!in_array($filename, $file_list)) {
			continue; //make sure the file still exists. if not skip
		}
		if(!isset($file[0])){
			continue;
		}
		$file['directory'] = '/var/local/tmmdata/import/';// <- please use this as default directory
		$file['guid']      = uniqid()."_"; // please use a unique id whe naming file to prevent overwriting
		$file['filename']  = 'filename.txt'; // name of file
		// full actual filename = $file['directory'].$file['guid'].$file['filename']
 
		if(isset($utils['dat_obj']['apidat_truncate']) && $utils['dat_obj']['apidat_truncate'] == 1){
			$file['import_options']['truncate'] = true;
		}
		if(isset($utils['dat_obj']['apidat_skiprows']) && $utils['dat_obj']['apidat_skiprows'] > 0){
			$file['import_options']['skip'] = $utils['dat_obj']['apidat_skiprows'];
		}
		if(isset($utils['dat_obj']['apidat_customfields']) && $utils['dat_obj']['apidat_customfields'] == 1){
			$file['import_options']['custom_fields'] = true;
		}
		if(isset($utils['dat_obj']['apidat_import_type'])
			&& isset($utils['dat_obj']['apidat_key_field'])
			&& $utils['dat_obj']['apidat_import_type'] != 'ins_all'
		){
			$file['import_options'][$utils['dat_obj']['apidat_import_type']] = $utils['dat_obj']['apidat_key_field'];
		}
		if(isset($utils['dat_obj']['apidat_file_encoding']) && $utils['dat_obj']['apidat_file_encoding'] != 'default'){
			$file['import_options']['file_encoding'] = $utils['dat_obj']['apidat_file_encoding'];
		}
		$files[] = $file;//['filename'];
	}
	return array(
		'status' => 'pass',
		'message'=> "Successfully downloaded " . count($files) . " files.",
		'files' => $files,
	);
}
function API_make_options($api_func_value='',$api_func_display='',$api_func_checked=0) {
	$api_tmp_array=array();
	$api_tmp_array=array(
		'value'=>$api_func_value,
		'display'=>$api_func_display,
		'checked'=>$api_func_checked
	);
	return $api_tmp_array;
}
require_once(dirname(__FILE__).'/../connectend.php');
?>

Base Export Index

<?php
require_once(dirname(__FILE__).'/../connect.php');
ini_set('display_errors', 'On');
date_default_timezone_set('America/New_York');
/**
Created by connect.php
Variables
	$dbo                         **Always
	$jobs_obj                    **Always
	$user_obj                    **Always
	$file_importer_locations_obj **Always
	$con_obj                     **send
	$dat_obj                     **send
**/
$output = array();
if ($_REQUEST['mode']) {
	switch ($_REQUEST['mode']) {
		case "con":
			$html = create_connection(array(
			"user_obj" => $user_obj
		));
		if(isset($html['error'])) {
			$output = array('status'=>'fail');
			$output['message'] = $html;
		} else {
			$output = array('status'=>'success');
			$output['message'] = array(
				"steps" => 3,
				"html" => $html
			);
		}
			break;
		case "send":
			$output = send_data(array(
				"user_obj" => $user_obj,
				"con_obj" => $con_obj,
				"dat_obj" => $dat_obj,
				"jobs_obj" => $jobs_obj
			));
			break;
		case "opt":
			$html = create_options(array(
				"user_obj" => $user_obj,
			));
			if(isset($html['error'])) {
				$output = array('status'=>'fail');
				$output['message'] = $html;
			} else {
				$output = array('status'=>'success');
				$output['message'] = array(
					"steps" => 1,
					"html" => $html
				);
			}
	}
}
function create_connection() {
	$html = array();
	if ($_REQUEST['step'] == 1 || $_REQUEST['step'] == 'final') {
		$html[] = array(
			"display"=>"Host",
			"id"=>"apicon_host",
			"name"=>"apicon_host",
			"helptext"=>"",
			"required"=>"1",
			"readonly"=>"0",
			"type"=>"text",
			"value"=>""
		);
		$html[] = array(
			"display"=>"Username",
			"id"=>"apicon_username",
			"name"=>"apicon_username",
			"helptext"=>"",
			"required"=>"1",
			"readonly"=>"0",
			"type"=>"text",
			"value"=>""
		);
		$html[] = array(
			"display"=>"Password",
			"id"=>"apicon_password",
			"name"=>"apicon_password",
			"helptext"=>"",
			"required"=>"1",
			"readonly"=>"0",
			"type"=>"password",
			"value"=>""
		);
	}
 
	if ($_REQUEST['step'] == 2 || $_REQUEST['step'] == 'final') {
		$html[] = array(
			"display"=>"Path",
			"id"=>"apicon_path",
			"name"=>"apicon_path",
			"helptext"=>"Only change if a special case. * Should start and end with a '/' *",
			"required"=>"1",
			"readonly"=>"0",
			"type"=>"text",
			"value"=>"/"
		);
		$options = array();
		$options[] = API_make_options("0","No",0);
		$options[] = API_make_options("1","Yes",0);
		$html[] = array(
			"display"=>"Binary",
			"id"=>"apicon_binary",
			"name"=>"apicon_binary",
			"helptext"=>"Only change if a special case.",
			"required"=>"1",
			"readonly"=>"0",
			"type"=>"select",
			"options"=>$options,
			"value"=>"0"
		);
		$html[] = array(
			"display"=>"Passive",
			"id"=>"apicon_passive",
			"name"=>"apicon_passive",
			"helptext"=>"Only change if a special case.",
			"required"=>"1",
			"readonly"=>"0",
			"type"=>"select",
			"options"=>$options,
			"value"=>"1"
		);
	}
	return $html;
}
 
function send_data($utils) {
	$pass = $utils['con_obj']['apicon_password'];
	if (is_array($utils['con_obj']['apicon_password'])) {
		$pass = decode_value(rawurldecode($utils['con_obj']['apicon_password']['hash']));
	} else {
		$pass = rawurldecode($pass);
	}
	$local_file = $utils['dat_obj']['directory'].$utils['dat_obj']['file_guid'].$utils['dat_obj']['filename'];
}
function create_options($utils) {
	$html = array();
	if ($_REQUEST['step'] == 1) {
		$options = array();
		$options[] = API_make_options("0","No",0);
		$options[] = API_make_options("1","Yes",0);
		$html[] = array(
			"display"=>"Chunking",
			"id"=>"apiopt_chunking",
			"name"=>"apiopt_chunking",
			"helptext"=>"",
			"required"=>"1",
			"readonly"=>"0",
			"type"=>"select",
			"options"=>$options,
			"value"=>"0"
		);
	}
	return $html;
}
function API_make_options($api_func_value='',$api_func_display='',$api_func_checked=0) {
	$api_tmp_array=array();
	$api_tmp_array=array(
		'value'=>$api_func_value,
		'display'=>$api_func_display,
		'checked'=>$api_func_checked
	);
	return $api_tmp_array;
}
require_once(dirname(__FILE__).'/../connectend.php');
?>