/** * Creates a lead from chat object * * @param unknown $chat * @throws Exception * @return unknown */ public function createLeadByChat(&$chat) { if ($this->settings['sugarcrm_enabled'] == true) { // Search for existing leads only if lead does not exists and phone is not empty if ((!isset($chat->chat_variables_array['sugarcrm_lead_id']) || $chat->chat_variables_array['sugarcrm_lead_id'] == '') && $chat->phone != '') { $leadExisting = $this->searchByModule(array('leads.phone_work' => $chat->phone)); if ($leadExisting !== false) { // Store associated lead data $chat->chat_variables_array['sugarcrm_lead_id'] = $leadExisting->id; $chat->chat_variables = json_encode($chat->chat_variables_array); $chat->saveThis(); // Return founded lead return $leadExisting; } } // Proceed normal workflow if lead not found $soapclient = new SoapClient($this->settings['wsdl_address']); $result_array = $soapclient->login(array('user_name' => $this->settings['wsdl_username'], 'password' => $this->settings['wsdl_password'], 'version' => '0.1'), 'soaplhcsugarcrm'); $session_id = $result_array->id; $user_guid = $soapclient->get_user_id($session_id); $leadData = array(array('name' => 'last_name', 'value' => $chat->nick), array('name' => 'department', 'value' => (string) $chat->department), array('name' => 'status', 'value' => 'New'), array('name' => 'phone_work', 'value' => (string) $chat->phone), array('name' => 'email1', 'value' => (string) $chat->email), array('name' => 'lead_source', 'value' => 'Web Site'), array('name' => 'website', 'value' => (string) $chat->referrer), array('name' => 'lead_source_description', 'value' => (string) $chat->remarks . "\n\n=====\n" . erTranslationClassLhTranslation::getInstance()->getTranslation('sugarcrm/module', 'Chat ID') . ' - ' . $chat->id), array('name' => 'assigned_user_id', 'value' => $user_guid)); $storeLead = true; if (isset($chat->chat_variables_array['sugarcrm_lead_id']) && $chat->chat_variables_array['sugarcrm_lead_id'] != '') { $leadData[] = array('name' => 'id', 'value' => $chat->chat_variables_array['sugarcrm_lead_id']); $storeLead = false; } $chatAdditionalData = $chat->additional_data_array; // Add custom fields if required if (isset($this->settings['lead_extra_fields']) && is_array($this->settings['lead_extra_fields']) && !empty($this->settings['lead_extra_fields']) && is_array($chatAdditionalData) && !empty($chatAdditionalData)) { $fieldsMappingSugar = array(); foreach ($this->settings['lead_extra_fields'] as $data) { if (isset($data['lhcfield']) && !empty($data['lhcfield'])) { $fieldsMappingSugar[$data['lhcfield']] = $data['sugarcrm']; } } foreach ($chatAdditionalData as $addItem) { $fieldIdentifier = isset($addItem->identifier) ? $addItem->identifier : str_replace(' ', '_', $addItem->key); if (key_exists($fieldIdentifier, $fieldsMappingSugar)) { $leadData[] = array('name' => $fieldsMappingSugar[$fieldIdentifier], 'value' => $addItem->value); } } } $result = $soapclient->set_entry($session_id, 'Leads', $leadData); if ($result->id != -1 && $storeLead == true) { $chat->chat_variables_array['sugarcrm_lead_id'] = $result->id; $chat->chat_variables = json_encode($chat->chat_variables_array); $chat->saveThis(); } if ($result->id == -1) { throw new Exception('Lead could not be created'); } return $result; } else { throw new Exception('SugarCRM extension is not enabled'); } }