Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Item stub #2211

Closed
wants to merge 11 commits into from
Closed
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
109 changes: 109 additions & 0 deletions application/asset/js/resource-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,115 @@
$('#values-json').val(JSON.stringify(collectValues()));
});

// Item stub form: handle "New item" nav click.
$(document).on('click', '#item-stub-section-label', function(e) {
$(this).closest('.section-nav').find('li').toggleClass('active');
$('#item-section').hide();
$('#item-stub-section').show();
$('.chosen-select').chosen({allow_single_deselect: true});
});
// Item stub form: handle "Existing item" nav click.
$(document).on('click', '#item-section-label', function(e) {
$(this).closest('.section-nav').find('li').toggleClass('active');
$('#item-section').show();
$('#item-stub-section').hide();
});
// Item stub form: handle item stub form submission.
$(document).on('click', '#item-stub-submit', function(e) {
e.preventDefault();
const itemStubForm = $('#item-stub-form');
const resourceTemplate = $('#item-stub-resource-template');
const resourceClass = $('#item-stub-resource-class');
const itemData = {};
if (resourceTemplate.val()) {
itemData['o:resource_template'] = {'o:id': resourceTemplate.val()};
}
if (resourceClass.val()) {
itemData['o:resource_class'] = {'o:id': resourceClass.val()};
}
itemStubForm.find('[data-property-id]').each(function() {
const propertyValue = $(this);
if (propertyValue.val()) {
const propertyId = propertyValue.data('propertyId');
const type = propertyValue.data('type') ?? 'literal';
if (!itemData.hasOwnProperty(propertyId)) {
itemData[propertyId] = [];
}
itemData[propertyId].push({
'property_id': propertyId,
'type': type,
'@value': propertyValue.val()
});
}
});
itemData['csrf'] = itemStubForm.find('input[name="csrf"]').val();
$.post(itemStubForm.data('submitUrl'), itemData, function(data) {
const selectedResource = $('.selecting-resource').find('.selected-resource');
selectedResource.prev('span.default').hide();
const a = $('<a>', {href: data['admin_url']}).text(data['display_title']);
selectedResource.find('.o-title').removeClass().addClass('o-title items').html(a);
selectedResource.find('.value').val(data['o:id']);
Omeka.closeSidebar($('#select-resource'));
});
});
// Item stub form: handle resource template change.
$(document).on('change', '#item-stub-resource-template', function(e) {
const itemStubForm = $('#item-stub-form');
const resourceTemplate = $('#item-stub-resource-template');
const resourceClass = $('#item-stub-resource-class');
const title = $('#item-stub-title');
const description = $('#item-stub-description');
const resourceTemplateUrl = itemStubForm.data('resourceTemplateUrl') + '/' + resourceTemplate.val();
$.get(resourceTemplateUrl, function(resourceTemplateData) {
const templateResourceClass = resourceTemplateData['o:resource_class'];
const templateTitleProperty = resourceTemplateData['o:title_property'];
const templateDescriptionProperty = resourceTemplateData['o:description_property'];
if (templateResourceClass) {
// Set the template-defined class.
resourceClass.val(templateResourceClass['o:id']);
resourceClass.trigger('chosen:updated');
}
if (templateTitleProperty) {
// Set the title defined by the template (including alt label).
const propertyUrl = itemStubForm.data('propertyUrl') + '/' + templateTitleProperty['o:id'];
$.get(propertyUrl, function(propertyData) {
let propertyLabel = propertyData['o:label'];
$.each(resourceTemplateData['o:resource_template_property'], function(key, value) {
if (value['o:property']['o:id'] === templateTitleProperty['o:id']) {
if (value['o:alternate_label']) propertyLabel = value['o:alternate_label'];
return false;
}
});
title.data('propertyId', templateTitleProperty['o:id']);
title.closest('.field').find('[for="item-stub-title"]').text(propertyLabel);
});
} else {
// Set the default title.
title.data('propertyId', title.data('propertyIdDefault'));
title.closest('.field').find('[for="item-stub-title"]').text(title.data('propertyLabelDefault'));
}
if (templateDescriptionProperty) {
// Set the description defined by the template (including alt label).
const propertyUrl = itemStubForm.data('propertyUrl') + '/' + templateDescriptionProperty['o:id'];
$.get(propertyUrl, function(propertyData) {
let propertyLabel = propertyData['o:label'];
$.each(resourceTemplateData['o:resource_template_property'], function(key, value) {
if (value['o:property']['o:id'] === templateDescriptionProperty['o:id']) {
if (value['o:alternate_label']) propertyLabel = value['o:alternate_label'];
return false;
}
});
description.data('propertyId', templateDescriptionProperty['o:id']);
description.closest('.field').find('[for="item-stub-description"]').text(propertyLabel);
});
} else {
// Set the default description.
description.data('propertyId', description.data('propertyIdDefault'));
description.closest('.field').find('[for="item-stub-description"]').text(description.data('propertyLabelDefault'));
}
});
});

