Skip to content

Commit

Permalink
FFWEB-2390: add update field roles functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfcdd committed Mar 8, 2022
1 parent c490724 commit 6786323
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog
## Unreleased
### Add
- add Update FieldRoles functionality that allows user to store Field Roles received from FACT-Finder search response in shop configuration

### Fix
- fix scrollTop top page is executed on search immediate event

Expand Down
66 changes: 60 additions & 6 deletions src/Controller/Admin/ModuleConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Omikron\FactFinder\Oxid\Controller\Admin;

use Omikron\FactFinder\Communication\Client\ClientBuilder;
use Omikron\FactFinder\Communication\Credentials;
use Omikron\FactFinder\Communication\Resource\AdapterFactory;
use Omikron\FactFinder\Oxid\Model\Config\Export as ExportConfig;
use OxidEsales\Eshop\Application\Model\Attribute;
use OxidEsales\Eshop\Application\Model\AttributeList;
Expand Down Expand Up @@ -37,6 +40,53 @@ public function render()
}

public function saveConfVars()
{
try {
$this->preparePostData();
parent::saveConfVars();
$this->addTplSuccessMessage('Module configuration was saved successfully');
} catch (\Exception $exception) {
$this->addTplErrorMessage($exception->getMessage());
}
}

public function updateFieldRoles()
{
try {
$clientBuilder = oxNew(ClientBuilder::class)
->withServerUrl($this->getConfigParam('ffServerUrl'))
->withCredentials($this->getCredentials());

$searchAdapter = (new AdapterFactory($clientBuilder, $this->getConfigParam('ffApiVersion')))->getSearchAdapter();
$response = $searchAdapter->search($this->getConfigParam('ffChannel')[Registry::getLang()->getLanguageAbbr()], '*');
$fieldRoles = $response['fieldRoles'] ?? $response['searchResult']['fieldRoles'];

$_POST['confstrs']['ffFieldRoles'] = json_encode($fieldRoles);

$this->preparePostData();
parent::saveConfVars();
$this->addTplSuccessMessage('Field roles was updated successfully');
} catch (\Exception $exception) {
$this->addTplErrorMessage($exception->getMessage());
}
}

protected function isFactFinder(): bool
{
return Registry::getRequest()->getRequestEscapedParameter('oxid') === 'ffwebcomponents';
}

protected function getCredentials(): Credentials
{
return new Credentials(...array_map([$this, 'getConfigParam'], ['ffUsername', 'ffPassword', 'ffAuthPrefix', 'ffAuthPostfix']));
}

protected function getConfigParam(string $key)
{
return Registry::getConfig()->getConfigParam($key);
}

