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

Feature/batch edit media #1101

Merged
merged 13 commits into from
Apr 2, 2018
2 changes: 1 addition & 1 deletion application/asset/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ var Omeka = {

$('.select-all').change(function() {
if (this.checked) {
$('.batch-edit td input[type=checkbox]').prop('checked', true);
$('.batch-edit td input[type=checkbox]:not(:disabled)').prop('checked', true);
} else {
$('.batch-edit td input[type=checkbox]:checked').prop('checked', false);
}
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 @@ -442,6 +442,7 @@
],
'factories' => [
'Omeka\Form\ResourceForm' => Service\Form\ResourceFormFactory::class,
'Omeka\Form\ResourceBatchUpdateForm' => Service\Form\ResourceBatchUpdateFormFactory::class,
'Omeka\Form\UserForm' => Service\Form\UserFormFactory::class,
'Omeka\Form\SettingForm' => Service\Form\SettingFormFactory::class,
'Omeka\Form\ModuleStateChangeForm' => Service\Form\ModuleStateChangeFormFactory::class,
Expand Down
12 changes: 12 additions & 0 deletions application/src/Api/Adapter/MediaAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,16 @@ public function hydrateOwner(Request $request, EntityInterface $entity)
$entity->setOwner($entity->getItem()->getOwner());
}
}

public function preprocessBatchUpdate(array $data, Request $request)
{
$rawData = $request->getContent();
$data = parent::preprocessBatchUpdate($data, $request);

if (array_key_exists('o:lang', $rawData)) {
$data['o:lang'] = $rawData['o:lang'];
}

return $data;
}
}
90 changes: 13 additions & 77 deletions application/src/Controller/Admin/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,21 +277,20 @@ public function batchEditAction()
}

