--- /dev/null
+<?php
+
+/*
+ * Nom des relations :
+ *
+ * maint_contenance_contacts
+ * accounts_maint_contrat_maintenance => accountsntrat_maintenance
+ * maint_contrat_maintenance_contacts => maint_contenance_contacts
+ *
+ * Prendre les 8 premier charactère du début du nom de la relation, puis les 17 derniers
+ *
+ *
+ */
+
+
+class SugarWrapper {
+
+ /**
+ * @var CurlWrapperForSugar
+ */
+ private $curlWrapperForSugar;
+ private $sugar_api_url;
+ private $username;
+ private $password;
+
+ private $session_id;
+
+ public function __construct($sugar_api_url,$username,$password){
+ $this->sugar_api_url = $sugar_api_url;
+ $this->username = $username;
+ $this->password = $password;
+ $this->setCurlWrapperForSugar(new CurlWrapperForSugar());
+ }
+
+ public function setCurlWrapperForSugar(CurlWrapperForSugar $curlWrapperForSugar){
+ $this->curlWrapperForSugar = $curlWrapperForSugar;
+ }
+
+ public function getEntryList($module_name,$query,$order_by,$offset,array $select_fields,array $link_name_to_fields_array,$max_results,$deleted,$favorites){
+ $get_entry_list_parameters = array(
+ 'session' => 0,
+ 'module_name' => $module_name,
+ 'query' => $query,
+ 'order_by' => $order_by,
+ 'offset' => $offset,
+ 'select_fields' => $select_fields,
+ 'link_name_to_fields_array' => $link_name_to_fields_array,
+ 'max_results' => $max_results,
+ 'deleted' => $deleted,
+ 'Favorites' => $favorites,
+ );
+ return $this->call('get_entry_list',$get_entry_list_parameters);
+ }
+
+ public function getModuleFields($module_name){
+ $param = array('session'=>0,'module_name'=>$module_name,array());
+ return $this->call('get_module_fields',$param);
+ }
+
+ public function getRelationships($module_name,$module_id,$link_field_name,$related_module_query,$related_fields,$related_module_link_name_to_fields_array = array(),$deleted,$order_by='',$offset=0,$limit=false){
+ $parameters = array(
+ 'session' => 0,
+ 'module_name' => $module_name,
+ 'module_id' => $module_id,
+ 'link_field_name' => $link_field_name,
+ 'related_module_query' => $related_module_query,
+ 'related_fields' => $related_fields,
+ 'related_module_link_name_to_fields_array' => $related_module_link_name_to_fields_array,
+ 'deleted' => $deleted,
+ 'order_by' =>$order_by,
+ 'offset' => $offset,
+ 'limit' => $limit,
+ );
+ return $this->call('get_relationships',$parameters);
+ }
+
+ public function getEntry($module_name,$id,$select_fields,$link_name_to_fields_array = array()){
+ $parameters = array(
+ 'session' => 0,
+ 'module_name' => $module_name,
+ 'id' => $id,
+ 'select_fields' => $select_fields,
+ 'link_name_to_fields_array' => $link_name_to_fields_array,
+ );
+ return $this->call('get_entry',$parameters);
+ }
+
+ public function getDropdownsInfo($module_name,$fields_name) {
+ $parameters = array(
+ 'session' => 0,
+ 'module_name' => $module_name,
+ );
+ $info = $this->call('get_module_fields', $parameters);
+ $result = array();
+ foreach($info['module_fields'][$fields_name]['options'] as $key => $value){
+ $result[$key] = $value['value'];
+ }
+ return $result;
+ }
+
+ public function setEntry($module_name,array $field){
+ $parameters = array(
+ "session" => 0,
+ "module_name" => $module_name,
+ "name_value_list" => array()
+ );
+ foreach($field as $key => $value){
+ $parameters['name_value_list'][] = array("name" => $key, "value" => $value);
+ }
+
+ return $this->call('set_entry',$parameters);
+ }
+
+ public function createRelationship($module_name, $sugar_id,$related_module_name,$related_sugar_id) {
+ $parameters = array(
+ "session" => 0,
+ "module_name" => $module_name,
+ 'module_id' => $sugar_id,
+ 'link_field_name' => $related_module_name,
+ 'related_ids' => array($related_sugar_id),
+ 'delete'=> 0,
+ );
+ return $this->call('set_relationship',$parameters);
+ }
+
+
+ public function entryExists($module_name,$id){
+ $entry = $this->getEntry($module_name,$id,array('id'));
+ if (empty($entry['entry_list'][0]['name_value_list']['id']['value'])){
+ return false;
+ }
+ return $entry['entry_list'][0]['name_value_list']['id']['value'] == $id;
+ }
+
+ public function getEntries($module,$id_list,$retrieve_field_list){
+ $result_list = array();
+ $entries_list = $this->getEntriesIntern($module,$id_list,$retrieve_field_list,array());
+
+ foreach($entries_list['entry_list'] as $i => $entry) {
+ $entry_result = array();
+ foreach ($retrieve_field_list as $j => $field_name) {
+ $entry_result[$field_name] = '';
+ if (! empty($entry['name_value_list'][$field_name]['value'])) {
+ $entry_result[$field_name] = $entry['name_value_list'][$field_name]['value'];
+ }
+ }
+ $result_list[$entry['id']] = $entry_result;
+ }
+ return $result_list;
+ }
+
+ private function getEntriesIntern($module_name,array $ids,array $select_fields,$link_name_to_fields_array){
+ $parameters = array(
+ 'session' => 0,
+ 'module_name' => $module_name,
+ 'ids' => $ids,
+ 'select_fields' => $select_fields,
+ 'link_name_to_fields_array' => $link_name_to_fields_array,
+ );
+ return $this->call('get_entries',$parameters);
+ }
+
+
+ private function call($method,$param){
+ if (! $this->session_id) {
+ $this->login();
+ }
+ $param['session'] = $this->session_id;
+ return $this->post($method,$param);
+ }
+
+ private function login(){
+ $login_parameters = array(
+ "user_auth" => array(
+ "user_name" => $this->username,
+ "password" => $this->password,
+ "version" => "1"
+ ),
+ "application_name" => "RestTest",
+ "name_value_list" => array(),
+ );
+
+ $login_result = $this->post("login",$login_parameters);
+
+ if (empty($login_result['id'])) {
+ throw new Exception("Authentication failed ({$this->username}) to SugarCRM ({$this->sugar_api_url}): " .
+ $login_result['description']);
+ }
+
+ $this->session_id = $login_result['id'];
+ }
+
+ private function post($method,$parameters){
+ $post_data = array(
+ "method" => $method,
+ "input_type" => "JSON",
+ "response_type" => "JSON",
+ "rest_data" => json_encode($parameters)
+ );
+ $output = $this->curlWrapperForSugar->call($this->sugar_api_url,$post_data);
+ $result = explode("\r\n\r\n", $output, 2);
+ if (empty($result[1])){
+ throw new Exception("SugarCRM ({$this->sugar_api_url}) send unexpected values");
+ }
+ $response = json_decode($result[1],true);
+ $this->nettoyage($response);
+ return $response;
+ }
+
+ private function nettoyage(&$tab){
+ foreach($tab as $i => $sub_tab){
+ if (is_array($sub_tab)){
+ $sub_tab = $this->nettoyage($sub_tab);
+ } else {
+ $sub_tab = html_entity_decode($sub_tab,ENT_QUOTES);
+ }
+ $tab[$i] = $sub_tab;
+ }
+ return $tab;
+ }
+
+ public function simpleRetrieve($module_name,array $field_to_retrieve,array $link_name_to_fields_array = array()) {
+ $offset = 0;
+ $nb_retrieve = 1000;
+ $max = 0;
+ $result = array();
+
+ do {
+ $get_entry_list_result = $this->getEntryList($module_name, "", "", $offset, $field_to_retrieve, $link_name_to_fields_array, $nb_retrieve, 0, false);
+
+ foreach ($get_entry_list_result['entry_list'] as $i => $entry) {
+ $entry_result = array();
+ foreach ($field_to_retrieve as $j => $field_name) {
+ $entry_result[$field_name] = $entry['name_value_list'][$field_name]['value'];
+ }
+
+ if (! empty($get_entry_list_result['relationship_list'][$i]['link_list'])) {
+
+ foreach ($get_entry_list_result['relationship_list'][$i]['link_list'] as $link_list_element) {
+ $entry_result['link'][$link_list_element['name']] = array();
+
+ foreach ($link_list_element['records'] as $num_rec => $records) {
+ foreach ($records['link_value'] as $key => $value) {
+ $entry_result['link'][$link_list_element['name']][$num_rec][$key] = $value['value'];
+ }
+ }
+
+ }
+ }
+
+ $result[$entry['id']] = $entry_result;
+ }
+
+ $offset = $get_entry_list_result['next_offset'];
+ } while (!empty($get_entry_list_result['entry_list']) && ($max <= 0 || $offset < $max));
+ return $result;
+ }
+
+}
\ No newline at end of file