Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Garry Yeatman committed Jul 9, 2019
2 parents d7aa022 + d2cd75c commit af45261
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ To set this up,
- 3. Enter your Ubiquity database information
- 4. Enter the user defined form you wish to edit.
- 5. Under the Ubiquity Config, select the Ubiquity database you want to work with.
- 6. You will be able to add the ubiquity form ids to a user defined form.
- 6. You will be able to add the ubiquity form ids to a user defined form.
17 changes: 9 additions & 8 deletions src/Extensions/UbiquityPageControllerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
*/
class UbiquityPageControllerExtension extends Extension
{
/**
* Fetch Ubiquity traking keys for use in templates
*/
public function UbiquityAnalyticsKeys()
{
// get the analytics keys, and check if tracking is enabled
$keys = UbiquityService::get_analytics_keys();
return (!empty($keys)) ? new ArrayList($keys) : null;
public function onAfterInit() {
$SiteConfig = SiteConfig::current_site_config();

if ($SiteConfig->UbiquityAnalyticsEnabled && $SiteConfig->UbiquityAnalyticsKey) {
Requirements::javascript('https://wt.engage.ubiquity.co.nz/device/register/'.$SiteConfig->UbiquityAnalyticsKey, [
'async' => true,
'set_write_js_to_body' => false
]);
}
}
}
9 changes: 7 additions & 2 deletions src/Extensions/UbiquitySiteConfigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class UbiquitySiteConfigExtension extends DataExtension
{
private static $db = [
'UbiquityEnabled' => 'Boolean',
'UbiquityAnalyticsEnabled' => 'Boolean'
'UbiquityAnalyticsEnabled' => 'Boolean',
'UbiquityAnalyticsKey' => 'Varchar(255)'
];

private static $has_many = [
Expand All @@ -23,11 +24,15 @@ public function updateCMSFields(FieldList $fields)
{
$config = GridFieldConfig_RecordEditor::create();
$gridfield = GridField::create('UbiquityDatabases', 'Ubiquity Database', $this->owner->UbiquityDatabases(), $config);

$analyticsKey = TextField::create('UbiquityAnalyticsKey', 'Ubiquity Analytics Key');

$fields->addFieldsToTab('Root.UbiquitySetup', [
CheckboxField::create('UbiquityEnabled', 'Ubiquity Enabled'),
CheckboxField::create('UbiquityAnalyticsEnabled', 'Ubiquity Analytics Enabled'),
$analyticsKey,
$gridfield
]);

$analyticsKey->displayIf('UbiquityAnalyticsEnabled')->isChecked();
}
}
8 changes: 4 additions & 4 deletions src/Models/UbiquityDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getCMSFields()

$environment = $this->isInDB() && $this->Environment
? $this->Environment
: UbiquityService::get_ubiquity_environment();
: Director::get_environment_type();

$fields->addFieldsToTab('Root.Main', [
ReadonlyField::create('Environment', 'Environment', $environment),
Expand All @@ -44,7 +44,7 @@ public function getCMSFields()
public function onBeforeWrite()
{
if (!$this->isInDB() || !$this->Environment) {
$this->Environment = UbiquityService::get_ubiquity_environment();
$this->Environment = Director::get_environment_type();
}

parent::onBeforeWrite();
Expand All @@ -68,7 +68,7 @@ public function NiceTitle()
public static function get_available_databases()
{
$siteConfig = SiteConfig::current_site_config();
$environment = Director::isLive() ? 'production' : 'staging';
$environment = Director::get_environment_type();

return $siteConfig->UbiquityDatabases()
->filter('Environment', $environment);
Expand All @@ -93,7 +93,7 @@ public static function get_database_options()
*/
public function isValidDatabase()
{
$environment = Director::isLive() ? 'production' : 'staging';
$environment = Director::get_environment_type();

if ($environment !== $this->Environment) {
return sprintf("Invalid Ubiquity database (%s) for environnment", $this->NiceTitle());
Expand Down
9 changes: 7 additions & 2 deletions src/Services/UbiquityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class UbiquityService

const METHOD_POST = 'POST';

const METHOD_PUT = 'PUT';

protected $database;

private static $base_uri = 'https://api.ubiquity.co.nz/';
Expand Down Expand Up @@ -152,7 +154,7 @@ public function getDefaultOptions()
*/
public function call($method = null, string $uri = null, $query = null, $data = null)
{
if (!in_array($method, [self::METHOD_GET, self::METHOD_POST])) {
if (!in_array($method, [self::METHOD_GET, self::METHOD_POST, self::METHOD_PUT])) {
throw new Exception('Invalid Ubiqutiy request method');
}

Expand Down Expand Up @@ -258,14 +260,17 @@ public function createOrUpdateContact($data)
$id = $contact['referenceID'];
$uri .= '/' . $id;
$data = $this->filterUpdateData($data, $contact);
$method = self::METHOD_PUT;
} else {
$method = self::METHOD_POST;
}

// If there is no data to update, exit here
if (empty($data)) {
return true;
}

$response = $this->call(self::METHOD_POST, $uri, null, $data);
$response = $this->call($method, $uri, null, $data);

if ($contact) {
if ($response->getStatusCode() !== 200) {
Expand Down

0 comments on commit af45261

Please sign in to comment.