diff --git a/config/module.config.php b/config/module.config.php index 3181ab7..1bee2dc 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -204,6 +204,7 @@ 'Solr\Form\Admin\SolrNodeForm' => Service\Form\SolrNodeFormFactory::class, 'Solr\Form\Admin\SolrMappingForm' => Service\Form\SolrMappingFormFactory::class, 'Solr\Form\Admin\SolrSearchFieldForm' => Service\Form\SolrSearchFieldFormFactory::class, + 'Solr\Form\Admin\SolrQuickMappingForm' => Service\Form\SolrQuickMappingFormFactory::class, ], 'invokables' => [ 'Solr\Form\Admin\SolrMappingImportForm' => Form\Admin\SolrMappingImportForm::class, diff --git a/src/Controller/Admin/MappingController.php b/src/Controller/Admin/MappingController.php index 2ce2ccc..544bcbc 100644 --- a/src/Controller/Admin/MappingController.php +++ b/src/Controller/Admin/MappingController.php @@ -36,6 +36,7 @@ use Solr\Api\Representation\SolrNodeRepresentation; use Solr\Form\Admin\SolrMappingForm; use Solr\Form\Admin\SolrMappingImportForm; +use Solr\Form\Admin\SolrQuickMappingForm; use Solr\ValueExtractor\Manager as ValueExtractorManager; class MappingController extends AbstractActionController @@ -131,6 +132,62 @@ public function addAction() return $view; } + public function quickAddAction() + { + $solrNodeId = $this->params('nodeId'); + $resourceName = $this->params('resourceName'); + + $form = $this->getForm(SolrQuickMappingForm::class, ['solr_node_id' => $solrNodeId]); + + $view = new ViewModel; + $view->setVariable('form', $form); + + if ($this->getRequest()->isPost()) { + $form->setData($this->params()->fromPost()); + if ($form->isValid()) { + $data = $form->getData(); + + $new_mappings = []; + foreach ($data["o:source"] as $source) { + foreach ($data["o:field_name"] as $field_name) { + $field_name = str_replace('*', preg_replace('/[^a-zA-Z0-9]/', '_', $source), $field_name); + $solr_mapping = $this->api()->searchOne('solr_mappings', [ + 'solr_node_id' => $solrNodeId, + 'resource_name' => $resourceName, + 'field_name' => $field_name, + 'source' => $source, + ])->getContent(); + + if ($solr_mapping) { + // Do not prevent creation, the new mapping may have different settings + $this->messenger()->addWarning(sprintf('A mapping with the name "%s" and the source "%s" already exists. This may be a duplicate.', $field_name, $source)); + } + + $new_mappings[] = [ + 'o:solr_node' => ['o:id' => $solrNodeId], + 'o:resource_name' => $resourceName, + 'o:field_name' => $field_name, + 'o:source' => $source, + 'o:settings' => $data['o:settings'], + ]; + } + } + + $this->api()->batchCreate('solr_mappings', $new_mappings); + $this->messenger()->addSuccess('Solr mapping modified.'); + + return $this->redirect()->toRoute('admin/solr/node-id-mapping-resource', [ + 'nodeId' => $solrNodeId, + 'resourceName' => $resourceName, + ]); + } else { + $this->messenger()->addError('There was an error during validation'); + } + } + + return $view; + } + public function editAction() { $solrNodeId = $this->params('nodeId'); @@ -269,6 +326,17 @@ protected function getSolrSchema($solrNodeId) } } + protected function getSolrSchemaClass($solrNodeId) + { + try { + $solrNode = $this->api()->read('solr_nodes', $solrNodeId)->getContent(); + $schema = $solrNode->schema(); + return $schema; + } catch (\Exception $e) { + return null; + } + } + protected function deleteMappings(SolrNodeRepresentation $solrNode) { $ids = $this->api()->search('solr_mappings', ['solr_node_id' => $solrNode->id()], ['returnScalar' => 'id'])->getContent(); diff --git a/src/Form/Admin/SolrQuickMappingForm.php b/src/Form/Admin/SolrQuickMappingForm.php new file mode 100644 index 0000000..aaaf995 --- /dev/null +++ b/src/Form/Admin/SolrQuickMappingForm.php @@ -0,0 +1,91 @@ +getTranslator(); + + $this->add([ + 'name' => 'o:source', + 'type' => Element\Select::class, + 'options' => [ + 'label' => $translator->translate('Sources'), + 'value_options' => array_combine( + $this->options['terms'], + $this->options['terms'] + ), + ], + 'attributes' => [ + 'id' => 'source', + 'multiple' => true, + 'required' => true, + ], + ]); + + $this->add([ + 'name' => 'o:field_name', + 'type' => Element\Select::class, + 'options' => [ + 'label' => $translator->translate('Solr fields'), + 'value_options' => array_combine( + $this->options['dynamic_fields'], + $this->options['dynamic_fields'] + ), + ], + 'attributes' => [ + 'id' => 'field-name', + 'multiple' => true, + 'required' => true, + ], + ]); + + $settingsFieldset = new Fieldset('o:settings'); + + $transformationNames = $this->transformationManager->getRegisteredNames($sortAlpha = true); + $transformationValueOptions = []; + foreach ($transformationNames as $name) { + $transformation = $this->transformationManager->get($name); + $transformationValueOptions[] = [ + 'value' => $name, + 'label' => $transformation->getLabel(), + 'empty_option' => '', + ]; + } + + $settingsFieldset->add([ + 'name' => 'transformations', + 'type' => Transformations::class, + 'options' => [ + 'label' => 'Transformations', // @translate + 'value_options' => $transformationValueOptions, + 'empty_option' => '', + ], + ]); + + $this->add($settingsFieldset); + } + + public function setTransformationManager(\Solr\Transformation\Manager $transformationManager) + { + $this->transformationManager = $transformationManager; + } + + public function getTransformationManager(): \Solr\Transformation\Manager + { + return $this->transformationManager; + } +} diff --git a/src/Schema.php b/src/Schema.php index 67f929e..82f0597 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -141,6 +141,11 @@ public function getDynamicFieldsMap() return $this->dynamicFieldsMap; } + public function getDynamicFields() + { + return $this->getSchema()['dynamicFields']; + } + public function getType($name) { $typesByName = $this->getTypesByName(); diff --git a/src/Service/Form/SolrQuickMappingFormFactory.php b/src/Service/Form/SolrQuickMappingFormFactory.php new file mode 100644 index 0000000..1f0ae10 --- /dev/null +++ b/src/Service/Form/SolrQuickMappingFormFactory.php @@ -0,0 +1,37 @@ +setTranslator($services->get('MvcTranslator')); + + $solrNode = $services->get('Omeka\ApiManager')->read('solr_nodes', $options['solr_node_id'])->getContent(); + $dynamicFieldsAux = $solrNode->schema()->getDynamicFields(); + + $dynamicFields = []; + foreach ($dynamicFieldsAux as $dynamicField) { + $dynamicFields[] = $dynamicField['name']; + } + + $properties = $services->get('Omeka\ApiManager')->search('properties')->getContent(); + $terms = []; + foreach ($properties as $property) { + $terms[] = $property->term(); + } + + $form->setOption('terms', $terms); + $form->setOption('dynamic_fields', $dynamicFields); + + $transformationManager = $services->get('Solr\TransformationManager'); + $form->setTransformationManager($transformationManager); + + return $form; + } +} diff --git a/view/solr/admin/mapping/browse-resource.phtml b/view/solr/admin/mapping/browse-resource.phtml index 066f324..0f0a8d0 100644 --- a/view/solr/admin/mapping/browse-resource.phtml +++ b/view/solr/admin/mapping/browse-resource.phtml @@ -36,6 +36,12 @@ ?>