Here is some code to tabulate questions in PHP

<?php
// url to test : http://[server]/[VirtualDirectory]/TOM.php?q=d1&m=&f=d1.containsany({japan,china})

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if (isset($_GET["q"]) && $_GET["q"] != ""){
// d1*d3 or d1]d3 or d1+d3
$q = $_GET["q"]; 
$q = str_replace("]","+",$q); 
echo $q;
} else {
die("No Question Specified"); 
}

if (isset($_GET["m"]) && $_GET["m"] != ""){
$m = "json"; 
} else {
$m="html";
}

if (isset($_GET["f"]) && $_GET["f"] != ""){
$filter =$_GET["f"]; 
} else {
$filter="";
}

//phpinfo();
echo 'Start';
// Create the Tables object and open the DataSet
//Set TableDoc = CreateObject("TOM.Document")
$oTableDoc = new COM("TOM.Document") or die("Unable to instantiate TOM.Document"); 

$sProject = "CSUITE_2019";
$oMDD = "D:\\FMRoot\\Master\\CSUITE_2019\\CSUITE_2019.mdd";


$oData = "Provider=SQLOLEDB.1;Data Source=IBMIBV001;User ID=;Password=;Initial Catalog=" . $sProject . ";Integrated Security=SSPI;Persist Security Info=true";

$oTableDoc->DataSet->Load($oMDD, "", $oData, "mrRdbDsc2", "", "{..}");
$oTableDoc->DataSet->View = 1 ;

// Set the properties of the table document
//$oTableDoc->Language = "ENA";
//$oTableDoc->Context = "Question";
//$oTableDoc->LabelType = "Label";


$oTableDoc->Default->Clear();
$oTableDoc->Default->CellItems->AddNew(0);
//$oTableDoc->Default->CellItems->AddNew(1); // Percents
$oTableDoc->ProfileDefault->Clear();

//Define the tables
$oTable = $oTableDoc->Tables->AddNew("Table1",$q, "Table1");
if ( $filter != "" ) {
echo $filter;
$oTable->Filters->AddNew("Filter1", $filter);
}
//Table.Filters.AddNew("Filter1", "d1.ContainsAny({China})")

//Populate the tables
$oTableDoc->Populate();
$oTableXML = $oTableDoc->GetTablesXml("Table1", 1);

$xml=simplexml_load_string($oTableXML) or die("Error: Cannot create object");
$json = json_encode($xml); // convert the XML string to JSON

if ( $m == "json" ) {
echo $json;
} else {
$oHtmlRender = new COM("ExportHtml.Renderer") or die("Unable to instantiate TOM.Document"); 
//$oHtmlRender->OutputBodyOnly = 1;
echo $oHtmlRender->Render($oTableXML);
}

// Render as an HTML table
//$oHtmlRender = new COM("ExportHtml.Renderer") or die("Unable to instantiate TOM.Document"); 
//$oHtmlRender->OutputBodyOnly = 1;
//echo $oHtmlRender->Render($oTableXML);

echo 'Done';

?>

PHP to List mdd labels

Here is some code to list the variables in an MDD

 

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

//phpinfo();
echo 'Start';
// Create the MDM object and open the Short Drinks .mdd file in read-write mode

$oMDM = new COM("MDM.Document") or die("Unable to instantiate MDM.Document"); 
$oMDM->Open("C:\\inetpub\\wwwroot\\IBV_USERS.mdd", '', 2);
foreach ($oMDM->Variables as $oVar) {
echo $oVar->Label ."<br/>";
}


echo 'Done';

?>

IBM Watson API Test

I have been meaning to post something like this code for a while. Here is an example of calling one of the IBM watson API’s from a MRS script.  Its just code and i think easy to understand. The full API i am playing with can be found here. https://cloud.ibm.com/apidocs/language-translator#translate

Dim sUrl
Dim http
Dim apiHash

sUrl = "https://gateway-syd.watsonplatform.net/language-translator/api/v3/translate?version=2018-05-01"
set oHTTP = createObject("Microsoft.XMLHTTP")

oHTTP.open("GET", sUrl, false)
'The string after "Basic" is the base64 encoded of apikey:[YourAPIKEY]
'I.e.: apihash = Base64Encode("apikey:[YourAPIKEY]")

apiHash = "[YOURHASH]"
oHTTP.setRequestHeader("Authorization", "Basic " + apiHash)
oHTTP.setRequestHeader("content-type", "application/json")
oHTTP.send("{""text"":[""Hello""],""model_id"":""en-es""}")

