Skip to content

Commit

Permalink
Merge pull request #4 from wisecare/master
Browse files Browse the repository at this point in the history
QueueMetrics - SugarCRM Integration over REST
Added new REST interface example for SugarCRM 6.5+
  • Loading branch information
l3nz committed Nov 28, 2014
2 parents cb85520 + 7269894 commit 3b8ccd6
Show file tree
Hide file tree
Showing 2 changed files with 299 additions and 0 deletions.
177 changes: 177 additions & 0 deletions sugarcrm-integration/Rest/qm.php
@@ -0,0 +1,177 @@
<?php
// =========================================================================
// QueueMetrics - SugarCRM integration
// This server side script could be used to integrate QueueMetrics with
// SugarCRM.
// -------------------------------------------------------------------------
//
// Project : Integrate QueueMetrics and SugarCRM
// Author : W.I.S.E, Care Team
// ** Project inspired by the script of Marco Signorini **
// Licence : LGPL or Public Domain
//
// -------------------------------------------------------------------------
// This is an entry point script.
// =========================================================================

/*
Customize the following variables:
*/
// Your SugarCRM server.
$url = "http://your.sugarcrm.server";
// The username for accessing SugarCRM data.
$username = "your_sugar_username";
// The password.
$password = "your_sugar_password";
/* End of customization */

/*
Here we are reading input variables passed by QM.
*/
// Get phone number of caller that is passed by QM.
$origCallerid = htmlspecialchars($_GET["callerid"]);
// Get code of agent that is passed by QM.
$agent = htmlspecialchars($_GET["agentcode"]);

// If no caller id passed, then we redirect to SugarCRM base URL.
if (empty($origCallerid)) {
header("Location: $finalDestination");
}
// We then remove leading zeros if any. It is necessary to fine tune search
// results since phone numbers in SugarCRM might be stored in international
// or local format with or without leading zeros.
$callerid = ltrim($origCallerid, '0');

// If no agent code passed, well, this is left to you to decide.
if (empty($agent)) {
// Do some work...
}
/* End of reading input variables. */

// Single Agent Mode:
// If you wish to use a single SugarCRM user account for all agents, then
// you may change this value to default to "true".
// Here, we assume that each agent has a corresponding SugarCRM user.
$autologon = false;

// Initial SugarCRM page URL to redirect to.
$finalDestination = $url;

/*
Connect to SugarCRM and open a session.
*/
// Include our custom class to make REST calls.
require_once("wSuiteRest.php");
// Initialize wSuiteRest class to open a session with SugaCRM.
$wRest = new wSuiteRest($url, $username, $password);
// In case of error, we print it on screen and get out of here.
if (!empty($wRest->error['number'])) {
echo "<pre>";
print('<b>Error#' . $wRest->error['number'] . ' ' . $wRest->error['name'] . '<br>Description: </b>' . $wRest->error['description']);
echo "</pre>";
return;
}

/*
Get a list of possible records and prepare the
*/
// Prepare parameters.
$get_entry_list_parameters = array(
// The session id.
'session' => $wRest->session_id,
// The name of the module from which to retrieve records.
'module_name' => 'Contacts',
// The SQL WHERE clause without the word "where".
'query' => "contacts.phone_work LIKE '%$callerid' OR contacts.phone_home LIKE '%$callerid' OR contacts.phone_mobile LIKE '%$callerid' OR contacts.phone_other LIKE '%$callerid' OR contacts.phone_fax LIKE '%$callerid'",
// The SQL ORDER BY clause without the phrase "order by".
'order_by' => "",
// The record offset from which to start.
'offset' => '0',
// A list of fields to include in the results.
'select_fields' => array('id',),
// A list of link names and the fields to be returned for each
// link name.
// Example:
//'link_name_to_fields_array' => array(
// array(
// 'name' => 'email_addresses',
// 'value' => array(
// 'id',
// 'email_address',
// 'opt_out',
// 'primary_address'
// )
// )
//)
'link_name_to_fields_array' => array(),
// The maximum number of results to return.
'max_results' => '10',
// Exclude deleted records?
'deleted' => '0',
// Whether records marked as favorites should only be returned.
'favorites' => false,
);

// Retreive data from SugarCRM.
$get_entry_list_result = $wRest->call('get_entry_list', $get_entry_list_parameters);
// In case of error, we print it on screen and get out of here.
if (!empty($wRest->error['number'])) {
echo "<pre>";
print('<b>Error#' . $wRest->error['number'] . ' ' . $wRest->error['name'] . '<br>Description: </b>' . $wRest->error['description']);
echo "</pre>";
return;
}

