After searching the forums for a good tutorial about using the sugarcrm soap interface and using XSL stylesheets I found no examples. I needed to connect the users within a Joomla environment with the contact // acount details of the SugarCRM enivornment
Hereby my first contribution to the Sugar community.
The purpose of this tutorial is to provide an example of how you can use the SugarCRM SOAP and using XSL stylesheets for the presentation. In this example I will fetch a record from the contacts and present it with an XSL stylesheet.
Step 1 is to make a soap connection to the SUGAR CRM environment and request the contact record:
The next step is to generate the HTML code through the XSL stylesheet, that is the hard part. The main issue is how you can fetch the specific attributes from the soap xml response data.PHP Code:require_once ( JPATH_BASE . DS . 'nusoap' . DS . 'lib' . DS . 'nusoap.php' );
$client = new nusoap_client( $this->portal_config->sugarhost . '/soap.php?wsdl', true );
$auth_array = array( 'user_auth' => array ('user_name' => $this->portal_config->sugar_username,
'password' => md5($this->portal_config->sugar_password),
)
);
$login_results = $client->call('login',$auth_array);
$sugar_session_id = $login_results['id'];
// The sugar_crm user is ID is stored in the J user table parameters;
$contact_record = $client->call( 'get_entry', array( $sugar_session_id, 'Contacts', $sugar_crm_user_id, null));
// Load the xsl stylesheet
$xsl_file = 'contacts.xslt';
$XSL = new DOMDocument();
$XSL->load( $xsl_file );
$xp->importStylesheet( $XSL );
$xml_doc = new DomDocument();
$xml_doc->loadXML( $client->responseData )
// transform the XML into HTML using the XSL file
if ($html = $xp->transformToXML($xml_doc)) {
echo $html;
} else {
trigger_error('XSL transformation failed.', E_USER_ERROR);
}
// Thats all folks ;-)
To make it easy you can define within your XSL stylesheet so called keys for the lookup of the specific attributes. In this example I need the label and the value from the specific attribute. Thereby you can define a key as follows:
further on in your template you can fetch a specific field with the following action in the style sheet, in this case I need the user assigned user name:HTML Code:<!-- define a key for the value --> <xsl:key name="myvalue" match="item//name_value_list//item" use="name" /> <!-- define a key for the label --> <xsl:key name="mylabel" match="field_list//item" use="name" />
Notice the fact that I using the key 'name' myvalue and mylabel to look up the value of the assigned_user_name.HTML Code:<xsl:value-of select="key('mylabel', 'assigned_user_name')/label" /> : <xsl:value-of select="key('myvalue', 'assigned_user_name')/value" />
See below the whole stylesheet:
The style sheet also makes a dump of the provide record of the contact person.HTML Code:<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method='html'/> <!-- param values may be changed during the XSL Transformation --> <xsl:param name="title">Dumping the contact record</xsl:param> <!-- define the keys for looking up the attributes of the contact record --> <xsl:key name="myvalue" match="item//name_value_list//item" use="name" /> <xsl:key name="mylabel" match="field_list//item" use="name" /> <xsl:template match="/"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title><xsl:value-of select="$title"/></title> </head> <body> Below is an example of fetching an atribute and the label from Sugar:<br/> <xsl:value-of select="key('mylabel', 'assigned_user_name')/label" /> : <xsl:value-of select="key('myvalue', 'assigned_user_name')/value" /> <hr/> Below is an example of looping the attributes from the requested value email1<br/> <xsl:for-each select="key('myvalue', 'email1')" > <xsl:value-of select="name"/> : <xsl:value-of select="value"/> </xsl:for-each> <hr/> <div class="center"> <table border="0"> <caption><xsl:value-of select="$title"/></caption> <thead> <tr> <th>Field name</th> <th>Value</th> </tr> </thead> <tbody> <xsl:apply-templates select="//name_value_list" /> </tbody> </table> <!-- insert the page navigation links --> </div> </body> </html> </xsl:template> <xsl:template match="name_value_list"> <xsl:for-each select="item"> <tr> <td><xsl:value-of select="name"/></td> <td><xsl:value-of select="value"/></td> </tr> </xsl:for-each> </xsl:template> </xsl:stylesheet>
See also the attached XML file if you want to test it out local (I recommend this when you are developing since this is much much faster then using the nusoap interface). This file is called sugar_contact.xml. If you want to test it out replace the load of the xml file with the lines below:
regards,PHP Code:$xml_doc = new DomDocument;
$xml_doc->load( 'sugar_contact.xml' );
Richard


LinkBack URL
About LinkBacks



Reply With Quote
Bookmarks