Skip to content

Commit afd7867

Browse files
committed
added backend ui
1 parent 73aae7f commit afd7867

File tree

10 files changed

+231
-4
lines changed

10 files changed

+231
-4
lines changed

UPGRADE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Upgrade Notes
22

3+
## 2.0.2
4+
added backend ui (supported in dynamic search >= 4.0.4) [#10](https://github.com/dachcom-digital/pimcore-dynamic-search-index-provider-opensearch/pull/10)
5+
6+
## 2.0.1
7+
size param for aggregation filter [#4](https://github.com/dachcom-digital/pimcore-dynamic-search-index-provider-opensearch/issues/4)
8+
39
## Migrating from Version 1.x to Version 2.0.0
410

511
### Global Changes

config/pimcore/routing.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ds_opensearch_controller_admin_index_rebuild_mapping:
2+
path: /admin/ds-index-provider-opensearch/index/rebuild-mapping
3+
methods: [ POST ]
4+
defaults: { _controller: DsOpenSearchBundle\Controller\Admin\IndexController::rebuildMappingAction }
5+
options:
6+
expose: true

config/services/controller.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
3+
_defaults:
4+
autowire: true
5+
autoconfigure: true
6+
public: true
7+
8+
DsOpenSearchBundle\Controller\Admin\IndexController:
9+
tags:
10+
- 'controller.service_arguments'

config/services/event_listener.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
3+
_defaults:
4+
autowire: true
5+
autoconfigure: true
6+
public: false
7+
8+
DsOpenSearchBundle\EventListener\Admin\AssetListener:
9+
tags:
10+
- { name: kernel.event_subscriber }

public/js/backend/settings.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
class DsIndexProviderOpensearch {
2+
3+
addLayoutToDsSettings(event) {
4+
5+
this.dsSettings = event.detail.subject;
6+
this.dsContextFullConfig = pimcore.globalmanager.get('dynamic_search.context.full_configuration') || {};
7+
8+
const dsActionsPanel = new Ext.panel.Panel({
9+
layout: 'hbox',
10+
title: 'DsIndexProviderOpenSearch',
11+
bodyPadding: 10,
12+
items: [
13+
14+
]
15+
});
16+
this.dsSettings.panel.add(dsActionsPanel);
17+
18+
if (Object.keys(this.dsContextFullConfig).length === 0) {
19+
dsActionsPanel.add({
20+
xtype: 'displayfield',
21+
value: 'Error: no dynamic search context configured'
22+
});
23+
return;
24+
}
25+
26+
dsActionsPanel.add({
27+
xtype: 'toolbar',
28+
docked: 'top',
29+
items: [
30+
{
31+
scale: 'small',
32+
margin: '0 10 0 0',
33+
text: t('ds_index_provider_opensearch.actions.index.rebuild_mapping'),
34+
icon: '/bundles/pimcoreadmin/img/flat-color-icons/data_configuration.svg',
35+
menu: this.buildContextButtonMenu( 'rebuild_mapping'),
36+
}
37+
]
38+
});
39+
40+
}
41+
42+
buildContextButtonMenu(action) {
43+
return Object.keys(this.dsContextFullConfig).map(function(context) {
44+
return {
45+
text: context,
46+
handler: function() {
47+
Ext.Msg.confirm(
48+
t(`ds_index_provider_opensearch.actions.index.${action}`) + ': ' + context,
49+
t(`ds_index_provider_opensearch.actions.index.${action}.confirmation.message`),
50+
function (confirmMsg) {
51+
52+
if (confirmMsg !== 'yes') {
53+
return;
54+
}
55+
56+
this.performContextIndexAction(context, action);
57+
58+
}.bind(this)
59+
);
60+
}.bind(this)
61+
}
62+
}.bind(this))
63+
}
64+
65+
performContextIndexAction(context, action) {
66+
let url = '';
67+
68+
try {
69+
url = Routing.generate('ds_opensearch_controller_admin_index_' + action);
70+
} catch (e) {
71+
Ext.Msg.alert('Error', e.message);
72+
return;
73+
}
74+
75+
Ext.Ajax.request({
76+
url: url,
77+
method: 'POST',
78+
params: {
79+
context: context
80+
},
81+
success: function(response) {
82+
if (response.status === 200) {
83+
pimcore.helpers.showNotification(t('success'), t(`ds_index_provider_opensearch.actions.index.${action}.success`), 'success');
84+
} else {
85+
pimcore.helpers.showNotification(t('error'), response.responseText, 'error');
86+
}
87+
}
88+
});
89+
}
90+
}
91+
92+
const dsIndexProviderOpensearch = new DsIndexProviderOpensearch();
93+
94+
document.addEventListener('dynamic_search.event.settings.postBuildLayout', dsIndexProviderOpensearch.addLayoutToDsSettings.bind(dsIndexProviderOpensearch));

src/Command/RebuildIndexCommand.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Console\Input\InputOption;
1515
use Symfony\Component\Console\Output\OutputInterface;
1616
use Symfony\Component\Console\Question\ConfirmationQuestion;
17+
use Symfony\Contracts\Translation\TranslatorInterface;
1718

1819
class RebuildIndexCommand extends Command
1920
{
@@ -23,7 +24,8 @@ class RebuildIndexCommand extends Command
2324
public function __construct(
2425
protected ContextDefinitionBuilderInterface $contextDefinitionBuilder,
2526
protected IndexDocumentGeneratorInterface $indexDocumentGenerator,
26-
protected ClientBuilderInterface $clientBuilder
27+
protected ClientBuilderInterface $clientBuilder,
28+
protected TranslatorInterface $translator
2729
) {
2830
parent::__construct();
2931
}
@@ -83,7 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8385
/** @var QuestionHelper $helper */
8486
$helper = $this->getHelper('question');
8587

86-
$text = 'This command will drop the selected index and all data will be lost! Continue?';
88+
$text = $this->translator->trans('ds_index_provider_opensearch.actions.index.rebuild_mapping.confirmation.message', [], 'admin');
8789
$commandText = sprintf(' <info>%s (y/n)</info> [<comment>%s</comment>]:', $text, 'no');
8890
$question = new ConfirmationQuestion($commandText, false);
8991

@@ -106,7 +108,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
106108
return 0;
107109
}
108110

109-
$output->writeln('<info>Index rebuild was successful</info>');
111+
$output->writeln(sprintf('<info>%s</info>', $this->translator->trans('ds_index_provider_opensearch.actions.index.rebuild_mapping.success', [], 'admin')));
110112

111113
return 0;
112114
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace DsOpenSearchBundle\Controller\Admin;
4+
5+
use DsOpenSearchBundle\Builder\ClientBuilderInterface;
6+
use DsOpenSearchBundle\Service\IndexPersistenceService;
7+
use DynamicSearchBundle\Builder\ContextDefinitionBuilderInterface;
8+
use DynamicSearchBundle\Context\ContextDefinitionInterface;
9+
use DynamicSearchBundle\Generator\IndexDocumentGeneratorInterface;
10+
use DynamicSearchBundle\Provider\PreConfiguredIndexProviderInterface;
11+
use Pimcore\Bundle\AdminBundle\Controller\AdminAbstractController;
12+
use Symfony\Component\HttpFoundation\Request;
13+
use Symfony\Component\HttpFoundation\Response;
14+
15+
class IndexController extends AdminAbstractController
16+
{
17+
public function __construct(
18+
protected ContextDefinitionBuilderInterface $contextDefinitionBuilder,
19+
protected IndexDocumentGeneratorInterface $indexDocumentGenerator,
20+
protected ClientBuilderInterface $clientBuilder,
21+
)
22+
{
23+
}
24+
25+
public function rebuildMappingAction(Request $request): Response
26+
{
27+
$contextName = $request->get('context');
28+
29+
if (empty($contextName)) {
30+
return new Response('no context given', 400);
31+
}
32+
33+
try {
34+
$contextDefinition = $this->contextDefinitionBuilder->buildContextDefinition($contextName, ContextDefinitionInterface::CONTEXT_DISPATCH_TYPE_INDEX);
35+
36+
if (!$contextDefinition instanceof ContextDefinitionInterface) {
37+
throw new \Exception(
38+
sprintf('no context definition with name "%s" found', $contextName)
39+
);
40+
}
41+
42+
$indexDocument = $this->indexDocumentGenerator->generateWithoutData($contextDefinition, ['preConfiguredIndexProvider' => true]);
43+
44+
if (!$indexDocument->hasIndexFields()) {
45+
throw new \Exception(
46+
sprintf(
47+
'No Index Document found. The current context index provider requires pre-configured indices. Please make sure your document definition implements the "%s" interface',
48+
PreConfiguredIndexProviderInterface::class
49+
)
50+
);
51+
}
52+
53+
$options = $contextDefinition->getIndexProviderOptions();
54+
55+
$client = $this->clientBuilder->build($options);
56+
$indexService = new IndexPersistenceService($client, $options);
57+
58+
if ($indexService->indexExists()) {
59+
$indexService->dropIndex();
60+
}
61+
62+
$indexService->createIndex($indexDocument);
63+
} catch (\Throwable $e) {
64+
return new Response($e->getMessage(), 500);
65+
}
66+
67+
return new Response();
68+
}
69+
70+
}

src/DsOpenSearchBundle.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
namespace DsOpenSearchBundle;
44

55
use DynamicSearchBundle\Provider\Extension\ProviderBundleInterface;
6+
use Pimcore\Extension\Bundle\AbstractPimcoreBundle;
67
use Symfony\Component\HttpKernel\Bundle\Bundle;
78

8-
final class DsOpenSearchBundle extends Bundle implements ProviderBundleInterface
9+
final class DsOpenSearchBundle extends AbstractPimcoreBundle implements ProviderBundleInterface
910
{
1011
public const PROVIDER_NAME = 'opensearch';
1112

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace DsOpenSearchBundle\EventListener\Admin;
4+
5+
use Pimcore\Event\BundleManager\PathsEvent;
6+
use Pimcore\Event\BundleManagerEvents;
7+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8+
9+
class AssetListener implements EventSubscriberInterface
10+
{
11+
public static function getSubscribedEvents(): array
12+
{
13+
return [
14+
BundleManagerEvents::JS_PATHS => 'addJsFiles'
15+
];
16+
}
17+
18+
public function addJsFiles(PathsEvent $event): void
19+
{
20+
$event->addPaths([
21+
'/bundles/dsopensearch/js/backend/settings.js',
22+
]);
23+
}
24+
25+
}

translations/admin.en.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ds_index_provider_opensearch.actions.index.rebuild_mapping: 'Rebuild index mapping'
2+
ds_index_provider_opensearch.actions.index.rebuild_mapping.confirmation.message: 'This will drop the selected index and all data will be lost! Continue?'
3+
ds_index_provider_opensearch.actions.index.rebuild_mapping.success: 'Index rebuild was successful'

0 commit comments

Comments
 (0)