private function preparePostData()
{
if ($this->isFactFinder()) {
$_POST['confaarrs'] = array_reduce($this->localizedFields, function (array $result, string $field): array {
Expand All @@ -47,12 +97,6 @@ public function saveConfVars()
$this->flatMap($this->prepareAttributes(), $_POST['confaarrs']['ffExportAttributes'] ?? [])
);
}
return parent::saveConfVars();
}

protected function isFactFinder(): bool
{
return Registry::getRequest()->getRequestEscapedParameter('oxid') === 'ffwebcomponents';
}

private function getAvailableAttributes(): array
Expand Down Expand Up @@ -86,4 +130,14 @@ private function flatMap(callable $fnc, array $arr): array
{
return array_merge([], ...array_map($fnc, $arr));
}

private function addTplSuccessMessage(string $message): void
{
$this->addTplParam('postSubmitMessage', sprintf('<div class="text-success">%s</div>', $message));
}

private function addTplErrorMessage(string $message): void
{
$this->addTplParam('postSubmitMessage', sprintf('<div class="text-error">%s</div>', $message));
}
}
7 changes: 7 additions & 0 deletions src/metadata.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@
'constraints' => '6.9|6.10|6.11|7.0|7.1|7.2|7.3|ng',
'position' => $settingPosition++,
],
[
'group' => 'ffMain',
'name' => 'ffFieldRoles',
'type' => 'str',
'value' => '{"brand":"Brand","campaignProductNumber":"ProductNumber","deeplink":"Deeplink","description":"Description","displayProductNumber":"ProductNumber","imageUrl":"ImageUrl","masterArticleNumber":"ProductNumber","price":"Price","productName":"Name","trackingProductNumber":"ProductNumber"}',
'position' => $settingPosition++,
],
[
'group' => 'ffAdvanced',
'name' => 'ffUseUrlParams',
Expand Down
8 changes: 7 additions & 1 deletion src/views/admin/blocks/factfinder_module_config.tpl
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
[{$smarty.block.parent}]
<link rel="stylesheet" type="text/css" href="[{$oViewConf->getModuleUrl('ffwebcomponents', 'out/admin/css/styles.css')}]"/>
[{if $var_group === 'ffExport'}]
<input type="submit"
id="ffFieldRoles"
class="confinput"
name="ffGetFieldRoles"
value="Update Field Roles"
onclick="Javascript:document.module_configuration.fnc.value='updateFieldRoles'"/>
<input type="button"
id="ffExportFeed"
class="confinput"
Expand All @@ -23,8 +29,8 @@
<div id="spinner">
</div>
<div id="result">
[{$postSubmitMessage}]
</div>

<script>
function testConnection() {
jQuery.ajax({
Expand Down
1 change: 1 addition & 0 deletions src/views/admin/de/ffwebcomponents_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
'SHOP_MODULE_ffHTTPExportUser' => 'Basic Auth user for HTTP export',
'SHOP_MODULE_ffHTTPExportPassword' => 'Basic Auth password for HTTP export',
'HELP_SHOP_MODULE_ffPushedImportTypes' => 'Verfügbare Werte: data, suggest',
'SHOP_MODULE_ffFieldRoles' => 'Update Field Roles',
'FF_TEST_CONNECTION_SUCCESS' => 'Connection has been established successfully',
'FF_ARTICLE_FEED_EXPORT_SUCCESS' => 'Article Feed has been exported succesfully',
'FF_ARTICLE_FEED_UPLOAD_SUCCESS' => 'Article Feed has been uploaded succesfully',
Expand Down
1 change: 1 addition & 0 deletions src/views/admin/en/ffwebcomponents_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
'SHOP_MODULE_ffPushedImportTypes' => 'Pushed import types',
'SHOP_MODULE_ffHTTPExportUser' => 'Basic Auth user for HTTP export',
'SHOP_MODULE_ffHTTPExportPassword' => 'Basic Auth password for HTTP export',
'SHOP_MODULE_ffFieldRoles' => 'Update Field Roles',
'FF_TEST_CONNECTION_SUCCESS' => 'Connection has been established successfully',
'FF_ARTICLE_FEED_EXPORT_SUCCESS' => 'Feed has been exported successfully',
'FF_ARTICLE_FEED_UPLOAD_SUCCESS' => 'Feed has been uploaded successfully',
Expand Down
2 changes: 1 addition & 1 deletion src/views/frontend/blocks/scripts.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<script>
document.addEventListener('ffReady', function (ff) {
factfinder.sdk = 'oe-v4.1.2';
factfinder.communication.fieldRoles = {"brand":"Brand","campaignProductNumber":"ProductNumber","deeplink":"Deeplink","description":"Description","displayProductNumber":"ProductNumber","ean":"EAN","imageUrl":"ImageURL","masterArticleNumber":"Master","price":"Price","productName":"Name","trackingProductNumber":"ProductNumber"};
factfinder.communication.fieldRoles = [{$oConfig->getConfigParam('ffFieldRoles')}];
[{if $oView->getClassKey() neq "search_result"}]
document.addEventListener('before-search', function (event) {
Expand Down

0 comments on commit 6786323

Please sign in to comment.