WordPress plugin shortcode calling webservice(wsdl) using jquery DataTable separate ajax call to details

Posted: April 4, 2013 in web

Plugin


<?php
/*
Plugin Name: Plugin Test
Plugin URI:
Description: Declares a plugin that will be visible in the
WordPress admin interface
Version: 1.0
License: GPLv2
*/
add_shortcode( 'webservice', 'webservice_shortcode' );

add_action( 'wp_enqueue_scripts', 'load_scripts' );
	function load_scripts(){
		wp_enqueue_script( 'js1',plugins_url( '/datatable/media/js/jquery.js', __FILE__ ) );				
		wp_enqueue_script( 'js2',plugins_url( '/datatable/media/js/jquery.dataTables.js', __FILE__ ) );				
		wp_enqueue_style( 'datatable_css1',plugins_url( '/datatable/media/css/demo_page.css', __FILE__ ) );		
		wp_enqueue_style( 'datatable_css2',plugins_url( '/datatable/media/css/demo_table.css', __FILE__ ) );		
	}

	function webservice_shortcode( $atts ) {
	
	?>
		<script type="text/javascript" charset="utf-8">
			var oTable;
			/* Formating function for row details */
			function fnFormatDetails ( nTr )
			{
				var aData = oTable.fnGetData( nTr );
				$.ajax({
					url: "http://localhost:88/data/getAccountDetails.php?iNumber=" + aData[3],
					dataType: "json",
					beforeSend: function(){
						$('.dataTables_processing').css('visibility','visible');
					},					
					success: function (data) {
						$('.dataTables_processing').css('visibility','hidden');
						$('#'+aData[3]+'status').html(data.AccountStatus);
						$('#'+aData[3]+'bal').html(data.CurrentBalance);
						$('#'+aData[3]+'name').html(data.CustomerName);
						
					}
				});			
				
				var sOut = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">';
				sOut += '<tr><td>Status:</td><td id="'+aData[3]+'status"></td></tr>';
				sOut += '<tr><td>Current Balance:</td><td id="'+aData[3]+'bal"></td></tr>';
				sOut += '<tr><td>Customer Name:</td><td id="'+aData[3]+'name"></td></tr>';
				sOut += '</table>';
				
				return sOut;
			}
			
			$(document).ready(function() {
				oTable = $('#example').dataTable( {
					"bProcessing": true,
					"sAjaxSource": "http://localhost:88/data/getAccountList.php"
				} );
				
				$('#example tbody td img').live( 'click', function () {
					var nTr = $(this).parents('tr')[0];
					if ( oTable.fnIsOpen(nTr) )
					{
						/* This row is already open - close it */
						this.src = "/wordpress/wp-content/plugins/ch2-plugin-header/details_open.png";
						oTable.fnClose( nTr );
					}
					else
					{
						/* Open this row */
						this.src = "/wordpress/wp-content/plugins/ch2-plugin-header/details_close.png";
						oTable.fnOpen( nTr, fnFormatDetails(nTr), 'details' );
					}
				} );				
				
			} );
			
		</script>	
		
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
	<thead>
		<tr>	
			<th width="4%"></th>		
			<th width="20%">AccountID</th>
			<th width="25%">Email</th>
			<th width="25%">INumber</th>
		</tr>
	</thead>
	<tbody>
		
	</tbody>
	<tfoot>
		<tr>
			<th></th>
			<th>AccountID</th>
			<th>Email</th>
			<th>INumber</th>
		</tr>
	</tfoot>
</table>
	<?php

	}
?>

getAccountDetails.php


<?php

	function getDetails($iNumber){
		$client = new SoapClient("http://somewebservice.com/services/Service.svc?wsdl");
		$params = array('accountNumber' => $iNumber);
		$result = $client->__soapCall('GetDetails', array('parameters' => $params));
		return $result->GetCustomerInformationResult;
	}
		$result = getDetails($_GET['iNumber']);
		echo json_encode($result);
?>

getAccountList.php

<?php

	$sIndexColumn = "id";
	
	/* DB table to use */
	$sTable = "ajax";
	
	/* Database connection information */
	$gaSql['user']       = "User";
	$gaSql['password']   = "password";
	$gaSql['db']         = "Account";
	$gaSql['server']     = "ServerDB";

	// Connect to mssql server 
	$handle = mssql_connect($gaSql['server'], $gaSql['user'], $gaSql['password']) or die("Cannot connect to server");

	// Select a database 
	$db = mssql_select_db($gaSql['db'], $handle) or die("Cannot select database");

	// Execute a query 
	$query = "select top 1000 a.accountid, a.email, a.inumber  from accounts a"; 
	$result = mssql_query($query);
	$out = array("aaData" => array());
	$aColumns = array( 'accountid', 'email', 'inumber');
	
	// Iterate over results
	while($aRow = mssql_fetch_array($result)) { 
				$row = array();
				/* Add the  details image at the start of the display array */
				$row[] = '<img src="/wordpress/wp-content/plugins/ch2-plugin-header/details_open.png">';
		
				for ( $i=0 ; $i<count($aColumns) ; $i++ )
				{
					if ( $aColumns[$i] == "version" )
					{
						/* Special output formatting for 'version' column */
						$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
					}
					else if ( $aColumns[$i] != ' ' )
					{
						/* General output */
						$row[] = $aRow[ $aColumns[$i] ];
					}
				}
		$row[] = 'active';
		$out['aaData'][]=$row;
	}
	echo json_encode($out);
	mssql_close($handle);	

?>
Advertisements
Comments
  1. Sam says:

    where is the instruction on how to use this plugin?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s