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

Filter default sites for items user setting #2167

Merged
merged 7 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 5 additions & 1 deletion application/asset/js/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,18 @@ var Omeka = {
tableRowCell.text(tableRowValue);
});
selectorRow.addClass('added');
table.append(tableRow).removeClass('empty').trigger('appendRow');
table.children('.resource-rows').append(tableRow).removeClass('empty').trigger('appendRow');
updateResourceCount(id);
}

var updateResourceCount = function(id) {
var resource = selector.find('[data-resource-id="' + id + '"]');
var resourceParent = resource.parents('.selector-parent');
var childCount = resourceParent.find('.selector-child-count').first();
// Update the count only when the resource exists in the selector.
if (!selector.find(`.selector-child[data-resource-id="${id}"]`).length) {
return;
}
if (resource.hasClass('added')) {
var newTotalCount = parseInt(selectorCount.text()) - 1;
var newChildCount = parseInt(childCount.text()) - 1;
Expand Down
22 changes: 16 additions & 6 deletions application/src/Api/Adapter/ItemAdapter.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Omeka\Api\Adapter;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\QueryBuilder;
use Omeka\Api\Exception;
use Omeka\Api\Request;
Expand Down Expand Up @@ -217,16 +218,19 @@ public function hydrate(Request $request, EntityInterface $entity,
}
}
}
if ($isCreate && !is_array($request->getValue('o:site'))) {
// On CREATE and when no "o:site" array is passed, assign this item
// to all sites where assignNewItems=true.
$dql = '
SELECT site
if ($isCreate) {
// On CREATE we will need sites where assignNewItems=true.
$dql = 'SELECT site
FROM Omeka\Entity\Site site
WHERE site.assignNewItems = true';
$query = $this->getEntityManager()->createQuery($dql);
$assignNewItemsSites = new ArrayCollection($query->getResult());
}
if ($isCreate && !is_array($request->getValue('o:site'))) {
// On CREATE and when no "o:site" array is passed, assign this item
// to all sites where assignNewItems=true.
$sites = $entity->getSites();
foreach ($query->getResult() as $site) {
foreach ($assignNewItemsSites as $site) {
$sites->set($site->getId(), $site);
}
} elseif ($this->shouldHydrate($request, 'o:site')) {
Expand Down Expand Up @@ -255,6 +259,12 @@ public function hydrate(Request $request, EntityInterface $entity,
// Assign site that was not already assigned.
$site = $siteAdapter->findEntity($siteId);
if ($acl->userIsAllowed($site, 'can-assign-items')) {
// A user with the "can-assign-items" privilege can assign
// a site at any time.
$sites->set($site->getId(), $site);
} elseif ($isCreate && $assignNewItemsSites->contains($site)) {
jimsafley marked this conversation as resolved.
Show resolved Hide resolved
// A user without the "can-assign-items" privilege can assign
// a site only on CREATE when the site has assignNewItems=true.
$sites->set($site->getId(), $site);
}
}
Expand Down
13 changes: 10 additions & 3 deletions application/src/Form/Element/AbstractGroupByOwnerSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,19 @@ public function getValueOptions(): array
$query = [];
}

$response = $this->getApiManager()->search($this->getResourceName(), $query);
$resourceReps = $this->getApiManager()->search($this->getResourceName(), $query)->getContent();

// Provide a way to filter the resource representations prior to
// building the value options.
$callback = $this->getOption('filter_resource_representations');
if (is_callable($callback)) {
$resourceReps = $callback($resourceReps);
}

if ($this->getOption('disable_group_by_owner')) {
// Group alphabetically by resource label without grouping by owner.
$resources = [];
foreach ($response->getContent() as $resource) {
foreach ($resourceReps as $resource) {
$resources[$this->getValueLabel($resource)][] = $resource->id();
}
ksort($resources);
Expand All @@ -68,7 +75,7 @@ public function getValueOptions(): array
} else {
// Group alphabetically by owner email.
$resourceOwners = [];
foreach ($response->getContent() as $resource) {
foreach ($resourceReps as $resource) {
$owner = $resource->owner();
$index = $owner ? $owner->email() : null;
$resourceOwners[$index]['owner'] = $owner;
Expand Down
9 changes: 9 additions & 0 deletions application/src/Form/UserForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ public function init()
'options' => [
'label' => 'Default sites for items', // @translate
'empty_option' => '',
'filter_resource_representations' => function ($sites) {
// The user must have permission to assign items to the site.
foreach ($sites as $index => $site) {
if (!$site->userIsAllowed('can-assign-items')) {
unset($sites[$index]);
}
}
return $sites;
},
],
]);
$settingsFieldset->add([
Expand Down
63 changes: 49 additions & 14 deletions application/view/omeka/admin/item/manage-sites.phtml
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
<?php
$canAssignItemsSites = [];
$cannotAssignItemsSites = [];

if ($item) {
$siteRepresentations = $item->sites();
// This is an existing item.

$sites = $item->sites();
foreach ($sites as $site) {
if ($site->userIsAllowed('can-assign-items')) {
$canAssignItemsSites[] = ['id' => $site->id()];
} else {
$cannotAssignItemsSites[$site->id()] = $site;
}
}
} else {
$siteRepresentations = $this->api()->search('sites', ['assign_new_items' => true])->getContent();
// This is a new item.

$userDefaultSites = $this->userSetting('default_item_sites');
if (is_array($userDefaultSites)) {
$userDefaultSiteRepresentations = $this->api()->search('sites', ['id' => $userDefaultSites])->getContent();
$siteRepresentations = array_merge($siteRepresentations, $userDefaultSiteRepresentations);
// Get all sites that allow item assignment in site settings.
$sites = $this->api()->search('sites', ['assign_new_items' => true])->getContent();
foreach ($sites as $site) {
if ($site->userIsAllowed('can-assign-items')) {
$canAssignItemsSites[] = ['id' => $site->id()];
} else {
$cannotAssignItemsSites[$site->id()] = $site;
}
}
}

$sites = [];
foreach ($siteRepresentations as $siteRepresentation) {
$sites[] = [
'id' => $siteRepresentation->id(),
];
// Get all sites that the user set as default in user settings.
$defaultItemSites = $this->userSetting('default_item_sites');
$sites = is_array($defaultItemSites)
? $this->api()->search('sites', ['id' => $defaultItemSites])->getContent()
: [];
foreach ($sites as $site) {
if ($site->userIsAllowed('can-assign-items')) {
$canAssignItemsSites[] = ['id' => $site->id()];
} else {
$cannotAssignItemsSites[$site->id()] = $site;
}
}
}

$siteTemplate = '
Expand All @@ -30,15 +52,28 @@ $siteTemplate = '
</td>
</tr>';
?>
<table id="item-sites" data-existing-rows="<?php echo $this->escapeHtml(json_encode(array_values($sites))); ?>" data-row-template="<?php echo $this->escapeHtml($siteTemplate); ?>" data-tablesaw-mode="stack" class="selector-table tablesaw tablesaw-stack <?php echo ($item && (count($sites) > 0)) ? '' : 'empty'; ?>">
<table id="item-sites" data-existing-rows="<?php echo $this->escapeHtml(json_encode($canAssignItemsSites)); ?>" data-row-template="<?php echo $this->escapeHtml($siteTemplate); ?>" data-tablesaw-mode="stack" class="selector-table tablesaw tablesaw-stack <?php echo ($item && (count($sites) > 0)) ? '' : ''; ?>">
<thead>
<tr>
<th><?php echo $this->translate('Title'); ?></th>
<th><?php echo $this->translate('Owner'); ?></th>
<th></th>
</tr>
</thead>
<tbody class="resource-rows"></tbody>
<tbody class="resource-rows">
<?php foreach ($cannotAssignItemsSites as $site): ?>
<tr class="resource-row">
<td class="data-value" data-row-key="child-search"><?php echo $this->escapeHtml($site->title()); ?></td>
<td class="data-value" data-row-key="resource-email"><?php echo $this->escapeHtml($site->owner()->email()); ?></td>
<td>
<ul class="actions">
<li><?php echo $this->hyperlink('', '#', ['class' => 'o-icon-delete', 'title' => $this->translate('Unassign from site')]); ?></li>
</ul>
<input type="hidden" name="o:site[]" class="resource-id" value="<?php echo $this->escapeHtml($site->id()); ?>">
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

<div class="no-resources">
Expand Down
Loading