diff --git a/_config/ubiquity.yml b/_config/ubiquity.yml index 13f2573..4d10306 100755 --- a/_config/ubiquity.yml +++ b/_config/ubiquity.yml @@ -1,7 +1,6 @@ --- Name: ubiquityconfig --- - SilverStripe\SiteConfig\SiteConfig: extensions: - Ubiquity\Extensions\UbiquitySiteConfigExtension diff --git a/composer.json b/composer.json index e5ba7c5..d7d909c 100755 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "dnadesign/silverstripe-ubiquity", "description": "Implements Ubiquity extensions for User Defined Forms", - "type": "silverstripe-vendormodule", + "type": "silverstripe-module", "keywords": ["silverstripe", "ubiquity", "newsletter"], "license": "MIT", "authors": [{ @@ -10,16 +10,14 @@ }], "minimum-stability": "dev", "require": { - "silverstripe/framework": "^4", - "silverstripe/cms": "^4", - "silverstripe/userforms": "^5", - "symbiote/silverstripe-gridfieldextensions": "^3", - "silverstripe/display-logic" : "^2" + "silverstripe/framework": "^5", + "silverstripe/cms": "^5", + "silverstripe/userforms": "^6" }, - "require-dev": { - "phpunit/phpunit": "7.5.x" - }, - "extra": { - "installer-name": "silverstripe-ubiquity" - } + "require-dev": { + "phpunit/phpunit": "^9.5" + }, + "extra": { + "installer-name": "silverstripe-ubiquity" + } } diff --git a/src/Extensions/UbiquityEditableFormFieldExtension.php b/src/Extensions/UbiquityEditableFormFieldExtension.php index d62d7a2..9b9ed4a 100755 --- a/src/Extensions/UbiquityEditableFormFieldExtension.php +++ b/src/Extensions/UbiquityEditableFormFieldExtension.php @@ -2,7 +2,6 @@ namespace Ubiquity\Extensions; -use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\TextField; use SilverStripe\ORM\DataExtension; diff --git a/src/Extensions/UbiquityEditableOptionExtension.php b/src/Extensions/UbiquityEditableOptionExtension.php index 9881c38..99d830e 100755 --- a/src/Extensions/UbiquityEditableOptionExtension.php +++ b/src/Extensions/UbiquityEditableOptionExtension.php @@ -2,7 +2,6 @@ namespace Ubiquity\Extensions; -use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\TextField; use SilverStripe\ORM\DataExtension; diff --git a/src/Extensions/UbiquityFormExtension.php b/src/Extensions/UbiquityFormExtension.php index 05932d4..ec4b533 100755 --- a/src/Extensions/UbiquityFormExtension.php +++ b/src/Extensions/UbiquityFormExtension.php @@ -41,13 +41,41 @@ public function updateCMSFields(FieldList $fields) ->setEmptyString('-- Select one --'); // Ubiquity allows submitting data to a form as well, usually tied to the Ubiquity database - $formID = TextField::create('UbiquityFormID', 'Ubiquity Form ID') - ->setDescription('ID of the form used to send the data to.'); + // By populating the Sucess Form ID, you can automatically trigger an email response to + // be sent to the user, as if the Form was submitted directly. + $formID = TextField::create('UbiquitySuccessFormID', 'Ubiquity Success Form ID') + ->setDescription('ID of the form that will be used to send confirmation email once the user has signed up.'); + + // To trigger emails of the form, you must define which field the form needs to have the email sent. + $formFieldID = TextField::create('UbiquitySuccessFormEmailTriggerID', 'Ubiquity Success Form Field ID') + ->setDescription('ID of the field that will be used to send the form action (ususally named EmailTrigger)'); + + // Ubiquity forms can send different emails, define the Action that will send the email + $formAction = TextField::create('UbiquitySuccessFormAction', 'Ubiquity Success Form Action') + ->setDescription('Name of the Email that should be sent.'); + + // Allows Ubiquity to track the soure of the submission, eg newsletter signup, event form, feedback form + // Define the FieldID of the Ubiquity Field + $sourceID = TextField::create('UbiquitySourceFieldID', 'Ubiquity Source Field ID') + ->setDescription('ID of the field that will be use as primary source for the user.'); + + // Define the Name of the source to populate the above field + $sourceName = TextField::create('UbiquitySourceName', 'Ubiquity Source Name') + ->setDescription('Reference of this form as source.'); + + // Submit the source even if a T&C's field is included yet is not checked + $submitSource = CheckboxField::create('UbiquitySubmitSource', 'Submit Source') + ->setDescription('Submit the source data even if a T&C\'s field exists but is not ticked'); $fields->addFieldsToTab('Root.Ubiquity', [ $enabled, $database, - $formID + $formID, + $formFieldID, + $formAction, + $sourceID, + $sourceName, + $submitSource ]); } } diff --git a/src/Extensions/UbiquityMultipleOptionExtension.php b/src/Extensions/UbiquityMultipleOptionExtension.php index 40592b7..7ce4381 100755 --- a/src/Extensions/UbiquityMultipleOptionExtension.php +++ b/src/Extensions/UbiquityMultipleOptionExtension.php @@ -4,7 +4,6 @@ use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\FieldList; -use SilverStripe\Forms\GridField\GridFieldDeleteAction; use SilverStripe\Forms\TextField; use SilverStripe\ORM\DataExtension; use Symbiote\GridFieldExtensions\GridFieldEditableColumns; @@ -53,7 +52,7 @@ public function updateCMSFields(FieldList $fields) 'callback' => function ($record, $column, $grid) { return TextField::create($column); } - ], + ], 'AllowOverride' => [ 'title' => 'Allow Override Ubiquity DB', 'callback' => function ($record, $column, $grid) { diff --git a/src/Extensions/UbiquityUserFormControllerExtension.php b/src/Extensions/UbiquityUserFormControllerExtension.php index 95a845b..3b3d813 100755 --- a/src/Extensions/UbiquityUserFormControllerExtension.php +++ b/src/Extensions/UbiquityUserFormControllerExtension.php @@ -2,6 +2,11 @@ namespace Ubiquity\Extensions; +use SilverStripe\Control\Director; +use SilverStripe\Core\Extension; + +namespace Ubiquity\Extensions; + use Exception; use Psr\Log\LoggerInterface; use SilverStripe\Control\Director; @@ -106,7 +111,7 @@ public function formatData() ->Fields() ->exclude('UbiquityFieldID', ['', NULL]); - // not all fields are set to update ubiquity + // not fields are set to update ubiquity if (empty($fields)) { return $data; } @@ -212,5 +217,7 @@ public function exitWithError(Exception $e) } Injector::inst()->get(LoggerInterface::class)->error($e->getMessage()); + SS_Log::log($e->getMessage(), SS_Log::WARN); + exit(); } } diff --git a/src/Forms/EditableSignupField.php b/src/Forms/EditableSignupField.php index d0d552c..ccc61f7 100755 --- a/src/Forms/EditableSignupField.php +++ b/src/Forms/EditableSignupField.php @@ -2,7 +2,6 @@ namespace Ubiquity\Forms\Fields; -use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\HTMLEditor\HTMLEditorField; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\ORM\FieldType\DBHTMLText; diff --git a/src/Models/UbiquityDatabase.php b/src/Models/UbiquityDatabase.php index 348a119..8dc3e26 100755 --- a/src/Models/UbiquityDatabase.php +++ b/src/Models/UbiquityDatabase.php @@ -14,7 +14,7 @@ */ class UbiquityDatabase extends DataObject { - private static $table_name = 'UbiquityDatabase'; + private static $table_name = "UbiquityDatabase"; private static $db = [ 'Title' => 'Varchar(255)', diff --git a/src/Services/UbiquityService.php b/src/Services/UbiquityService.php index 6c0b7c3..9c90b63 100755 --- a/src/Services/UbiquityService.php +++ b/src/Services/UbiquityService.php @@ -1,14 +1,20 @@ >>>>>> 9ffaf6d (SS5 upgrade) class UbiquityService { @@ -165,7 +171,11 @@ public function call($method = null, string $uri = null, $query = null, $data = $options = $this->getDefaultOptions(); +<<<<<<< HEAD if ($query && is_array($query)) { +======= + if ($query) { +>>>>>>> 9ffaf6d (SS5 upgrade) $options['query'] = array_merge($options['query'], $query); } @@ -194,7 +204,11 @@ public function getContact($params) } // get the email address submitted +<<<<<<< HEAD $email = (isset($params['value'])) ? $params['value'] : null; +======= + $email = (isset($emailData['value'])) ? $emailData['value'] : null; +>>>>>>> 9ffaf6d (SS5 upgrade) // check the email address is valid if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) { @@ -205,6 +219,10 @@ public function getContact($params) 'filter' => $this->buildFilterQueryString(array($params)) ]; +<<<<<<< HEAD +======= + //self::get()? +>>>>>>> 9ffaf6d (SS5 upgrade) $response = $this->call(self::METHOD_GET, 'database/contacts', $query); if ($response->getStatusCode() !== 200) { @@ -252,6 +270,59 @@ public function getEmailData(array $data) } /** +<<<<<<< HEAD +======= + * @param $fields SS_List list of fields assigned an ubiquity ID + * @param $options SS_List list of EditableOption assigned an ubiquity ID + * @param $data Array submitted data (merged with source data) + */ + public function createOrUpdateContact($data) + { + // Check if contact already exists given the email form field + $emailData = $this->getEmailData($data); + + $contact = $this->getContact($emailData); + + $uri = 'database/contacts'; + + if ($contact) { + // update an existing contact + $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($method, $uri, null, $data); + + if ($contact) { + if ($response->getStatusCode() !== 200) { + throw new Exception('An ubiquity API error occured (update)'); + } + + $result = $this->decodeResponse($response); + + // creating a new contect needs to return the referenceID + return $result['referenceID']; + } else { + if ($response->getStatusCode() !== 201) { + throw new Exception('An ubiquity API error occured (c)reate'); + } + + // updating an existing contact needs to return true if successful + return true; + } + } + + /** +>>>>>>> 9ffaf6d (SS5 upgrade) * Send data to a Ubiquity Form * Usually to trigger an email being sent from their end. */ @@ -333,4 +404,39 @@ public function filterUpdateData($data, $contact) return array_values($updatedData); } +<<<<<<< HEAD +======= + + /** + * Determine if ubiquity analytis is enabled + * + * @return boolean + */ + public static function get_ubiquity_analytics_enabled() + { + return SiteConfig::current_site_config()->UbiquityAnalyticsEnabled; + } + + /** + * Helper to get the Analytics keys + */ + public static function get_analytics_keys() + { + $analyticsKeys = []; + + if (!self::get_ubiquity_analytics_enabled()) { + return $analyticsKeys; + } + + $keys = Config::inst()->get('UbiquityService', 'analytics_keys'); + + if ($keys && is_array($keys)) { + foreach ($keys as $key) { + array_push($analyticsKeys, ['Key' => $key]); + } + } + + return $analyticsKeys; + } +>>>>>>> 9ffaf6d (SS5 upgrade) } diff --git a/src/Tasks/SS4UbiquityMigrationTask.php b/src/Tasks/SS4UbiquityMigrationTask.php deleted file mode 100755 index 04f69dc..0000000 --- a/src/Tasks/SS4UbiquityMigrationTask.php +++ /dev/null @@ -1,59 +0,0 @@ -updateUbiquityModels(); - exit('DONE'); - } catch (Exception $e) { - echo "Error: " . $e->getMessage() . "\r\n"; - } - } - - public function updateUbiquityModels() - { - echo "Updating Ubiquity models\r\n"; - - $baseRecords = UbiquityDatabase::get(); - foreach ($baseRecords as $record) { - $className = $originalClassName = $record->ClassName; - $pos = strrpos($className, '\\'); - if ($pos !== false) { - $className = substr($className, $pos + 1); - } - // Lookup the new name space - $mapping = Config::inst()->get('SilverStripe\ORM\DatabaseAdmin', 'classname_value_remapping'); - if (array_key_exists($className, $mapping)) { - $qualifiedClassName = $mapping[$className]; - - // Alter base table - $update = SQLUpdate::create('"UbiquityDatabase"')->addWhere(['ClassName' => $originalClassName]); - $update->assign('"ClassName"', $qualifiedClassName); - $update->execute(); - } else { - $err = ($className === null) ? "Null" : $className; - echo "Unable to find a mapping for " . $err . "\r\n"; - } - } - } -} diff --git a/src/Tasks/UbiquityFieldsList.php b/src/Tasks/UbiquityFieldsList.php index 60ed7bd..7c86cce 100755 --- a/src/Tasks/UbiquityFieldsList.php +++ b/src/Tasks/UbiquityFieldsList.php @@ -4,7 +4,6 @@ use SilverStripe\Dev\BuildTask; use Ubiquity\Models\UbiquityDatabase; -use Ubiquity\Services\UbiquityService; class UbiquityFieldsList extends BuildTask {