$form = $this->getForm(ResourceBatchUpdateForm::class, ['resource_type' => 'item']);
$form->setAttribute('id', 'batch-edit-item');
if ($this->params()->fromPost('batch_update')) {
$data = $this->params()->fromPost();
$form->setData($data);

if ($form->isValid()) {
list($dataRemove, $dataAppend) = $this->preprocessBatchUpdateData($data);
$data = $form->preprocessData();

$this->api($form)->batchUpdate('items', $resourceIds, $dataRemove, [
'continueOnError' => true,
'collectionAction' => 'remove',
]);
$this->api($form)->batchUpdate('items', $resourceIds, $dataAppend, [
'continueOnError' => true,
'collectionAction' => 'append',
]);
foreach ($data as $collectionAction => $properties) {
$this->api($form)->batchUpdate('items', $resourceIds, $properties, [
'continueOnError' => true,
'collectionAction' => $collectionAction,
]);
}

$this->messenger()->addSuccess('Items successfully edited'); // @translate
return $this->redirect()->toRoute(null, ['action' => 'browse'], true);
Expand Down Expand Up @@ -324,18 +323,20 @@ public function batchEditAllAction()
$count = $this->api()->search('items', ['limit' => 0] + $query)->getTotalResults();

$form = $this->getForm(ResourceBatchUpdateForm::class, ['resource_type' => 'item']);
$form->setAttribute('id', 'batch-edit-item');
if ($this->params()->fromPost('batch_update')) {
$data = $this->params()->fromPost();
$form->setData($data);

if ($form->isValid()) {
list($dataRemove, $dataAppend) = $this->preprocessBatchUpdateData($data);
$data = $form->preprocessData();

$job = $this->jobDispatcher()->dispatch('Omeka\Job\BatchUpdate', [
'resource' => 'items',
'query' => $query,
'data_remove' => $dataRemove,
'data_append' => $dataAppend,
'data' => isset($data['replace']) ? $data['replace'] : [],
'data_remove' => isset($data['remove']) ? $data['remove'] : [],
'data_append' => isset($data['append']) ? $data['append'] : [],
]);

$this->messenger()->addSuccess('Editing items. This may take a while.'); // @translate
Expand All @@ -354,71 +355,6 @@ public function batchEditAllAction()
return $view;
}

/**
* Preprocess batch update data.
*
* Batch update data contains instructions on what to update. It needs to be
* preprocessed before it's sent to the API.
*
* @param array $data
* @return array An array containing the collectionAction=remove data as the
* first element and the collectionAction=append data as the second.
*/
protected function preprocessBatchUpdateData(array $data)
{
$dataRemove = [];
$dataAppend = [];

// Set the data to change and data to remove.
if (in_array($data['is_public'], ['0', '1'])) {
$dataRemove['o:is_public'] = $data['is_public'];
}
if (-1 == $data['resource_template']) {
$dataRemove['o:resource_template'] = ['o:id' => null];
} elseif (is_numeric($data['resource_template'])) {
$dataRemove['o:resource_template'] = ['o:id' => $data['resource_template']];
}
if (-1 == $data['resource_class']) {
$dataRemove['o:resource_class'] = ['o:id' => null];
} elseif (is_numeric($data['resource_class'])) {
$dataRemove['o:resource_class'] = ['o:id' => $data['resource_class']];
}
if (isset($data['remove_from_item_set'])) {
$dataRemove['o:item_set'] = $data['remove_from_item_set'];
}
if (isset($data['clear_property_values'])) {
$dataRemove['clear_property_values'] = $data['clear_property_values'];
}

// Set the data to append.
if (isset($data['value'])) {
foreach ($data['value'] as $value) {
$valueObj = [
'property_id' => $value['property_id'],
'type' => $value['type'],
];
switch ($value['type']) {
case 'uri':
$valueObj['@id'] = $value['id'];
$valueObj['o:label'] = $value['label'];
break;
case 'resource':
$valueObj['value_resource_id'] = $value['value_resource_id'];
break;
case 'literal':
default:
$valueObj['@value'] = $value['value'];
}
$dataAppend[$value['property_id']][] = $valueObj;
}
}
if (isset($data['add_to_item_set'])) {
$dataAppend['o:item_set'] = array_unique($data['add_to_item_set']);
}

return [$dataRemove, $dataAppend];
}

protected function getMediaForms()
{
$mediaHelper = $this->viewHelpers()->get('media');
Expand Down
91 changes: 15 additions & 76 deletions application/src/Controller/Admin/ItemSetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,22 +248,21 @@ public function batchEditAction()
$resources[] = $this->api()->read('item_sets', $resourceId)->getContent();
}

$form = $this->getForm(ResourceBatchUpdateForm::class);
$form = $this->getForm(ResourceBatchUpdateForm::class, ['resource_type' => 'itemSet']);
$form->setAttribute('id', 'batch-edit-item-set');
if ($this->params()->fromPost('batch_update')) {
$data = $this->params()->fromPost();
$form->setData($data);

if ($form->isValid()) {
list($dataRemove, $dataAppend) = $this->preprocessBatchUpdateData($data);
$data = $form->preprocessData();

$this->api($form)->batchUpdate('item_sets', $resourceIds, $dataRemove, [
'continueOnError' => true,
'collectionAction' => 'remove',
]);
$this->api($form)->batchUpdate('item_sets', $resourceIds, $dataAppend, [
'continueOnError' => true,
'collectionAction' => 'append',
]);
foreach ($data as $collectionAction => $properties) {
$this->api($form)->batchUpdate('item_sets', $resourceIds, $properties, [
'continueOnError' => true,
'collectionAction' => $collectionAction,
]);
}

$this->messenger()->addSuccess('Item sets successfully edited'); // @translate
return $this->redirect()->toRoute(null, ['action' => 'browse'], true);
Expand Down Expand Up @@ -295,19 +294,21 @@ public function batchEditAllAction()
$query['offset'], $query['sort_by'], $query['sort_order']);
$count = $this->api()->search('item_sets', ['limit' => 0] + $query)->getTotalResults();

$form = $this->getForm(ResourceBatchUpdateForm::class);
$form = $this->getForm(ResourceBatchUpdateForm::class, ['resource_type' => 'itemSet']);
$form->setAttribute('id', 'batch-edit-item-set');
if ($this->params()->fromPost('batch_update')) {
$data = $this->params()->fromPost();
$form->setData($data);

if ($form->isValid()) {
list($dataRemove, $dataAppend) = $this->preprocessBatchUpdateData($data);
$data = $form->preprocessData();

$job = $this->jobDispatcher()->dispatch('Omeka\Job\BatchUpdate', [
'resource' => 'item_sets',
'query' => $query,
'data_remove' => $dataRemove,
'data_append' => $dataAppend,
'data' => isset($data['replace']) ? $data['replace'] : [],
'data_remove' => isset($data['remove']) ? $data['remove'] : [],
'data_append' => isset($data['append']) ? $data['append'] : [],
]);

$this->messenger()->addSuccess('Editing item sets. This may take a while.'); // @translate
Expand All @@ -325,66 +326,4 @@ public function batchEditAllAction()
$view->setVariable('count', $count);
return $view;
}

/**
* Preprocess batch update data.
*
* Batch update data contains instructions on what to update. It needs to be
* preprocessed before it's sent to the API.
*
* @param array $data
* @return array An array containing the collectionAction=remove data as the
* first element and the collectionAction=append data as the second.
*/
protected function preprocessBatchUpdateData(array $data)
{
$dataRemove = [];
$dataAppend = [];

// Set the data to change and data to remove.
if (in_array($data['is_public'], ['0', '1'])) {
$dataRemove['o:is_public'] = $data['is_public'];
}
if (in_array($data['is_open'], ['0', '1'])) {
$dataRemove['o:is_open'] = $data['is_open'];
}
if (-1 == $data['resource_template']) {
$dataRemove['o:resource_template'] = ['o:id' => null];
} elseif (is_numeric($data['resource_template'])) {
$dataRemove['o:resource_template'] = ['o:id' => $data['resource_template']];
}
if (-1 == $data['resource_class']) {
$dataRemove['o:resource_class'] = ['o:id' => null];
} elseif (is_numeric($data['resource_class'])) {
$dataRemove['o:resource_class'] = ['o:id' => $data['resource_class']];
}
if (isset($data['clear_property_values'])) {
$dataRemove['clear_property_values'] = $data['clear_property_values'];
}

// Set the data to append.
if (isset($data['value'])) {
foreach ($data['value'] as $value) {
$valueObj = [
'property_id' => $value['property_id'],
'type' => $value['type'],
];
switch ($value['type']) {
case 'uri':
$valueObj['@id'] = $value['id'];
$valueObj['o:label'] = $value['label'];
break;
case 'resource':
$valueObj['value_resource_id'] = $value['value_resource_id'];
break;
case 'literal':
default:
$valueObj['@value'] = $value['value'];
}
$dataAppend[$value['property_id']][] = $valueObj;
}
}

return [$dataRemove, $dataAppend];
}
}
Loading