// Now, here what this is all about...
$total_entries = $get_entry_list_result->total_count;
if ($total_entries == 0) {
// No entries found; create a new contact with pre-filled caller number.
$finalDestination = "$url/index.php?module=Contacts&action=EditView&return_module=Contacts&return_action=index&phone_work=$origCallerid";
}
else if ($total_entries == 1) {
// One match; take the entry ID.
$entry_id = $get_entry_list_result->entry_list[0]->id;
$finalDestination = "$url/index.php?module=Contacts&action=DetailView&record=$entry_id";
}
else if ($total_entries > 1) {
// More than one match; show entries in search page.
$finalDestination = "$url/index.php?&searchFormTab=advanced_search&module=Contacts&action=index&query=true&phone_advanced=%25$callerid";
}

// In case Single Agent Mode (autologon) is enabled, we verify that SugarCRM
// session is authenticated; otherwise, logout current session.
if ($autologon) {
$seamless_login_parameters = array(
'session' => $wRest->session_id,
);

$seamless_login_result = $wRest->call('seamless_login', $seamless_login_parameters);

if (!empty($wRest->error['number'])) {
echo "<pre>";
print('<b>Error#' . $wRest->error['number'] . ' ' . $wRest->error['name'] . '<br>Description: </b>' . $wRest->error['description']);
echo "</pre>";
return;
}

if ($seamless_login_result == 1) {
$finalDestination = $finalDestination . "&MSID={$wRest->session_id}";
}
}
else {
$wRest->endSession();
}

// Finally results...
// Redirect to SugarCRM page
header("Location: $finalDestination");

// =========================================================================
// Change History:
// -------------------------------------------------------------------------
//
// 2014.11.12:
// First check-in.
//
// =========================================================================
?>
122 changes: 122 additions & 0 deletions sugarcrm-integration/Rest/wSuiteRest.php
@@ -0,0 +1,122 @@
<?php
// =========================================================================
// QueueMetrics - SugarCRM integration
// This server side script could be used to integrate QueueMetrics with
// SugarCRM.
// -------------------------------------------------------------------------
//
// Project : Integrate QueueMetrics and SugarCRM
// Author : W.I.S.E, Care Team
// ** Project inspired by the script of Marco Signorini **
// Licence : LGPL or Public Domain
//
// -------------------------------------------------------------------------
// wSuiteRest class
// Designed to handle communications with SugarCRM over REST
// =========================================================================

class wSuiteRest {
var $rest_url;
var $session_id;
var $error;

/*
Constructor.
*/
function wSuiteRest($url, $username, $password) {
$this->error = array('number'=>'0', 'name'=>'No error', 'description'=>'No error');
$this->rest_url = $url.'/service/v4_1/rest.php'; // Path to REST Service V4.1 (SugarCRM 6.5 and later)
// Attempt to login
$parameters = array(
'user_auth' => array(
'user_name' => $username,
'password' => md5($password),
'version' => '1'
),
'application_name' => 'wSuiteCRM',
'name_value_list' => array(),
);

$result = $this->call('login', $parameters);
// get session id
if (empty($this->error['number'])) {
if (!empty($result->id)) {
$this->session_id = $result->id;
}
}
}

/*
Logs out and ends SugarCRM session.
*/
function endSession() {
if (!empty($this->session_id)) {
$parameters = array(
'session' => $this->session_id,
);

$this->call('logout', $parameters);
$this->session_id = null;
}
}

/*
Makes cURL request.
*/
function call($method, $parameters) {
$result = null;
try {
ob_start();
$curl_request = curl_init();

curl_setopt($curl_request, CURLOPT_URL, $this->rest_url);
curl_setopt($curl_request, CURLOPT_POST, 1);
// curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl_request, CURLOPT_HEADER, 0);
// curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);

$jsonEncodedData = json_encode($parameters);
$postArgs = array(
'method' => $method,
'input_type' => 'JSON',
'response_type' => 'JSON',
'rest_data' => $jsonEncodedData
);

curl_setopt($curl_request, CURLOPT_POSTFIELDS, $postArgs);
$response = curl_exec($curl_request);
curl_close($curl_request);
// $response = explode("\r\n\r\n", $response, 2);
$result = json_decode($response);
ob_end_flush();

// When Sugar returns nothing...
if (!$result) {
$this->error = array('number'=>'1', 'name'=>'Call error', 'description'=>'Invalid session, parameters or method call.');
return;
}

// When Sugar returns error...
if (!empty($result->number)) {
$this->error = array('number'=>$result->number, 'name'=>$result->name, 'description'=>$result->description);
return;
}
}
catch (Exception $e) {
$this->error = array('number'=>$e->getCode(), 'name'=>$e->getMessage(), 'description'=>$e->getMessage());
}

return $result;
}
}
// =========================================================================
// Change History:
// -------------------------------------------------------------------------
//
// 2014.11.12:
// First check-in.
//
// =========================================================================
?>

0 comments on commit 3b8ccd6

Please sign in to comment.