If oHTTP.status = 200 Then
    debug.Log("RESPONSE : " + oHTTP.responseText)
else
    debug.Log("ERRCODE : " + ctext(oHTTP.status))
    debug.Log("RESPONSE : " + oHTTP.responseText)
End If

Create a nested folder structure with the file system object.

We needed to make some nested folder structures for a project we are running with mrs scripts and i thought i would post some code to do that.  I am sure there are many ways to do this , but this is what we did in just a few lines

 

Dim sPath , oItem, sCurrentPath,oFSO


Set oFSO = CreateObject("Scripting.FileSystemObject")

For each oItem in split("C:\Temp\Doug\Doug1\Doug2\","\")

if ( find(oItem,":") > -1 ) then 
      sCurrentPath = oItem + "\"
else
      sCurrentPath = replace(sCurrentPath + "\" + oItem,"\\","\")
End If

If oFSO.FolderExists(sCurrentPath) = False Then
      debug.Log(sCurrentPath)
      oFSO.CreateFolder(sCurrentPath)
End If


Next

Just Code : Delete all files with specific month

I needed a quick bit of code today so that i could clean up my hard disk. This script will delete all the files that match a specific month number. One day it may save you a few minutes.

LoopFolders("D:\Servers\116\SQLBackups",10)

Sub LoopFolders(sFullPath,iMonth)
Dim oFolder,oSubFolder,oFSO,oFile

Set oFSO = CreateObject("Scripting.FileSystemObject")

If oFSO.FolderExists(sFullPath) Then
Set oFolder = oFSO.GetFolder(sFullPath)

For Each oFile In oFolder.Files
if ( Month(oFile.DateCreated) = iMonth ) Then
debug.Log(oFolder.Path + "\" + oFile.Name +":" + ctext(oFile.DateCreated))
oFSO.DeleteFile(oFolder.Path + "\" + oFile.Name)
End If
Next

For each oSubFolder in oFolder.SubFolders
debug.Log("“Folder:"+ oSubFolder.Name)
LoopFolders(oSubFolder.Path,iMonth)
Next

End If

Set oFSO = null
Set oFolder = null
Set oFile = null
End Sub

Testing a carousel feature in WP

Here are some images/links to IBM Thought Leadership that the team i work for has helped produce. In most cases Unicom intelligence, SPSS Statistics and Modeler have been used to provide analytical insight into the data collected to help drive and validate the content in these documents. If you work in the Government or Education sectors, then these are for you. Happy Clicking (If you want more info then please feel free to drop me a email at dporton@au1.ibm.com )

PHP 7.2.2 and Unicom

I am currently looking into writing a PHP interface that schedules Unicom tasks and i thought i would make a few post’s as i go along. Obviously, the first thing you have to do is to install PHP and set it up for COM use. Setting PHP up for COM is easy , just make sure you have this in your PHP.ini file. ( Search for [COM] )

; allow Distributed-COM calls
; http://php.net/com.allow-dcom
extension=php_com_dotnet.dll
com.allow_dcom = true

The way to check that com is enabled it so run a PHP file that has this in it

<?php
phpinfo(); 
?>

And when you run the file , make sure you see this

next we need to see if com is activated and works so i tried this on Excel. I have excel 2013 my machine

<?php 
     ini_set('display_errors', 1);
     ini_set('display_startup_errors', 1);
     error_reporting(E_ALL); 
     $xl = new COM("Excel.Application"); 
?>

and it produced this ,

Fatal error: Uncaught com_exception: 
Failed to create COM object `Excel.Application': 
Access is denied. in C:\inetpub\wwwroot\LoopMDD.php:9 Stack trace: #0 C:\inetpub\wwwroot\LoopMDD.php(9): 
com->com('Excel.Applicati...') #1 {main} thrown in C:\inetpub\wwwroot\LoopMDD.php on line 9

and i am ok with that as it shows the COM statement worked. If you want to take the excel thing further then you can take a look at this post and it will show you how to get access to XL this way. So whats next .. Well one of the first things i need to do is open an MDD so here is the PHP code to do that


<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

	echo 'Start';
  	$oMDM = new COM("MDM.Document") or die("Unable to instantiate MDM.Document"); 
	$oMDM->Open("C:\\inetpub\\wwwroot\\USERS.mdd", '', 2);
	foreach ($oMDM->Variables as $oVar) {
		echo $oVar->Label ."<br/>";
	}
	echo 'Done';

?>

and so when i ran the PHP file i got the labels. So PHP COM , 64bit is enabled and working , now onto the next bit, watch this space.

Send SMTP Email via Gmail

Just helped someone get this sorted and thought i woulds share. The trick was to allow Less Secure apps in your account. Here is the link

https://myaccount.google.com/lesssecureapps?pli=1

Turn it on and then just use the normal CDO code.

Dim oEmail, sEmailFrom, sEmailTo, sEmailSubject, sEmailBody,sBCC

' Send email

sEmailTo = "admin@SmarterDimensions.com"
sEmailFrom = "FromAddress@SmarterDimensions.com"
sEmailSubject = "Email Subject"
sEmailBody = "Email Body this is bold"
sBCC = "SmarterDimensions@live.com.au"


Set oEmail      = CreateObject("CDO.Message")

With oEmail
 With .Configuration.Fields
  .Item["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = True
  .Item["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "smtp.gmail.com"
  .Item["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2
  .Item["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "douglas.porton@gmail.com"
  .Item["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "xxxxxxxxxxx"	
  .Item["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = 465
  .Item["http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"] = 60
  .Item["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1	
  .Update()		
 End With
End With	

    
oEmail.From     = sEmailFrom
oEmail.To       = sEmailTo
oEmail.BCC      = sBCC
oEmail.Subject  = sEmailSubject
oEmail.HTMLBody = sEmailBody

oEmail.Send()

set oEmail = Null

Open SAV file and create an XL map of contents

Needed to write some code today to open up an Statistics sav file and create an XL map file of the metadata. The next step in the process will be to write a IBM Statistics syntax script to change the content in the SAV file based on the information in the XL file. Here is the first bit of the code , lets call it stage 1. If you want stage 2 let me know.

So lets declare some vars

CONST sDataPath = "c:\temp"
CONST sDataName = "IBV_A069_EPH.sav"
CONST sXLMap = "SAV_MAP.xls"


Dim oDSCs, oDSC, oMDM, oVar,oElement
Dim oExcel,oSheet

Dim iVarCount,iRow

And now lets go ahead and open up the SAV file using the Statistics DCS

Set oDSCs = CreateObject("MRDSCReg.Components")
Set oDSC = oDSCs["mrSavDsc"]
Set oMDM = oDSC.Metadata.Open(sDataPath + sDataName)

Then lets open up our XL file. I am assuming it is already made and contains a tab called Labels in it. We are also going to give the columns some names

Set oSheet = oExcel.Sheets["Labels"]
oSheet.Activate()
	
iRow = 1
oSheet.cells[1][1].value = "Variable Name"
oSheet.cells[1][2].value = "New Variable Name"
oSheet.cells[1][3].value = "Type"
oSheet.cells[1][4].value = "Value"
oSheet.cells[1][5].value = "Label"

Next we need to loop the variables in the file outputting the data we find to the relevant places in our sheet

	for iVarCount = 0 to oMDM.Variables.count-1
		iRow = iRow + 1
		Set oVar = oMDM.Variables[iVarCount]	
		
			oSheet.cells[iRow][1].value = oVar.Name
			oSheet.cells[iRow][3].value = "L"
			oSheet.cells[iRow][5].value = TextOnly(oVar.Label)
		
			oSheet.cells[iRow][6].value = oVar.dataType

			For each oElement in oVar.Elements.Elements
	   			iRow = iRow + 1
	   			oSheet.cells[iRow][3].value = "C"
	   			oSheet.cells[iRow][4].value = oElement.Element.NativeValue
	   			oSheet.cells[iRow][5].value = TextOnly(oElement.Label)	   			
   			Next
   		
	Next
oMDM.Close()	

and finally we need out TextOnly Function. This will allow us to strip out HTML codes that may be in our labels

Function TextOnly(sValue)
	Dim sNew,iCount,bStart
	For iCount = 0 to len(sValue) -1
		
		if ( find( "<",mid(sValue,iCount,1) )>-1 ) then 
			bStart = true
		End if
			
		if ( bStart = true ) Then
		else
			sNew = sNew + mid(sValue,iCount,1)
		End If
			
		if ( find( ">",mid(sValue,iCount,1) )>-1 ) then 
			bStart = false
		End if
	Next
	TextOnly = sNew
End Function

%d bloggers like this: