diff --git a/Controller/SettingsListController.php b/Controller/SettingsListController.php index ae98967..9454443 100644 --- a/Controller/SettingsListController.php +++ b/Controller/SettingsListController.php @@ -11,14 +11,7 @@ namespace ONGR\SettingsBundle\Controller; -use ONGR\ElasticsearchBundle\ORM\Manager; use ONGR\FilterManagerBundle\Filters\ViewData; -use ONGR\FilterManagerBundle\Filters\Widget\Choice\SingleTermChoice; -use ONGR\FilterManagerBundle\Filters\Widget\Pager\Pager; -use ONGR\FilterManagerBundle\Filters\Widget\Search\MatchSearch; -use ONGR\FilterManagerBundle\Filters\Widget\Sort\Sort; -use ONGR\FilterManagerBundle\Search\FiltersContainer; -use ONGR\FilterManagerBundle\Search\FiltersManager; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -54,53 +47,12 @@ public function listAction(Request $request) */ protected function getListData(Request $request) { - /** @var Manager $manager */ - $manager = $this->container->get('es.manager'); - - /** @var FiltersContainer $container */ - $container = new FiltersContainer(); - - /** @var Pager $pager */ - $pager = new Pager(); - $pager->setRequestField('page'); - $pager->setCountPerPage(15); - $container->set('pager', $pager); - - /** @var Sort $sort */ - $sort = new Sort(); - $sort->setRequestField('sort'); - $choices = [ - 'nameAsc' => ['label' => 'Name Asc', 'field' => 'name', 'order' => 'asc', 'default' => true], - 'nameDesc' => ['label' => 'Name Desc', 'field' => 'name', 'order' => 'desc', 'default' => false], - ]; - $sort->setChoices($choices); - $container->set('sort', $sort); - - /** @var MatchSearch $search */ - $search = new MatchSearch(); - if ($request->query->has('q')) { - $queryString = $request->query->get('q'); - if (!empty($queryString)) { - $search->setRequestField('q'); - $search->setField('name'); - } - } - $container->set('search', $search); - - /** @var SingleTermChoice $profile */ - $profile = new SingleTermChoice(); - $profile->setRequestField('profile'); - $profile->setField('profile'); - $container->set('profile', $profile); - - /** @var FiltersManager $fm */ - $fm = new FiltersManager($container, $manager->getRepository('ONGRSettingsBundle:Setting')); - $fmr = $fm->execute($request); + $filterManager = $this->get('ongr_settings.filters_manager')->execute($request); return [ - 'data' => iterator_to_array($fmr->getResult()), - 'filters' => $fmr->getFilters(), - 'routeParams' => $fmr->getUrlParameters(), + 'data' => iterator_to_array($filterManager->getResult()), + 'filters' => $filterManager->getFilters(), + 'routeParams' => $filterManager->getUrlParameters(), ]; } } diff --git a/DependencyInjection/Compiler/ProviderPass.php b/DependencyInjection/Compiler/ProviderPass.php index f965d54..d05b635 100644 --- a/DependencyInjection/Compiler/ProviderPass.php +++ b/DependencyInjection/Compiler/ProviderPass.php @@ -88,11 +88,29 @@ function ($a, $b) { protected function generateProvider(ContainerBuilder $container, $profile) { $id = "ongr_settings.dynamic_provider.{$profile}"; + + $managerName = $this->getElasticManagerFromRepository($container); + $provider = new Definition($container->getParameter('ongr_settings.settings_provider.class'), [$profile]); - $provider->addMethodCall('setManager', [new Reference('es.manager')]); + $provider->addMethodCall('setManager', [new Reference($managerName)]); $provider->addTag('ongr_settings.settings_provider', ['profile' => $profile]); $container->setDefinition($id, $provider); return $id; } + + /** + * Gets Elasticsearch manager name from repository name. + * + * @param ContainerBuilder $container + * + * @return string + */ + private function getElasticManagerFromRepository(ContainerBuilder $container) + { + $repositoryName = $container->getParameter('ongr_settings.connection.repository'); + $managerName = substr($repositoryName, 0, strrpos($repositoryName, '.')); + + return $managerName; + } } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index ce99390..96ff255 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -47,6 +47,10 @@ public function getConfigTreeBuilder() ->defaultValue('ongr-settings') ->info('Index name for settings') ->end() + ->scalarNode('repository') + ->defaultValue('es.manager.settings.setting') + ->info('Elasticsearch repository for settings') + ->end() ->scalarNode('host') ->info('Address of your settings database') ->defaultValue('127.0.0.1') diff --git a/DependencyInjection/ONGRSettingsExtension.php b/DependencyInjection/ONGRSettingsExtension.php index 1682f0b..ac534ef 100644 --- a/DependencyInjection/ONGRSettingsExtension.php +++ b/DependencyInjection/ONGRSettingsExtension.php @@ -13,7 +13,9 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Loader; +use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpKernel\DependencyInjection\Extension; /** @@ -33,6 +35,7 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('ongr_settings.connection.index_name', $config['connection']['index_name']); $container->setParameter('ongr_settings.connection.port', $config['connection']['port']); $container->setParameter('ongr_settings.connection.host', $config['connection']['host']); + $container->setParameter('ongr_settings.connection.repository', $config['connection']['repository']); // Set profiles. $container->setParameter('ongr_settings.settings_container.profiles', $config['profiles']); @@ -45,11 +48,15 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('services/general_settings.yml'); $loader->load('services/pair_storage.yml'); + $loader->load('filters_container.yml'); + if (isset($config['admin_user'])) { $this->loadPersonalSettings($config['admin_user'], $container); } $this->injectPersonalSettings($container); + + $this->setFiltersManager($container); } /** @@ -64,6 +71,34 @@ protected function loadPersonalSettings($config, ContainerBuilder $containerBuil $containerBuilder->setParameter('ongr_settings.settings.settings', $config['settings']); } + /** + * Sets parameters for settings provider. + * + * @param array $config + * @param ContainerBuilder $containerBuilder + */ + protected function loadSettingsProvider($config, ContainerBuilder $containerBuilder) + { + $containerBuilder->setParameter('ongr_settings.settings.categories', $config['categories']); + } + + /** + * Sets parameters for filter manager. + * + * @param ContainerBuilder $container + */ + protected function setFiltersManager(ContainerBuilder $container) + { + $definition = new Definition( + 'ONGR\FilterManagerBundle\Search\FiltersManager', + [ + new Reference('ongr_settings.filters_container'), + new Reference($container->getParameter('ongr_settings.connection.repository')), + ] + ); + $container->setDefinition('ongr_settings.filters_manager', $definition); + } + /** * Injects additional General User settings to service container. * diff --git a/README.rst b/README.rst index 0a41831..e25977d 100644 --- a/README.rst +++ b/README.rst @@ -41,7 +41,7 @@ In your shell enter the following: .. code-block:: bash - composer require ongr/SettingsBundle "~0.1" + composer require ongr/settings-bundle "~0.1" .. @@ -62,8 +62,8 @@ Then register SettingsBundle and it dependant bundles in ``AppKernel.php``: new Tedivm\StashBundle\TedivmStashBundle(), new ONGR\CookiesBundle\ONGRCookiesBundle(), new ONGR\ElasticsearchBundle\ONGRElasticsearchBundle(), + new ONGR\FilterManagerBundle\ONGRFilterManagerBundle(), new ONGR\SettingsBundle\ONGRSettingsBundle(), - new ONGR\PagerBundle\ONGRPagerBundle(), ]; } @@ -117,53 +117,23 @@ You should add an entry to your ``config.yml`` you should add an entry: mappings: - ONGRSettingsBundle -... +.. Using this config, console command below will create an Elasticsearch index called ``settings`` with 2 shards and 0 replicas, after running the console command mentioned above. -If you are planing to use SettingsBundle outside an -`ongr.io `_ platform you should also add following configuration to your ``config.yml``: +In case if you wish to use different Elasticsearch connection options, you can configure manager used in SettingsBundle +with following ``config.yml`` entry: .. code-block:: yaml - parameters: - ongr_settings.settings_provider_es_manager: es.manager.settings - - services: - ongr_settings.settings_manager: - class: %ongr_settings.settings_manager.class% - arguments: - - @translator - - @event_dispatcher - - @es.manager.settings - - ongr_settings.listener.cookie_profile: - class: %ongr_settings.listener.cookie_profile.class% - calls: - - [ setSettingsContainer, [ @ongr_settings.settings_container ] ] - - [ setPersonalSettingsManager, [ @ongr_settings.settings.personal_settings_manager ] ] - - [ setManager, [ @es.manager.settings ] ] - tags: - - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } - - ongr_settings.profiles_manager: - class: %ongr_settings.profiles_manager.class% - arguments: - - @es.manager.settings - - ongr_settings.settings_provider: - class: %ongr_settings.profiles_manager.class% - arguments: - - @es.manager.settings - - ongr_settings.pair_storage: - class: %ongr_settings.pair_storage.class% - arguments: - - @es.manager.settings + ongr_settings: + connection: + repository: es.manager.default.setting .. +This example shows how you can configure settings repository, that is access in manager called ``default``. Second - new index in Elasticsearch should be created. This can be done by running a command in console: diff --git a/Resources/config/filters_container.yml b/Resources/config/filters_container.yml new file mode 100644 index 0000000..3f0df8c --- /dev/null +++ b/Resources/config/filters_container.yml @@ -0,0 +1,64 @@ +parameters: + ongr_settings.filter.pager.class: ONGR\FilterManagerBundle\Filters\Widget\Pager\Pager + ongr_settings.filter.pager.request_field: page + ongr_settings.filter.pager.count_per_page: 15 + ongr_settings.filter.pager.max_pages: 99 + + ongr_settings.filter.sort.class: ONGR\FilterManagerBundle\Filters\Widget\Sort\Sort + ongr_settings.filter.sort.request_field: sort + ongr_settings.filter.sort.choices: + nameAsc: + label: "Name Asc" + field: "name" + order: "asc" + default: true + nameDesc: + label: "Name Desc" + field: "name" + order: "desc" + default: false + + ongr_settings.filter.match_search.class: ONGR\FilterManagerBundle\Filters\Widget\Search\MatchSearch + ongr_settings.filter.match_search.request_field: q + ongr_settings.filter.match_search.field: "name" + + ongr_settings.filter.single_term.class: ONGR\FilterManagerBundle\Filters\Widget\Choice\SingleTermChoice + ongr_settings.filter.single_term.profile.request_field: profile + ongr_settings.filter.single_term.profile.field: profile + + + ongr_settings.filters_container.class: ONGR\FilterManagerBundle\Search\FiltersContainer + +services: + ongr_settings.filter.pager: + class: "%ongr_settings.filter.pager.class%" + calls: + - ["setRequestField", ["%ongr_settings.filter.pager.request_field%"]] + - ["setCountPerPage", ["%ongr_settings.filter.pager.count_per_page%"]] + - ["setMaxPages", ["%ongr_settings.filter.pager.max_pages%"]] + + ongr_settings.filter.sort: + class: "%ongr_settings.filter.sort.class%" + calls: + - ["setRequestField", ["%ongr_settings.filter.sort.request_field%"]] + - ["setChoices", ["%ongr_settings.filter.sort.choices%"]] + + ongr_settings.filter.match_search: + class: "%ongr_settings.filter.match_search.class%" + calls: + - ["setRequestField", ["%ongr_settings.filter.match_search.request_field%"]] + - ["setField", ["%ongr_settings.filter.match_search.field%"]] + + ongr_settings.filter.single_term.profile: + class: "%ongr_settings.filter.single_term.class%" + calls: + - ["setRequestField", ["%ongr_settings.filter.single_term.profile.request_field%"]] + - ["setField", ["%ongr_settings.filter.single_term.profile.field%"]] + + ongr_settings.filters_container: + class: "%ongr_settings.filters_container.class%" + calls: + - ["set", ["pager", "@ongr_settings.filter.pager"]] + - ["set", ["sort", "@ongr_settings.filter.sort"]] + - ["set", ["search", "@ongr_settings.filter.match_search"]] + - ["set", ["profile", "@ongr_settings.filter.single_term.profile"]] diff --git a/Resources/config/services/general_settings.yml b/Resources/config/services/general_settings.yml index f015a2c..41be0b7 100644 --- a/Resources/config/services/general_settings.yml +++ b/Resources/config/services/general_settings.yml @@ -15,7 +15,7 @@ services: arguments: - @translator - @event_dispatcher - - @es.manager + - @es.manager.settings ongr_settings.settings_container: class: %ongr_settings.settings_container.class% @@ -39,11 +39,11 @@ services: calls: - [ setSettingsContainer, [ @ongr_settings.settings_container ] ] - [ setPersonalSettingsManager, [ @ongr_settings.settings.personal_settings_manager ] ] - - [ setManager, [ @es.manager ] ] + - [ setManager, [ @es.manager.settings ] ] tags: - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } ongr_settings.profiles_manager: class: %ongr_settings.profiles_manager.class% arguments: - - @es.manager + - @es.manager.settings diff --git a/Resources/config/services/pair_storage.yml b/Resources/config/services/pair_storage.yml index 026a811..ce69e70 100644 --- a/Resources/config/services/pair_storage.yml +++ b/Resources/config/services/pair_storage.yml @@ -5,4 +5,4 @@ services: ongr_settings.pair_storage: class: %ongr_settings.pair_storage.class% arguments: - - @es.manager + - @es.manager.settings diff --git a/Resources/doc/index.rst b/Resources/doc/index.rst index 0a41831..e25977d 100644 --- a/Resources/doc/index.rst +++ b/Resources/doc/index.rst @@ -41,7 +41,7 @@ In your shell enter the following: .. code-block:: bash - composer require ongr/SettingsBundle "~0.1" + composer require ongr/settings-bundle "~0.1" .. @@ -62,8 +62,8 @@ Then register SettingsBundle and it dependant bundles in ``AppKernel.php``: new Tedivm\StashBundle\TedivmStashBundle(), new ONGR\CookiesBundle\ONGRCookiesBundle(), new ONGR\ElasticsearchBundle\ONGRElasticsearchBundle(), + new ONGR\FilterManagerBundle\ONGRFilterManagerBundle(), new ONGR\SettingsBundle\ONGRSettingsBundle(), - new ONGR\PagerBundle\ONGRPagerBundle(), ]; } @@ -117,53 +117,23 @@ You should add an entry to your ``config.yml`` you should add an entry: mappings: - ONGRSettingsBundle -... +.. Using this config, console command below will create an Elasticsearch index called ``settings`` with 2 shards and 0 replicas, after running the console command mentioned above. -If you are planing to use SettingsBundle outside an -`ongr.io `_ platform you should also add following configuration to your ``config.yml``: +In case if you wish to use different Elasticsearch connection options, you can configure manager used in SettingsBundle +with following ``config.yml`` entry: .. code-block:: yaml - parameters: - ongr_settings.settings_provider_es_manager: es.manager.settings - - services: - ongr_settings.settings_manager: - class: %ongr_settings.settings_manager.class% - arguments: - - @translator - - @event_dispatcher - - @es.manager.settings - - ongr_settings.listener.cookie_profile: - class: %ongr_settings.listener.cookie_profile.class% - calls: - - [ setSettingsContainer, [ @ongr_settings.settings_container ] ] - - [ setPersonalSettingsManager, [ @ongr_settings.settings.personal_settings_manager ] ] - - [ setManager, [ @es.manager.settings ] ] - tags: - - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } - - ongr_settings.profiles_manager: - class: %ongr_settings.profiles_manager.class% - arguments: - - @es.manager.settings - - ongr_settings.settings_provider: - class: %ongr_settings.profiles_manager.class% - arguments: - - @es.manager.settings - - ongr_settings.pair_storage: - class: %ongr_settings.pair_storage.class% - arguments: - - @es.manager.settings + ongr_settings: + connection: + repository: es.manager.default.setting .. +This example shows how you can configure settings repository, that is access in manager called ``default``. Second - new index in Elasticsearch should be created. This can be done by running a command in console: diff --git a/Resources/views/Settings/inc/navigation.html.twig b/Resources/views/Settings/inc/navigation.html.twig index cb55c6f..0fb47d9 100644 --- a/Resources/views/Settings/inc/navigation.html.twig +++ b/Resources/views/Settings/inc/navigation.html.twig @@ -1,7 +1,7 @@