This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
268 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
<?php | ||
|
||
namespace Solspace\FreeformPro\Integrations\CRM; | ||
|
||
use GuzzleHttp\Client; | ||
use Solspace\Freeform\Library\Exceptions\Integrations\IntegrationException; | ||
use Solspace\Freeform\Library\Integrations\CRM\AbstractCRMIntegration; | ||
use Solspace\Freeform\Library\Integrations\DataObjects\FieldObject; | ||
use Solspace\Freeform\Library\Integrations\IntegrationStorageInterface; | ||
use Solspace\Freeform\Library\Integrations\SettingBlueprint; | ||
|
||
class Insightly extends AbstractCRMIntegration | ||
{ | ||
const SETTING_API_KEY = 'api_key'; | ||
|
||
const TITLE = 'Insightly'; | ||
const LOG_CATEGORY = 'Insightly'; | ||
|
||
/** | ||
* Returns a list of additional settings for this integration | ||
* Could be used for anything, like - AccessTokens | ||
* | ||
* @return SettingBlueprint[] | ||
*/ | ||
public static function getSettingBlueprints(): array | ||
{ | ||
return [ | ||
new SettingBlueprint( | ||
SettingBlueprint::TYPE_TEXT, | ||
self::SETTING_API_KEY, | ||
'API Key', | ||
'Enter your Insightly API key here.', | ||
true | ||
), | ||
]; | ||
} | ||
|
||
/** | ||
* Push objects to the CRM | ||
* | ||
* @param array $keyValueList | ||
* | ||
* @return bool | ||
*/ | ||
public function pushObject(array $keyValueList): bool | ||
{ | ||
$response = $this | ||
->getAuthorizedClient() | ||
->post( | ||
$this->getEndpoint('/Leads'), | ||
['json' => $keyValueList] | ||
); | ||
|
||
return $response->getStatusCode() === 200; | ||
} | ||
|
||
/** | ||
* Check if it's possible to connect to the API | ||
* | ||
* @return bool | ||
*/ | ||
public function checkConnection(): bool | ||
{ | ||
$response = $this | ||
->getAuthorizedClient() | ||
->get($this->getEndpoint('/Leads')); | ||
|
||
return $response->getStatusCode() === 200; | ||
} | ||
|
||
/** | ||
* Fetch the custom fields from the integration | ||
* | ||
* @return FieldObject[] | ||
*/ | ||
public function fetchFields(): array | ||
{ | ||
$fieldList = [ | ||
new FieldObject('SALUTATION', 'Salutation', FieldObject::TYPE_STRING), | ||
new FieldObject('FIRST_NAME', 'First Name', FieldObject::TYPE_STRING), | ||
new FieldObject('LAST_NAME', 'Last Name', FieldObject::TYPE_STRING), | ||
new FieldObject('TITLE', 'Title', FieldObject::TYPE_STRING), | ||
new FieldObject('EMAIL', 'Email', FieldObject::TYPE_STRING), | ||
new FieldObject('PHONE', 'Phone', FieldObject::TYPE_STRING), | ||
new FieldObject('MOBILE', 'Mobile', FieldObject::TYPE_STRING), | ||
new FieldObject('FAX', 'Fax', FieldObject::TYPE_STRING), | ||
new FieldObject('WEBSITE', 'Website', FieldObject::TYPE_STRING), | ||
new FieldObject('ORGANISATION_NAME', 'Organisation Name', FieldObject::TYPE_STRING), | ||
new FieldObject('INDUSTRY', 'Industry', FieldObject::TYPE_STRING), | ||
new FieldObject('EMPLOYEE_COUNT', 'Employee Count', FieldObject::TYPE_STRING), | ||
new FieldObject('IMAGE_URL', 'Image URL', FieldObject::TYPE_STRING), | ||
new FieldObject('ADDRESS_STREET', 'Address - Street', FieldObject::TYPE_STRING), | ||
new FieldObject('ADDRESS_CITY', 'Address - City', FieldObject::TYPE_STRING), | ||
new FieldObject('ADDRESS_STATE', 'Address - State', FieldObject::TYPE_STRING), | ||
new FieldObject('ADDRESS_POSTCODE', 'Address - Postcode', FieldObject::TYPE_STRING), | ||
new FieldObject('ADDRESS_COUNTRY', 'Address - Country', FieldObject::TYPE_STRING), | ||
new FieldObject('LEAD_DESCRIPTION', 'Lead Description', FieldObject::TYPE_STRING), | ||
new FieldObject('LEAD_RATING', 'Lead Rating', FieldObject::TYPE_STRING), | ||
]; | ||
|
||
$response = $this | ||
->getAuthorizedClient() | ||
->get($this->getEndpoint('/CustomFields/Leads')); | ||
|
||
$data = json_decode($response->getBody(), false); | ||
foreach ($data as $field) { | ||
if (!$field->EDITABLE) { | ||
continue; | ||
} | ||
|
||
$type = null; | ||
|
||
switch ($field->FIELD_TYPE) { | ||
case 'TEXT': | ||
case 'DROPDOWN': | ||
case 'URL': | ||
case 'MULTILINETEXT': | ||
$type = FieldObject::TYPE_STRING; | ||
break; | ||
|
||
case 'DATE': | ||
$type = FieldObject::TYPE_DATETIME; | ||
break; | ||
|
||
case 'BIT': | ||
$type = FieldObject::TYPE_BOOLEAN; | ||
break; | ||
|
||
case 'NUMERIC': | ||
$type = FieldObject::TYPE_NUMERIC; | ||
break; | ||
} | ||
|
||
if (null === $type) { | ||
continue; | ||
} | ||
|
||
$fieldObject = new FieldObject( | ||
$field->FIELD_NAME, | ||
$field->FIELD_LABEL, | ||
$type, | ||
false | ||
); | ||
|
||
$fieldList[] = $fieldObject; | ||
} | ||
|
||
return $fieldList; | ||
} | ||
|
||
/** | ||
* Authorizes the application | ||
* Returns the access_token | ||
* | ||
* @return string | ||
* @throws IntegrationException | ||
*/ | ||
public function fetchAccessToken(): string | ||
{ | ||
return $this->getSetting(self::SETTING_API_KEY); | ||
} | ||
|
||
/** | ||
* A method that initiates the authentication | ||
*/ | ||
public function initiateAuthentication() | ||
{ | ||
} | ||
|
||
/** | ||
* Perform anything necessary before this integration is saved | ||
* | ||
* @param IntegrationStorageInterface $model | ||
*/ | ||
public function onBeforeSave(IntegrationStorageInterface $model) | ||
{ | ||
$model->updateAccessToken($this->getSetting(self::SETTING_API_KEY)); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function getApiRootUrl(): string | ||
{ | ||
return 'https://api.insightly.com/v3.0/'; | ||
} | ||
|
||
/** | ||
* @return Client | ||
*/ | ||
private function getAuthorizedClient(): Client | ||
{ | ||
$client = new Client([ | ||
'headers' => ['Content-Type' => 'application/json'], | ||
'auth' => [$this->getAccessToken(), ''], | ||
]); | ||
|
||
return $client; | ||
} | ||
} |