--- /dev/null
+<?php
+
+class SugarConnector extends SugarHttpClient {
+
+ private static $instance;
+
+ /* TODO En attendant d'avoir une injection de dépendance*/
+ public static function getInstance(){
+ if (! self::$instance) {
+ self::$instance = new SugarConnector(SUGAR_FQDN);
+ self::$instance->login(SUGAR_LOGIN, SUGAR_PASSWORD);
+ }
+ return self::$instance;
+ }
+
+ protected $url, $sessionId;
+
+ /* TODO idem */
+ private function __construct($url = null, $sessionId = null) {
+ $this -> url = 'http://'.$url.'/service/v4_1/rest.php';
+ $this -> sessionId = $sessionId;
+ }
+
+ /// GETTERS
+ public function getUrl() { return $this -> url; }
+ public function getSessionId() { return $this -> sessionId; }
+
+ /// SETTERS
+ public function setUrl($url) { $this -> url = $url; }
+ public function setSessionId($sessionId) { $this -> sessionId = $sessionId; }
+
+ public function login ($login, $pass) {
+ // Set the POST arguments to pass to the Sugar server
+ $parameters = array(
+ 'user_auth' => array(
+ 'user_name' => $login,
+ 'password' => $pass //md5($pass),
+ ),
+ );
+
+ $json = json_encode($parameters);
+
+ $postArgs = array(
+ 'method' => 'login',
+ 'input_type' => 'JSON',
+ 'response_type' => 'JSON',
+ 'rest_data' => $json,
+ );
+
+ $postArgs = http_build_query($postArgs);
+
+ // Make the REST call, returning the result
+ $response = $this->callRest($this -> url, $postArgs);
+
+ if ( $response === false ) {
+ die("Request failed");
+ //throw new Exception("SugarConnector : Response is empty");
+
+ }
+
+ // Convert the result from JSON format to a PHP array
+ $result = json_decode($response);
+
+ if ( !is_object($result) )
+ {
+ die("Error handling result.\n");
+ }
+
+ if ( !isset($result->id) )
+ {
+ die("Error: {$result->name} - {$result->description}\n.");
+ }
+
+ // Get the session id
+ $this -> sessionId = $result->id;
+ }
+
+ public function getInfosModule ($moduleName, $requestedInfos, $sqlWhereClause = "", $sqlOrderByClause = "") {
+ $get_entry_list_parameters = array(
+ //session id
+ 'session' => $this -> sessionId,
+ //The name of the module from which to retrieve records
+ 'module_name' => $moduleName,
+ //The SQL WHERE clause without the word "where".
+ 'query' => $sqlWhereClause,
+ //The SQL ORDER BY clause without the phrase "order by".
+ 'order_by' => $sqlOrderByClause,
+ //The record offset from which to start.
+ 'offset' => 0,
+ //A list of fields to include in the results.
+ 'select_fields' => $requestedInfos,
+ //The maximum number of results to return.
+ 'max_results' => 100000,
+ 'wl_list_max_entries_per_page' => 100000,
+ //If deleted records should be included in results.
+ 'deleted' => 0,
+ //If only records marked as favorites should be returned.
+ 'favorites' => false,
+ );
+
+ $json = json_encode($get_entry_list_parameters);
+
+ $postArgs = array(
+ 'method' => 'get_entry_list',
+ 'input_type' => 'JSON',
+ 'response_type' => 'JSON',
+ 'rest_data' => $json,
+ );
+
+ $postArgs = http_build_query($postArgs);
+
+ // Make the REST call, returning the result
+ $response = $this -> callRest($this -> url, $postArgs);
+
+ // Convert the result from JSON format to a PHP array
+ //$result = json_decode($response);
+
+ return $response;
+ }
+
+ public function updateOrCreateEntryModule($moduleName, $parameters) {
+ $set_entry_parameters = array(
+ //session id
+ "session" => $this -> sessionId,
+
+ //The name of the module from which to retrieve records.
+ "module_name" => $moduleName,
+
+ //Record attributes
+ "name_value_list" => /*array(
+ //to update a record, you will nee to pass in a record id as commented below
+ //array("name" => "id", "value" => "9b170af9-3080-e22b-fbc1-4fea74def88f"),
+ array("name" => "name", "value" => $parameters['name']),
+ ),*/$parameters
+ );
+
+ $json = json_encode($set_entry_parameters);
+
+ $postArgs = array(
+ 'method' => 'set_entry',
+ 'input_type' => 'JSON',
+ 'response_type' => 'JSON',
+ 'rest_data' => $json,
+ );
+
+ $postArgs = http_build_query($postArgs);
+
+ // Make the REST call, returning the result
+ $response = $this -> callRest($this -> url, $postArgs);
+ return $response;
+ }
+
+ public function createRelationship($moduleName, $parameters) {
+ $set_relationship_parameters = array(
+ //session id
+ "session" => $this -> sessionId,
+
+ //The name of the primary module
+ "module_name" => $moduleName,
+
+ //The ID of the specified primary module bean.
+ 'module_id' => $parameters['primary_module_id'],
+
+ //The relationship name of the linked field from which to relate records.
+ 'link_field_name' => $parameters['link_name'],
+ //The list of record ids to relate
+ 'related_ids' => array($parameters['related_module_id']),
+ //Sets the value for relationship based fields
+ /*'name_value_list' => array(
+ array(
+ 'name' => 'contact_role',
+ 'value' => 'Other'
+ ),
+ ),*/
+ //Whether or not to delete the relationship. 0:create, 1:delete
+ 'delete'=> 0,
+
+ );
+
+ $json = json_encode($set_relationship_parameters);
+
+ $postArgs = array(
+ 'method' => 'set_relationship',
+ 'input_type' => 'JSON',
+ 'response_type' => 'JSON',
+ 'rest_data' => $json,
+ );
+
+ $postArgs = http_build_query($postArgs);
+
+ // Make the REST call, returning the result
+ $response = $this -> callRest($this -> url, $postArgs);
+
+ return $response;
+ }
+
+ public function getRelationships($moduleName, $parameters, $requestedInfos, $sqlWhereClause = '') {
+ $get_relationships_parameters = array(
+ //session id
+ "session" => $this -> sessionId,
+
+ //The name of the primary module
+ "module_name" => $moduleName,
+
+ //The ID of the specified primary module bean.
+ 'module_id' => $parameters['primary_module_id'],
+
+ //The relationship name of the linked field from which to relate records.
+ 'link_field_name' => $parameters['link_name'],
+
+ 'related_module_query' => $sqlWhereClause,
+
+ //Sets the value for relationship based fields
+ 'related_fields' => $requestedInfos,
+ //Whether or not to delete the relationship. 0:create, 1:delete
+ 'delete'=> 0,
+
+ //order by
+ //'order_by' => ' opportunities.name ',
+
+ //offset
+ //'offset' => 0,
+
+ //limit
+ //'limit' => 200,
+ );
+
+ $json = json_encode($get_relationships_parameters);
+
+ $postArgs = array(
+ 'method' => 'get_relationships',
+ 'input_type' => 'JSON',
+ 'response_type' => 'JSON',
+ 'rest_data' => $json,
+ );
+
+ $postArgs = http_build_query($postArgs);
+
+ // Make the REST call, returning the result
+ $response = $this -> callRest($this -> url, $postArgs);
+ return $response;
+ }
+
+}