initPage();
});

Expand Down
1 change: 1 addition & 0 deletions application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@
],
'factories' => [
'Omeka\Form\ResourceForm' => Service\Form\ResourceFormFactory::class,
'Omeka\Form\ItemStubForm' => Service\Form\ItemStubFormFactory::class,
'Omeka\Form\VocabularyForm' => Service\Form\VocabularyFormFactory::class,
'Omeka\Form\ResourceBatchUpdateForm' => Service\Form\ResourceBatchUpdateFormFactory::class,
'Omeka\Form\UserForm' => Service\Form\UserFormFactory::class,
Expand Down
26 changes: 26 additions & 0 deletions application/src/Controller/Admin/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Omeka\Controller\Admin;

use Omeka\Form\ConfirmForm;
use Omeka\Form\ItemStubForm;
use Omeka\Form\ResourceForm;
use Omeka\Form\ResourceBatchUpdateForm;
use Omeka\Media\Ingester\Manager;
Expand Down Expand Up @@ -103,6 +104,7 @@ public function sidebarSelectAction()
$view->setVariable('itemSetId', $this->params()->fromQuery('item_set_id'));
$view->setVariable('id', $this->params()->fromQuery('id'));
$view->setVariable('showDetails', true);
$view->setVariable('itemStubForm', $this->getForm(ItemStubForm::class));
$view->setTerminal(true);
return $view;
}
Expand Down Expand Up @@ -236,6 +238,30 @@ public function addAction()
return $view;
}

public function addItemStubAction()
{
$request = $this->getRequest();
$response = $this->getResponse();
if (!$request->isPost()) {
$response->setStatusCode(500);
return $response;
}
$itemData = $this->params()->fromPost();
$form = $this->getForm(ItemStubForm::class);
$form->setData($itemData);
if (!$form->isValid()) {
$response->setStatusCode(500);
return $response;
}
$item = $this->api()->create('items', $itemData)->getContent();
$itemJson = json_decode(json_encode($item), true);
$itemJson['admin_url'] = $this->url()->fromRoute('admin/id', ['action' => 'show', 'id' => $item->id()], true);
$itemJson['display_title'] = $item->displayTitle();
$response->getHeaders()->addHeaders(['Content-Type' => 'application/ld+json']);
$response->setContent(json_encode($itemJson));
return $response;
}

