Skip to content

Commit

Permalink
Refactor IdentifierLinker helper for simplicity.
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Jan 14, 2025
1 parent 3e567fd commit b75e986
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 108 deletions.
57 changes: 25 additions & 32 deletions module/VuFind/src/VuFind/View/Helper/Root/IdentifierLinker.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,6 @@
*/
class IdentifierLinker extends \Laminas\View\Helper\AbstractHelper
{
/**
* Current RecordDriver
*
* @var RecordDriver
*/
protected RecordDriver $recordDriver;

/**
* Identifier link context ('results', 'record' or 'holdings')
*
* @var string
*/
protected string $context;

/**
* Instance counter (used for keeping track of records)
*
Expand Down Expand Up @@ -86,35 +72,35 @@ public function __construct(protected Context $contextHelper, protected array $c
}

/**
* Set up context for helper
* Display identifier links (or blank string, if not active).
*
* @param RecordDriver $driver The current record driver
* @param string $context Display context ('results', 'record' or 'holdings')
*
* @return static
* @return string
*/
public function __invoke(RecordDriver $driver, string $context): static
public function __invoke(RecordDriver $driver, string $context): string
{
$this->recordDriver = $driver;
$this->context = $context;
return $this;
return $this->isActive($driver, $context) ? $this->renderTemplate($driver) : '';
}

/**
* Get all available identifiers.
*
* @param RecordDriver $driver The current record driver
*
* @return array
*/
protected function getIdentifiers(): array
protected function getIdentifiers(RecordDriver $driver): array
{
$ids = [];
if (in_array('doi', $this->supportedIdentifiers) && $doi = $this->recordDriver->tryMethod('getCleanDOI')) {
if (in_array('doi', $this->supportedIdentifiers) && $doi = $driver->tryMethod('getCleanDOI')) {
$ids['doi'] = $doi;
}
if (in_array('isbn', $this->supportedIdentifiers) && $isbn = $this->recordDriver->tryMethod('getCleanISBN')) {
if (in_array('isbn', $this->supportedIdentifiers) && $isbn = $driver->tryMethod('getCleanISBN')) {
$ids['isbn'] = $isbn;
}
if (in_array('issn', $this->supportedIdentifiers) && $issn = $this->recordDriver->tryMethod('getCleanISSN')) {
if (in_array('issn', $this->supportedIdentifiers) && $issn = $driver->tryMethod('getCleanISSN')) {
$ids['issn'] = $issn;
}
return $ids;
Expand All @@ -123,13 +109,15 @@ protected function getIdentifiers(): array
/**
* Public method to render the identifier links template
*
* @param RecordDriver $driver The current record driver
*
* @return string
*/
public function renderTemplate(): string
protected function renderTemplate(RecordDriver $driver): string
{
// Build parameters needed to display the control:
$instance = $this->counter++;
$params = $this->getIdentifiers() + compact('instance');
$params = $this->getIdentifiers($driver) + compact('instance');

// Render the subtemplate:
return ($this->contextHelper)($this->getView())
Expand All @@ -140,34 +128,39 @@ public function renderTemplate(): string
* Does the configuration indicate that we should display identifier links in
* the specified context?
*
* @param string $context Display context ('results', 'record' or 'holdings')
*
* @return bool
*/
protected function checkContext(): bool
protected function checkContext(string $context): bool
{
// Doesn't matter the target context if no resolver is specified:
if (empty($this->config['resolver'])) {
return false;
}

// If a setting exists, return that:
$key = 'show_in_' . $this->context;
$key = 'show_in_' . $context;
if (isset($this->config[$key])) {
return $this->config[$key];
}

// If we got this far, use the defaults -- true for results, false for
// everywhere else.
return $this->context == 'results';
return $context == 'results';
}

/**
* Public method to check whether identifier links are active for current record
*
* @param RecordDriver $driver The current record driver
* @param string $context Display context ('results', 'record' or 'holdings')
*
* @return bool
*/
public function isActive(): bool
protected function isActive(RecordDriver $driver, string $context): bool
{
$ids = $this->getIdentifiers();
return !empty($ids) && $this->checkContext();
$ids = $this->getIdentifiers($driver);
return !empty($ids) && $this->checkContext($context);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
$openUrl = $this->openUrl($this->driver, 'record');
$openUrlActive = $openUrl->isActive();
$identifierLinker = $this->identifierLinker($this->driver, 'record');
$identifierLinkerActive = $identifierLinker->isActive();
// Account for replace_other_urls setting
$urls = $this->record($this->driver)->getLinkDetails($openUrlActive);
?>
Expand All @@ -12,4 +10,4 @@
<?php if ($openUrlActive): ?>
<?=$openUrl->renderTemplate()?><br>
<?php endif; ?>
<?php if ($identifierLinkerActive): ?><?=$identifierLinker->renderTemplate()?><?php endif; ?>
<?=$this->identifierLinker($this->driver, 'record')?>
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,19 @@
*/
$openUrl = $this->openUrl($this->driver, 'results');
$openUrlActive = $openUrl->isActive();
$identifierLinker = $this->identifierLinker($this->driver, 'results');
$identifierLinkerActive = $identifierLinker->isActive();
$identifierLinksHtml = $this->identifierLinker($this->driver, 'results');
// Account for replace_other_urls setting
$urls = $this->record($this->driver)->getLinkDetails($openUrlActive);
?>
<?php if ($openUrlActive || $identifierLinkerActive || !empty($urls)): ?>
<?php if ($openUrlActive || $identifierLinksHtml || !empty($urls)): ?>
<?php if ($openUrlActive): ?>
<br>
<?=$openUrl->renderTemplate()?>
<?php endif;?>

<?php if ($identifierLinkerActive): ?>
<?php if ($identifierLinksHtml): ?>
<br>
<?=$identifierLinker->renderTemplate()?>
<?=$identifierLinksHtml?>
<?php endif; ?>

<?php $urls = is_array($urls) ? $urls : []; ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
*/
$openUrl = $this->openUrl($this->driver, 'results');
$openUrlActive = $openUrl->isActive();
$identifierLinker = $this->identifierLinker($this->driver, 'results');
$identifierLinkerActive = $identifierLinker->isActive();
$identifierLinksHtml = $this->identifierLinker($this->driver, 'results');
// Account for replace_other_urls setting
$urls = $this->record($this->driver)->getLinkDetails($openUrlActive);
$recordLinker = $this->recordLinker($this->results);
Expand Down Expand Up @@ -35,13 +34,13 @@ $showCheckboxes = $this->searchSettings($this->results->getParams())->checkboxes
<?=$this->record($this->driver)->getTitleHtml(80)?>
</a>
</h2>
<?php if ($openUrlActive || $identifierLinkerActive || !empty($urls)): ?>
<?php if ($openUrlActive || $identifierLinksHtml || !empty($urls)): ?>
<br><br>
<?php if ($openUrlActive): ?>
<?=$openUrl->renderTemplate()?><br>
<?php endif; ?>
<?php if ($identifierLinkerActive): ?>
<?=$identifierLinker->renderTemplate()?><br>
<?php if ($identifierLinksHtml): ?>
<?=$identifierLinksHtml?><br>
<?php endif; ?>
<?php foreach (is_array($urls) ? $urls : [] as $current): ?>
<a href="<?=$this->escapeHtmlAttr($this->proxyUrl($current['url']))?>" class="fulltext" target="new">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,18 @@
// Display an OpenURL link if configured to do so:
$openUrl = $this->openUrl($this->driver, 'results');
$openUrlActive = $openUrl->isActive();
$identifierLinker = $this->identifierLinker($this->driver, 'results');
$identifierLinkerActive = $identifierLinker->isActive();
$identifierLinksHtml = $this->identifierLinker($this->driver, 'results');
// Account for replace_other_urls setting
$urls = $this->record($this->driver)->getLinkDetails($openUrlActive);
?>
<?php if ($openUrlActive || $identifierLinkerActive || !empty($urls)): ?>
<?php if ($openUrlActive || $identifierLinksHtml || !empty($urls)): ?>
<?php if ($openUrlActive): ?>
<br>
<?=$openUrl->renderTemplate()?>
<?php endif; ?>
<?php if ($identifierLinkerActive): ?>
<?php if ($identifierLinksHtml): ?>
<br>
<?=$identifierLinker->renderTemplate()?>
<?=$identifierLinksHtml?>
<?php endif; ?>
<?php $urls = is_array($urls) ? $urls : []; ?>
<?php if (!$this->driver->isCollection()): ?>
Expand Down
6 changes: 3 additions & 3 deletions themes/bootstrap3/templates/RecordDriver/EDS/core.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@
<?php endforeach; ?>
<?php endif; ?>

<?php $identifierLinker = $this->identifierLinker($this->driver, 'record'); ?>
<?php if ($identifierLinker->isActive()): ?>
<div><?=$identifierLinker->renderTemplate()?></div>
<?php $identifierLinksHtml = $this->identifierLinker($this->driver, 'record'); ?>
<?php if ($identifierLinksHtml): ?>
<div><?=$identifierLinksHtml?></div>
<?php endif; ?>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@
?>

<?php /* Links from identifier linker */ ?>
<?php $identifierLinker = $this->identifierLinker($this->driver, 'results'); ?>
<?php if ($identifierLinker->isActive()): ?>
<div><?=$identifierLinker->renderTemplate()?></div>
<?php $identifierLinksHtml = $this->identifierLinker($this->driver, 'results'); ?>
<?php if ($identifierLinksHtml): ?>
<div><?=$identifierLinksHtml?></div>
<?php endif; ?>
</div>
<div class="result-links hidden-print">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,18 @@
*/
$openUrl = $this->openUrl($this->driver, 'results');
$openUrlActive = $openUrl->isActive();
$identifierLinker = $this->identifierLinker($this->driver, 'results');
$identifierLinkerActive = $identifierLinker->isActive();
$identifierLinksHtml = $this->identifierLinker($this->driver, 'results');
// Account for replace_other_urls setting
$urls = $this->record($this->driver)->getLinkDetails($openUrlActive);

if ($openUrlActive || $identifierLinkerActive || !empty($urls)): ?>
if ($openUrlActive || $identifierLinksHtml || !empty($urls)): ?>
<?php if ($openUrlActive): ?>
<br>
<?=$openUrl->renderTemplate()?>
<?php endif; ?>
<?php if ($identifierLinkerActive): ?>
<?php if ($identifierLinksHtml): ?>
<br>
<?=$identifierLinker->renderTemplate()?>
<?=$identifierLinksHtml?>
<?php endif; ?>
<?php $urls = is_array($urls) ? $urls : []; ?>
<?php if (!$this->driver->isCollection()): ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,18 @@
// Display an OpenURL link if configured to do so:
$openUrl = $this->openUrl($this->driver, 'results');
$openUrlActive = $openUrl->isActive();
$identifierLinker = $this->identifierLinker($this->driver, 'results');
$identifierLinkerActive = $identifierLinker->isActive();
$identifierLinksHtml = $this->identifierLinker($this->driver, 'results');
// Account for replace_other_urls setting
$urls = $this->record($this->driver)->getLinkDetails($openUrlActive);
?>
<?php if ($openUrlActive || $identifierLinkerActive || !empty($urls)): ?>
<?php if ($openUrlActive || $identifierLinksHtml || !empty($urls)): ?>
<?php if ($openUrlActive): ?>
<br>
<?=$openUrl->renderTemplate()?>
<?php endif; ?>
<?php if ($identifierLinkerActive): ?>
<?php if ($identifierLinksHtml): ?>
<br>
<?=$identifierLinker->renderTemplate()?>
<?=$identifierLinksHtml?>
<?php endif; ?>
<?php $urls = is_array($urls) ? $urls : []; ?>
<?php if (!$this->driver->isCollection()): ?>
Expand Down
7 changes: 3 additions & 4 deletions themes/bootstrap3/templates/RecordTab/holdingsils.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
$user = $account->getUserObject();
$openUrl = $this->openUrl($this->driver, 'holdings');
$openUrlActive = $openUrl->isActive();
$identifierLinker = $this->identifierLinker($this->driver, 'holdings');
$identifierLinkerActive = $identifierLinker->isActive();
$identifierLinksHtml = $this->identifierLinker($this->driver, 'holdings');
// Account for replace_other_urls setting
$urls = $this->record($this->driver)->getLinkDetails($openUrlActive);
$offlineMode = $this->ils()->getOfflineMode();
Expand Down Expand Up @@ -62,15 +61,15 @@
<span class="icon-link__label"><?=$this->transEsc('title_hold_place')?></span>
</a>
<?php endif; ?>
<?php if (!empty($urls) || $openUrlActive || $identifierLinkerActive): ?>
<?php if (!empty($urls) || $openUrlActive || $identifierLinksHtml): ?>
<h2><?=$this->transEsc('Internet')?></h2>
<?php if (!empty($urls)): ?>
<?php foreach ($urls as $current): ?>
<a href="<?=$this->escapeHtmlAttr($this->proxyUrl($current['url']))?>"><?=$this->escapeHtml($current['desc'])?></a><br>
<?php endforeach; ?>
<?php endif; ?>
<?php if ($openUrlActive): ?><?=$openUrl->renderTemplate()?><?php endif; ?>
<?php if ($identifierLinkerActive): ?><?=$identifierLinker->renderTemplate()?><?php endif; ?>
<?=$identifierLinksHtml?>
<?php endif; ?>

<?php if (!empty($holdings['electronic_holdings'])): ?>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
$openUrl = $this->openUrl($this->driver, 'record');
$openUrlActive = $openUrl->isActive();
$identifierLinker = $this->identifierLinker($this->driver, 'record');
$identifierLinkerActive = $identifierLinker->isActive();
// Account for replace_other_urls setting
$urls = $this->record($this->driver)->getLinkDetails($openUrlActive);
?>
Expand All @@ -12,4 +10,4 @@
<?php if ($openUrlActive): ?>
<?=$openUrl->renderTemplate()?><br>
<?php endif; ?>
<?php if ($identifierLinkerActive): ?><?=$identifierLinker->renderTemplate()?><?php endif; ?>
<?=$this->identifierLinker($this->driver, 'record')?>
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,19 @@
*/
$openUrl = $this->openUrl($this->driver, 'results');
$openUrlActive = $openUrl->isActive();
$identifierLinker = $this->identifierLinker($this->driver, 'results');
$identifierLinkerActive = $identifierLinker->isActive();
$identifierLinksHtml = $this->identifierLinker($this->driver, 'results');
// Account for replace_other_urls setting
$urls = $this->record($this->driver)->getLinkDetails($openUrlActive);
?>
<?php if ($openUrlActive || $identifierLinkerActive || !empty($urls)): ?>
<?php if ($openUrlActive || $identifierLinksHtml || !empty($urls)): ?>
<?php if ($openUrlActive): ?>
<br>
<?=$openUrl->renderTemplate()?>
<?php endif;?>

<?php if ($identifierLinkerActive): ?>
<?php if ($identifierLinksHtml): ?>
<br>
<?=$identifierLinker->renderTemplate()?>
<?=$identifierLinksHtml?>
<?php endif; ?>

<?php $urls = is_array($urls) ? $urls : []; ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
*/
$openUrl = $this->openUrl($this->driver, 'results');
$openUrlActive = $openUrl->isActive();
$identifierLinker = $this->identifierLinker($this->driver, 'results');
$identifierLinkerActive = $identifierLinker->isActive();
$identifierLinksHtml = $this->identifierLinker($this->driver, 'results');
// Account for replace_other_urls setting
$urls = $this->record($this->driver)->getLinkDetails($openUrlActive);
$recordLinker = $this->recordLinker($this->results);
Expand Down Expand Up @@ -35,13 +34,13 @@ $showCheckboxes = $this->searchSettings($this->results->getParams())->checkboxes
<?=$this->record($this->driver)->getTitleHtml(80)?>
</a>
</h2>
<?php if ($openUrlActive || $identifierLinkerActive || !empty($urls)): ?>
<?php if ($openUrlActive || $identifierLinksHtml || !empty($urls)): ?>
<br><br>
<?php if ($openUrlActive): ?>
<?=$openUrl->renderTemplate()?><br>
<?php endif; ?>
<?php if ($identifierLinkerActive): ?>
<?=$identifierLinker->renderTemplate()?><br>
<?php if ($identifierLinksHtml): ?>
<?=$identifierLinksHtml?><br>
<?php endif; ?>
<?php foreach (is_array($urls) ? $urls : [] as $current): ?>
<a href="<?=$this->escapeHtmlAttr($this->proxyUrl($current['url']))?>" class="fulltext" target="new">
Expand Down
Loading

0 comments on commit b75e986

Please sign in to comment.