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

Added a button to link to the public page of a resource. #1259

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,12 +421,14 @@
'blockLayout' => Service\ViewHelper\BlockLayoutFactory::class,
'blockThumbnailTypeSelect' => Service\ViewHelper\BlockThumbnailTypeSelectFactory::class,
'dataType' => Service\ViewHelper\DataTypeFactory::class,
'defaultSiteSlug' => Service\ViewHelper\DefaultSiteSlugFactory::class,
'i18n' => Service\ViewHelper\I18nFactory::class,
'logger' => Service\ViewHelper\LoggerFactory::class,
'media' => Service\ViewHelper\MediaFactory::class,
'navigationLink' => Service\ViewHelper\NavigationLinkFactory::class,
'pagination' => Service\ViewHelper\PaginationFactory::class,
'params' => Service\ViewHelper\ParamsFactory::class,
'publicResourceUrl' => Service\ViewHelper\PublicResourceUrlFactory::class,
'setting' => Service\ViewHelper\SettingFactory::class,
'userSetting' => Service\ViewHelper\UserSettingFactory::class,
'siteSetting' => Service\ViewHelper\SiteSettingFactory::class,
Expand Down
33 changes: 33 additions & 0 deletions application/src/Service/ViewHelper/DefaultSiteSlugFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Omeka\Service\ViewHelper;

use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Omeka\View\Helper\DefaultSiteSlug;

/**
* Service factory to get the default site slug, or the first site slug.
*
* @todo Store the default site as slug instead of id?
* @todo Set a setting for the default site of the user?
*/
class DefaultSiteSlugFactory implements FactoryInterface
{
/**
* Create and return the DefaultSiteSlug view helper.
*
* @return DefaultSiteSlug
*/
public function __invoke(ContainerInterface $services, $requestedName, array $options = null)
{
$defaultSiteId = $services->get('Omeka\Settings')->get('default_site');
$api = $services->get('Omeka\ApiManager');
if ($defaultSiteId) {
$slugs = $api->search('sites', ['id' => $defaultSiteId], ['returnScalar' => 'slug'])->getContent();
} else {
$slugs = $api->search('sites', ['limit' => 1], ['returnScalar' => 'slug'])->getContent();
}
$defaultSiteSlug = (string) reset($slugs);
return new DefaultSiteSlug($defaultSiteSlug);
}
}
28 changes: 28 additions & 0 deletions application/src/Service/ViewHelper/PublicResourceUrlFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace Omeka\Service\ViewHelper;

use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Omeka\View\Helper\PublicResourceUrl;

/**
* Service factory for the PublicResourceUrlFactory view helper.
*
* @todo Set a setting for the default site of the user.
*/
class PublicResourceUrlFactory implements FactoryInterface
{
/**
* Create and return the PublicResourceUrl view helper
*
* @return PublicResourceUrl
*/
public function __invoke(ContainerInterface $services, $requestedName, array $options = null)
{
return new PublicResourceUrl(
$services->get('Omeka\ApiManager')->search('sites', [], ['returnScalar' => 'slug'])->getContent(),
$services->get('ViewHelperManager')->get('defaultSiteSlug')()
);
}
}
35 changes: 35 additions & 0 deletions application/src/View/Helper/DefaultSiteSlug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace Omeka\View\Helper;

use Laminas\View\Helper\AbstractHelper;

/**
* View helper to get the default site slug, or the first one.
*/
class DefaultSiteSlug extends AbstractHelper
{
/**
* @var string
*/
protected $defaultSiteSlug;

/**
* Construct the helper.
*
* @param string|null $defaultSiteSlug
*/
public function __construct($defaultSiteSlug)
{
$this->defaultSiteSlug = $defaultSiteSlug;
}

/**
* Return the default site slug, or the first one.
*
* @return string|null
*/
public function __invoke()
{
return $this->defaultSiteSlug;
}
}
66 changes: 66 additions & 0 deletions application/src/View/Helper/PublicResourceUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php declare(strict_types=1);

namespace Omeka\View\Helper;

use Laminas\View\Helper\AbstractHelper;
use Omeka\Api\Representation\AbstractResourceRepresentation;

/**
* View helper to return the url to the public default site page of a resource.
*/
class PublicResourceUrl extends AbstractHelper
{
/**
* @var string[]
*/
protected $siteSlugs;

/**
* @var ?string
*/
protected $defaultSiteSlug;

/**
* @var int
*/
protected $defaultSiteId;

public function __construct(array $siteSlugs, ?string $defaultSiteSlug)
{
$this->siteSlugs = $siteSlugs;
$this->defaultSiteSlug = $defaultSiteSlug;
$this->defaultSiteId = (int) array_search($defaultSiteSlug, $siteSlugs);
}

/**
* Return the url to the public default site page or a resource.
*
* @uses AbstractResourceRepresentation::siteUrl()
*
* @param AbstractResourceRepresentation $resource
* @param bool $canonical Whether to return an absolute URL
* @return string
*/
public function __invoke(AbstractResourceRepresentation $resource, $canonical = false): ?string
{
// Use default site by default.
$siteSlug = $this->defaultSiteSlug;

// The resource should belong to the site.
$res = $resource;
if ($resource->getResourceJsonLdType() === 'o:Media') {
$res = $resource->item();
}
if (method_exists($res, 'sites')) {
$resourceSites = $res->sites();
if (!isset($resourceSites[$this->defaultSiteId]) && count($resourceSites)) {
$siteSlug = (reset($resourceSites))->slug();
}
}

// Manage the case where there is no site.
return $siteSlug
? $resource->siteUrl($siteSlug, $canonical)
: null;
}
}
1 change: 1 addition & 0 deletions application/view/omeka/admin/item-set/show.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ $sectionNavs = [
<?php echo $this->pageTitle($itemSet->displayTitle(), 1, $translate('Item sets')); ?>

<div id="page-actions">
<?php echo $this->hyperlink($translate('Public view'), $this->publicResourceUrl($itemSet), ['class' => 'button', 'target' => '_blank']); ?>
<?php if ($itemSet->userIsAllowed('update')): ?>
<?php echo $itemSet->link($translate('Edit item set'), 'edit', ['class' => 'button']); ?>
<?php endif; ?>
Expand Down
1 change: 1 addition & 0 deletions application/view/omeka/admin/item/show.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ $itemMedia = $item->media();
?>
<?php echo $this->pageTitle($item->displayTitle(), 1, $translate('Items')); ?>
<div id="page-actions">
<?php echo $this->hyperlink($translate('Public view'), $this->publicResourceUrl($item), ['class' => 'button', 'target' => '_blank']); ?>
<?php if ($item->userIsAllowed('update')): ?>
<?php echo $item->link($translate('Edit item'), 'edit', ['class' => 'button']); ?>
<?php endif; ?>
Expand Down
3 changes: 3 additions & 0 deletions application/view/omeka/admin/media/show.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ $sectionNavs = [
<?php echo $this->pageTitle($media->displayTitle(), 1, $translate('Media')); ?>

<div id="page-actions">
<?php echo $this->hyperlink($translate('Public view'), $this->publicResourceUrl($media), ['class' => 'button', 'target' => '_blank']); ?>
<?php if ($media->userIsAllowed('update')): ?>
<?php echo $media->link($translate('Edit media'), 'edit', ['class' => 'button']); ?>
<?php endif; ?>
</div>

<?php echo $this->sectionNav($sectionNavs, 'view.show.section_nav', $media); ?>
Expand Down