public function editAction()
{
$item = $this->api()->read('items', $this->params('id'))->getContent();
Expand Down
164 changes: 164 additions & 0 deletions application/src/Form/ItemStubForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php
namespace Omeka\Form;

use Laminas\EventManager\Event;
use Laminas\EventManager\EventManagerAwareTrait;
use Laminas\Form\Form;
use Laminas\View\HelperPluginManager;
use Omeka\Api\Manager as ApiManager;
use Omeka\Form\Element as OmekaElement;

class ItemStubForm extends Form
{
use EventManagerAwareTrait;

protected $apiManager;

protected $viewHelperManager;

public function init()
{
$url = $this->getViewHelperManager()->get('url');
$translate = $this->getViewHelperManager()->get('translate');
$api = $this->getViewHelperManager()->get('api');

$this->setAttribute('id', 'item-stub-form');
$this->setAttribute('data-submit-url', $url(
'admin/default',
['controller' => 'item', 'action' => 'add-item-stub']
));
$this->setAttribute('data-resource-template-url', $url(
'api/default',
['resource' => 'resource_templates']
));
$this->setAttribute('data-property-url', $url(
'api/default',
['resource' => 'properties']
));

$this->add([
'type' => OmekaElement\ResourceSelect::class,
'name' => 'resource_template',
'options' => [
'label' => 'Resource template', // @translate
'empty_option' => '',
'resource_value_options' => [
'resource' => 'resource_templates',
'query' => [
'sort_by' => 'label',
],
'option_text_callback' => function ($resourceTemplate) {
return $resourceTemplate->label();
},
],
],
'attributes' => [
'id' => 'item-stub-resource-template',
'class' => 'chosen-select',
'data-placeholder' => 'Select a template', // @translate
],
]);

$this->add([
'type' => OmekaElement\ResourceClassSelect::class,
'name' => 'resource_class',
'options' => [
'label' => 'Class', // @translate
'empty_option' => '',
],
'attributes' => [
'id' => 'item-stub-resource-class',
'class' => 'chosen-select',
'data-placeholder' => 'Select a class', // @translate
],
]);

$property = $api->searchOne(
'properties',
['term' => 'dcterms:title']
)->getContent();
$this->add([
'type' => 'textarea',
'name' => 'title',
'options' => [
'label' => 'Title', // @translate
],
'attributes' => [
'id' => 'item-stub-title',
'data-property-id' => $property->id(),
'data-property-id-default' => $property->id(),
'data-property-label-default' => $translate('Title'),
],
]);

$property = $api->searchOne(
'properties',
['term' => 'dcterms:description']
)->getContent();
$this->add([
'type' => 'textarea',
'name' => 'description',
'options' => [
'label' => 'Description', // @translate
],
'attributes' => [
'id' => 'item-stub-description',
'data-property-id' => $property->id(),
'data-property-id-default' => $property->id(),
'data-property-label-default' => $translate('Description'),
],
]);

$this->add([
'type' => 'submit',
'name' => 'submit',
'attributes' => [
'id' => 'item-stub-submit',
'value' => 'Add and select item', // @translate
],
]);

// Allow modules to modify this form. Modules may add value elements by
// adding a "data-property-id" attribute to the element, set to the
// property ID. They may also add a "data-type" attribute to the element
// to set a data type that is not "literal".
$addEvent = new Event('form.add_elements', $this);
$this->getEventManager()->triggerEvent($addEvent);

$inputFilter = $this->getInputFilter();
$inputFilter->add([
'name' => 'resource_template',
'required' => false,
'allow_empty' => true,
]);
$inputFilter->add([
'name' => 'resource_class',
'required' => false,
'allow_empty' => true,
]);

// Allow modules to modify this form's input filters.
$filterEvent = new Event('form.add_input_filters', $this, ['inputFilter' => $inputFilter]);
$this->getEventManager()->triggerEvent($filterEvent);
}

public function setApiManager(ApiManager $apiManager)
{
$this->apiManager = $apiManager;
}

public function getApiManager()
{
return $this->apiManager;
}

public function setViewHelperManager(HelperPluginManager $viewHelperManager)
{
$this->viewHelperManager = $viewHelperManager;
}

public function getViewHelperManager()
{
return $this->viewHelperManager;
}
}
18 changes: 18 additions & 0 deletions application/src/Service/Form/ItemStubFormFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Omeka\Service\Form;

use Omeka\Form\ItemStubForm;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;

class ItemStubFormFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $services, $requestedName, array $options = null)
{
$form = new ItemStubForm;
$form->setApiManager($services->get('Omeka\ApiManager'));
$form->setViewHelperManager($services->get('ViewHelperManager'));
$form->setEventManager($services->get('EventManager'));
return $form;
}
}
14 changes: 13 additions & 1 deletion application/view/omeka/admin/item/sidebar-select.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ $itemsFound = count($items) > 0;
$expanded = $resourceClassId || $itemSetId || $id;
?>

<?php echo $this->sectionNav([
'item-section' => $translate('Existing item'),
'item-stub-section' => $translate('New item'),
]); ?>

<div id="item-section" class="active section">

<div id="item-results">
<h3><?php echo $translate('Select item'); ?></h3>

<div class="search-nav">
<div id="sidebar-resource-search" class="resource-search" data-search-url="<?php echo $escape($this->url(null, [], [], true)); ?>">
<?php
Expand Down Expand Up @@ -104,3 +110,9 @@ $expanded = $resourceClassId || $itemSetId || $id;
<div class="confirm-panel">
<button type="button" class="select-resources-button"><?php echo $translate('Add selected'); ?></button>
</div>

</div>

<div id="item-stub-section" class="section">
<?php echo $this->form($itemStubForm, false); ?>
</div>
Loading