Print Properties
Integrating Inforama with SugarCRM

The new Rest services in Inforama now make it even easier to call Inforama from PHP based systems such as SugarCRM or Moodle. In this example we will show how easy it is to start producing documents from Inforama templates via the SugarCRM interface. We'll use the SugarCRM

Getting Started

Anyone who is familiar with SugarCRM will know that its default installation uses a MySQL database and sets up a contacts table which allows users to instantly start recording contact information. Within Inforama Studio create a new project and define a new connection to the SugarCRM database.


 

Connecting to the SugarCRM database in Inforama Studio


Next create a dataset which will return the details of a particular contact based on the unique id field.

Define the dataset that will be used in letter templates

 

This project has a 'contact_id' parameter defined in it which will be passed to the Inforama engine when generating contact correspondence. The query which is used is "SELECT id, first_name, last_name FROM contacts WHERE id = '$P{contact_id}'".

 

Next create letter templates that you wish to make available from SugarCRM. As usual the fields defined in the data frame on the right of the Studio are listed and can be double-clicked in order to insert the data bindings into the template.

Define a letter template

 

Setup on Inforama Enterprise

Now that the basics of the project have been created it can be deployed to the server running Inforama Enterprise. Click the Project - Compile menu item and then the Project - Deploy.

The Studio deployment dialog

Now in the Inforama Enterprise you will see the project listed.
 

The project within Inforama Enterprise

 

Customisation of SugarCRM

The project has been deployed to Inforama Enterprise and is ready to be called from SugarCRM. In this example we want to print Inforama documents from the contact details page so we need to create a custom button on that page. Begin by editing the file 'sugarcrm\custom\modules\Contacts\metadata\detailviewdefs.php'. This file is created by the studio in SugarCRM whenever customisations are made to the application. The following code shows how a print button is added to the list of buttons at the top of the page.
 

    array (
    0 => 'EDIT',
    1 => 'DUPLICATE',
    2 => 'DELETE',
    3 => 'FIND_DUPLICATES',
    4 =>
        array (
        'customCode' => '<input title="Print Document" class="button"
            onclick="javascript:printDoc(\'{$fields.id.value}\');return false;"
            type="submit" name="Print Document" value="Print Document">',
    ),
    ),

 

This code creates a new custom print button on the contacts details page as shown below.
 

The custom print button in the Contact details page

The customCode element in the array defines a new input button and attaches a javascript handler to it for click events. The printDoc javascript event is defined in the following HTML code. This code is copied to the top of the sugarcrm\modules\Contacts\views\view.detail.php file so that it's available for each contact.
 

<div id="inforamaDiv" style="width:300px; height:200px; background-color:#FFFFFF;
        visibility:hidden;position:absolute;top:100px;left:100px; border:1px solid #AAAAAA>

    <div style="background-color:#3333AA; color:#FFFFFF; padding:3px">Select a document</div>
    <div style="padding:10px">

        <div style="padding-bottom:2px">Available documents</div>
        <select id="documentList" size="8" style="width:100%"></select>
        <div style="width:100%; padding-top:5px; text-align:right">
            <input type="button" value="Print" onclick="javascript:generateDoc();"/>
            <input type="button" value="Cancel" onclick="javascript:cancel();"/>
        </div>
    </div>
</div>
<script>
var currentContactId;
var inforamaDiv = document.getElementById( 'inforamaDiv' );
var documentList = document.getElementById( 'documentList' );

function cancel()    {
    inforamaDiv.style.visibility = "hidden";
}
function printDoc( contactId )    {
    currentContactId = contactId;
    var url = "inforama.php?action=gettemplates";
    var resp = null;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
        xmlhttp.open("GET",url,false);
        xmlhttp.send(null);
        resp = xmlhttp.responseText;
    } else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        xmlhttp.open("GET",url,false);
        xmlhttp.send();
        resp = xmlhttp.responseText;
    }
    var docs = resp.split( "," );
    documentList.options.length = 0;
    for ( var i = 0; i &lt; docs.length; i++ )    {
        var elOptNew = document.createElement('option');
        elOptNew.text = docs[ i ];
        elOptNew.value = docs[ i ];
        try {
          documentList.add(elOptNew, null); // standards compliant; doesn't work in IE
        } catch(ex) {
          documentList.add(elOptNew); // IE only
        }

    }
    inforamaDiv.style.visibility = "visible";
}

function generateDoc()    {
    var url = "inforama.php?contactid=" + currentContactId + "&doc=" + documentList.selectedItem;
   
    var resp = null;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
        xmlhttp.open("GET",url,false);
        xmlhttp.send(null);
        resp = xmlhttp.responseText;
    } else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        xmlhttp.open("GET",url,false);
        xmlhttp.send();
        resp = xmlhttp.responseText;
    }
    inforamaDiv.style.visibility = "hidden";
}

</script>

At the beginning of this HTML there is a div defined which contains a list which will display the documents available on the Inforama server. The printDoc function calls the inforama.php file which resides in the root sugarcrm directory and is shown below.
 

<?php
$action = $_GET[ "action" ];
if ( $action == 'gettemplates' )    {
    getTemplates();
} else {
$printDestination = "";
$customerId = $_GET[ "contactid" ];
$doc = $_GET[ "doc" ];

$requestStr = '{"request":{"project_name":"SugarTest","document_requests":[{"parameters":[{"value":"' . $customerId;
$requestStr .= '","name":"contact_id"}],"output_type":"pdf","input_type":"Compound",
"actions":[{"properties":[{"value":"' . $printDestination;
$requestStr .= '","name":"printdestination"}],"name":"print"}],"template_name":"' . $doc . '"}],"type":""}}';
callInforama( $requestStr );
}

function callInforama( $requestStr )    {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://localhost:8080/Inforama/restful/requests');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, 'admin:password');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestStr);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));

    $data = curl_exec($ch);
    curl_close($ch);
}

function getTemplates()    {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://localhost:8080/Inforama/restful/projects/SugarTest/templates');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, 'admin:password');

    $data = curl_exec($ch);
    curl_close($ch);
    $jsonResp = json_decode($data);
   
    $resp = "";
    $iCount = 0;
   
    foreach ($jsonResp as $value)    {
        foreach ($value as $value2)    {
            if ( $iCount > 0 )    {
                $resp .= ",";
            }
            $resp .= $value2->{'name'};           
            $iCount++;
        }
    }
    echo $resp;
}
?>

 

Now when the print button is clicked the div appears with the list of documents in Inforama as shown below.
 

Letters available for printing are listed in the new custom dialog

 

Now when the user selects a document template and clicks the print button the generateDoc function is called with the contact id and the template name. This example simply prints the resulting document to the default printer but it could easily be changed to print to specific printers or to email to the contact's email address.
 

2594 Views, 0 Attachments 0 Attachments

  • Comments
Copyright © 2008 - 2010