Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
68 changes: 68 additions & 0 deletions src/Controller/Admin/MappingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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();
Expand Down
91 changes: 91 additions & 0 deletions src/Form/Admin/SolrQuickMappingForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Solr\Form\Admin;

use Laminas\Form\Form;
use Laminas\Form\Element;
use Laminas\Form\Fieldset;
use Solr\Form\Element\Transformations;
use Laminas\I18n\Translator\TranslatorAwareInterface;
use Laminas\I18n\Translator\TranslatorAwareTrait;

class SolrQuickMappingForm extends Form implements TranslatorAwareInterface
{
use TranslatorAwareTrait;

protected $transformationManager;

public function init()
{
$translator = $this->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;
}
}
5 changes: 5 additions & 0 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ public function getDynamicFieldsMap()
return $this->dynamicFieldsMap;
}

public function getDynamicFields()
{
return $this->getSchema()['dynamicFields'];
}

public function getType($name)
{
$typesByName = $this->getTypesByName();
Expand Down
37 changes: 37 additions & 0 deletions src/Service/Form/SolrQuickMappingFormFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace Solr\Service\Form;

use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Solr\Form\Admin\SolrQuickMappingForm;

class SolrQuickMappingFormFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $services, $requestedName, array $options = null)
{
$form = new SolrQuickMappingForm;
$form->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;
}
}
6 changes: 6 additions & 0 deletions view/solr/admin/mapping/browse-resource.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
?>

<div id="page-actions">
<?php $quickAddUrl = $this->url('admin/solr/node-id-mapping-resource', [
'nodeId' => $solrNode->id(),
'resourceName' => $resourceName,
'action' => 'quickAdd',
]); ?>
<a class="button" href="<?php echo $quickAddUrl ?>"><?php echo $this->translate('Quick add')?></a>
<?php $addUrl = $this->url('admin/solr/node-id-mapping-resource', [
'nodeId' => $solrNode->id(),
'resourceName' => $resourceName,
Expand Down
48 changes: 48 additions & 0 deletions view/solr/admin/mapping/quick-add.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/*
* Copyright BibLibre, 2017
*
* This software is governed by the CeCILL license under French law and abiding
* by the rules of distribution of free software. You can use, modify and/ or
* redistribute the software under the terms of the CeCILL license as circulated
* by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy, modify
* and redistribute granted by the license, users are provided only with a
* limited warranty and the software's author, the holder of the economic
* rights, and the successive licensors have only limited liability.
*
* In this respect, the user's attention is drawn to the risks associated with
* loading, using, modifying and/or developing or reproducing the software by
* the user in light of its specific status of free software, that may mean that
* it is complicated to manipulate, and that also therefore means that it is
* reserved for developers and experienced professionals having in-depth
* computer knowledge. Users are therefore encouraged to load and test the
* software's suitability as regards their requirements in conditions enabling
* the security of their systems and/or data to be ensured and, more generally,
* to use and operate it in the same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*/
?>

<?php echo $this->pageTitle($this->translate('Quickly add Solr mappings')); ?>

<?php echo $this->partial('solr/admin/mapping/form'); ?>

<script>
(function ($) {
$(document).ready(function () {
const placeholder = <?= json_encode($this->translate('Search and select...')) ?>;
// Activate Chosen
$('select#source, select#field-name').chosen({
allow_single_deselect: true,
width: '100%',
search_contains: true,
placeholder_text_multiple: placeholder,
placeholder_text_single: placeholder,
});
});
})(jQuery);
</script>